File: test_cli.py

package info (click to toggle)
firefox 141.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,550,588 kB
  • sloc: cpp: 7,426,506; javascript: 6,367,238; ansic: 3,707,351; python: 1,369,002; xml: 623,983; asm: 426,918; java: 184,324; sh: 64,488; makefile: 19,203; objc: 13,059; perl: 12,955; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,071; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (174 lines) | stat: -rw-r--r-- 5,245 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
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import io
import os
import subprocess
import sys
from shutil import which

import mozunit
import pytest

from mozlint import cli

here = os.path.abspath(os.path.dirname(__file__))


@pytest.fixture
def parser():
    return cli.MozlintParser()


@pytest.fixture
def run(parser, files):
    def inner(args=None):
        args = args or []
        if not any("--stdin-filename" in a for a in args):
            args.extend(files)
        lintargs = vars(parser.parse_args(args))
        lintargs["root"] = here
        lintargs["config_paths"] = [os.path.join(here, "linters")]
        return cli.run(**lintargs)

    return inner


def test_cli_with_ascii_encoding(run, monkeypatch, capfd):
    cmd = [sys.executable, "runcli.py", "-l=string", "-f=stylish", "files/foobar.js"]
    env = os.environ.copy()
    env["PYTHONPATH"] = os.pathsep.join(sys.path)
    env["PYTHONIOENCODING"] = "ascii"
    proc = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=here,
        env=env,
        universal_newlines=True,
    )
    out = proc.communicate()[0]
    assert "Traceback" not in out


def test_cli_run_with_fix(run, capfd):
    ret = run(["-f", "json", "--fix", "--linter", "external"])
    out, err = capfd.readouterr()
    assert ret == 0
    assert out.endswith("{}\n")


@pytest.mark.skipif(not which("echo"), reason="No `echo` executable found.")
def test_cli_run_with_edit(run, parser, capfd):
    os.environ["EDITOR"] = "echo"

    ret = run(["-f", "compact", "--edit", "--linter", "external"])
    out, err = capfd.readouterr()
    out = out.splitlines()
    assert ret == 1
    assert out[0].endswith("foobar.js")  # from the `echo` editor
    assert "foobar.js: line 1, col 1, Error" in out[1]
    assert "foobar.js: line 2, col 1, Error" in out[2]
    assert "2 problems" in out[-1]
    assert len(out) == 5

    del os.environ["EDITOR"]
    with pytest.raises(SystemExit):
        parser.parse_args(["--edit"])


def test_cli_run_with_setup(run, capfd):
    # implicitly call setup
    ret = run(["-l", "setup", "-l", "setupfailed", "-l", "setupraised"])
    out, err = capfd.readouterr()
    assert "setup passed" in out
    assert "setup failed" in out
    assert "setup raised" in out
    assert ret == 1

    # explicitly call setup
    ret = run(["-l", "setup", "-l", "setupfailed", "-l", "setupraised", "--setup"])
    out, err = capfd.readouterr()
    assert "setup passed" in out
    assert "setup failed" in out
    assert "setup raised" in out
    assert ret == 1


def test_cli_run_with_all_skipped(run, capfd):
    # implicitly call setup
    ret = run(["-l", "setupskipped"])
    out, err = capfd.readouterr()
    assert "setup skipped" in out
    assert "ERROR" in err
    assert ret == 1


def test_cli_for_exclude_list(run, monkeypatch, capfd):
    ret = run(["-l", "excludes", "--check-exclude-list"])
    out, err = capfd.readouterr()

    assert "**/foobar.js" in out
    assert (
        "The following list of paths are now green and can be removed from the exclude list:"
        in out
    )

    ret = run(["-l", "excludes_empty", "--check-exclude-list"])
    out, err = capfd.readouterr()

    assert "No path in the exclude list is green." in out
    assert ret == 1


def test_cli_run_with_wrong_linters(run, capfd):
    run(["-l", "external", "-l", "foobar"])
    out, err = capfd.readouterr()

    # Check if it identifies foobar as invalid linter
    assert "A failure occurred in the foobar linter." in out

    # Check for exception message
    assert "Invalid linters given, run again using valid linters or no linters" in out


def test_cli_run_with_stdin_filename(run, filedir, capfd, monkeypatch, tmp_path):
    for arg in ("bar.txt", "--workdir", "--outgoing", "--rev=abc"):
        with pytest.raises(SystemExit):
            run(["--stdin-filename=foo.txt", arg])

    capfd.readouterr()
    monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"foobar\n")))
    run(["-l", "string", f"--stdin-filename={filedir}/foobar.py"])
    out, err = capfd.readouterr()
    assert out == "✖ 0 problems (0 errors, 0 warnings, 0 fixed)\n"

    monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"foobar\n")))
    run(["-l", "string", f"--stdin-filename={filedir}/foobar.py", "--dump-stdin-file"])
    out, err = capfd.readouterr()
    assert out == "foobar\n"

    monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"foobar\n")))
    run(["-l", "string", f"--stdin-filename={filedir}/foobar.py", "--fix"])
    out, err = capfd.readouterr()
    assert out == "foobar\n"

    monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"foobar\n")))
    tmpfile = tmp_path / "temp"
    run(
        [
            "-l",
            "string",
            f"--stdin-filename={filedir}/foobar.py",
            "--dump-stdin-file",
            str(tmpfile),
        ]
    )
    out, err = capfd.readouterr()
    assert out == ""
    assert tmpfile.read_text() == "foobar\n"


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