Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(shim): add unit tests for shim parsing #9248

Merged
merged 2 commits into from
Oct 15, 2024
Merged
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
207 changes: 202 additions & 5 deletions crates/turborepo-lib/src/shim/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct MultipleCwd {
flag4: Option<SourceSpan>,
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct ShimArgs {
pub cwd: AbsoluteSystemPathBuf,
pub invocation_dir: AbsoluteSystemPathBuf,
Expand All @@ -56,6 +56,14 @@ pub struct ShimArgs {

impl ShimArgs {
pub fn parse() -> Result<Self, Error> {
let invocation_dir = AbsoluteSystemPathBuf::cwd()?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We make this call here so testing is easier. It also means we avoid calling std::env::current_dir once for each --cwd flag and reuse this value.

The only behavior change from this move is that if you:

  • pass the args turbo --cwd
  • Execute turbo in a directory where current_dir would fail (process doesn't have permission to access cwd or cwd doesn't exist)
    Then we will now report the current_dir failure instead of complaining that you didn't pass a value for the --cwd flag.

Self::parse_from_iter(invocation_dir, std::env::args())
}

fn parse_from_iter(
invocation_dir: AbsoluteSystemPathBuf,
args: impl Iterator<Item = String>,
) -> Result<Self, Error> {
let mut cwd_flag_idx = None;
let mut cwds = Vec::new();
let mut skip_infer = false;
Expand All @@ -68,7 +76,7 @@ impl ShimArgs {
let mut color = false;
let mut no_color = false;

let args = env::args().skip(1);
let args = args.skip(1);
for (idx, arg) in args.enumerate() {
// We've seen a `--` and therefore we do no parsing
if is_forwarded_args {
Expand Down Expand Up @@ -100,15 +108,21 @@ impl ShimArgs {
} else if cwd_flag_idx.is_some() {
// We've seen a `--cwd` and therefore add this to the cwds list along with
// the index of the `--cwd` (*not* the value)
cwds.push((AbsoluteSystemPathBuf::from_cwd(arg)?, idx - 1));
cwds.push((
AbsoluteSystemPathBuf::from_unknown(&invocation_dir, arg),
idx - 1,
));
cwd_flag_idx = None;
} else if arg == "--cwd" {
// If we see a `--cwd` we expect the next arg to be a path.
cwd_flag_idx = Some(idx)
} else if let Some(cwd_arg) = arg.strip_prefix("--cwd=") {
// In the case where `--cwd` is passed as `--cwd=./path/to/foo`, that
// entire chunk is a single arg, so we need to split it up.
cwds.push((AbsoluteSystemPathBuf::from_cwd(cwd_arg)?, idx));
cwds.push((
AbsoluteSystemPathBuf::from_unknown(&invocation_dir, cwd_arg),
idx,
));
} else if arg == "--color" {
color = true;
} else if arg == "--no-color" {
Expand Down Expand Up @@ -146,7 +160,6 @@ impl ShimArgs {
})));
}

let invocation_dir = AbsoluteSystemPathBuf::cwd()?;
let cwd = cwds
.pop()
.map(|(cwd, _)| cwd)
Expand Down Expand Up @@ -258,7 +271,9 @@ impl ShimArgs {
#[cfg(test)]
mod test {
use miette::SourceSpan;
use pretty_assertions::assert_eq;
use test_case::test_case;
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};

use super::ShimArgs;

Expand All @@ -275,4 +290,186 @@ mod test {
ShimArgs::get_spans_in_args_string(arg_indices, args.into_iter());
assert_eq!(indices_in_args_string, expected_indices_in_arg_string);
}

#[derive(Default)]
struct ExpectedArgs {
pub skip_infer: bool,
pub verbosity: usize,
pub force_update_check: bool,
pub remaining_turbo_args: &'static [&'static str],
pub forwarded_args: &'static [&'static str],
pub color: bool,
pub no_color: bool,
pub relative_cwd: Option<&'static [&'static str]>,
}

impl ExpectedArgs {
fn build(self, invocation_dir: &AbsoluteSystemPath) -> ShimArgs {
let Self {
skip_infer,
verbosity,
force_update_check,
remaining_turbo_args,
forwarded_args,
color,
no_color,
relative_cwd,
} = self;
ShimArgs {
cwd: relative_cwd.map_or_else(
|| invocation_dir.to_owned(),
|components| invocation_dir.join_components(components),
),
invocation_dir: invocation_dir.to_owned(),
remaining_turbo_args: remaining_turbo_args
.iter()
.map(|arg| arg.to_string())
.collect(),
forwarded_args: forwarded_args.iter().map(|arg| arg.to_string()).collect(),
skip_infer,
verbosity,
force_update_check,
color,
no_color,
}
}
}

#[test_case(
&["turbo"],
ExpectedArgs {
..Default::default()
}
; "no args"
)]
#[test_case(
&["turbo", "-v"],
ExpectedArgs {
verbosity: 1,
remaining_turbo_args: &["-v"],
..Default::default()
}
; "verbosity count 1"
)]
#[test_case(
&["turbo", "-vv"],
ExpectedArgs {
verbosity: 2,
remaining_turbo_args: &["-vv"],
..Default::default()
}
; "verbosity count 2"
)]
#[test_case(
&["turbo", "--verbosity", "3"],
ExpectedArgs {
verbosity: 3,
remaining_turbo_args: &["--verbosity", "3"],
..Default::default()
}
; "verbosity flag 3"
)]
#[test_case(
&["turbo", "--verbosity=3"],
ExpectedArgs {
verbosity: 3,
remaining_turbo_args: &["--verbosity=3"],
..Default::default()
}
; "verbosity equals 3"
)]
#[test_case(
&["turbo", "--verbosity=3", "-vv"],
ExpectedArgs {
verbosity: 2,
remaining_turbo_args: &["--verbosity=3", "-vv"],
..Default::default()
}
; "multi verbosity"
)]
#[test_case(
&["turbo", "--color"],
ExpectedArgs {
color: true,
..Default::default()
}
; "color"
)]
#[test_case(
&["turbo", "--no-color"],
ExpectedArgs {
no_color: true,
..Default::default()
}
; "no color"
)]
#[test_case(
&["turbo", "--no-color", "--color"],
ExpectedArgs {
color: true,
no_color: true,
..Default::default()
}
; "confused color"
)]
#[test_case(
&["turbo", "--skip-infer"],
ExpectedArgs {
skip_infer: true,
..Default::default()
}
; "skip infer"
)]
#[test_case(
&["turbo", "--", "another", "--skip-infer"],
ExpectedArgs {
forwarded_args: &["another", "--skip-infer"],
..Default::default()
}
; "forwarded args"
)]
#[test_case(
&["turbo", "--check-for-update"],
ExpectedArgs {
force_update_check: true,
..Default::default()
}
; "check for update"
)]
#[test_case(
&["turbo", "--check-for-update=true"],
ExpectedArgs {
force_update_check: false,
remaining_turbo_args: &["--check-for-update=true"],
..Default::default()
}
; "check for update value"
)]
#[test_case(
&["turbo", "--cwd", "another-dir"],
ExpectedArgs {
relative_cwd: Some(&["another-dir"]),
..Default::default()
}
; "cwd value"
)]
#[test_case(
&["turbo", "--cwd=another-dir"],
ExpectedArgs {
relative_cwd: Some(&["another-dir"]),
..Default::default()
}
; "cwd equals"
)]
fn test_shim_parsing(args: &[&str], expected: ExpectedArgs) {
let cwd = AbsoluteSystemPathBuf::new(if cfg!(windows) {
"Z:\\some\\dir"
} else {
"/some/dir"
})
.unwrap();
let expected = expected.build(&cwd);
let actual = ShimArgs::parse_from_iter(cwd, args.iter().map(|s| s.to_string())).unwrap();
assert_eq!(expected, actual);
}
}
Loading