Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only allow new contract names beginning with an alphabetic character #219

Merged
merged 3 commits into from
Mar 9, 2021
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
22 changes: 22 additions & 0 deletions src/cmd/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ where
anyhow::bail!("Contract names can only contain alphanumeric characters and underscores");
}

if !name
.chars()
.next()
.map(|c| c.is_alphabetic())
.unwrap_or(false)
{
anyhow::bail!("Contract names must begin with an alphabetic character");
}

let out_dir = dir
.map_or(env::current_dir()?, |p| p.as_ref().to_path_buf())
.join(name);
Expand Down Expand Up @@ -127,6 +136,19 @@ mod tests {
})
}

#[test]
fn rejects_name_beginning_with_number() {
with_tmp_dir(|path| {
let result = execute("1xxx", Some(path));
assert!(result.is_err(), "Should fail");
assert_eq!(
result.err().unwrap().to_string(),
"Contract names must begin with an alphabetic character"
);
Ok(())
})
}

#[test]
fn contract_cargo_project_already_exists() {
with_tmp_dir(|path| {
Expand Down