Skip to content

Commit 1499972

Browse files
committed
Clean up discovery API
1 parent 54c0af6 commit 1499972

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

src/lab.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::console::Console;
1616
use crate::outcome::{LabOutcome, Phase, PhaseResult, ScenarioOutcome};
1717
use crate::output::OutputDir;
1818
use crate::process::Process;
19-
use crate::visit::discover_mutants;
2019
use crate::*;
2120

2221
/// Run all possible mutation experiments.
@@ -37,7 +36,7 @@ pub fn test_unmutated_then_all_mutants(
3736
let output_dir = OutputDir::new(output_in_dir)?;
3837
console.set_debug_log(output_dir.open_debug_log()?);
3938

40-
let mut mutants = discover_mutants(tool, source_tree, &options)?;
39+
let mut mutants = walk_tree(tool, source_tree, &options)?.mutants;
4140
if options.shuffle {
4241
fastrand::shuffle(&mut mutants);
4342
}

src/main.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::path::Utf8PathSlashes;
5151
use crate::scenario::Scenario;
5252
use crate::source::SourceFile;
5353
use crate::tool::Tool;
54-
use crate::visit::{discover_files, discover_mutants};
54+
use crate::visit::walk_tree;
5555

5656
const VERSION: &str = env!("CARGO_PKG_VERSION");
5757
const NAME: &str = env!("CARGO_PKG_NAME");
@@ -217,15 +217,15 @@ fn main() -> Result<()> {
217217
if args.list_files {
218218
list_files(&tool, &source_tree_root, &options, args.json)?;
219219
} else if args.list {
220-
let mutants = discover_mutants(&tool, &source_tree_root, &options)?;
220+
let discovered = walk_tree(&tool, &source_tree_root, &options)?;
221221
if args.json {
222222
if args.diff {
223223
eprintln!("--list --diff --json is not (yet) supported");
224224
exit(exit_code::USAGE);
225225
}
226-
serde_json::to_writer_pretty(io::BufWriter::new(io::stdout()), &mutants)?;
226+
serde_json::to_writer_pretty(io::BufWriter::new(io::stdout()), &discovered.mutants)?;
227227
} else {
228-
console::list_mutants(&mutants, args.diff);
228+
console::list_mutants(&discovered.mutants, args.diff);
229229
}
230230
} else {
231231
let lab_outcome =
@@ -236,11 +236,12 @@ fn main() -> Result<()> {
236236
}
237237

238238
fn list_files(tool: &dyn Tool, source: &Utf8Path, options: &Options, json: bool) -> Result<()> {
239-
let files = discover_files(tool, source, options)?;
239+
let discovered = walk_tree(tool, source, options)?;
240240
let mut out = io::BufWriter::new(io::stdout());
241241
if json {
242242
let json_list = Value::Array(
243-
files
243+
discovered
244+
.files
244245
.iter()
245246
.map(|source_file| {
246247
json!({
@@ -253,7 +254,7 @@ fn list_files(tool: &dyn Tool, source: &Utf8Path, options: &Options, json: bool)
253254
);
254255
serde_json::to_writer_pretty(out, &json_list)?;
255256
} else {
256-
for file in files {
257+
for file in discovered.files {
257258
writeln!(out, "{}", file.tree_relative_path)?;
258259
}
259260
}

src/mutate.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ mod test {
263263
let tool = CargoTool::new();
264264
let source_tree = tool.find_root(tree_path).unwrap();
265265
let options = Options::default();
266-
let mutants = discover_mutants(&tool, &source_tree, &options).unwrap();
266+
let mutants = walk_tree(&tool, &source_tree, &options).unwrap().mutants;
267267
assert_eq!(mutants.len(), 2);
268268
assert_eq!(
269269
format!("{:?}", mutants[0]),
@@ -288,7 +288,9 @@ mod test {
288288
let tree_path = Utf8Path::new("testdata/tree/hang_avoided_by_attr");
289289
let tool = CargoTool::new();
290290
let source_tree = tool.find_root(tree_path).unwrap();
291-
let mutants = discover_mutants(&tool, &source_tree, &Options::default()).unwrap();
291+
let mutants = walk_tree(&tool, &source_tree, &Options::default())
292+
.unwrap()
293+
.mutants;
292294
let descriptions = mutants.iter().map(Mutant::describe_change).collect_vec();
293295
insta::assert_snapshot!(
294296
descriptions.join("\n"),
@@ -301,7 +303,9 @@ mod test {
301303
let tree_path = Utf8Path::new("testdata/tree/factorial");
302304
let tool = CargoTool::new();
303305
let source_tree = tool.find_root(tree_path).unwrap();
304-
let mutants = discover_mutants(&tool, &source_tree, &Options::default()).unwrap();
306+
let mutants = walk_tree(&tool, &source_tree, &Options::default())
307+
.unwrap()
308+
.mutants;
305309
assert_eq!(mutants.len(), 2);
306310

307311
let mut mutated_code = mutants[0].mutated_code();

src/visit.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,21 @@ use crate::path::TreeRelativePathBuf;
2323
use crate::source::SourceFile;
2424
use crate::*;
2525

26-
pub fn discover_mutants(
27-
tool: &dyn Tool,
28-
root: &Utf8Path,
29-
options: &Options,
30-
) -> Result<Vec<Mutant>> {
31-
walk_tree(tool, root, options).map(|x| x.0)
32-
}
33-
34-
pub fn discover_files(
35-
tool: &dyn Tool,
36-
root: &Utf8Path,
37-
options: &Options,
38-
) -> Result<Vec<Arc<SourceFile>>> {
39-
walk_tree(tool, root, options).map(|x| x.1)
26+
/// Mutants and files discovered in a source tree.
27+
///
28+
/// Files are listed separately so that we can represent files that
29+
/// were visited but that produced no mutants.
30+
pub struct Discovered {
31+
pub mutants: Vec<Mutant>,
32+
pub files: Vec<Arc<SourceFile>>,
4033
}
4134

4235
/// Discover all mutants and all source files.
4336
///
4437
/// The list of source files includes even those with no mutants.
45-
fn walk_tree(
46-
tool: &dyn Tool,
47-
root: &Utf8Path,
48-
options: &Options,
49-
) -> Result<(Vec<Mutant>, Vec<Arc<SourceFile>>)> {
38+
pub fn walk_tree(tool: &dyn Tool, root: &Utf8Path, options: &Options) -> Result<Discovered> {
5039
let mut mutants = Vec::new();
51-
let mut seen_files: Vec<Arc<SourceFile>> = Vec::new();
40+
let mut files: Vec<Arc<SourceFile>> = Vec::new();
5241

5342
let mut file_queue: VecDeque<Arc<SourceFile>> = tool.root_files(root)?.into();
5443
while let Some(source_file) = file_queue.pop_front() {
@@ -86,9 +75,9 @@ fn walk_tree(
8675
}
8776
}
8877
mutants.append(&mut file_mutants);
89-
seen_files.push(Arc::clone(&source_file));
78+
files.push(Arc::clone(&source_file));
9079
}
91-
Ok((mutants, seen_files))
80+
Ok(Discovered { mutants, files })
9281
}
9382

9483
/// Find all possible mutants in a source file.

0 commit comments

Comments
 (0)