Skip to content

Commit

Permalink
twoliter: fix make command trailing varargs
Browse files Browse the repository at this point in the history
Fixes issue #139. Clap was not configured correctly such that trailing
arguments intended to be passed along to the cargo make invocation were
not handled correctly. This commit fixes it and adds regression tests
in case we change the command arguments or Clap changes behavior.
  • Loading branch information
webern committed Jan 30, 2024
1 parent 99f88c7 commit b9547be
Showing 1 changed file with 101 additions and 3 deletions.
104 changes: 101 additions & 3 deletions twoliter/src/cmd/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::path::PathBuf;
/// Run a cargo make command in Twoliter's build environment. Known Makefile.toml environment
/// variables will be passed-through to the cargo make invocation.
#[derive(Debug, Parser)]
#[clap(trailing_var_arg = true)]
pub(crate) struct Make {
/// Path to the project file. Will search for Twoliter.toml when absent.
#[clap(long)]
Expand All @@ -20,15 +21,17 @@ pub(crate) struct Make {
#[clap(long)]
cargo_home: PathBuf,

/// This can be passed by environment variable. We require it as part of the command arguments
/// because we need it to pull the right SDK target architecture.
#[clap(long, env = "BUILDSYS_ARCH")]
arch: String,

/// Cargo make task. E.g. the word "build" if we want to execute `cargo make build`.
makefile_task: String,

/// Uninspected arguments to be passed to cargo make after the target name. For example, --foo
/// in the following command : cargo make test --foo.
additional_args: Vec<String>,

#[clap(env = "BUILDSYS_ARCH")]
arch: String,
}

impl Make {
Expand All @@ -48,3 +51,98 @@ impl Make {
.await
}
}

#[test]
fn test_trailing_args_1() {
let args = Make::try_parse_from(&[
"make",
"--cargo-home",
"/tmp/foo",
"--arch",
"x86_64",
"testsys",
"--",
"add",
"secret",
"map",
"--name",
"foo",
"something=bar",
"something-else=baz",
])
.unwrap();

assert_eq!(args.makefile_task, "testsys");
assert_eq!(args.additional_args[0], "add");
assert_eq!(args.additional_args[1], "secret");
assert_eq!(args.additional_args[2], "map");
assert_eq!(args.additional_args[3], "--name");
assert_eq!(args.additional_args[4], "foo");
assert_eq!(args.additional_args[5], "something=bar");
assert_eq!(args.additional_args[6], "something-else=baz");
}

#[test]
fn test_trailing_args_2() {
let args = Make::try_parse_from(&[
"make",
"--cargo-home",
"/tmp/foo",
"--arch",
"x86_64",
"testsys",
"add",
"secret",
"map",
"--name",
"foo",
"something=bar",
"something-else=baz",
])
.unwrap();

assert_eq!(args.makefile_task, "testsys");
assert_eq!(args.additional_args[0], "add");
assert_eq!(args.additional_args[1], "secret");
assert_eq!(args.additional_args[2], "map");
assert_eq!(args.additional_args[3], "--name");
assert_eq!(args.additional_args[4], "foo");
assert_eq!(args.additional_args[5], "something=bar");
assert_eq!(args.additional_args[6], "something-else=baz");
}

#[test]
fn test_trailing_args_3() {
let args = Make::try_parse_from(&[
"make",
"--cargo-home",
"/tmp/foo",
"--arch",
"x86_64",
"testsys",
"--",
"add",
"secret",
"map",
"--",
"--name",
"foo",
"something=bar",
"something-else=baz",
"--",
])
.unwrap();

assert_eq!(args.makefile_task, "testsys");
assert_eq!(args.additional_args[0], "add");
assert_eq!(args.additional_args[1], "secret");
assert_eq!(args.additional_args[2], "map");
// The first instance of `--`, between `testsys` and `add`, is not passed through to the
// varargs. After that, instances of `--` are passed through the varargs.
assert_eq!(args.additional_args[3], "--");
assert_eq!(args.additional_args[4], "--name");
assert_eq!(args.additional_args[5], "foo");
assert_eq!(args.additional_args[6], "something=bar");
assert_eq!(args.additional_args[7], "something-else=baz");
assert_eq!(args.additional_args[8], "--");
}

0 comments on commit b9547be

Please sign in to comment.