Skip to content

Commit 41a9cba

Browse files
committed
minicrater: move all the implementation in driver.rs
1 parent d68b832 commit 41a9cba

File tree

2 files changed

+154
-161
lines changed

2 files changed

+154
-161
lines changed

tests/minicrater/driver.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
use crate::common::CommandCraterExt;
2+
use assert_cmd::prelude::*;
3+
use difference::Changeset;
4+
use rand::{self, distributions::Alphanumeric, Rng};
5+
use serde_json::{self, Value};
6+
use std::env;
7+
use std::path::PathBuf;
8+
use std::process::Command;
9+
10+
trait CommandMinicraterExt {
11+
fn minicrater_exec(&mut self);
12+
}
13+
14+
impl CommandMinicraterExt for Command {
15+
fn minicrater_exec(&mut self) {
16+
if env::var_os("MINICRATER_SHOW_OUTPUT").is_some() {
17+
assert!(self.status().unwrap().success());
18+
} else {
19+
self.assert().success();
20+
}
21+
}
22+
}
23+
24+
pub(super) struct MinicraterRun {
25+
pub(super) ex: &'static str,
26+
pub(super) crate_select: &'static str,
27+
pub(super) multithread: bool,
28+
pub(super) ignore_blacklist: bool,
29+
}
30+
31+
impl MinicraterRun {
32+
pub(super) fn execute(&self) {
33+
let ex_dir = PathBuf::from("tests").join("minicrater").join(self.ex);
34+
let config_file = ex_dir.join("config.toml");
35+
let expected_file = ex_dir.join("results.expected.json");
36+
let actual_file = ex_dir.join("results.actual.json");
37+
38+
let threads_count = if self.multithread { num_cpus::get() } else { 1 };
39+
40+
let report_dir = tempfile::tempdir().expect("failed to create report dir");
41+
let ex_arg = format!(
42+
"--ex=minicrater-{}-{}",
43+
self.ex,
44+
rand::thread_rng()
45+
.sample_iter(&Alphanumeric)
46+
.take(10)
47+
.collect::<String>()
48+
);
49+
50+
// Create local list in the temp work dir
51+
Command::crater()
52+
.args(&["create-lists", "local"])
53+
.env("CRATER_CONFIG", &config_file)
54+
.minicrater_exec();
55+
56+
// Define the experiment
57+
let crate_select = format!("--crate-select={}", self.crate_select);
58+
let mut define_args = vec!["define-ex", &ex_arg, "stable", "beta", &crate_select];
59+
if self.ignore_blacklist {
60+
define_args.push("--ignore-blacklist");
61+
}
62+
Command::crater()
63+
.args(&define_args)
64+
.env("CRATER_CONFIG", &config_file)
65+
.minicrater_exec();
66+
67+
// Execute the experiment
68+
Command::crater()
69+
.args(&[
70+
"run-graph",
71+
&ex_arg,
72+
"--threads",
73+
&threads_count.to_string(),
74+
])
75+
.env("CRATER_CONFIG", &config_file)
76+
.minicrater_exec();
77+
78+
// Generate the report
79+
Command::crater()
80+
.args(&["gen-report", &ex_arg])
81+
.env("CRATER_CONFIG", &config_file)
82+
.arg(report_dir.path())
83+
.minicrater_exec();
84+
85+
// Read the JSON report
86+
let json_report = ::std::fs::read(report_dir.path().join("results.json"))
87+
.expect("failed to read json report");
88+
89+
// Delete the experiment
90+
Command::crater()
91+
.args(&["delete-ex", &ex_arg])
92+
.env("CRATER_CONFIG", &config_file)
93+
.minicrater_exec();
94+
95+
// Load the generated JSON report
96+
let parsed_report: Value =
97+
serde_json::from_slice(&json_report).expect("invalid json report");
98+
let mut actual_report = serde_json::to_vec_pretty(&parsed_report).unwrap();
99+
actual_report.push(b'\n');
100+
101+
// Load the expected JSON report
102+
let expected_report = ::std::fs::read(&expected_file).unwrap_or(Vec::new());
103+
104+
// Write the actual JSON report
105+
::std::fs::write(&actual_file, &actual_report)
106+
.expect("failed to write copy of the json report");
107+
108+
let changeset = Changeset::new(
109+
&String::from_utf8(expected_report).expect("invalid utf-8 in the expected report"),
110+
&String::from_utf8(actual_report.clone()).expect("invalid utf-8 in the actual report"),
111+
"\n",
112+
);
113+
if changeset.distance != 0 {
114+
eprintln!(
115+
"Difference between expected and actual reports:\n{}",
116+
changeset
117+
);
118+
eprintln!("To expect the new report in the future run:");
119+
eprintln!(
120+
"$ cp {} {}\n",
121+
actual_file.to_string_lossy(),
122+
expected_file.to_string_lossy()
123+
);
124+
panic!("invalid report generated by Crater");
125+
}
126+
}
127+
}
128+
129+
#[macro_export]
130+
macro_rules! minicrater {
131+
($($name:ident $opts:tt,)*) => {
132+
$(
133+
#[test]
134+
#[ignore]
135+
fn $name() {
136+
use $crate::minicrater::driver::MinicraterRun;
137+
MinicraterRun $opts.execute();
138+
}
139+
)*
140+
}
141+
}

