From d3be7bba78462ea34397921e1edaa071de49e4f9 Mon Sep 17 00:00:00 2001 From: Jake Deichert Date: Tue, 8 Oct 2019 00:26:20 -0400 Subject: [PATCH 1/2] Allow any shell executor that supports -c evaluation Also: * Error when command doesn't have a script * Error when command script doesn't have a lang code to determine the exector --- README.md | 3 +- src/command.rs | 2 +- src/executor.rs | 24 ++++---- tests/arguments_and_flags_test.rs | 49 ++++++++++++++++ tests/subcommands_test.rs | 95 ++++++++----------------------- tests/supported_runtimes_test.rs | 91 +++++++++++++++++------------ 6 files changed, 144 insertions(+), 120 deletions(-) diff --git a/README.md b/README.md index f71c34f..16b1ef8 100644 --- a/README.md +++ b/README.md @@ -189,8 +189,7 @@ On top of shell/bash scripts, `mask` also supports using node, python, ruby and > An example shell script -Valid lang codes: sh, bash, zsh, fish -The fallback is sh for unknown language codes. +Valid lang codes: sh, bash, zsh, fish... any shell that supports -c ~~~zsh echo "Hello, $name!" diff --git a/src/command.rs b/src/command.rs index 336b95c..fcaf0e3 100644 --- a/src/command.rs +++ b/src/command.rs @@ -57,7 +57,7 @@ impl Script { } pub fn has_script(&self) -> bool { - self.source != "" + self.source != "" && self.executor != "" } } diff --git a/src/executor.rs b/src/executor.rs index 336cbd7..00cda3a 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -14,6 +14,16 @@ pub fn execute_command( cmd: Command, maskfile_path: String, ) -> Result { + if cmd.script.source == "" { + let msg = "Command has no script."; + return Err(Error::new(ErrorKind::Other, msg)); + } + + if cmd.script.executor == "" { + let msg = "Command script requires a lang code which determines which executor to use."; + return Err(Error::new(ErrorKind::Other, msg)); + } + let mut child; if init_script.has_script() { if !validate_init_script(&init_script) { @@ -57,13 +67,9 @@ fn prepare_command_without_init_script(cmd: &Command) -> process::Command { child.arg("-r").arg(source); child } - "bash" | "zsh" | "fish" => { - let mut child = process::Command::new(executor); - child.arg("-c").arg(source); - child - } + // Any shell script that uses -c (sh, bash, zsh, fish, dash, etc...) _ => { - let mut child = process::Command::new("sh"); + let mut child = process::Command::new(executor); child.arg("-c").arg(source); child } @@ -78,10 +84,8 @@ fn prepare_command_with_init_script(init_script: Script, cmd: &Command) -> proce "py" | "python" => run_with_init_script(&init_script, &cmd, "python -c"), "rb" | "ruby" => run_with_init_script(&init_script, &cmd, "ruby -e"), "php" => run_with_init_script(&init_script, &cmd, "php -r"), - "bash" | "zsh" | "fish" => { - run_with_init_script(&init_script, &cmd, &format!("{} -c", executor)) - } - _ => run_with_init_script(&init_script, &cmd, "sh -c"), + // Any other executor that uses -c (sh, bash, zsh, fish, dash, etc...) + _ => run_with_init_script(&init_script, &cmd, &format!("{} -c", executor)), } } diff --git a/tests/arguments_and_flags_test.rs b/tests/arguments_and_flags_test.rs index 3e64105..8f85b48 100644 --- a/tests/arguments_and_flags_test.rs +++ b/tests/arguments_and_flags_test.rs @@ -86,6 +86,55 @@ fi .success(); } +mod when_entering_negative_numbers { + use super::*; + + #[test] + fn allows_entering_negative_numbers_as_values() { + let (_temp, maskfile_path) = common::maskfile( + r#" +## add (a) (b) +~~~bash +echo $(($a + $b)) +~~~ +"#, + ); + + common::run_mask(&maskfile_path) + .cli("add -1 -3") + .assert() + .stdout(contains("-4")) + .success(); + } + + #[test] + fn allows_entering_negative_numbers_as_flag_values() { + let (_temp, maskfile_path) = common::maskfile( + r#" +## add + +**OPTIONS** +* a + * flags: --a + * type: string +* b + * flags: --b + * type: string + +~~~bash +echo $(($a + $b)) +~~~ +"#, + ); + + common::run_mask(&maskfile_path) + .cli("add --a -33 --b 17") + .assert() + .stdout(contains("-16")) + .success(); + } +} + mod numerical_option_flag { use super::*; diff --git a/tests/subcommands_test.rs b/tests/subcommands_test.rs index 278c6c0..5cadcea 100644 --- a/tests/subcommands_test.rs +++ b/tests/subcommands_test.rs @@ -1,8 +1,8 @@ use assert_cmd::prelude::*; -use predicates::str::{contains, is_empty}; +use colored::*; +use predicates::str::contains; mod common; - use common::MaskCommandExt; #[test] @@ -43,109 +43,62 @@ echo "Stopping service $service_name" fn exits_with_error_when_missing_subcommand() { let (_temp, maskfile_path) = common::maskfile( r#" -## foo +## service +### service start "#, ); common::run_mask(&maskfile_path) + .command("service") .assert() .code(1) .stderr(contains( - "error: 'mask' requires a subcommand, but one was not provided", + "error: 'mask service' requires a subcommand, but one was not provided", )) .failure(); } -mod when_entering_negative_numbers { +mod when_command_has_no_source { use super::*; #[test] - fn allows_entering_negative_numbers_as_values() { + fn exits_with_error_when_it_has_no_script_lang_code() { let (_temp, maskfile_path) = common::maskfile( r#" -## add (a) (b) -~~~bash -echo $(($a + $b)) +## start ~~~ -"#, - ); - - common::run_mask(&maskfile_path) - .cli("add -1 -3") - .assert() - .stdout(contains("-4")) - .success(); - } - - #[test] - fn allows_entering_negative_numbers_as_flag_values() { - let (_temp, maskfile_path) = common::maskfile( - r#" -## add - -**OPTIONS** -* a - * flags: --a - * type: string -* b - * flags: --b - * type: string - -~~~bash -echo $(($a + $b)) +echo "system, online" ~~~ "#, ); common::run_mask(&maskfile_path) - .cli("add --a -33 --b 17") + .command("start") .assert() - .stdout(contains("-16")) - .success(); - } -} - -mod when_command_has_no_source { - use super::*; - - #[test] - fn exits_gracefully_when_it_has_no_subcommands() { - let (_temp, maskfile_path) = common::maskfile( - r#" -## system -"#, - ); - - // NOTE: Right now we exit without an error. Perhaps there should at least - // be a warning logged to the console? - common::run_mask(&maskfile_path) - .command("system") - .assert() - .stdout(is_empty()) - .success(); + .code(1) + .stderr(contains(format!( + "{} Command script requires a lang code which determines which executor to use.", + "ERROR:".red() + ))) + .failure(); } #[test] - fn exits_with_error_when_it_has_subcommands() { + fn exits_with_error_when_it_has_no_subcommands() { let (_temp, maskfile_path) = common::maskfile( r#" -## system - -### start - -~~~sh -echo "system, online" -~~~ +## start "#, ); common::run_mask(&maskfile_path) - .command("system") + .command("start") .assert() .code(1) - .stderr(contains( - "error: 'mask system' requires a subcommand, but one was not provided", - )) + .stderr(contains(format!( + "{} Command has no script.", + "ERROR:".red() + ))) .failure(); } } diff --git a/tests/supported_runtimes_test.rs b/tests/supported_runtimes_test.rs index a9e35ff..79fbd22 100644 --- a/tests/supported_runtimes_test.rs +++ b/tests/supported_runtimes_test.rs @@ -1,22 +1,60 @@ use assert_cmd::prelude::*; +use colored::*; use predicates::str::contains; mod common; use common::MaskCommandExt; #[test] -fn bash() { +fn errors_when_no_lang_code_is_specified() { let (_temp, maskfile_path) = common::maskfile( - " -# Integration tests + r#" +## missing +~~~ +echo "this won't do anything..." +~~~ +"#, + ); -## bash + common::run_mask(&maskfile_path) + .command("missing") + .assert() + .code(1) + .stderr(contains(format!( + "{} Command script requires a lang code which determines which executor to use.", + "ERROR:".red() + ))) + .failure(); +} -```bash +#[test] +fn sh() { + let (_temp, maskfile_path) = common::maskfile( + r#" +## sh +~~~sh echo Hello, $name! -``` +~~~ +"#, + ); -", + common::run_mask(&maskfile_path) + .command("sh") + .env("name", "World") + .assert() + .stdout(contains("Hello, World!")) + .success(); +} + +#[test] +fn bash() { + let (_temp, maskfile_path) = common::maskfile( + r#" +## bash +~~~bash +echo Hello, $name! +~~~ +"#, ); common::run_mask(&maskfile_path) @@ -30,17 +68,13 @@ echo Hello, $name! #[test] fn node() { let (_temp, maskfile_path) = common::maskfile( - " -# Integration tests - + r#" ## node - -```js +~~~js const { name } = process.env; console.log(`Hello, ${name}!`); -``` - -", +~~~ +"#, ); common::run_mask(&maskfile_path) @@ -55,18 +89,12 @@ console.log(`Hello, ${name}!`); fn python() { let (_temp, maskfile_path) = common::maskfile( r#" -# Integration tests - ## python - -```py +~~~py import os - name = os.getenv("name", "WORLD") - print("Hello, " + name + "!") -``` - +~~~ "#, ); @@ -82,16 +110,11 @@ print("Hello, " + name + "!") fn ruby() { let (_temp, maskfile_path) = common::maskfile( r#" -# Integration tests - ## ruby - -```ruby +~~~ruby name = ENV["name"] || "WORLD" - puts "Hello, #{name}!" -``` - +~~~ "#, ); @@ -107,16 +130,12 @@ puts "Hello, #{name}!" fn php() { let (_temp, maskfile_path) = common::maskfile( r#" -# Integration tests - ## php - -```php +~~~php $name = getenv("name") ?: "WORLD"; echo "Hello, " . $name . "!\n"; -``` - +~~~ "#, ); From d7e7c47040a58a7102fc1defd0e13e14639980d4 Mon Sep 17 00:00:00 2001 From: Jake Deichert Date: Tue, 8 Oct 2019 00:39:20 -0400 Subject: [PATCH 2/2] Adjust wording --- src/executor.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/executor.rs b/src/executor.rs index 00cda3a..615b5f3 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -67,7 +67,7 @@ fn prepare_command_without_init_script(cmd: &Command) -> process::Command { child.arg("-r").arg(source); child } - // Any shell script that uses -c (sh, bash, zsh, fish, dash, etc...) + // Any other executor that supports -c (sh, bash, zsh, fish, dash, etc...) _ => { let mut child = process::Command::new(executor); child.arg("-c").arg(source); @@ -84,7 +84,7 @@ fn prepare_command_with_init_script(init_script: Script, cmd: &Command) -> proce "py" | "python" => run_with_init_script(&init_script, &cmd, "python -c"), "rb" | "ruby" => run_with_init_script(&init_script, &cmd, "ruby -e"), "php" => run_with_init_script(&init_script, &cmd, "php -r"), - // Any other executor that uses -c (sh, bash, zsh, fish, dash, etc...) + // Any other executor that supports -c (sh, bash, zsh, fish, dash, etc...) _ => run_with_init_script(&init_script, &cmd, &format!("{} -c", executor)), } }