Skip to content

Commit f7889aa

Browse files
committed
Added tests
1 parent abd0db0 commit f7889aa

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

rewatch/src/cli.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub enum FileExtension {
2121
/// ReScript - Fast, Simple, Fully Typed JavaScript from the Future
2222
#[derive(Parser, Debug)]
2323
#[command(version)]
24-
#[command(args_conflicts_with_subcommands = true)]
2524
#[command(
2625
after_help = "Note: If no command is provided, the build command is run by default. See `rescript help build` for more information."
2726
)]

rewatch/src/main.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rescript::{build, cli, cmd, format, lock, watcher};
77

88
fn main() -> Result<()> {
99
let raw_args: Vec<String> = env::args().collect();
10-
let cli = parse_cli(raw_args);
10+
let cli = parse_cli(raw_args).unwrap_or_else(|err| err.exit());
1111

1212
let log_level_filter = cli.verbose.log_level_filter();
1313

@@ -113,21 +113,21 @@ fn get_lock(folder: &str) -> lock::Lock {
113113
}
114114
}
115115

116-
fn parse_cli(raw_args: Vec<String>) -> cli::Cli {
116+
fn parse_cli(raw_args: Vec<String>) -> Result<cli::Cli, clap::Error> {
117117
match cli::Cli::try_parse_from(&raw_args) {
118-
Ok(cli) => cli,
118+
Ok(cli) => Ok(cli),
119119
Err(err) => {
120120
if should_default_to_build(&err, &raw_args) {
121121
let mut fallback_args = raw_args.clone();
122122
let insert_at = index_after_global_flags(&fallback_args);
123123
fallback_args.insert(insert_at, "build".into());
124124

125125
match cli::Cli::try_parse_from(&fallback_args) {
126-
Ok(cli) => cli,
127-
Err(fallback_err) => fallback_err.exit(),
126+
Ok(cli) => Ok(cli),
127+
Err(fallback_err) => Err(fallback_err),
128128
}
129129
} else {
130-
err.exit()
130+
Err(err)
131131
}
132132
}
133133
}
@@ -173,3 +173,51 @@ fn is_global_flag(arg: &str) -> bool {
173173
| "--version"
174174
)
175175
}
176+
177+
#[cfg(test)]
178+
mod tests {
179+
use super::*;
180+
181+
fn parse(args: &[&str]) -> Result<cli::Cli, clap::Error> {
182+
parse_cli(args.iter().map(|arg| arg.to_string()).collect())
183+
}
184+
185+
#[test]
186+
fn defaults_to_build_without_args() {
187+
let cli = parse(&["rescript"]).expect("expected default build command");
188+
189+
match cli.command {
190+
cli::Command::Build(build_args) => assert_eq!(build_args.folder.folder, "."),
191+
other => panic!("expected build command, got {other:?}"),
192+
}
193+
}
194+
195+
#[test]
196+
fn defaults_to_build_with_folder_shortcut() {
197+
let cli = parse(&["rescript", "someFolder"]).expect("expected build command");
198+
199+
match cli.command {
200+
cli::Command::Build(build_args) => assert_eq!(build_args.folder.folder, "someFolder"),
201+
other => panic!("expected build command, got {other:?}"),
202+
}
203+
}
204+
205+
#[test]
206+
fn respects_global_flag_before_subcommand() {
207+
let cli = parse(&["rescript", "-v", "watch"]).expect("expected watch command");
208+
209+
assert!(matches!(cli.command, cli::Command::Watch(_)));
210+
}
211+
212+
#[test]
213+
fn help_flag_does_not_default_to_build() {
214+
let err = parse(&["rescript", "--help"]).expect_err("expected clap help error");
215+
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
216+
}
217+
218+
#[test]
219+
fn version_flag_does_not_default_to_build() {
220+
let err = parse(&["rescript", "--version"]).expect_err("expected clap version error");
221+
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
222+
}
223+
}

0 commit comments

Comments
 (0)