Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/cmd/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub(crate) fn execute<P>(name: &str, dir: Option<P>) -> Result<Option<String>>
where
P: AsRef<Path>,
{
if name.contains('-') {
anyhow::bail!("Contract names cannot contain hyphens");
if !name.chars().all(|c| c.is_alphanumeric() || c == '_') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allows _ as identifier which is invalid. however, I think this isn't too bad?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted. Is that even a valid crate name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and cargo does not immediately put a stop to having _ as crate name but my filesystem definitely does. xD

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note sure we really need to guard against this. What is your opinion?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unnecessary

anyhow::bail!("Contract names can only contain alphanumeric characters and underscores");
}

let out_dir = dir
Expand Down Expand Up @@ -108,7 +108,20 @@ mod tests {
assert!(result.is_err(), "Should fail");
assert_eq!(
result.err().unwrap().to_string(),
"Contract names cannot contain hyphens"
"Contract names can only contain alphanumeric characters and underscores"
);
Ok(())
})
}

#[test]
fn rejects_name_with_period() {
with_tmp_dir(|path| {
let result = execute("../xxx", Some(path));
assert!(result.is_err(), "Should fail");
assert_eq!(
result.err().unwrap().to_string(),
"Contract names can only contain alphanumeric characters and underscores"
);
Ok(())
})
Expand Down