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

[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 { ...@@ -27,6 +27,7 @@ pub enum CommandErrorKind {
UnimplementedCommand, UnimplementedCommand,
UnimplementedAbstractionLayer, UnimplementedAbstractionLayer,
ZipError(ZipError), ZipError(ZipError),
Other(String),
} }
/// An error that can be encountered while executing commands. /// An error that can be encountered while executing commands.
...@@ -51,6 +52,14 @@ impl From<ZipError> for CommandError { ...@@ -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 { impl CommandError {
/// Creates a new command error from the given kind. /// Creates a new command error from the given kind.
pub fn new(kind: CommandErrorKind) -> Self { pub fn new(kind: CommandErrorKind) -> Self {
......
...@@ -37,6 +37,9 @@ use std::{ ...@@ -37,6 +37,9 @@ use std::{
/// Initializes a project structure for the `lang` abstraction layer. /// Initializes a project structure for the `lang` abstraction layer.
fn initialize_for_lang(name: &str) -> Result<()> { fn initialize_for_lang(name: &str) -> Result<()> {
if name.contains("-") {
return Err("Contract names cannot contain hyphens".into())
}
fs::create_dir(name)?; fs::create_dir(name)?;
let out_dir = path::Path::new(name); let out_dir = path::Path::new(name);
...@@ -99,3 +102,15 @@ pub(crate) fn execute_new(layer: AbstractionLayer, name: &str) -> Result<()> { ...@@ -99,3 +102,15 @@ pub(crate) fn execute_new(layer: AbstractionLayer, name: &str) -> Result<()> {
AbstractionLayer::Lang => initialize_for_lang(name), 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") })"#
)
}
}
Supports Markdown
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