Skip to content

Commit

Permalink
Add tests for run command
Browse files Browse the repository at this point in the history
  • Loading branch information
james-w committed Jul 15, 2024
1 parent 47ffa17 commit 411862b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
19 changes: 9 additions & 10 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ impl Execute for BuildCommand {
self.artifact
))
}
},
CommandLookupResult::NotFound => {
Err(anyhow!(
"Target <{}> not found in config file <{}>",
self.artifact,
context.config_path
))
},
CommandLookupResult::Duplicates(duplicates) => {
}
CommandLookupResult::NotFound => Err(anyhow!(
"Target <{}> not found in config file <{}>",
self.artifact,
context.config_path
)),
CommandLookupResult::Duplicates(ref mut duplicates) => {
duplicates.sort();
Err(anyhow!(
"Target <{}> is ambiguous, possible values are <{}>, please specify the command to run using one of those names",
self.artifact, duplicates.join(", ")
))
},
}
}
}
}
24 changes: 10 additions & 14 deletions src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,21 @@ impl Execute for RunCommand {
if let Some(runner) = runner {
runner.run(&context, &mut outputs, cleanup_manager, self.args.clone())
} else {
Err(anyhow!(
"Target <{}> is not runnable",
self.name
))
Err(anyhow!("Target <{}> is not runnable", self.name))
}
},
CommandLookupResult::NotFound => {
Err(anyhow!(
"Target <{}> not found in config file <{}>",
self.name,
context.config_path
))
},
CommandLookupResult::Duplicates(duplicates) => {
}
CommandLookupResult::NotFound => Err(anyhow!(
"Target <{}> not found in config file <{}>",
self.name,
context.config_path
)),
CommandLookupResult::Duplicates(ref mut duplicates) => {
duplicates.sort();
Err(anyhow!(
"Target <{}> is ambiguous, possible values are <{}>, please specify the command to run using one of those names",
self.name, duplicates.join(", ")
))
},
}
}
}
}
2 changes: 1 addition & 1 deletion tests/test_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,6 @@ fn test_error_when_ambiguous() {
cmd.assert().failure();

cmd.assert().stderr(predicate::str::contains(
"Target <copy> is ambiguous, possible values are <artifact.exec.copy, artifact.container_image.copy>",
"Target <copy> is ambiguous, possible values are <artifact.container_image.copy, artifact.exec.copy>",
));
}
48 changes: 48 additions & 0 deletions tests/test_run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use assert_cmd::prelude::*;
use predicates::prelude::*;

mod common;

#[test]
fn test_error_when_does_not_exist() {
let config_src = r#"
[command.exec.copy]
command = "cp hello world"
"#;

let test_context = common::TestContext::new();
test_context.write_config(config_src);

let mut cmd = test_context.get_command();
cmd.arg("run").arg("non_existent");

cmd.assert().failure();

cmd.assert().stderr(predicate::str::contains(
"Target <non_existent> not found in config file <",
));
}

#[test]
fn test_error_when_ambiguous() {
let config_src = r#"
[artifact.exec.copy]
command = "cp hello world"
[artifact.container_image.copy]
context = "."
tag = "latest"
"#;

let test_context = common::TestContext::new();
test_context.write_config(config_src);

let mut cmd = test_context.get_command();
cmd.arg("run").arg("copy");

cmd.assert().failure();

cmd.assert().stderr(predicate::str::contains(
"Target <copy> is ambiguous, possible values are <artifact.container_image.copy, artifact.exec.copy>",
));
}

0 comments on commit 411862b

Please sign in to comment.