1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 from distutils
.spawn
import find_executable
13 from mozlint
import cli
15 here
= os
.path
.abspath(os
.path
.dirname(__file__
))
20 return cli
.MozlintParser()
24 def run(parser
, lintdir
, files
):
28 lintargs
= vars(parser
.parse_args(args
))
29 lintargs
["root"] = here
30 lintargs
["config_paths"] = [os
.path
.join(here
, "linters")]
31 return cli
.run(**lintargs
)
36 def test_cli_with_ascii_encoding(run
, monkeypatch
, capfd
):
37 cmd
= [sys
.executable
, "runcli.py", "-l=string", "-f=stylish"]
38 env
= os
.environ
.copy()
39 env
["PYTHONPATH"] = os
.pathsep
.join(sys
.path
)
40 env
["PYTHONIOENCODING"] = "ascii"
41 proc
= subprocess
.Popen(
43 stdout
=subprocess
.PIPE
,
44 stderr
=subprocess
.STDOUT
,
47 universal_newlines
=True,
49 out
= proc
.communicate()[0]
50 assert "Traceback" not in out
53 def test_cli_run_with_fix(run
, capfd
):
54 ret
= run(["-f", "json", "--fix", "--linter", "external"])
55 out
, err
= capfd
.readouterr()
57 assert out
.endswith("{}\n")
60 @pytest.mark
.skipif(not find_executable("echo"), reason
="No `echo` executable found.")
61 def test_cli_run_with_edit(run
, parser
, capfd
):
62 os
.environ
["EDITOR"] = "echo"
64 ret
= run(["-f", "compact", "--edit", "--linter", "external"])
65 out
, err
= capfd
.readouterr()
66 out
= out
.splitlines()
68 assert out
[0].endswith("foobar.js") # from the `echo` editor
69 assert "foobar.js: line 1, col 1, Error" in out
[1]
70 assert "foobar.js: line 2, col 1, Error" in out
[2]
71 assert "2 problems" in out
[-1]
74 del os
.environ
["EDITOR"]
75 with pytest
.raises(SystemExit):
76 parser
.parse_args(["--edit"])
79 def test_cli_run_with_setup(run
, capfd
):
80 # implicitly call setup
81 ret
= run(["-l", "setup", "-l", "setupfailed", "-l", "setupraised"])
82 out
, err
= capfd
.readouterr()
83 assert "setup passed" in out
84 assert "setup failed" in out
85 assert "setup raised" in out
88 # explicitly call setup
89 ret
= run(["-l", "setup", "-l", "setupfailed", "-l", "setupraised", "--setup"])
90 out
, err
= capfd
.readouterr()
91 assert "setup passed" in out
92 assert "setup failed" in out
93 assert "setup raised" in out
97 def test_cli_for_exclude_list(run
, monkeypatch
, capfd
):
98 ret
= run(["-l", "excludes", "--check-exclude-list"])
99 out
, err
= capfd
.readouterr()
101 assert "**/foobar.js" in out
103 "The following list of paths are now green and can be removed from the exclude list:"
107 ret
= run(["-l", "excludes_empty", "--check-exclude-list"])
108 out
, err
= capfd
.readouterr()
110 assert "No path in the exclude list is green." in out
114 def test_cli_run_with_wrong_linters(run
, capfd
):
116 run(["-l", "external", "-l", "foobar"])
117 out
, err
= capfd
.readouterr()
119 # Check if it identifes foobar as invalid linter
120 assert "A failure occurred in the foobar linter." in out
122 # Check for exception message
123 assert "Invalid linters given, run again using valid linters or no linters" in out
126 if __name__
== "__main__":