summaryrefslogtreecommitdiffstats
path: root/tools/lint/test/test_clippy.py
blob: 553aba88764e05d6263b2de49442a9d8a0f8eb0a (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
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
import mozunit
import os

LINTER = "clippy"


def test_basic(lint, config, paths):
    results = lint(paths("test1/"))
    print(results)
    assert len(results) > 7

    assert (
        "is never read" in results[0].message or "but never used" in results[0].message
    )
    assert results[0].level == "warning"
    assert results[0].lineno == 7
    assert results[0].column == 9
    assert results[0].rule == "unused_assignments"
    assert results[0].relpath == "test1/bad.rs"
    assert "tools/lint/test/files/clippy/test1/bad.rs" in results[0].path

    assert "this looks like you are trying to swap `a` and `b`" in results[1].message
    assert results[1].level == "error"
    assert results[1].relpath == "test1/bad.rs"
    assert results[1].rule == "clippy::almost_swapped"
    assert "or maybe you should use `std::mem::replace`" in results[1].hint

    assert "value assigned to `b` is never read" in results[2].message
    assert results[2].level == "warning"
    assert results[2].relpath == "test1/bad.rs"

    assert "variable does not need to be mutable" in results[5].message
    assert results[5].relpath == "test1/bad2.rs"
    assert results[5].rule == "unused_mut"

    assert "this range is empty so" in results[6].message
    assert results[6].level == "error"
    assert results[6].relpath == "test1/bad2.rs"
    assert results[6].rule == "clippy::reversed_empty_ranges"


def test_error(lint, config, paths):
    results = lint(paths("test1/Cargo.toml"))
    # Should fail. We don't accept Cargo.toml as input
    assert results == 1


def test_file_and_path_provided(lint, config, paths):
    results = lint(paths("./test2/src/bad_1.rs", "test1/"))
    # even if clippy analyzed it
    # we should not have anything from bad_2.rs
    # as mozlint is filtering out the file
    print(results)
    assert len(results) > 16
    assert "value assigned to `a` is never read" in results[0].message
    assert results[0].level == "warning"
    assert results[0].lineno == 7
    assert results[0].column == 9
    assert results[0].rule == "unused_assignments"
    assert results[0].relpath == "test1/bad.rs"
    assert "tools/lint/test/files/clippy/test1/bad.rs" in results[0].path
    assert "value assigned to `a` is never read" in results[0].message
    assert results[8].level == "warning"
    assert results[8].lineno == 1
    assert results[8].column == 4
    assert results[8].rule == "dead_code"
    assert results[8].relpath == "test2/src/bad_1.rs"
    assert "tools/lint/test/files/clippy/test2/src/bad_1.rs" in results[8].path
    for r in results:
        assert "bad_2.rs" not in r.relpath


def test_file_provided(lint, config, paths):
    results = lint(paths("./test2/src/bad_1.rs"))
    # even if clippy analyzed it
    # we should not have anything from bad_2.rs
    # as mozlint is filtering out the file
    print(results)
    assert len(results) > 8
    assert results[0].level == "warning"
    assert results[0].lineno == 1
    assert results[0].column == 4
    assert results[0].rule == "dead_code"
    assert results[0].relpath == "test2/src/bad_1.rs"
    assert "tools/lint/test/files/clippy/test2/src/bad_1.rs" in results[0].path
    for r in results:
        assert "bad_2.rs" not in r.relpath


def test_cleanup(lint, paths, root):
    # If Cargo.lock does not exist before clippy run, delete it
    lint(paths("test1/"))
    assert not os.path.exists(os.path.join(root, "test1/target/"))
    assert not os.path.exists(os.path.join(root, "test1/Cargo.lock"))

    # If Cargo.lock exists before clippy run, keep it after cleanup
    lint(paths("test2/"))
    assert not os.path.exists(os.path.join(root, "test2/target/"))
    assert os.path.exists(os.path.join(root, "test2/Cargo.lock"))


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