tests/minicrater/mod.rs

Lines changed: 13 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,187 +1,39 @@
1-
use crate::common::CommandCraterExt;
2-
use assert_cmd::prelude::*;
3-
use difference::Changeset;
4-
use rand::{self, distributions::Alphanumeric, Rng};
5-
use serde_json::{self, Value};
6-
use std::env;
7-
use std::path::PathBuf;
8-
use std::process::Command;
1+
#[macro_use]
2+
mod driver;
93

10-
trait CommandMinicraterExt {
11-
fn minicrater_exec(&mut self);
12-
}
13-
14-
impl CommandMinicraterExt for Command {
15-
fn minicrater_exec(&mut self) {
16-
if env::var_os("MINICRATER_SHOW_OUTPUT").is_some() {
17-
assert!(self.status().unwrap().success());
18-
} else {
19-
self.assert().success();
20-
}
21-
}
22-
}
23-
24-
struct MinicraterRun {
25-
ex: &'static str,
26-
crate_select: &'static str,
27-
multithread: bool,
28-
ignore_blacklist: bool,
29-
}
30-
31-
impl MinicraterRun {
32-
fn execute(&self) {
33-
let ex_dir = PathBuf::from("tests").join("minicrater").join(self.ex);
34-
let config_file = ex_dir.join("config.toml");
35-
let expected_file = ex_dir.join("results.expected.json");
36-
let actual_file = ex_dir.join("results.actual.json");
37-
38-
let threads_count = if self.multithread { num_cpus::get() } else { 1 };
39-
40-
let report_dir = tempfile::tempdir().expect("failed to create report dir");
41-
let ex_arg = format!(
42-
"--ex=minicrater-{}-{}",
43-
self.ex,
44-
rand::thread_rng()
45-
.sample_iter(&Alphanumeric)
46-
.take(10)
47-
.collect::<String>()
48-
);
49-
50-
// Create local list in the temp work dir
51-
Command::crater()
52-
.args(&["create-lists", "local"])
53-
.env("CRATER_CONFIG", &config_file)
54-
.minicrater_exec();
55-
56-
// Define the experiment
57-
let crate_select = format!("--crate-select={}", self.crate_select);
58-
let mut define_args = vec!["define-ex", &ex_arg, "stable", "beta", &crate_select];
59-
if self.ignore_blacklist {
60-
define_args.push("--ignore-blacklist");
61-
}
62-
Command::crater()
63-
.args(&define_args)
64-
.env("CRATER_CONFIG", &config_file)
65-
.minicrater_exec();
66-
67-
// Execute the experiment
68-
Command::crater()
69-
.args(&[
70-
"run-graph",
71-
&ex_arg,
72-
"--threads",
73-
&threads_count.to_string(),
74-
])
75-
.env("CRATER_CONFIG", &config_file)
76-
.minicrater_exec();
77-
78-
// Generate the report
79-
Command::crater()
80-
.args(&["gen-report", &ex_arg])
81-
.env("CRATER_CONFIG", &config_file)
82-
.arg(report_dir.path())
83-
.minicrater_exec();
84-
85-
// Read the JSON report
86-
let json_report = ::std::fs::read(report_dir.path().join("results.json"))
87-
.expect("failed to read json report");
88-
89-
// Delete the experiment
90-
Command::crater()
91-
.args(&["delete-ex", &ex_arg])
92-
.env("CRATER_CONFIG", &config_file)
93-
.minicrater_exec();
94-
95-
// Load the generated JSON report
96-
let parsed_report: Value =
97-
serde_json::from_slice(&json_report).expect("invalid json report");
98-
let mut actual_report = serde_json::to_vec_pretty(&parsed_report).unwrap();
99-
actual_report.push(b'\n');
100-
101-
// Load the expected JSON report
102-
let expected_report = ::std::fs::read(&expected_file).unwrap_or(Vec::new());
103-
104-
// Write the actual JSON report
105-
::std::fs::write(&actual_file, &actual_report)
106-
.expect("failed to write copy of the json report");
107-
108-
let changeset = Changeset::new(
109-
&String::from_utf8(expected_report).expect("invalid utf-8 in the expected report"),
110-
&String::from_utf8(actual_report.clone()).expect("invalid utf-8 in the actual report"),
111-
"\n",
112-
);
113-
if changeset.distance != 0 {
114-
eprintln!(
115-
"Difference between expected and actual reports:\n{}",
116-
changeset
117-
);
118-
eprintln!("To expect the new report in the future run:");
119-
eprintln!(
120-
"$ cp {} {}\n",
121-
actual_file.to_string_lossy(),
122-
expected_file.to_string_lossy()
123-
);
124-
panic!("invalid report generated by Crater");
125-
}
126-
}
127-
}
128-
129-
#[ignore]
130-
#[test]
131-
fn single_thread_small() {
132-
MinicraterRun {
4+
minicrater! {
5+
single_thread_small {
1336
ex: "small",
1347
crate_select: "demo",
1358
multithread: false,
1369
ignore_blacklist: false,
137-
}
138-
.execute();
139-
}
10+
},
14011

141-
#[ignore]
142-
#[test]
143-
fn single_thread_full() {
144-
MinicraterRun {
12+
single_thread_full {
14513
ex: "full",
14614
crate_select: "local",
14715
multithread: false,
14816
ignore_blacklist: false,
149-
}
150-
.execute();
151-
}
17+
},
15218

153-
#[ignore]
154-
#[test]
155-
fn single_thread_blacklist() {
156-
MinicraterRun {
19+
single_thread_blacklist {
15720
ex: "blacklist",
15821
crate_select: "demo",
15922
multithread: false,
16023
ignore_blacklist: false,
161-
}
162-
.execute();
163-
}
24+
},
16425

165-
#[ignore]
166-
#[test]
167-
fn single_thread_ignore_blacklist() {
168-
MinicraterRun {
26+
single_thread_ignore_blacklist {
16927
ex: "ignore-blacklist",
17028
crate_select: "demo",
17129
multithread: false,
17230
ignore_blacklist: true,
173-
}
174-
.execute();
175-
}
31+
},
17632

177-
#[ignore]
178-
#[test]
179-
fn multi_thread_full() {
180-
MinicraterRun {
33+
multi_thread_full {
18134
ex: "full",
18235
crate_select: "local",
18336
multithread: true,
18437
ignore_blacklist: false,
185-
}
186-
.execute();
38+
},
18739
}

0 commit comments

Comments
 (0)