Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,12 @@ pub struct TestFlags {
pub reporter: TestReporterConfig,
pub junit_path: Option<String>,
pub hide_stacktraces: bool,
/// Run only test modules affected by files changed in git.
/// `None` when `--changed` is absent, `Some(None)` for `--changed` with no
/// value (uncommitted changes), `Some(Some(ref))` for `--changed=<ref>`.
pub changed: Option<Option<String>>,
/// Run only test modules that depend on the given source files (`--related`).
pub related: Vec<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Default)]
Expand Down Expand Up @@ -4914,6 +4920,29 @@ or <c>**/__tests__/**</>:
.action(ArgAction::Append)
.value_hint(ValueHint::AnyPath),
)
.arg(
Arg::new("changed")
.long("changed")
.help(cstr!("Run only test modules affected by files changed in git.
<p(245)>With no value, uses uncommitted changes (staged, unstaged and untracked).
Pass a git ref to compare against, e.g. --changed=main or --changed=HEAD~1.</>"))
.value_name("REF")
.num_args(0..=1)
.require_equals(true)
.conflicts_with("watch")
.help_heading(TEST_HEADING),
)
.arg(
Arg::new("related")
.long("related")
.help("Run only test modules that depend on the given source files")
.num_args(1..)
.require_equals(true)
.action(ArgAction::Append)
.value_hint(ValueHint::AnyPath)
.conflicts_with("watch")
.help_heading(TEST_HEADING),
)
.arg(
watch_arg(true)
.conflicts_with("no-run")
Expand Down Expand Up @@ -8343,6 +8372,19 @@ fn test_parse(

let hide_stacktraces = matches.get_flag("hide-stacktraces");

let changed = if matches.contains_id("changed") {
Some(matches.remove_one::<String>("changed"))
} else {
None
};

let related = match matches.remove_many::<String>("related") {
Some(f) => f
.flat_map(flat_escape_split_commas)
.collect::<Result<_, _>>()?,
None => vec![],
};

flags.subcommand = DenoSubcommand::Test(TestFlags {
no_run,
doc,
Expand All @@ -8362,6 +8404,8 @@ fn test_parse(
reporter,
junit_path,
hide_stacktraces,
changed,
related,
});
Ok(())
}
Expand Down Expand Up @@ -12781,6 +12825,8 @@ mod tests {
reporter: Default::default(),
junit_path: None,
hide_stacktraces: false,
changed: None,
related: vec![],
}),
no_npm: true,
no_remote: true,
Expand Down Expand Up @@ -12890,6 +12936,8 @@ mod tests {
reporter: Default::default(),
junit_path: None,
hide_stacktraces: false,
changed: None,
related: vec![],
}),
type_check_mode: TypeCheckMode::Local,
permissions: PermissionFlags {
Expand Down Expand Up @@ -12936,6 +12984,8 @@ mod tests {
reporter: Default::default(),
junit_path: None,
hide_stacktraces: false,
changed: None,
related: vec![],
}),
permissions: PermissionFlags {
no_prompt: true,
Expand All @@ -12948,6 +12998,62 @@ mod tests {
);
}

#[test]
fn test_changed() {
let r = flags_from_vec(svec!["deno", "test", "--changed"]);
assert_eq!(
r.unwrap().subcommand,
DenoSubcommand::Test(TestFlags {
changed: Some(None),
..Default::default()
})
);

let r = flags_from_vec(svec!["deno", "test", "--changed=origin/main"]);
assert_eq!(
r.unwrap().subcommand,
DenoSubcommand::Test(TestFlags {
changed: Some(Some("origin/main".to_string())),
..Default::default()
})
);

// space-separated value is not allowed (would be ambiguous with file args)
let r = flags_from_vec(svec!["deno", "test", "--changed", "HEAD~1"]);
assert_eq!(
r.unwrap().subcommand,
DenoSubcommand::Test(TestFlags {
changed: Some(None),
files: FileFlags {
include: vec!["HEAD~1".to_string()],
ignore: vec![],
},
..Default::default()
})
);
}

#[test]
fn test_related() {
let r =
flags_from_vec(svec!["deno", "test", "--related=src/a.ts,src/b.ts"]);
assert_eq!(
r.unwrap().subcommand,
DenoSubcommand::Test(TestFlags {
related: svec!["src/a.ts", "src/b.ts"],
..Default::default()
})
);
}

#[test]
fn test_changed_conflicts_with_watch() {
let r = flags_from_vec(svec!["deno", "test", "--changed", "--watch"]);
assert!(r.is_err());
let r = flags_from_vec(svec!["deno", "test", "--related=a.ts", "--watch"]);
assert!(r.is_err());
}

#[test]
fn test_reporter() {
let r = flags_from_vec(svec!["deno", "test", "--reporter=pretty"]);
Expand Down Expand Up @@ -13076,6 +13182,8 @@ mod tests {
reporter: Default::default(),
junit_path: None,
hide_stacktraces: false,
changed: None,
related: vec![],
}),
permissions: PermissionFlags {
no_prompt: true,
Expand Down Expand Up @@ -13115,6 +13223,8 @@ mod tests {
reporter: Default::default(),
junit_path: None,
hide_stacktraces: false,
changed: None,
related: vec![],
}),
permissions: PermissionFlags {
no_prompt: true,
Expand Down Expand Up @@ -13153,6 +13263,8 @@ mod tests {
reporter: Default::default(),
junit_path: None,
hide_stacktraces: false,
changed: None,
related: vec![],
}),
permissions: PermissionFlags {
no_prompt: true,
Expand Down Expand Up @@ -13198,6 +13310,8 @@ mod tests {
reporter: Default::default(),
junit_path: None,
hide_stacktraces: false,
changed: None,
related: vec![],
}),
type_check_mode: TypeCheckMode::Local,
permissions: PermissionFlags {
Expand Down
Loading
Loading