Skip to content

Commit

Permalink
Ensure lockfiles are files
Browse files Browse the repository at this point in the history
This patch introduces a metadata check for lockfile and manifest paths
to ensure that they're files instead of directories.

Closes #1177.
  • Loading branch information
cd-work committed Aug 17, 2023
1 parent 725f5ad commit a56ff06
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lockfile/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ impl Parse for Cargo {
}

fn is_path_lockfile(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("Cargo.lock"))
path.file_name() == Some(OsStr::new("Cargo.lock")) && path.is_file()
}

fn is_path_manifest(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("Cargo.toml"))
path.file_name() == Some(OsStr::new("Cargo.toml")) && path.is_file()
}

#[cfg(feature = "generator")]
Expand Down
2 changes: 1 addition & 1 deletion lockfile/src/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Parse for PackagesLock {
};

// Accept both `packages.lock.json` and `packages.<project_name>.lock.json`.
file_name.starts_with("packages.") && file_name.ends_with(".lock.json")
file_name.starts_with("packages.") && file_name.ends_with(".lock.json") && path.is_file()
}

fn is_path_manifest(&self, path: &Path) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions lockfile/src/golang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ impl Parse for GoSum {
}

fn is_path_lockfile(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("go.sum"))
path.file_name() == Some(OsStr::new("go.sum")) && path.is_file()
}

fn is_path_manifest(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("go.mod"))
path.file_name() == Some(OsStr::new("go.mod")) && path.is_file()
}

#[cfg(feature = "generator")]
Expand Down
4 changes: 2 additions & 2 deletions lockfile/src/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ impl Parse for GradleLock {
}

fn is_path_lockfile(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("gradle.lockfile"))
path.file_name() == Some(OsStr::new("gradle.lockfile")) && path.is_file()
}

fn is_path_manifest(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("build.gradle"))
path.file_name() == Some(OsStr::new("build.gradle")) && path.is_file()
}

#[cfg(feature = "generator")]
Expand Down
7 changes: 4 additions & 3 deletions lockfile/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ impl Parse for PackageLock {

fn is_path_lockfile(&self, path: &Path) -> bool {
let file_name = path.file_name();
file_name == Some(OsStr::new("package-lock.json"))
|| file_name == Some(OsStr::new("npm-shrinkwrap.json"))
(file_name == Some(OsStr::new("package-lock.json"))
|| file_name == Some(OsStr::new("npm-shrinkwrap.json")))
&& path.is_file()
}

fn is_path_manifest(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("package.json"))
path.file_name() == Some(OsStr::new("package.json")) && path.is_file()
}

#[cfg(feature = "generator")]
Expand Down
8 changes: 7 additions & 1 deletion lockfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,14 @@ mod tests {
(".spdx.yaml", LockfileFormat::Spdx),
];

let dir = tempfile::tempdir().unwrap();

for (file, expected_type) in test_cases {
let pkg_type = get_path_format(Path::new(file));
// Create file, so we can read its metadata.
let path = dir.path().join(file);
File::create(&path).unwrap();

let pkg_type = get_path_format(&path);
assert_eq!(pkg_type, Some(*expected_type), "{}", file);
}
}
Expand Down
7 changes: 4 additions & 3 deletions lockfile/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ impl Parse for PyRequirements {
}

fn is_path_lockfile(&self, path: &Path) -> bool {
is_requirements_file(path)
is_requirements_file(path) && path.is_file()
}

fn is_path_manifest(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("requirements.in"))
(path.file_name() == Some(OsStr::new("requirements.in"))
|| path.file_name() == Some(OsStr::new("pyproject.toml"))
|| path.file_name() == Some(OsStr::new("setup.py"))
|| is_requirements_file(path)
|| is_requirements_file(path))
&& path.is_file()
}

#[cfg(feature = "generator")]
Expand Down
4 changes: 2 additions & 2 deletions lockfile/src/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ impl Parse for GemLock {
}

fn is_path_lockfile(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("Gemfile.lock"))
path.file_name() == Some(OsStr::new("Gemfile.lock")) && path.is_file()
}

fn is_path_manifest(&self, path: &Path) -> bool {
path.file_name() == Some(OsStr::new("Gemfile"))
path.file_name() == Some(OsStr::new("Gemfile")) && path.is_file()
}

#[cfg(feature = "generator")]
Expand Down
5 changes: 3 additions & 2 deletions lockfile/src/spdx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ impl Parse for Spdx {
}

fn is_path_lockfile(&self, path: &Path) -> bool {
path.ends_with(".spdx.json")
(path.ends_with(".spdx.json")
|| path.ends_with(".spdx.yaml")
|| path.ends_with(".spdx.yml")
|| path.ends_with(".spdx")
|| path.ends_with(".spdx"))
&& path.is_file()
}

fn is_path_manifest(&self, _path: &Path) -> bool {
Expand Down

0 comments on commit a56ff06

Please sign in to comment.