Bug 1735097 - Geolocation: use EpochTimeStamp instead of DOMTimeStamp r=saschanaz...
[gecko.git] / python / mozlint / test / test_cli.py
blob54081547001856814658bae17fa45ff9c870905d
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/.
5 import os
6 import subprocess
7 import sys
8 from distutils.spawn import find_executable
10 import mozunit
11 import pytest
13 from mozlint import cli
15 here = os.path.abspath(os.path.dirname(__file__))
18 @pytest.fixture
19 def parser():
20 return cli.MozlintParser()
23 @pytest.fixture
24 def run(parser, lintdir, files):
25 def inner(args=None):
26 args = args or []
27 args.extend(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)
33 return inner
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(
42 cmd,
43 stdout=subprocess.PIPE,
44 stderr=subprocess.STDOUT,
45 cwd=here,
46 env=env,
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()
56 assert ret == 0
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()
67 assert ret == 1
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]
72 assert len(out) == 5
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
86 assert ret == 1
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
94 assert ret == 1
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
102 assert (
103 "The following list of paths are now green and can be removed from the exclude list:"
104 in out
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
111 assert ret == 1
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__":
127 mozunit.main()