Skip to content

Commit

Permalink
Split out Options::allows_source_file_path and allows_mutant
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Jan 3, 2025
1 parent 64888a7 commit 90c15ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
24 changes: 23 additions & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::env;
use std::ffi::OsString;
use std::time::Duration;

use camino::Utf8PathBuf;
use camino::{Utf8Path, Utf8PathBuf};
use globset::GlobSet;
use regex::RegexSet;
use serde::Deserialize;
Expand All @@ -25,6 +25,7 @@ use tracing::warn;

use crate::config::Config;
use crate::glob::build_glob_set;
use crate::mutate::Mutant;
use crate::{Args, BaselineStrategy, Context, Phase, Result, ValueEnum};

/// Options for mutation testing, based on both command-line arguments and the
Expand Down Expand Up @@ -368,6 +369,27 @@ impl Options {
})
.collect()
}

/// True if the options allow mutants to be generated from the given path.
///
/// That is: it matches the examine globset (if specified) and does not match the exclude globset
/// (if specified).
pub fn allows_source_file_path(&self, path: &Utf8Path) -> bool {
self.examine_globset
.as_ref()
.map_or(true, |g| g.is_match(path))
&& !self
.exclude_globset
.as_ref()
.map_or(false, |g| g.is_match(path))
}

/// True if the options allow this mutant to be tested.
pub fn allows_mutant(&self, mutant: &Mutant) -> bool {
let name = mutant.name(true);
(self.examine_names.is_empty() || self.examine_names.is_match(&name))
&& (self.exclude_names.is_empty() || !self.exclude_names.is_match(&name))
}
}

/// If the first slices is non-empty, return that, otherwise the second.
Expand Down
26 changes: 9 additions & 17 deletions src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ fn walk_package(
console.walk_tree_update(files.len(), mutants.len());
check_interrupted()?;
let (mut file_mutants, external_mods) = walk_file(&source_file, error_exprs, options)?;
// TODO: It would be better not to spend time generating mutants from
// files that are not going to be visited later. However, we probably do
// still want to walk them to find modules that are referenced by them.
// since otherwise it could be pretty confusing that lower files are not
// visited.
//
// We'll still walk down through files that don't match globs, so that
// we have a chance to find modules underneath them. However, we won't
// collect any mutants from them, and they don't count as "seen" for
Expand All @@ -114,27 +120,13 @@ fn walk_package(
filename_queue.push_back((mod_path, false));
}
}
let path = &source_file.tree_relative_path;
if let Some(examine_globset) = &options.examine_globset {
if !examine_globset.is_match(path) {
trace!("{path:?} does not match examine globset");
continue;
}
}
if let Some(exclude_globset) = &options.exclude_globset {
if exclude_globset.is_match(path) {
trace!("{path:?} excluded by globset");
continue;
}
if !options.allows_source_file_path(&source_file.tree_relative_path) {
continue;
}
file_mutants.retain(|m| options.allows_mutant(m));
mutants.append(&mut file_mutants);
files.push(source_file);
}
mutants.retain(|m| {
let name = m.name(true);
(options.examine_names.is_empty() || options.examine_names.is_match(&name))
&& (options.exclude_names.is_empty() || !options.exclude_names.is_match(&name))
});
Ok(())
}

Expand Down

0 comments on commit 90c15ac

Please sign in to comment.