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
pub mod cargo_runner;
pub mod test_reporter;
pub mod test_runner;
use crate::test_runner::TestRunner;
use move_core_types::language_storage::ModuleId;
use move_lang::{
self,
diagnostics::{self, codes::Severity},
shared::{self, AddressBytes},
unit_test::{self, TestPlan},
Compiler, Flags, PASS_CFGIR,
};
use move_vm_runtime::native_functions::NativeFunctionTable;
use std::{
collections::BTreeMap,
io::{Result, Write},
marker::Send,
sync::Mutex,
};
use structopt::*;
#[derive(Debug, StructOpt, Clone)]
#[structopt(name = "Move Unit Test", about = "Unit testing for Move code.")]
pub struct UnitTestingConfig {
#[structopt(
name = "instructions",
default_value = "5000",
short = "i",
long = "instructions"
)]
pub instruction_execution_bound: u64,
#[structopt(name = "filter", short = "f", long = "filter")]
pub filter: Option<String>,
#[structopt(name = "list", short = "l", long = "list")]
pub list: bool,
#[structopt(
name = "num_threads",
default_value = "8",
short = "t",
long = "threads"
)]
pub num_threads: usize,
#[structopt(name = "dependencies", long = "dependencies", short = "d")]
pub dep_files: Vec<String>,
#[structopt(name = "report_statistics", short = "s", long = "statistics")]
pub report_statistics: bool,
#[structopt(name = "global_state_on_error", short = "g", long = "state_on_error")]
pub report_storage_on_error: bool,
#[structopt(
name = "NAMED_ADDRESSES",
short = "a",
long = "addresses",
parse(try_from_str = shared::parse_named_address)
)]
pub named_address_values: Vec<(String, AddressBytes)>,
#[structopt(name = "sources")]
pub source_files: Vec<String>,
#[structopt(long = "stackless")]
pub check_stackless_vm: bool,
#[structopt(short = "v", long = "verbose")]
pub verbose: 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,
source_files: vec![],
dep_files: vec![],
check_stackless_vm: false,
verbose: false,
list: false,
named_address_values: vec![],
}
}
pub fn with_named_addresses(
mut self,
named_address_values: BTreeMap<String, AddressBytes>,
) -> 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: &[String], deps: &[String]) -> Option<TestPlan> {
let (files, comments_and_compiler_res) = Compiler::new(source_files, deps)
.set_flags(Flags::testing())
.set_named_address_values(
shared::verify_and_create_named_address_mapping(self.named_address_values.clone())
.ok()?,
)
.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, &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 mut deps = self.dep_files.clone();
deps.extend(move_stdlib::unit_testing_files());
let TestPlan {
files, module_info, ..
} = self.compile_to_test_plan(&deps, &[])?;
let mut test_plan = self.compile_to_test_plan(&self.source_files, &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,
test_plan,
native_function_table,
shared::verify_and_create_named_address_mapping(self.named_address_values.clone())
.unwrap(),
)
.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))
}
}