Skip to content

Commit 4f114ab

Browse files
committed
minicrater: refactor to use a struct to hold configuration
1 parent 386f2a9 commit 4f114ab

File tree

1 file changed

+124
-95
lines changed

1 file changed

+124
-95
lines changed

tests/minicrater/mod.rs

Lines changed: 124 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -21,122 +21,151 @@ impl CommandMinicraterExt for Command {
2121
}
2222
}
2323

24-
fn execute(ex: &str, crate_select: &str, multithread: bool) {
25-
let ex_dir = PathBuf::from("tests").join("minicrater").join(ex);
26-
let config_file = ex_dir.join("config.toml");
27-
let expected_file = ex_dir.join("results.expected.json");
28-
let actual_file = ex_dir.join("results.actual.json");
29-
30-
let threads_count = if multithread { ::num_cpus::get() } else { 1 };
31-
32-
let report_dir = ::tempfile::tempdir().expect("failed to create report dir");
33-
let ex_arg = format!(
34-
"--ex=minicrater-{}-{}",
35-
ex,
36-
rand::thread_rng()
37-
.sample_iter(&Alphanumeric)
38-
.take(10)
39-
.collect::<String>()
40-
);
41-
42-
// Create local list in the temp work dir
43-
Command::crater()
44-
.args(&["create-lists", "local"])
45-
.env("CRATER_CONFIG", &config_file)
46-
.minicrater_exec();
47-
48-
// Define the experiment
49-
Command::crater()
50-
.args(&[
51-
"define-ex",
52-
&ex_arg,
53-
"stable",
54-
"beta",
55-
&format!("--crate-select={}", crate_select),
56-
])
57-
.env("CRATER_CONFIG", &config_file)
58-
.minicrater_exec();
59-
60-
// Execute the experiment
61-
Command::crater()
62-
.args(&[
63-
"run-graph",
64-
&ex_arg,
65-
"--threads",
66-
&threads_count.to_string(),
67-
])
68-
.env("CRATER_CONFIG", &config_file)
69-
.minicrater_exec();
70-
71-
// Generate the report
72-
Command::crater()
73-
.args(&["gen-report", &ex_arg])
74-
.env("CRATER_CONFIG", &config_file)
75-
.arg(report_dir.path())
76-
.minicrater_exec();
77-
78-
// Read the JSON report
79-
let json_report = ::std::fs::read(report_dir.path().join("results.json"))
80-
.expect("failed to read json report");
81-
82-
// Delete the experiment
83-
Command::crater()
84-
.args(&["delete-ex", &ex_arg])
85-
.env("CRATER_CONFIG", &config_file)
86-
.minicrater_exec();
87-
88-
// Load the generated JSON report
89-
let parsed_report: Value = serde_json::from_slice(&json_report).expect("invalid json report");
90-
let mut actual_report = serde_json::to_vec_pretty(&parsed_report).unwrap();
91-
actual_report.push(b'\n');
92-
93-
// Load the expected JSON report
94-
let expected_report = ::std::fs::read(&expected_file).unwrap_or(Vec::new());
95-
96-
// Write the actual JSON report
97-
::std::fs::write(&actual_file, &actual_report)
98-
.expect("failed to write copy of the json report");
99-
100-
let changeset = Changeset::new(
101-
&String::from_utf8(expected_report).expect("invalid utf-8 in the expected report"),
102-
&String::from_utf8(actual_report.clone()).expect("invalid utf-8 in the actual report"),
103-
"\n",
104-
);
105-
if changeset.distance != 0 {
106-
eprintln!(
107-
"Difference between expected and actual reports:\n{}",
108-
changeset
24+
struct MinicraterRun {
25+
ex: &'static str,
26+
crate_select: &'static str,
27+
multithread: bool,
28+
}
29+
30+
impl MinicraterRun {
31+
fn execute(&self) {
32+
let ex_dir = PathBuf::from("tests").join("minicrater").join(self.ex);
33+
let config_file = ex_dir.join("config.toml");
34+
let expected_file = ex_dir.join("results.expected.json");
35+
let actual_file = ex_dir.join("results.actual.json");
36+
37+
let threads_count = if self.multithread { num_cpus::get() } else { 1 };
38+
39+
let report_dir = tempfile::tempdir().expect("failed to create report dir");
40+
let ex_arg = format!(
41+
"--ex=minicrater-{}-{}",
42+
self.ex,
43+
rand::thread_rng()
44+
.sample_iter(&Alphanumeric)
45+
.take(10)
46+
.collect::<String>()
10947
);
110-
eprintln!("To expect the new report in the future run:");
111-
eprintln!(
112-
"$ cp {} {}\n",
113-
actual_file.to_string_lossy(),
114-
expected_file.to_string_lossy()
48+
49+
// Create local list in the temp work dir
50+
Command::crater()
51+
.args(&["create-lists", "local"])
52+
.env("CRATER_CONFIG", &config_file)
53+
.minicrater_exec();
54+
55+
// Define the experiment
56+
Command::crater()
57+
.args(&[
58+
"define-ex",
59+
&ex_arg,
60+
"stable",
61+
"beta",
62+
&format!("--crate-select={}", self.crate_select),
63+
])
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",
115112
);
116-
panic!("invalid report generated by Crater");
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+
}
117126
}
118127
}
119128

120129
#[ignore]
121130
#[test]
122131
fn single_thread_small() {
123-
execute("small", "demo", false);
132+
MinicraterRun {
133+
ex: "small",
134+
crate_select: "demo",
135+
multithread: false,
136+
}
137+
.execute();
124138
}
125139

126140
#[ignore]
127141
#[test]
128142
fn single_thread_full() {
129-
execute("full", "local", false);
143+
MinicraterRun {
144+
ex: "full",
145+
crate_select: "local",
146+
multithread: false,
147+
}
148+
.execute();
130149
}
131150

132151
#[ignore]
133152
#[test]
134153
fn single_thread_blacklist() {
135-
execute("blacklist", "demo", false);
154+
MinicraterRun {
155+
ex: "blacklist",
156+
crate_select: "demo",
157+
multithread: false,
158+
}
159+
.execute();
136160
}
137161

138162
#[ignore]
139163
#[test]
140164
fn multi_thread_full() {
141-
execute("full", "local", true);
165+
MinicraterRun {
166+
ex: "full",
167+
crate_select: "local",
168+
multithread: true,
169+
}
170+
.execute();
142171
}

0 commit comments

Comments
 (0)