1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
pub mod cargo_runner;
mod extensions;
pub mod test_reporter;
pub mod test_runner;
use crate::test_runner::TestRunner;
use clap::*;
use move_command_line_common::files::verify_and_create_named_address_mapping;
use move_compiler::{
self,
diagnostics::{self, codes::Severity},
shared::{self, NumericalAddress},
unit_test::{self, TestPlan},
Compiler, Flags, PASS_CFGIR,
};
use move_core_types::language_storage::ModuleId;
use move_vm_runtime::native_functions::NativeFunctionTable;
use std::{
collections::BTreeMap,
io::{Result, Write},
marker::Send,
sync::Mutex,
};
#[derive(Debug, Parser, Clone)]
#[clap(author, version, about)]
pub struct UnitTestingConfig {
#[clap(
name = "instructions",
default_value = "5000",
short = 'i',
long = "instructions"
)]
pub instruction_execution_bound: u64,
#[clap(name = "filter", short = 'f', long = "filter")]
pub filter: Option<String>,
#[clap(name = "list", short = 'l', long = "list")]
pub list: bool,
#[clap(
name = "num_threads",
default_value = "8",
short = 't',
long = "threads"
)]
pub num_threads: usize,
#[clap(
name = "dependencies",
long = "dependencies",
short = 'd',
takes_value(true),
multiple_values(true),
multiple_occurrences(true)
)]
pub dep_files: Vec<String>,
#[clap(name = "report_statistics", short = 's', long = "statistics")]
pub report_statistics: bool,
#[clap(name = "global_state_on_error", short = 'g', long = "state_on_error")]
pub report_storage_on_error: bool,
#[clap(
name = "report_stacktrace_on_abort",
short = 'r',
long = "stacktrace_on_abort"
)]
pub report_stacktrace_on_abort: bool,
#[clap(
name = "NAMED_ADDRESSES",
short = 'a',
long = "addresses",
parse(try_from_str = shared::parse_named_address)
)]
pub named_address_values: Vec<(String, NumericalAddress)>,
#[clap(
name = "sources",
takes_value(true),
multiple_values(true),
multiple_occurrences(true)
)]
pub source_files: Vec<String>,
#[clap(long = "stackless")]
pub check_stackless_vm: bool,
#[clap(short = 'v', long = "verbose")]
pub verbose: bool,
#[cfg(feature = "evm-backend")]
#[clap(long = "evm")]
pub evm: bool,
}
fn format_module_id(module_id: &ModuleId) -> String {
format!(
"0x{}::{}",
module_id.address().short_str_lossless(),
module_id.name()
)
}
impl UnitTestingConfig {
pub fn default_with_bound(bound: Option<u64>) -> Self {
Self {
instruction_execution_bound: bound.unwrap_or(5000),
filter: None,
num_threads: 8,
report_statistics: false,
report_storage_on_error: false,
report_stacktrace_on_abort: false,
source_files: vec![],
dep_files: vec![],
check_stackless_vm: false,
verbose: false,
list: false,
named_address_values: vec![],
#[cfg(feature = "evm-backend")]
evm: false,
}
}
pub fn with_named_addresses(
mut self,
named_address_values: BTreeMap<String, NumericalAddress>,
) -> Self {
assert!(self.named_address_values.is_empty());
self.named_address_values = named_address_values.into_iter().collect();
self
}
fn compile_to_test_plan(
&self,
source_files: Vec<String>,
deps: Vec<String>,
) -> Option<TestPlan> {
let addresses =
verify_and_create_named_address_mapping(self.named_address_values.clone()).ok()?;
let (files, comments_and_compiler_res) =
Compiler::from_files(source_files, deps, addresses)
.set_flags(Flags::testing())
.run::<PASS_CFGIR>()
.unwrap();
let (_, compiler) =
diagnostics::unwrap_or_report_diagnostics(&files, comments_and_compiler_res);
let (mut compiler, cfgir) = compiler.into_ast();
let compilation_env = compiler.compilation_env();
let test_plan = unit_test::plan_builder::construct_test_plan(compilation_env, None, &cfgir);
if let Err(diags) = compilation_env.check_diags_at_or_above_severity(Severity::Warning) {
diagnostics::report_diagnostics(&files, diags);
}
let compilation_result = compiler.at_cfgir(cfgir).build();
let (units, warnings) =
diagnostics::unwrap_or_report_diagnostics(&files, compilation_result);
diagnostics::report_warnings(&files, warnings);
test_plan.map(|tests| TestPlan::new(tests, files, units))
}
pub fn build_test_plan(&self) -> Option<TestPlan> {
let deps = self.dep_files.clone();
let TestPlan {
files, module_info, ..
} = self.compile_to_test_plan(deps.clone(), vec![])?;
let mut test_plan = self.compile_to_test_plan(self.source_files.clone(), deps)?;
test_plan.module_info.extend(module_info.into_iter());
test_plan.files.extend(files.into_iter());
Some(test_plan)
}
pub fn run_and_report_unit_tests<W: Write + Send>(
&self,
test_plan: TestPlan,
native_function_table: Option<NativeFunctionTable>,
writer: W,
) -> Result<(W, bool)> {
let shared_writer = Mutex::new(writer);
if self.list {
for (module_id, test_plan) in &test_plan.module_tests {
for test_name in test_plan.tests.keys() {
writeln!(
shared_writer.lock().unwrap(),
"{}::{}: test",
format_module_id(module_id),
test_name
)?;
}
}
return Ok((shared_writer.into_inner().unwrap(), true));
}
writeln!(shared_writer.lock().unwrap(), "Running Move unit tests")?;
let mut test_runner = TestRunner::new(
self.instruction_execution_bound,
self.num_threads,
self.check_stackless_vm,
self.verbose,
self.report_storage_on_error,
self.report_stacktrace_on_abort,
test_plan,
native_function_table,
verify_and_create_named_address_mapping(self.named_address_values.clone()).unwrap(),
#[cfg(feature = "evm-backend")]
self.evm,
)
.unwrap();
if let Some(filter_str) = &self.filter {
test_runner.filter(filter_str)
}
let test_results = test_runner.run(&shared_writer).unwrap();
if self.report_statistics {
test_results.report_statistics(&shared_writer)?;
}
let all_tests_passed = test_results.summarize(&shared_writer)?;
let writer = shared_writer.into_inner().unwrap();
Ok((writer, all_tests_passed))
}
}