Skip to content

Commit

Permalink
Remove commands without code blocks (#70)
Browse files Browse the repository at this point in the history
* Remove commands without code blocks

* Fix tests for commands with no scripts

* command can exist without executor

This is a design decision. It is possible that the user might have
missed giving the executor for a command. In such a case it is better
to retain the command and provide a helpful error message indicating
the problem. Telling the user that the executor is missing is better
than saying the command does not exist (because it has been pruned)

* Fix typo

* Use is_empty to check for source
  • Loading branch information
twitu authored Aug 10, 2020
1 parent 7c31651 commit f5460d9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
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.executor != ""
!self.source.is_empty()
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ fn treeify_commands(commands: Vec<Command>) -> Vec<Command> {
}
}

// the command or any one of its subcommands must have script to be included in the tree
// root level commands must be retained
command_tree.retain(|c| c.script.has_script() || !c.subcommands.is_empty() || c.cmd_level == 1);

command_tree
}

Expand Down Expand Up @@ -375,14 +379,13 @@ mod build_command_structure {
}

#[test]
fn does_not_add_verbose_optional_flag_to_command_with_no_script() {
fn does_not_add_command_with_no_script() {
let tree = build_command_structure(TEST_MASKFILE.to_string());
let no_script_command = tree
.subcommands
.iter()
.find(|cmd| cmd.name == "no_script")
.expect("no_script command missing");
let no_script_command = tree.subcommands.iter().find(|cmd| cmd.name == "no_script");

assert_eq!(no_script_command.option_flags.len(), 0);
assert!(
no_script_command.is_none(),
"no_script command should not exist"
)
}
}
10 changes: 9 additions & 1 deletion tests/arguments_and_flags_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,15 @@ mod version_flag {

#[test]
fn exits_with_error_when_subcommand_has_version_flag() {
let (_temp, maskfile_path) = common::maskfile("## foo");
let (_temp, maskfile_path) = common::maskfile(
r#"
## foo
~~~bash
echo "This subcommand should exist"
~~~
"#,
);

// The setting "VersionlessSubcommands" removes the version flags (-V, --version)
// from subcommands. Only the root command has a version flag.
Expand Down
23 changes: 4 additions & 19 deletions tests/subcommands_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ fn exits_with_error_when_missing_subcommand() {
r#"
## service
### service start
~~~bash
echo "subcommand should exist"
~~~
"#,
);

Expand Down Expand Up @@ -102,23 +106,4 @@ echo "system, online"
)))
.failure();
}

#[test]
fn exits_with_error_when_it_has_no_subcommands() {
let (_temp, maskfile_path) = common::maskfile(
r#"
## start
"#,
);

common::run_mask(&maskfile_path)
.command("start")
.assert()
.code(1)
.stderr(contains(format!(
"{} Command has no script.",
"ERROR:".red()
)))
.failure();
}
}

0 comments on commit f5460d9

Please sign in to comment.