summaryrefslogtreecommitdiffstats
path: root/tools/lint/test/test_rustfmt.py
blob: c176a2695dbc2920e5194d6418824ebce34d39e0 (plain)
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
import mozunit

LINTER = "rustfmt"
fixed = 0


def test_good(lint, config, paths):
    results = lint(paths("subdir/good.rs"))
    print(results)
    assert len(results) == 0


def test_basic(lint, config, paths):
    results = lint(paths("subdir/bad.rs"))
    print(results)
    assert len(results) >= 1

    assert "Reformat rust" in results[0].message
    assert results[0].level == "warning"
    assert results[0].lineno == 4
    assert "bad.rs" in results[0].path
    assert "Print text to the console" in results[0].diff


def test_dir(lint, config, paths):
    results = lint(paths("subdir/"))
    print(results)
    assert len(results) >= 4

    assert "Reformat rust" in results[0].message
    assert results[0].level == "warning"
    assert results[0].lineno == 4
    assert "bad.rs" in results[0].path
    assert "Print text to the console" in results[0].diff

    assert "Reformat rust" in results[1].message
    assert results[1].level == "warning"
    assert results[1].lineno == 4
    assert "bad2.rs" in results[1].path
    assert "Print text to the console" in results[1].diff


def test_fix(lint, create_temp_file):

    contents = """fn main() {
    // Statements here are executed when the compiled binary is called

    // Print text to the console
    println!("Hello World!");
    let mut a;
    let mut b=1;
    let mut vec = Vec::new();
    vec.push(1);
    vec.push(2);


    for x in 5..10 - 5 {
        a = x;
    }

    }
"""

    path = create_temp_file(contents, "bad.rs")
    lint([path], fix=True)
    assert fixed == 3


if __name__ == "__main__":
    mozunit.main()