Skip to content

Commit a910410

Browse files
authored
Replace deprecated failure crate with anyhow (#186)
* Replace deprecated failure crate with anyhow This commit fixes #185 * fixup! Replace deprecated failure crate with anyhow
1 parent c5481b4 commit a910410

File tree

7 files changed

+39
-41
lines changed

7 files changed

+39
-41
lines changed

mutagen-core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "Apache-2.0/MIT"
88
[dependencies]
99
serde = { version = "1.0.130", features = ["derive"] }
1010
serde_json = "1.0.68"
11-
failure = "0.1.8"
11+
anyhow = "1.0.57"
1212
json = "0.12.4"
1313
lazy_static = "1.4.0"
1414

mutagen-core/src/comm/mutagen_files.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use failure::{bail, format_err, Fallible};
1+
use anyhow::{bail, Context, Result};
22
use serde::{de::DeserializeOwned, Serialize};
33
use std::fs::File;
44
use std::io::{BufRead, BufReader, BufWriter, Write};
@@ -13,20 +13,20 @@ const JSON_MUTAGEN_FILENAME: &str = "mutations.json";
1313
const DEFAULT_COVERAGE_FILENAME: &str = "coverage";
1414

1515
/// Finds the file that contains the descriptions of all mutations as written by the procedural macro
16-
pub fn get_mutations_file() -> Fallible<PathBuf> {
16+
pub fn get_mutations_file() -> Result<PathBuf> {
1717
Ok(mutagen_dir()?.join(DEFAULT_MUTAGEN_FILENAME))
1818
}
1919

20-
pub fn get_mutations_file_json() -> Fallible<PathBuf> {
20+
pub fn get_mutations_file_json() -> Result<PathBuf> {
2121
Ok(mutagen_dir()?.join(JSON_MUTAGEN_FILENAME))
2222
}
2323

24-
pub fn get_coverage_file() -> Fallible<PathBuf> {
24+
pub fn get_coverage_file() -> Result<PathBuf> {
2525
Ok(mutagen_dir()?.join(DEFAULT_COVERAGE_FILENAME))
2626
}
2727

2828
/// queries `cargo` for the workspace root and locates the directory to write mutagen-specific information
29-
fn mutagen_dir() -> Fallible<PathBuf> {
29+
fn mutagen_dir() -> Result<PathBuf> {
3030
let metadata = Command::new("cargo").arg("metadata").output()?;
3131
if !metadata.status.success() {
3232
bail!("{}", str::from_utf8(&metadata.stderr)?);
@@ -35,21 +35,19 @@ fn mutagen_dir() -> Fallible<PathBuf> {
3535
let root_dir = Path::new(
3636
meta_json["workspace_root"]
3737
.as_str()
38-
.ok_or_else(|| format_err!("cargo metadata misses workspace_root"))?,
38+
.with_context(|| "cargo metadata misses workspace_root")?,
3939
);
4040
Ok(root_dir.join(DEFAULT_MUTAGEN_DIR))
4141
}
4242

43-
pub fn read_items<T: DeserializeOwned>(filepath: &Path) -> Fallible<Vec<T>> {
43+
pub fn read_items<T: DeserializeOwned>(filepath: &Path) -> Result<Vec<T>> {
4444
BufReader::new(File::open(filepath)?)
4545
.lines()
46-
.map(|line| {
47-
serde_json::from_str(&line?).map_err(|e| format_err!("mutation format error: {}", e))
48-
})
46+
.map(|line| serde_json::from_str(&line?).with_context(|| "mutation format error"))
4947
.collect()
5048
}
5149

52-
pub fn append_item<T: Serialize + ?Sized>(file: &mut File, item: &T) -> Fallible<()> {
50+
pub fn append_item<T: Serialize + ?Sized>(file: &mut File, item: &T) -> Result<()> {
5351
let mut w = BufWriter::new(file);
5452
serde_json::to_writer(&mut w, item)?;
5553
writeln!(&mut w)?; // write newline

mutagen-runner/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "Apache-2.0/MIT"
77

88
[dependencies]
99
json = "0.12.4"
10-
failure = "0.1.8"
10+
anyhow = "1.0.57"
1111
wait-timeout = "0.2.0"
1212
serde_json = "1.0.68"
1313
mutagen-core = { path = "../mutagen-core"}

mutagen-runner/src/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use failure::{bail, Fallible};
1+
use anyhow::{bail, Result};
22
use std::collections::HashMap;
33
use std::env;
44
use std::fs::File;
@@ -42,7 +42,7 @@ struct Options {
4242
workspace: bool,
4343
}
4444

45-
fn run() -> Fallible<()> {
45+
fn run() -> Result<()> {
4646
let mutagen_start = Instant::now();
4747

4848
// drop "mutagen" arg in cargo-subcommand mode
@@ -76,7 +76,7 @@ fn run() -> Fallible<()> {
7676
.map(|bin| Some(bin).filter(|bin| bin.coveres_any_mutation()))
7777
.transpose()
7878
})
79-
.collect::<Fallible<Vec<_>>>()?;
79+
.collect::<Result<Vec<_>>>()?;
8080

8181
let coverage = CoverageCollection::merge(num_mutations, test_bins.iter().map(|b| &b.coverage));
8282
progress.summary_testsuite_unmutated(coverage.num_covered())?;
@@ -102,7 +102,7 @@ fn run_mutations(
102102
test_bins: &[TestBinTested],
103103
mutations: Vec<BakedMutation>,
104104
coverage: &CoverageCollection,
105-
) -> Fallible<MutagenReport> {
105+
) -> Result<MutagenReport> {
106106
let mut mutagen_report = MutagenReport::new();
107107

108108
for m in mutations {
@@ -131,7 +131,7 @@ fn run_mutations(
131131
}
132132

133133
/// build all tests and collect test-suite executables
134-
fn compile_tests(opt: &Options) -> Fallible<Vec<PathBuf>> {
134+
fn compile_tests(opt: &Options) -> Result<Vec<PathBuf>> {
135135
let mut tests: Vec<PathBuf> = Vec::new();
136136

137137
let mut feature_args: Vec<&str> = vec![];
@@ -195,7 +195,7 @@ fn compile_tests(opt: &Options) -> Fallible<Vec<PathBuf>> {
195195
///
196196
/// This functions gets the file that describes all mutations performed on the target program and ensures that it exists.
197197
/// The list of mutations is also preserved
198-
fn read_mutations() -> Fallible<Vec<BakedMutation>> {
198+
fn read_mutations() -> Result<Vec<BakedMutation>> {
199199
let mutations_file = comm::get_mutations_file()?;
200200
if !mutations_file.exists() {
201201
bail!(

mutagen-runner/src/progress.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! The main challenges is to be able to continue writing to the line above the progress bar.
88
//! The output to the terminal should look identical to piped output but contains a progress bar.
99
10-
use failure::Fallible;
10+
use anyhow::Result;
1111

1212
use std::path::Path;
1313
use std::time::Duration;
@@ -35,7 +35,7 @@ impl Progress {
3535
}
3636

3737
/// Print summary information after the compilation of the test binaries.
38-
pub fn summary_compile(&mut self, num_mutations: usize, num_testsuites: usize) -> Fallible<()> {
38+
pub fn summary_compile(&mut self, num_mutations: usize, num_testsuites: usize) -> Result<()> {
3939
self.bar.println("")?;
4040
self.bar
4141
.println(&format!("Total mutations: {}", num_mutations))?;
@@ -46,29 +46,29 @@ impl Progress {
4646
}
4747

4848
/// Start the section that runs the test suites unmutated.
49-
pub fn section_testsuite_unmutated(&mut self, num_tests: usize) -> Fallible<()> {
49+
pub fn section_testsuite_unmutated(&mut self, num_tests: usize) -> Result<()> {
5050
self.bar.println("")?;
5151
self.bar.println(&format!("Run {} tests", num_tests))?;
5252
Ok(())
5353
}
5454

5555
/// start the section of test-runs for each mutation
56-
pub fn section_mutants(&mut self) -> Fallible<()> {
56+
pub fn section_mutants(&mut self) -> Result<()> {
5757
self.bar.println("")?;
5858
self.bar
5959
.println(&format!("Test {} Mutants", self.num_mutations))?;
6060
Ok(())
6161
}
6262

6363
/// start the section of the
64-
pub fn section_summary(&mut self) -> Fallible<()> {
64+
pub fn section_summary(&mut self) -> Result<()> {
6565
self.bar.println("")?;
6666
self.bar.clear_bar()?;
6767
Ok(())
6868
}
6969

7070
/// indicate the start of a run of a single testsuite without mutations
71-
pub fn start_testsuite_unmutated(&mut self, bin: &Path, id: usize) -> Fallible<()> {
71+
pub fn start_testsuite_unmutated(&mut self, bin: &Path, id: usize) -> Result<()> {
7272
let log_string = format!("{} ... ", bin.display());
7373
self.bar.print(log_string)?;
7474

@@ -86,7 +86,7 @@ impl Progress {
8686
}
8787

8888
/// indicate the end of a run of a single testsuite and display the result.
89-
pub fn finish_testsuite_unmutated(&mut self, ok: bool, num_covered: usize) -> Fallible<()> {
89+
pub fn finish_testsuite_unmutated(&mut self, ok: bool, num_covered: usize) -> Result<()> {
9090
if ok && num_covered > 0 {
9191
self.bar.println(&format!(
9292
"ok ({}/{} covered)",
@@ -100,7 +100,7 @@ impl Progress {
100100
}
101101

102102
/// print a summary after the testsuites have been run, especially coverage information.
103-
pub fn summary_testsuite_unmutated(&mut self, num_covered: usize) -> Fallible<()> {
103+
pub fn summary_testsuite_unmutated(&mut self, num_covered: usize) -> Result<()> {
104104
self.num_covered = num_covered;
105105
self.bar.set_total(num_covered);
106106

@@ -115,7 +115,7 @@ impl Progress {
115115
///
116116
/// The information about the mutation is logged to the console.
117117
/// A call to `finish_mutation` should follow a call to this function
118-
pub fn start_mutation_covered(&mut self, m: &BakedMutation) -> Fallible<()> {
118+
pub fn start_mutation_covered(&mut self, m: &BakedMutation) -> Result<()> {
119119
let mut mutant_log_string = mutation_log_string(m);
120120
mutant_log_string += " ... ";
121121

@@ -141,7 +141,7 @@ impl Progress {
141141
Ok(())
142142
}
143143

144-
pub fn skip_mutation_uncovered(&mut self, m: &BakedMutation) -> Fallible<()> {
144+
pub fn skip_mutation_uncovered(&mut self, m: &BakedMutation) -> Result<()> {
145145
self.bar.println(&format!(
146146
"{} ... {}",
147147
mutation_log_string(m),
@@ -152,15 +152,15 @@ impl Progress {
152152
/// indicate that a mutation started with `start_mutation` has been finished.
153153
///
154154
/// The status is printed and progress bar is updated
155-
pub fn finish_mutation(&mut self, status: MutantStatus) -> Fallible<()> {
155+
pub fn finish_mutation(&mut self, status: MutantStatus) -> Result<()> {
156156
self.bar.println(&format!("{}", status))?;
157157
Ok(())
158158
}
159159

160160
/// indicate that mutation-testing is finished
161161
///
162162
/// clears the progress-bar
163-
pub fn finish(mut self, mutagen_time: Duration) -> Fallible<()> {
163+
pub fn finish(mut self, mutagen_time: Duration) -> Result<()> {
164164
let rounded_time = Duration::from_secs(mutagen_time.as_secs());
165165
self.bar.println(&format!(
166166
"Total time: {}",

mutagen-runner/src/progress_bar.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//!
77
//! The main challenges is to be able to continue writing to the line above the progress bar.
88
9+
use anyhow::Result;
910
use console::Term;
10-
use failure::Fallible;
1111
use std::io::Write;
1212

1313
/// Print progress during mutation testing
@@ -55,7 +55,7 @@ impl ProgressBar {
5555
///
5656
/// If the progress bar is shown, the text is printed above the progress bar.
5757
/// The next call to `println` will continue writing the line started by this function.
58-
pub fn print(&mut self, s: String) -> Fallible<()> {
58+
pub fn print(&mut self, s: String) -> Result<()> {
5959
if self.show_progress {
6060
self.term.clear_line()?;
6161
}
@@ -85,7 +85,7 @@ impl ProgressBar {
8585
/// prints a line to stdout.
8686
///
8787
/// If the progress bar is shown, the line is printed above the progress bar.
88-
pub fn println(&mut self, s: &str) -> Fallible<()> {
88+
pub fn println(&mut self, s: &str) -> Result<()> {
8989
if self.show_progress {
9090
self.term.clear_line()?;
9191

@@ -108,7 +108,7 @@ impl ProgressBar {
108108
}
109109

110110
/// clears the progress bar
111-
pub fn clear_bar(&mut self) -> Fallible<()> {
111+
pub fn clear_bar(&mut self) -> Result<()> {
112112
if self.current_bar_state.take().is_some() {
113113
self.term.clear_line()?;
114114
}
@@ -118,7 +118,7 @@ impl ProgressBar {
118118
/// finish the progress bar
119119
///
120120
/// clears the progress-indicator
121-
pub fn finish(self) -> Fallible<()> {
121+
pub fn finish(self) -> Result<()> {
122122
if self.show_progress {
123123
self.term.clear_line()?;
124124
writeln!(&self.term)?;
@@ -127,15 +127,15 @@ impl ProgressBar {
127127
}
128128

129129
/// updates the state of the progress bar and draws the new state if the progress is shown
130-
pub fn set_state(&mut self, bar: ProgressBarState) -> Fallible<()> {
130+
pub fn set_state(&mut self, bar: ProgressBarState) -> Result<()> {
131131
if self.show_progress {
132132
self.current_bar_state = Some(bar);
133133
self.write_progress_bar()?;
134134
}
135135
Ok(())
136136
}
137137

138-
fn write_progress_bar(&self) -> Fallible<()> {
138+
fn write_progress_bar(&self) -> Result<()> {
139139
if let Some(bar) = &self.current_bar_state {
140140
if self.total == 0 {
141141
return Ok(());

mutagen-runner/src/test_bin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use failure::{bail, Fallible};
1+
use anyhow::{bail, Result};
22
use std::fs;
33
use std::io::Write;
44
use std::path::Path;
@@ -36,7 +36,7 @@ impl<'a> TestBin<'a> {
3636
self,
3737
progress: &mut Progress,
3838
mutations: &[BakedMutation],
39-
) -> Fallible<TestBinTested<'a>> {
39+
) -> Result<TestBinTested<'a>> {
4040
let num_mutations = mutations.len();
4141
let test_start = Instant::now();
4242

@@ -93,7 +93,7 @@ impl<'a> TestBinTested<'a> {
9393
self.coverage.num_covered() != 0
9494
}
9595

96-
pub fn check_mutant(&self, mutation: &BakedMutation) -> Fallible<MutantStatus> {
96+
pub fn check_mutant(&self, mutation: &BakedMutation) -> Result<MutantStatus> {
9797
// run command and wait for its output
9898
let mut command = Command::new(self.test_bin.bin_path);
9999
command.env("MUTATION_ID", mutation.id().to_string());

0 commit comments

Comments
 (0)