Bug 1387555 - [mozlint] After using --edit, run lint check again, r?dylan
MozReview-Commit-ID: BlJbWVv1CeO
--- a/python/mozlint/mozlint/cli.py
+++ b/python/mozlint/mozlint/cli.py
@@ -106,16 +106,21 @@ class MozlintParser(ArgumentParser):
self.validate(args)
return args, extra
def validate(self, args):
if args.edit and not os.environ.get('EDITOR'):
self.error("must set the $EDITOR environment variable to use --edit")
+ if args.paths:
+ invalid = [p for p in args.paths if not os.path.exists(p)]
+ if invalid:
+ self.error("the following paths do not exist:\n{}".format("\n".join(invalid)))
+
def find_linters(linters=None):
lints = []
for search_path in SEARCH_PATHS:
if not os.path.isdir(search_path):
continue
sys.path.insert(0, search_path)
@@ -139,33 +144,28 @@ def run(paths, linters, fmt, outgoing, w
from mozlint import LintRoller, formatters
if list_linters:
lint_paths = find_linters(linters)
print("Available linters: {}".format(
[os.path.splitext(os.path.basename(l))[0] for l in lint_paths]
))
return 0
+
lint = LintRoller(**lintargs)
lint.read(find_linters(linters))
- # Check if the path that is entered is a valid one.
- invalid_paths = [path for path in paths if not os.path.exists(path)]
- if invalid_paths:
- print("Error: The following paths do not exist:\n{}".format("\n".join(invalid_paths)))
- return 1
-
# run all linters
results = lint.roll(paths, outgoing=outgoing, workdir=workdir)
if edit:
editor = os.environ['EDITOR']
for path in results:
subprocess.call([editor, path])
- return 1 if lint.failed else 0
+ results = lint.roll(results.keys())
formatter = formatters.get(fmt)
# Encode output with 'replace' to avoid UnicodeEncodeErrors on
# environments that aren't using utf-8.
out = formatter(results, failed=lint.failed).encode(
sys.stdout.encoding or 'ascii', 'replace')
if out:
--- a/python/mozlint/test/test_cli.py
+++ b/python/mozlint/test/test_cli.py
@@ -36,20 +36,24 @@ def test_cli_run_with_fix(run, capfd):
out, err = capfd.readouterr()
assert ret == 0
assert out.endswith('{}\n')
def test_cli_run_with_edit(run, parser, capfd):
os.environ['EDITOR'] = 'echo'
- ret = run(['-f', 'json', '--edit', '--linter', 'external'])
- out = capfd.readouterr()[0].strip()
- assert ret == 0
- assert os.path.basename(out) == 'foobar.js'
+ ret = run(['-f', 'compact', '--edit', '--linter', 'external'])
+ out, err = capfd.readouterr()
+ out = out.splitlines()
+ assert ret == 1
+ assert len(out) == 5
+ assert out[0].endswith('foobar.js') # from the `echo` editor
+ assert "files/foobar.js: line 1, col 1, Error" in out[1]
+ assert "files/foobar.js: line 2, col 1, Error" in out[2]
del os.environ['EDITOR']
with pytest.raises(SystemExit):
parser.parse_args(['--edit'])
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))