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

Use relative paths when project root is set #1172

Merged
merged 11 commits into from
Aug 8, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Changed
- Include lockfile paths when analyzing projects
- Remove root path from lockfile paths

## [5.5.0] - 2023-07-18

Expand Down
12 changes: 11 additions & 1 deletion cli/src/commands/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ impl IntoIterator for ParsedLockfile {
type Item = PackageDescriptorAndLockfile;

fn into_iter(self) -> Self::IntoIter {
ParsedLockfileIterator { path: self.path, packages: self.packages.into_iter() }
// Get .phylum_project path
let root = phylum_project::get_current_project().map(|p| p.root().to_owned());
kylewillmon marked this conversation as resolved.
Show resolved Hide resolved
// Strip root path when set
let relative_path = match root {
Some(base) => match self.path.strip_prefix(&base) {
Ok(rel_path) => rel_path.to_path_buf(),
Err(_) => self.path.to_owned(),
},
None => self.path.to_owned(),
};
ParsedLockfileIterator { path: relative_path, packages: self.packages.into_iter() }
}
}

Expand Down
19 changes: 19 additions & 0 deletions cli/tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ fn parse_with_project_lockfile() {
.success()
.stdout(predicate::str::contains("\"name\": \"typescript\""));
}

#[test]
fn parse_with_project_lockfile_relative_paths() {
// Setup CLI with temp dir.
let test_cli = TestCli::builder().cwd_temp().build();
let temp_path = test_cli.temp_path();

// Write .phylum_project to temp dir.
let config = "id: 00000000-0000-0000-0000-000000000000\nname: test\ncreated_at: \
2000-01-01T00:00:00.0Z\nlockfile_path: ./package-lock.json\nlockfile_type: npm";
fs::write(temp_path.join(".phylum_project"), config).unwrap();

// Copy lockfile to temp dir.
fs::copy("../tests/fixtures/package-lock.json", temp_path.join("./package-lock.json")).unwrap();

let not_contain_var_folders =
predicate::function(|x: &str| !x.contains("\"lockfile\": \"/private/var/folders"));
test_cli.cmd().args(["parse"]).assert().success().stdout(not_contain_var_folders);
ejortega marked this conversation as resolved.
Show resolved Hide resolved
}