-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.rs
More file actions
182 lines (160 loc) · 6.14 KB
/
cli.rs
File metadata and controls
182 lines (160 loc) · 6.14 KB
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
// Copyright © Eiger
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
use aptos::common::types::MovePackageOptions;
use aptos_framework::extended_checks;
use clap::Parser;
use move_model::metadata::{CompilerVersion, LanguageVersion};
use move_mutator::cli::{FunctionFilter, ModuleFilter, OperatorModeArg};
use move_package::CompilerConfig;
use std::path::PathBuf;
/// Command line options for mutation test tool.
#[derive(Parser, Default, Debug, Clone)]
pub struct CLIOptions {
/// Work only over specified modules.
#[clap(
long,
value_parser,
default_value = "all",
conflicts_with = "use_generated_mutants"
)]
pub mutate_modules: ModuleFilter,
/// Work only over specified functions (these are not qualified functions).
#[clap(
long,
value_parser,
default_value = "all",
conflicts_with = "use_generated_mutants"
)]
pub mutate_functions: FunctionFilter,
/// Save report to a JSON file.
#[clap(long, value_parser)]
pub output: Option<PathBuf>,
/// Use previously generated mutants.
#[clap(long, value_parser)]
pub use_generated_mutants: Option<PathBuf>,
/// Remove averagely given percentage of mutants. See the doc for more details.
#[clap(long, conflicts_with = "use_generated_mutants")]
pub downsampling_ratio_percentage: Option<usize>,
/// Mutation operator mode to balance speed and test gap detection.
///
/// - light: binary_operator_swap, break_continue_replacement, delete_statement
/// - medium: light + literal_replacement
/// - medium-only: literal_replacement (only what's added in medium)
/// - heavy (default): all 7 operators
/// - heavy-only: unary_operator_replacement, binary_operator_replacement, if_else_replacement (only what's added in heavy)
#[clap(
long,
value_enum,
conflicts_with = "operators",
conflicts_with = "use_generated_mutants"
)]
pub mode: Option<OperatorModeArg>,
/// Custom operator selection to run mutations on (comma-separated).
///
/// Available operators: unary_operator_replacement, delete_statement, break_continue_replacement, binary_operator_replacement, if_else_replacement, literal_replacement, binary_operator_swap
#[clap(
long,
value_parser,
value_delimiter = ',',
conflicts_with = "mode",
conflicts_with = "use_generated_mutants"
)]
pub operators: Option<Vec<String>>,
}
/// This function creates a mutator CLI options from the given mutation-test options.
#[must_use]
pub fn create_mutator_options(
options: &CLIOptions,
apply_coverage: bool,
) -> move_mutator::cli::CLIOptions {
move_mutator::cli::CLIOptions {
mutate_functions: options.mutate_functions.clone(),
mutate_modules: options.mutate_modules.clone(),
downsampling_ratio_percentage: options.downsampling_ratio_percentage,
apply_coverage,
// To run tests, compilation must succeed
verify_mutants: true,
mode: options.mode,
operators: options.operators.clone(),
..Default::default()
}
}
/// The configuration options for running the tests.
// Info: this set struct is based on TestPackage in `aptos-core/crates/aptos/src/move_tool/mod.rs`.
#[derive(Parser, Debug, Clone)]
pub struct TestBuildConfig {
/// A filter string to determine which unit tests to run
#[clap(long, short)]
pub filter: Option<String>,
/// A boolean value to skip warnings.
#[clap(long)]
pub ignore_compile_warnings: bool,
#[clap(flatten)]
pub move_options: MovePackageOptions,
/// Collect coverage information for later use with the various `aptos move coverage` subcommands
#[clap(long = "coverage")]
pub compute_coverage: bool,
/// Dump storage state on failure.
#[clap(long = "dump")]
pub dump_state: bool,
/// The maximum gas limit for each test.
///
/// Used mainly for disabling mutants with infinite loops.
/// The default value is large enough for all normal tests in most projects.
#[clap(long, default_value_t = 1_000_000)]
pub gas_limit: u64,
}
impl TestBuildConfig {
/// Create a [`CompilerConfig`] from the [`TestBuildConfig`].
pub fn compiler_config(&self) -> CompilerConfig {
let known_attributes = extended_checks::get_all_attribute_names().clone();
CompilerConfig {
known_attributes: known_attributes.clone(),
skip_attribute_checks: self.move_options.skip_attribute_checks,
bytecode_version: get_bytecode_version(
self.move_options.bytecode_version,
self.move_options.language_version,
),
compiler_version: self
.move_options
.compiler_version
.or_else(|| Some(CompilerVersion::latest_stable())),
language_version: self
.move_options
.language_version
.or_else(|| Some(LanguageVersion::latest_stable())),
experiments: self.move_options.compute_experiments(),
}
}
}
fn get_bytecode_version(
bytecode_version_in: Option<u32>,
language_version: Option<LanguageVersion>,
) -> Option<u32> {
bytecode_version_in.or_else(|| language_version.map(|lv| lv.infer_bytecode_version(None)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cli_options_starts_empty() {
let options = CLIOptions::default();
assert_eq!(ModuleFilter::All, options.mutate_modules);
assert_eq!(FunctionFilter::All, options.mutate_functions);
assert!(options.output.is_none());
}
#[test]
fn create_mutator_options_copies_fields() {
let options = crate::cli::CLIOptions {
mutate_modules: ModuleFilter::Selected(vec!["mod1".to_string(), "mod2".to_string()]),
mutate_functions: FunctionFilter::Selected(vec![
"func1".to_string(),
"func2".to_string(),
]),
..Default::default()
};
let mutator_options = create_mutator_options(&options, false);
assert_eq!(mutator_options.mutate_modules, options.mutate_modules);
}
}