Commit 53520c15 authored by Andrew Jones's avatar Andrew Jones Committed by Hero Bird

[cli] reject creating new contract with hyphenated name (#161)

* [cli] reject creating new contract with hyphenated name

* [cli] add test, make kcov happy
parent a5ab8633
......@@ -27,6 +27,7 @@ pub enum CommandErrorKind {
UnimplementedCommand,
UnimplementedAbstractionLayer,
ZipError(ZipError),
Other(String),
}
/// An error that can be encountered while executing commands.
......@@ -51,6 +52,14 @@ impl From<ZipError> for CommandError {
}
}
impl From<&str> for CommandError {
fn from(error: &str) -> Self {
Self {
kind: CommandErrorKind::Other(error.to_string()),
}
}
}
impl CommandError {
/// Creates a new command error from the given kind.
pub fn new(kind: CommandErrorKind) -> Self {
......
......@@ -37,6 +37,9 @@ use std::{
/// Initializes a project structure for the `lang` abstraction layer.
fn initialize_for_lang(name: &str) -> Result<()> {
if name.contains("-") {
return Err("Contract names cannot contain hyphens".into())
}
fs::create_dir(name)?;
let out_dir = path::Path::new(name);
......@@ -99,3 +102,15 @@ pub(crate) fn execute_new(layer: AbstractionLayer, name: &str) -> Result<()> {
AbstractionLayer::Lang => initialize_for_lang(name),
}
}
#[cfg(test)]
mod tests {
#[test]
fn rejects_hyphenated_name() {
let result = super::initialize_for_lang("should-fail");
assert_eq!(
format!("{:?}", result),
r#"Err(CommandError { kind: Other("Contract names cannot contain hyphens") })"#
)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment