Skip to content

Commit

Permalink
Classify lockfiles and manifests independently (#1263)
Browse files Browse the repository at this point in the history
This patch changes the logic of `LockableFiles::at` to build independent
lockfile and manifest lists, allowing one file to be present in both.

This does not have any impact on `find_lockable_files_at`, so a file
that is both a manifest and a lockfile will still only appear once.
  • Loading branch information
cd-work authored Oct 24, 2023
1 parent 11eb4b1 commit 1c0da54
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions lockfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,20 @@ impl LockableFiles {
for format in LockfileFormat::iter() {
let parser = format.parser();

let mut format_found = false;
if parser.is_path_lockfile(path) {
lockables.lockfiles.push((path.to_path_buf(), format));
break;
} else if parser.is_path_manifest(path) {
format_found = true;
}

if parser.is_path_manifest(path) {
// Select first matching format for manifests.
lockables.manifests.push((path.to_path_buf(), format));
format_found = true;
}

// Avoid classifying lockable file as multiple formats.
if format_found {
break;
}
}
Expand Down Expand Up @@ -655,4 +663,23 @@ mod tests {
let expected = vec![(tempdir.path().join("setup.py"), LockfileFormat::Pip)];
assert_eq!(lockable_files, expected);
}

#[test]
fn no_duplicate_requirements() {
// Create desired directory structure.
let tempdir = tempfile::tempdir().unwrap();
let files = [tempdir.path().join("requirements.txt")];
for file in &files {
let dir = file.parent().unwrap();
fs::create_dir_all(dir).unwrap();
File::create(file).unwrap();
}

// Find lockable files.
let lockable_files = find_lockable_files_at(tempdir.path());

// Ensure requirements.txt is only reported once.
let expected = vec![(files[0].clone(), LockfileFormat::Pip)];
assert_eq!(lockable_files, expected);
}
}

0 comments on commit 1c0da54

Please sign in to comment.