-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtest_cmd.rs
369 lines (314 loc) · 9.56 KB
/
test_cmd.rs
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! Contains various tests for checking `forge test`
use foundry_common::rpc;
use foundry_config::Config;
use foundry_test_utils::util::{OutputExt, OTHER_SOLC_VERSION, SOLC_VERSION};
use std::{path::PathBuf, process::Command, str::FromStr};
// tests that test filters are handled correctly
forgetest!(can_set_filter_values, |prj, cmd| {
let patt = regex::Regex::new("test*").unwrap();
let glob = globset::Glob::from_str("foo/bar/baz*").unwrap();
// explicitly set patterns
let config = Config {
test_pattern: Some(patt.clone().into()),
test_pattern_inverse: None,
contract_pattern: Some(patt.clone().into()),
contract_pattern_inverse: None,
path_pattern: Some(glob.clone()),
path_pattern_inverse: None,
..Default::default()
};
prj.write_config(config);
let config = cmd.config();
assert_eq!(config.test_pattern.unwrap().as_str(), patt.as_str());
assert_eq!(config.test_pattern_inverse, None);
assert_eq!(config.contract_pattern.unwrap().as_str(), patt.as_str());
assert_eq!(config.contract_pattern_inverse, None);
assert_eq!(config.path_pattern.unwrap(), glob);
assert_eq!(config.path_pattern_inverse, None);
});
// tests that warning is displayed when there are no tests in project
forgetest!(warn_no_tests, |prj, cmd| {
prj.add_source(
"dummy",
r"
contract Dummy {}
",
)
.unwrap();
// set up command
cmd.args(["test"]);
// run command and assert
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/warn_no_tests.stdout"),
);
});
// tests that warning is displayed with pattern when no tests match
forgetest!(warn_no_tests_match, |prj, cmd| {
prj.add_source(
"dummy",
r"
contract Dummy {}
",
)
.unwrap();
// set up command
cmd.args(["test", "--match-test", "testA.*", "--no-match-test", "testB.*"]);
cmd.args(["--match-contract", "TestC.*", "--no-match-contract", "TestD.*"]);
cmd.args(["--match-path", "*TestE*", "--no-match-path", "*TestF*"]);
// run command and assert
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/warn_no_tests_match.stdout"),
);
});
// tests that suggestion is provided with pattern when no tests match
forgetest!(suggest_when_no_tests_match, |prj, cmd| {
// set up project
prj.add_source(
"TestE.t.sol",
r"
contract TestC {
function test1() public {
}
}
",
)
.unwrap();
// set up command
cmd.args(["test", "--match-test", "testA.*", "--no-match-test", "testB.*"]);
cmd.args(["--match-contract", "TestC.*", "--no-match-contract", "TestD.*"]);
cmd.args(["--match-path", "*TestE*", "--no-match-path", "*TestF*"]);
// run command and assert
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/suggest_when_no_tests_match.stdout"),
);
});
// tests that direct import paths are handled correctly
forgetest!(can_fuzz_array_params, |prj, cmd| {
prj.insert_ds_test();
prj.add_source(
"ATest.t.sol",
r#"
import "./test.sol";
contract ATest is DSTest {
function testArray(uint64[2] calldata values) external {
assertTrue(true);
}
}
"#,
)
.unwrap();
cmd.arg("test");
cmd.stdout_lossy().contains("[PASS]");
});
// tests that `bytecode_hash` will be sanitized
forgetest!(can_test_pre_bytecode_hash, |prj, cmd| {
prj.insert_ds_test();
prj.add_source(
"ATest.t.sol",
r#"
// pre bytecode hash version, was introduced in 0.6.0
pragma solidity 0.5.17;
import "./test.sol";
contract ATest is DSTest {
function testArray(uint64[2] calldata values) external {
assertTrue(true);
}
}
"#,
)
.unwrap();
cmd.arg("test");
cmd.stdout_lossy().contains("[PASS]");
});
// tests that using the --match-path option only runs files matching the path
forgetest!(can_test_with_match_path, |prj, cmd| {
prj.insert_ds_test();
prj.add_source(
"ATest.t.sol",
r#"
import "./test.sol";
contract ATest is DSTest {
function testArray(uint64[2] calldata values) external {
assertTrue(true);
}
}
"#,
)
.unwrap();
prj.add_source(
"FailTest.t.sol",
r#"
import "./test.sol";
contract FailTest is DSTest {
function testNothing() external {
assertTrue(false);
}
}
"#,
)
.unwrap();
cmd.args(["test", "--match-path", "*src/ATest.t.sol"]);
assert!(cmd.stdout_lossy().contains("[PASS]") && !cmd.stdout_lossy().contains("[FAIL]"));
});
// tests that `forge test` will pick up tests that are stored in the `test = <path>` config value
forgetest!(can_run_test_in_custom_test_folder, |prj, cmd| {
prj.insert_ds_test();
// explicitly set the test folder
let config = Config { test: "nested/forge-tests".into(), ..Default::default() };
prj.write_config(config);
let config = cmd.config();
assert_eq!(config.test, PathBuf::from("nested/forge-tests"));
prj.add_source(
"nested/forge-tests/MyTest.t.sol",
r#"
import "../../test.sol";
contract MyTest is DSTest {
function testTrue() public {
assertTrue(true);
}
}
"#,
)
.unwrap();
cmd.arg("test");
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_run_test_in_custom_test_folder.stdout"),
);
});
// checks that forge test repeatedly produces the same output
forgetest_init!(can_test_repeatedly, |_prj, cmd| {
cmd.arg("test");
cmd.assert_non_empty_stdout();
for _ in 0..5 {
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_test_repeatedly.stdout"),
);
}
});
// tests that `forge test` will run a test only once after changing the version
forgetest!(runs_tests_exactly_once_with_changed_versions, |prj, cmd| {
prj.insert_ds_test();
prj.add_source(
"Contract.t.sol",
r#"
pragma solidity *;
import "./test.sol";
contract ContractTest is DSTest {
function setUp() public {}
function testExample() public {
assertTrue(true);
}
}
"#,
)
.unwrap();
// pin version
let config = Config { solc: Some(SOLC_VERSION.into()), ..Default::default() };
prj.write_config(config);
cmd.arg("test");
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/runs_tests_exactly_once_with_changed_versions.1.stdout"),
);
// pin version
let config = Config { solc: Some(OTHER_SOLC_VERSION.into()), ..Default::default() };
prj.write_config(config);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/runs_tests_exactly_once_with_changed_versions.2.stdout"),
);
});
// checks that we can test forge std successfully
// `forgetest_init!` will install with `forge-std` under `lib/forge-std`
forgetest_init!(
#[serial_test::serial]
can_test_forge_std,
|prj, cmd| {
let forge_std_dir = prj.root().join("lib/forge-std");
let status = Command::new("git")
.current_dir(&forge_std_dir)
.args(["pull", "origin", "master"])
.status()
.unwrap();
if !status.success() {
panic!("failed to update forge-std");
}
// execute in subdir
cmd.cmd().current_dir(forge_std_dir);
cmd.args(["test", "--root", "."]);
let stdout = cmd.stdout_lossy();
assert!(stdout.contains("[PASS]"), "No tests passed:\n{stdout}");
assert!(!stdout.contains("[FAIL]"), "Tests failed:\n{stdout}");
}
);
// tests that libraries are handled correctly in multiforking mode
forgetest_init!(can_use_libs_in_multi_fork, |prj, cmd| {
prj.wipe_contracts();
prj.add_source(
"Contract.sol",
r"
library Library {
function f(uint256 a, uint256 b) public pure returns (uint256) {
return a + b;
}
}
contract Contract {
uint256 c;
constructor() {
c = Library.f(1, 2);
}
}
",
)
.unwrap();
let endpoint = rpc::next_http_archive_rpc_endpoint();
prj.add_test(
"Contract.t.sol",
&r#"
import "forge-std/Test.sol";
import "src/Contract.sol";
contract ContractTest is Test {
function setUp() public {
vm.createSelectFork("<url>");
}
function test() public {
new Contract();
}
}
"#
.replace("<url>", &endpoint),
)
.unwrap();
cmd.arg("test");
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_use_libs_in_multi_fork.stdout"),
);
});
static FAILING_TEST: &str = r#"
import "forge-std/Test.sol";
contract FailingTest is Test {
function testShouldFail() public {
assertTrue(false);
}
}
"#;
forgetest_init!(exit_code_error_on_fail_fast, |prj, cmd| {
prj.wipe_contracts();
prj.add_source("failing_test", FAILING_TEST).unwrap();
// set up command
cmd.args(["test", "--fail-fast"]);
// run command and assert error exit code
cmd.assert_err();
});
forgetest_init!(exit_code_error_on_fail_fast_with_json, |prj, cmd| {
prj.wipe_contracts();
prj.add_source("failing_test", FAILING_TEST).unwrap();
// set up command
cmd.args(["test", "--fail-fast", "--json"]);
// run command and assert error exit code
cmd.assert_err();
});