Skip to content

Commit

Permalink
chore: test ignored directories correctly
Browse files Browse the repository at this point in the history
The previous test wasn't testing properly ignored files: indeed, the
`.gitignore` for this very repository (that of cargo-machete) would be
taken into account, instead of the one we wanted.

To make this clearer, the code now uses an `.ignore` file in the main
directory, and a special option is introduced for testing purposes, to
make it possible to disable respecting `.gitignore` (while respecting
other ignore options).

Fixes #140.
  • Loading branch information
bnjbvr committed Jan 14, 2025
1 parent 83604af commit e550550
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@ struct MacheteArgs {
}

struct CollectPathOptions {
/// Should we avoid scanning `target` directories?
skip_target_dir: bool,

/// Should we ignore files as specified in .gitignore (in the target directory, or any parent),
/// and `.ignore`?
respect_ignore_files: bool,

// As an override to the above `respect_ignore_files`, should we use `.gitignore` overall?
//
// This is used only in testing, to avoid reading this repository's `.gitignore` file for
// testing the `collect_path()` function.
override_respect_git_ignore: Option<bool>,
}

fn collect_paths(path: &Path, options: CollectPathOptions) -> Result<Vec<PathBuf>, ignore::Error> {
Expand All @@ -69,6 +79,10 @@ fn collect_paths(path: &Path, options: CollectPathOptions) -> Result<Vec<PathBuf

builder.standard_filters(options.respect_ignore_files);

if let Some(val) = options.override_respect_git_ignore {
builder.git_ignore(val);
}

if options.skip_target_dir {
builder.filter_entry(|entry| !entry.path().ends_with("target"));
}
Expand Down Expand Up @@ -139,6 +153,7 @@ fn run_machete() -> anyhow::Result<bool> {
CollectPathOptions {
skip_target_dir: args.skip_target_dir,
respect_ignore_files: !args.no_ignore,
override_respect_git_ignore: None,
},
) {
Ok(entries) => entries,
Expand Down Expand Up @@ -296,6 +311,7 @@ fn test_ignore_target() {
CollectPathOptions {
skip_target_dir: true,
respect_ignore_files: false,
override_respect_git_ignore: Some(false),
},
);
assert!(entries.unwrap().is_empty());
Expand All @@ -305,6 +321,7 @@ fn test_ignore_target() {
CollectPathOptions {
skip_target_dir: false,
respect_ignore_files: true,
override_respect_git_ignore: Some(false),
},
);
assert!(entries.unwrap().is_empty());
Expand All @@ -314,6 +331,7 @@ fn test_ignore_target() {
CollectPathOptions {
skip_target_dir: false,
respect_ignore_files: false,
override_respect_git_ignore: Some(false),
},
);
assert!(!entries.unwrap().is_empty());
Expand Down

0 comments on commit e550550

Please sign in to comment.