Skip to content

Commit

Permalink
Merge pull request #37 from jakedeichert/allow-any-shell-executor
Browse files Browse the repository at this point in the history
Allow any shell executor that supports -c evaluation
  • Loading branch information
jacobdeichert authored Oct 9, 2019
2 parents 0819463 + d7e7c47 commit 66003ff
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 120 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Script {
}

pub fn has_script(&self) -> bool {
self.source != ""
self.source != "" && self.executor != ""
}
}

Expand Down
24 changes: 14 additions & 10 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ pub fn execute_command(
cmd: Command,
maskfile_path: String,
) -> Result<ExitStatus> {
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) {
Expand Down Expand Up @@ -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 other executor that supports -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
}
Expand All @@ -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 supports -c (sh, bash, zsh, fish, dash, etc...)
_ => run_with_init_script(&init_script, &cmd, &format!("{} -c", executor)),
}
}

Expand Down
49 changes: 49 additions & 0 deletions tests/arguments_and_flags_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
95 changes: 24 additions & 71 deletions tests/subcommands_test.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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();
}
}
Loading

0 comments on commit 66003ff

Please sign in to comment.