bulloak 0.9.2

A Solidity test generator based on the Branching Tree Technique.
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#![allow(missing_docs)]
use std::env;

use common::{cmd, get_binary_path};
use owo_colors::OwoColorize;
use pretty_assertions::assert_eq;

mod common;

#[test]
fn checks_invalid_structural_match() {
    let binary_path = get_binary_path();
    let cwd = env::current_dir().unwrap();
    let tree_path =
        cwd.join("tests").join("check").join("invalid_sol_structure.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();
    let actual = stderr.lines().filter(|line| line.starts_with("warn:"));

    let expected = r#"warn: function "givenTheStreamIsCold" is missing in .sol
warn: function "whenTheSenderDoesNotRevert" is missing in .sol
warn: incorrect position for function "test_RevertGiven_TheStreamsStatusIsCANCELED"
warn: incorrect position for function "test_RevertGiven_TheStreamsStatusIsSETTLED"
warn: incorrect position for function "test_WhenTheSenderReverts"
warn: incorrect position for function "test_WhenThereIsReentrancy""#
        .lines();

    for (expected, actual) in expected.zip(actual) {
        assert_eq!(expected, actual);
    }
}

#[test]
fn checks_valid_structural_match() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("extra_codegen_sol.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    assert_eq!("", stderr);
    assert!(
        stdout.contains("All checks completed successfully! No issues found.")
    );
}

#[test]
fn checks_modifiers_skipped() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path = cwd.join("tests").join("check").join("skip_modifiers.tree");

    let output = cmd(&binary_path, "check", &tree_path, &["-m"]);
    let stderr = String::from_utf8(output.stderr).unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    assert_eq!("", stderr);
    assert!(
        stdout.contains("All checks completed successfully! No issues found.")
    );
}

#[test]
fn checks_modifiers_skipped_issue_81() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path = cwd.join("tests").join("check").join("issue_81.tree");

    let output = cmd(&binary_path, "check", &tree_path, &["-m"]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    assert!(stderr.contains(
        "function \"test_WhenLastUpdatedTimeInPast\" is missing in .sol"
    ));
}

#[test]
fn checks_missing_sol_file() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("no_matching_sol.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    assert!(stderr.contains("the tree is missing its matching Solidity file"));
    assert!(stderr.contains("no_matching_sol.tree"));
}

#[test]
fn checks_empty_contract() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path = cwd.join("tests").join("check").join("empty_contract.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    assert!(stderr
        .contains(r#"function "test_ShouldNeverRevert" is missing in .sol"#));
    assert!(stderr.contains(
        r#"function "test_ShouldNotFindTheSolidityFile" is missing in .sol"#
    ));
}

#[test]
fn checks_missing_contract() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("missing_contract.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    assert!(stderr.contains(r#"contract "MissingContract" is missing in .sol"#));
}

#[cfg(not(target_os = "windows"))]
#[test]
fn checks_missing_contract_identifier() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path = cwd
        .join("tests")
        .join("check")
        .join("missing_contract_identifier.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    let formatted_message = format!(
        "{}: an error occurred while parsing the tree: contract name missing at tree root #2\n   {} {}",
        "warn".yellow(),
        "-->".blue(),
        tree_path.display()
    );

    assert!(
        stderr.contains(&formatted_message),
        "stderr: {}\nmessage: {}",
        stderr,
        formatted_message
    );
}

#[test]
fn checks_contract_name_mismatch() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("contract_names_mismatch.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    assert!(stderr.contains(
        r#"contract "ContractName" is missing in .sol -- found "ADifferentName" instead"#
    ));
}

#[cfg(not(target_os = "windows"))]
#[test]
fn checks_contract_name_mismatch_multiple_roots() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path = cwd
        .join("tests")
        .join("check")
        .join("contract_names_mismatch_multiple_roots.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    let formatted_message = format!(
        "{}: an error occurred while parsing the tree: contract name mismatch: expected 'ContractName', found 'MismatchedContractName'\n   {} {}",
        "warn".yellow(),
        "-->".blue(),
        tree_path.display()
    );

    assert!(stderr.contains(&formatted_message));
}

#[test]
fn checks_invalid_tree() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path = cwd.join("tests").join("check").join("invalid.tree");

    let output = cmd(&binary_path, "check", &tree_path, &[]);
    let stderr = String::from_utf8(output.stderr).unwrap();

    assert!(stderr.contains(
        r#"an error occurred while parsing the tree: unexpected token '├'"#
    ));
}

#[test]
fn fixes_non_matching_contract_names() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("contract_names_mismatch.tree");

    let output = cmd(&binary_path, "check", &tree_path, &["--fix", "--stdout"]);
    let expected = "// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;

contract ContractName {
    function test_ShouldHaveANameMismatchInTheContracts() external {
        // It should match the result of `keccak256(abi.encodePacked(a,b))`.
    }
}
";

    let actual = String::from_utf8(output.stdout).unwrap();
    assert!(actual.contains(expected));
    assert!(actual.contains("1 issue fixed."));
}

#[test]
fn fixes_contract_missing() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("missing_contract.tree");

    let output = cmd(&binary_path, "check", &tree_path, &["--fix", "--stdout"]);
    let expected = "// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;

contract MissingContract {
    function test_ShouldNotFindAContractInTheSolidityFile() external {
        // It should not find a contract in the Solidity file.
    }
}";

    let actual = String::from_utf8(output.stdout).unwrap();
    assert!(actual.contains(expected));
    assert!(actual.contains("1 issue fixed."));
}

