Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Jul 31, 2024
1 parent 1a093a7 commit 8f58a06
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 35 deletions.
23 changes: 23 additions & 0 deletions crates/turborepo-lib/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl Opts {
cmd.push_str(pattern);
}

if self.scope_opts.affected_range.is_some() {
cmd.push_str(" --affected");
}

if self.run_opts.parallel {
cmd.push_str(" --parallel");
}
Expand Down Expand Up @@ -398,6 +402,7 @@ mod test {
parallel: bool,
continue_on_error: bool,
dry_run: Option<DryRunMode>,
affected: Option<(String, String)>,
}

#[test_case(TestCaseOpts {
Expand Down Expand Up @@ -460,6 +465,23 @@ mod test {
},
"turbo run build --filter=my-app --dry=json"
)]
#[test_case (
TestCaseOpts {
filter_patterns: vec!["my-app".to_string()],
tasks: vec!["build".to_string()],
affected: Some(("HEAD".to_string(), "my-branch".to_string())),
..Default::default()
},
"turbo run build --filter=my-app --affected"
)]
#[test_case (
TestCaseOpts {
tasks: vec!["build".to_string()],
affected: Some(("HEAD".to_string(), "my-branch".to_string())),
..Default::default()
},
"turbo run build --affected"
)]
fn test_synthesize_command(opts_input: TestCaseOpts, expected: &str) {
let run_opts = RunOpts {
tasks: opts_input.tasks,
Expand Down Expand Up @@ -487,6 +509,7 @@ mod test {
pkg_inference_root: None,
global_deps: vec![],
filter_patterns: opts_input.filter_patterns,
affected_range: opts_input.affected,
};
let opts = Opts {
run_opts,
Expand Down
21 changes: 12 additions & 9 deletions crates/turborepo-lib/src/run/scope/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ mod test {
};

use super::{FilterResolver, PackageInference, TargetSelector};
use crate::run::scope::{change_detector::GitChangeDetector, ResolutionError};
use crate::run::scope::{
change_detector::GitChangeDetector, target_selector::GitRange, ResolutionError,
};

fn get_name(name: &str) -> (Option<&str>, &str) {
if let Some(idx) = name.rfind('/') {
Expand Down Expand Up @@ -1121,7 +1123,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None }),
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None, ..Default::default() }),
..Default::default()
}
],
Expand All @@ -1131,7 +1133,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None }),
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None, ..Default::default() }),
parent_dir: Some(AnchoredSystemPathBuf::try_from(".").unwrap()),
..Default::default()
}
Expand All @@ -1142,7 +1144,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None }),
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None, ..Default::default() }),
parent_dir: Some(AnchoredSystemPathBuf::try_from("package-2").unwrap()),
..Default::default()
}
Expand All @@ -1153,7 +1155,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None }),
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None, ..Default::default() }),
name_pattern: "package-2*".to_string(),
..Default::default()
}
Expand All @@ -1164,7 +1166,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None }),
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None, ..Default::default() }),
name_pattern: "package-1".to_string(),
match_dependencies: true,
..Default::default()
Expand All @@ -1176,7 +1178,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~2".to_string(), to_ref: None }),
git_range: Some(GitRange { from_ref: "HEAD~2".to_string(), to_ref: None, ..Default::default() }),
..Default::default()
}
],
Expand All @@ -1186,7 +1188,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~2".to_string(), to_ref: Some("HEAD~1".to_string()) }),
git_range: Some(GitRange { from_ref: "HEAD~2".to_string(), to_ref: Some("HEAD~1".to_string()), ..Default::default() }),
..Default::default()
}
],
Expand All @@ -1196,7 +1198,7 @@ mod test {
#[test_case(
vec![
TargetSelector {
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None }),
git_range: Some(GitRange { from_ref: "HEAD~1".to_string(), to_ref: None, ..Default::default() }),
parent_dir: Some(AnchoredSystemPathBuf::try_from("package-*").unwrap()),
match_dependencies: true, ..Default::default()
}
Expand Down Expand Up @@ -1251,6 +1253,7 @@ mod test {
from: &str,
to: Option<&str>,
_include_uncommitted: bool,
_allow_unknown_objects: bool,
) -> Result<HashSet<PackageName>, ResolutionError> {
Ok(self
.0
Expand Down
24 changes: 12 additions & 12 deletions crates/turborepo-lib/src/run/scope/target_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use regex::Regex;
use thiserror::Error;
use turbopath::AnchoredSystemPathBuf;

#[derive(Debug, PartialEq)]
#[derive(Debug, Default, PartialEq)]
pub struct GitRange {
pub from_ref: String,
pub to_ref: Option<String>,
Expand Down Expand Up @@ -252,17 +252,17 @@ mod test {
#[test_case("...{./foo}", TargetSelector { raw: "...{./foo}".to_string(), parent_dir: Some(AnchoredSystemPathBuf::try_from("foo").unwrap()), include_dependents: true, ..Default::default() }; "dot dot dot curly bracket foo")]
#[test_case(".", TargetSelector { raw: ".".to_string(), parent_dir: Some(AnchoredSystemPathBuf::try_from(".").unwrap()), ..Default::default() }; "parent dir dot")]
#[test_case("..", TargetSelector { raw: "..".to_string(), parent_dir: Some(AnchoredSystemPathBuf::try_from("..").unwrap()), ..Default::default() }; "parent dir dot dot")]
#[test_case("[master]", TargetSelector { raw: "[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), ..Default::default() }; "square brackets master")]
#[test_case("[from...to]", TargetSelector { raw: "[from...to]".to_string(), git_range: Some(GitRange { from_ref: "from".to_string(), to_ref: Some("to".to_string()) }), ..Default::default() }; "[from...to]")]
#[test_case("{foo}[master]", TargetSelector { raw: "{foo}[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), parent_dir: Some(AnchoredSystemPathBuf::try_from("foo").unwrap()), ..Default::default() }; "{foo}[master]")]
#[test_case("pattern{foo}[master]", TargetSelector { raw: "pattern{foo}[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), parent_dir: Some(AnchoredSystemPathBuf::try_from("foo").unwrap()), name_pattern: "pattern".to_string(), ..Default::default() }; "pattern{foo}[master]")]
#[test_case("[master]...", TargetSelector { raw: "[master]...".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), include_dependencies: true, ..Default::default() }; "square brackets master dot dot dot")]
#[test_case("...[master]", TargetSelector { raw: "...[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), include_dependents: true, ..Default::default() }; "dot dot dot master square brackets")]
#[test_case("...[master]...", TargetSelector { raw: "...[master]...".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), include_dependencies: true, include_dependents: true, ..Default::default() }; "dot dot dot master square brackets dot dot dot")]
#[test_case("...[from...to]...", TargetSelector { raw: "...[from...to]...".to_string(), git_range: Some(GitRange { from_ref: "from".to_string(), to_ref: Some("to".to_string()) }), include_dependencies: true, include_dependents: true, ..Default::default() }; "dot dot dot [from...to] dot dot dot")]
#[test_case("foo...[master]", TargetSelector { raw: "foo...[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), name_pattern: "foo".to_string(), match_dependencies: true, ..Default::default() }; "foo...[master]")]
#[test_case("foo...[master]...", TargetSelector { raw: "foo...[master]...".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), name_pattern: "foo".to_string(), match_dependencies: true, include_dependencies: true, ..Default::default() }; "foo...[master] dot dot dot")]
#[test_case("{foo}...[master]", TargetSelector { raw: "{foo}...[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None }), parent_dir: Some(AnchoredSystemPathBuf::try_from("foo").unwrap()), match_dependencies: true, ..Default::default() }; "curly brackets foo...[master]")]
#[test_case("[master]", TargetSelector { raw: "[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), ..Default::default() }; "square brackets master")]
#[test_case("[from...to]", TargetSelector { raw: "[from...to]".to_string(), git_range: Some(GitRange { from_ref: "from".to_string(), to_ref: Some("to".to_string()), ..Default::default() }), ..Default::default() }; "[from...to]")]
#[test_case("{foo}[master]", TargetSelector { raw: "{foo}[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), parent_dir: Some(AnchoredSystemPathBuf::try_from("foo").unwrap()), ..Default::default() }; "{foo}[master]")]
#[test_case("pattern{foo}[master]", TargetSelector { raw: "pattern{foo}[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), parent_dir: Some(AnchoredSystemPathBuf::try_from("foo").unwrap()), name_pattern: "pattern".to_string(), ..Default::default() }; "pattern{foo}[master]")]
#[test_case("[master]...", TargetSelector { raw: "[master]...".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), include_dependencies: true, ..Default::default() }; "square brackets master dot dot dot")]
#[test_case("...[master]", TargetSelector { raw: "...[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), include_dependents: true, ..Default::default() }; "dot dot dot master square brackets")]
#[test_case("...[master]...", TargetSelector { raw: "...[master]...".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), include_dependencies: true, include_dependents: true, ..Default::default() }; "dot dot dot master square brackets dot dot dot")]
#[test_case("...[from...to]...", TargetSelector { raw: "...[from...to]...".to_string(), git_range: Some(GitRange { from_ref: "from".to_string(), to_ref: Some("to".to_string()), ..Default::default() }), include_dependencies: true, include_dependents: true, ..Default::default() }; "dot dot dot [from...to] dot dot dot")]
#[test_case("foo...[master]", TargetSelector { raw: "foo...[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), name_pattern: "foo".to_string(), match_dependencies: true, ..Default::default() }; "foo...[master]")]
#[test_case("foo...[master]...", TargetSelector { raw: "foo...[master]...".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), name_pattern: "foo".to_string(), match_dependencies: true, include_dependencies: true, ..Default::default() }; "foo...[master] dot dot dot")]
#[test_case("{foo}...[master]", TargetSelector { raw: "{foo}...[master]".to_string(), git_range: Some(GitRange { from_ref: "master".to_string(), to_ref: None, include_uncommitted: true, ..Default::default() }), parent_dir: Some(AnchoredSystemPathBuf::try_from("foo").unwrap()), match_dependencies: true, ..Default::default() }; " curly brackets foo...[master]")]
fn parse_target_selector(raw_selector: &str, want: TargetSelector) {
let result = TargetSelector::from_str(raw_selector);

Expand Down
50 changes: 39 additions & 11 deletions crates/turborepo-scm/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ mod tests {

use git2::{Oid, Repository};
use tempfile::TempDir;
use turbopath::{AbsoluteSystemPathBuf, PathError};
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf, PathError};
use which::which;

use super::previous_content;
use crate::{git::changed_files, Error, Git, SCM};
use super::{previous_content, ChangedFiles};
use crate::{Error, SCM};

fn setup_repository() -> Result<(TempDir, Repository), Error> {
let repo_root = tempfile::tempdir()?;
Expand All @@ -234,7 +234,6 @@ mod tests {
Ok((repo_root, repo))
}


fn changed_files(
git_root: PathBuf,
turbo_root: PathBuf,
Expand All @@ -246,18 +245,24 @@ mod tests {
let scm = SCM::new(git_root);

let turbo_root = AbsoluteSystemPathBuf::try_from(turbo_root.as_path())?;
let files = scm.changed_files(&turbo_root, from_commit, to_commit, include_uncommitted)?;
let ChangedFiles::Some(files) = scm.changed_files(
&turbo_root,
from_commit,
to_commit,
include_uncommitted,
false,
)?
else {
unreachable!("changed_files should always return Some");
};

Ok(files
.into_iter()
.map(|f| f.to_string())
.collect::<HashSet<_>>())
}

fn commit_file(
repo: &Repository,
path: &Path,
previous_commit: Option<Oid>,
) -> Oid {
fn commit_file(repo: &Repository, path: &Path, previous_commit: Option<Oid>) -> Oid {
let mut index = repo.index().unwrap();
index.add_path(path).unwrap();
let tree_oid = index.write_tree().unwrap();
Expand Down Expand Up @@ -323,6 +328,7 @@ mod tests {
tmp_dir.path().to_owned(),
"HEAD~1",
Some("HEAD"),
false,
)
.is_ok());

Expand All @@ -331,6 +337,7 @@ mod tests {
tmp_dir.path().to_owned(),
"HEAD",
None,
true,
)
.is_ok());

Expand All @@ -353,7 +360,13 @@ mod tests {
let first_commit_sha = first_commit_oid.to_string();
let git_root = repo_root.path().to_owned();
let turborepo_root = repo_root.path().to_owned();
let files = changed_files(git_root, turborepo_root, &first_commit_sha, Some("HEAD"))?;
let files = changed_files(
git_root,
turborepo_root,
&first_commit_sha,
Some("HEAD"),
false,
)?;

assert_eq!(files, HashSet::from(["foo.js".to_string()]));
Ok(())
Expand Down Expand Up @@ -396,6 +409,7 @@ mod tests {
repo_root.path().to_path_buf(),
&third_commit_oid.to_string(),
Some(&fourth_commit_oid.to_string()),
false,
)?;

assert_eq!(
Expand Down Expand Up @@ -427,6 +441,7 @@ mod tests {
turbo_root.to_path_buf(),
"HEAD",
None,
true,
)?;
assert_eq!(files, HashSet::from(["bar.js".to_string()]));

Expand All @@ -440,6 +455,7 @@ mod tests {
turbo_root.to_path_buf(),
"HEAD",
None,
true,
)?;
assert_eq!(files, HashSet::from(["bar.js".to_string()]));

Expand All @@ -452,6 +468,7 @@ mod tests {
turbo_root.to_path_buf(),
first_commit_oid.to_string().as_str(),
Some(second_commit_oid.to_string().as_str()),
false,
)?;
assert_eq!(files, HashSet::from(["bar.js".to_string()]));

Expand All @@ -466,6 +483,7 @@ mod tests {
repo_root.path().to_path_buf(),
first_commit_oid.to_string().as_str(),
Some(second_commit_oid.to_string().as_str()),
false,
)?;
assert_eq!(files, HashSet::from(["bar.js".to_string()]));

Expand All @@ -475,6 +493,7 @@ mod tests {
repo_root.path().to_path_buf(),
second_commit_oid.to_string().as_str(),
None,
true,
)?;
assert_eq!(
files,
Expand All @@ -494,6 +513,7 @@ mod tests {
repo_root.path().join("subdir"),
first_commit_oid.to_string().as_str(),
Some(third_commit_oid.to_string().as_str()),
false,
)?;
assert_eq!(files, HashSet::from(["baz.js".to_string()]));

Expand All @@ -520,6 +540,7 @@ mod tests {
repo_root.path().to_path_buf(),
"HEAD",
None,
true,
)?;
assert_eq!(files, HashSet::from(["bar.js".to_string()]));

Expand Down Expand Up @@ -549,6 +570,7 @@ mod tests {
repo_root.path().join("subdir"),
"HEAD",
None,
true,
)?;

#[cfg(unix)]
Expand Down Expand Up @@ -576,6 +598,7 @@ mod tests {
.to_string()
.as_str(),
),
false,
)?;

#[cfg(unix)]
Expand Down Expand Up @@ -650,6 +673,7 @@ mod tests {
repo_root.path().to_path_buf(),
"HEAD^",
Some("HEAD"),
false,
)?;
assert_eq!(files, HashSet::from(["foo.js".to_string()]));

Expand All @@ -667,6 +691,7 @@ mod tests {
repo_root.path().to_path_buf(),
"HEAD~1",
Some("release-1"),
false,
)?;
assert_eq!(files, HashSet::from(["bar.js".to_string()]));

Expand All @@ -681,6 +706,7 @@ mod tests {
repo_dir.path().to_path_buf(),
"HEAD",
None,
true,
);

assert_matches!(repo_does_not_exist, Err(Error::GitRequired(_)));
Expand All @@ -693,6 +719,7 @@ mod tests {
repo_root.path().to_path_buf(),
"does-not-exist",
None,
true,
);

assert_matches!(commit_does_not_exist, Err(Error::Git(_, _)));
Expand All @@ -710,6 +737,7 @@ mod tests {
turbo_root.path().to_path_buf(),
"HEAD",
None,
true,
);

assert_matches!(
Expand Down
Loading

0 comments on commit 8f58a06

Please sign in to comment.