File: test_clippy.py

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (106 lines) | stat: -rw-r--r-- 3,304 bytes parent folder | download | duplicates (2)
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
import mozunit

LINTER = "clippy"
fixed = 0


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


def test_basic(lint, config, paths):
    # Test basic linting functionality
    # Note: These tests may not produce clippy results if cargo/clippy aren't available
    # in the test environment, but they verify the code path works without crashes
    results = lint(paths("test1/bad.rs"))
    print(results)
    # Verify the linting process completes and returns expected structure
    assert isinstance(results, list)
    # If clippy is available and detects issues, check the structure
    if len(results) > 0:
        # bad.rs contains a swap pattern that clippy should detect
        assert "bad.rs" in results[0].path
        assert results[0].level in ["warning", "error"]


def test_multiple_files(lint, config, paths):
    # Test linting multiple files in a directory
    results = lint(paths("test1/"))
    print(results)
    # Should lint both good.rs and bad.rs files
    assert isinstance(results, list)
    # If results are found, they should only be from bad files (not good.rs)
    if len(results) > 0:
        for result in results:
            assert "good.rs" not in result.path, "Good file should not have lint issues"


def test_non_gkrust_crate(lint, config, paths):
    # Test the non-gkrust crate path (test2 directory)
    results = lint(paths("test2/"))
    print(results)
    # Verify the linting process handles non-gkrust crates
    assert isinstance(results, list)
    # If clippy runs and finds issues, verify structure
    if len(results) > 0:
        assert results[0].level in ["warning", "error"]
        assert "test2" in results[0].path


def test_fix_gkrust(lint, paths, create_temp_file):
    """Test --fix option for gkrust crate files"""
    # Create a temporary Rust file with fixable clippy issues
    contents = """fn main() {
    let mut x = 5;
    x = 6;
    println!("{}", x);
}
"""
    path = create_temp_file(contents, "fixable.rs")

    # Run clippy without fix first to see baseline
    results_before = lint([path], fix=False)

    # Run clippy with fix=True
    results_after = lint([path], fix=True)

    # Verify that the linting process completed
    assert isinstance(results_before, list)
    assert isinstance(results_after, list)

    # The fixed counter should be non-negative and track attempts
    assert fixed >= 0


def test_fix_non_gkrust(lint, paths, create_temp_file):
    """Test --fix option for non-gkrust crate files"""
    # Create a temporary Rust file with common fixable clippy patterns
    contents = """fn main() {
    let mut unused_var = 5;
    unused_var = 6;
    let x = vec![1, 2, 3];
    for i in 0..x.len() {
        println!("{}", x[i]);
    }
}
"""
    path = create_temp_file(contents, "non_gkrust_fixable.rs")

    # Reset the fixed counter
    fixed_before = fixed

    # Run clippy with fix=True
    results = lint([path], fix=True)

    # Verify that the linting process completed
    assert isinstance(results, list)

    # Check that the fixed counter is properly tracked
    # The counter should be >= what it was before (may increment if fixes applied)
    assert fixed >= fixed_before


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