#[test]
fn fixes_extra_codegen_tree() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("extra_codegen_tree.tree");

    let output = cmd(&binary_path, "check", &tree_path, &["--fix", "--stdout"]);
    let actual = String::from_utf8(output.stdout).unwrap();
    assert!(actual.contains("test_ShouldNeverRevert"));
    assert!(actual.contains("Third"));
    assert!(actual.contains("2 issues fixed."));
}

#[test]
fn fixes_extra_fn_plus_wrong_order() {
    let cwd = env::current_dir().unwrap();
    let binary_path = get_binary_path();
    let tree_path =
        cwd.join("tests").join("check").join("fix_extra_fn_plus_order.tree");

    let output = cmd(&binary_path, "check", &tree_path, &["--fix", "--stdout"]);
    let actual = String::from_utf8(output.stdout).unwrap();

    let expected = r"// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;

contract Foo {
    function test_WhenB() external {
        // it Y
    }

    function test_WhenA() external {
        // it X
    }

    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    // ==================== BULLOAK AUTOGENERATED SEPARATOR ====================
    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //    Code below this section could not be automatically moved by bulloak
    // =========================================================================

    function test_WhenTheMethodIsCalledASecondTime() external {
        // it Z
    }
}";

    assert!(actual.contains(expected));
    assert!(actual.contains("1 issue fixed."));
}

#[test]
fn fixes_invalid_structural_match() {
    let binary_path = get_binary_path();
    let cwd = env::current_dir().unwrap();
    let tree_path =
        cwd.join("tests").join("check").join("invalid_sol_structure.tree");

    let output = cmd(&binary_path, "check", &tree_path, &["--fix", "--stdout"]);
    let actual = String::from_utf8(output.stdout).unwrap();

    let expected = r"// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;

contract CancelTest {
    function test_RevertWhen_DelegateCalled() external {
        // it should revert
    }

    modifier whenNotDelegateCalled() {
        _;
    }

    function test_RevertGiven_TheIdReferencesANullStream() external whenNotDelegateCalled {
        // it should revert
    }

    modifier givenTheIdDoesNotReferenceANullStream() {
        _;
    }

    modifier givenTheStreamIsCold() {
        _;
    }

    function test_RevertGiven_TheStreamsStatusIsDEPLETED()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsCold
    {
        // it should revert
    }

    function test_RevertGiven_TheStreamsStatusIsCANCELED()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsCold
    {
        // it should revert
    }

    function test_RevertGiven_TheStreamsStatusIsSETTLED()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsCold
    {
        // it should revert
    }

    modifier givenTheStreamIsWarm() {
        _;
    }

    modifier whenTheCallerIsAuthorized() {
        _;
    }

    function test_RevertGiven_TheStreamIsNotCancelable()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsWarm
        whenTheCallerIsAuthorized
    {
        // it should revert
    }

    function test_GivenTheSenderIsNotAContract()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsWarm
        whenTheCallerIsAuthorized
    {
        // it should cancel the stream
        // it should mark the stream as canceled
    }

    modifier givenTheSenderIsAContract() {
        _;
    }

    function test_GivenTheSenderDoesNotImplementTheHook()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsWarm
        whenTheCallerIsAuthorized
        givenTheSenderIsAContract
    {
        // it should cancel the stream
        // it should mark the stream as canceled
        // it should call the sender hook
        // it should ignore the revert
    }

    modifier givenTheSenderImplementsTheHook() {
        _;
    }

    function test_WhenTheSenderReverts()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsWarm
        whenTheCallerIsAuthorized
        givenTheSenderIsAContract
        givenTheSenderImplementsTheHook
    {
        // it should cancel the stream
        // it should mark the stream as canceled
        // it should call the sender hook
        // it should ignore the revert
    }

    modifier whenTheSenderDoesNotRevert() {
        _;
    }

    function test_WhenThereIsReentrancy()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsWarm
        whenTheCallerIsAuthorized
        givenTheSenderIsAContract
        givenTheSenderImplementsTheHook
        whenTheSenderDoesNotRevert
    {
        // it should cancel the stream
        // it should mark the stream as canceled
        // it should call the sender hook
        // it should ignore the revert
    }

    function test_WhenThereIsNoReentrancy()
        external
        whenNotDelegateCalled
        givenTheIdDoesNotReferenceANullStream
        givenTheStreamIsWarm
        whenTheCallerIsAuthorized
        givenTheSenderIsAContract
        givenTheSenderImplementsTheHook
        whenTheSenderDoesNotRevert
    {
        // it should cancel the stream
        // it should mark the stream as canceled
        // it should make the stream not cancelable
        // it should update the refunded amount
        // it should refund the sender
        // it should call the sender hook
        // it should emit a {MetadataUpdate} event
        // it should emit a {CancelLockupStream} event
    }
}";

    assert!(actual.contains(expected));
    assert!(actual.contains("4 issues fixed."));
}

/// If you pass an invalid glob to `bulloak check`,
/// it should warn but still exit code = 0 and report “no issues found.”
#[test]
fn check_invalid_glob_warns_but_reports_success() {
    let cwd = env::current_dir().unwrap();
    let bin = common::get_binary_path();

    let bad_glob = cwd.join("tests").join("check").join("*[.tree");
    let out = cmd(&bin, "check", &bad_glob, &[]);

    assert!(
        out.status.success(),
        "check should succeed even on invalid glob, got {:?}",
        out
    );

    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("could not expand"),
        "did not see warn on stderr: {}",
        stderr
    );

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("All checks completed successfully"),
        "expected success message, got: {}",
        stdout
    );
}