Bug 1499510 - [mozlint] Fix regression to |mach lint --edit| when using vim, r=egao
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Wed, 17 Oct 2018 20:19:37 +0000
changeset 490178 86e072fc085e8b990d879feb95c6b62941028db5
parent 490177 bb1b80139e377d9f5202e9fa1b94d39985381caf
child 490179 943c79ca40a960aeb293e812838ec8bea6762cb5
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersegao
bugs1499510
milestone64.0a1
Bug 1499510 - [mozlint] Fix regression to |mach lint --edit| when using vim, r=egao Differential Revision: https://phabricator.services.mozilla.com/D8905
python/mozlint/mozlint/cli.py
python/mozlint/mozlint/editor.py
python/mozlint/test/python.ini
python/mozlint/test/test_editor.py
--- a/python/mozlint/mozlint/cli.py
+++ b/python/mozlint/mozlint/cli.py
@@ -169,17 +169,17 @@ def run(paths, linters, fmt, outgoing, w
     ret = lint.setup()
     if setup:
         return ret
 
     # run all linters
     result = lint.roll(paths, outgoing=outgoing, workdir=workdir)
 
     if edit and result.issues:
-        edit_issues(result.issues)
+        edit_issues(result)
         result = lint.roll(result.issues.keys())
 
     formatter = formatters.get(fmt)
 
     # Encode output with 'replace' to avoid UnicodeEncodeErrors on
     # environments that aren't using utf-8.
     out = formatter(result).encode(sys.stdout.encoding or 'ascii', 'replace')
     if out:
--- a/python/mozlint/mozlint/editor.py
+++ b/python/mozlint/mozlint/editor.py
@@ -10,18 +10,18 @@ import tempfile
 
 from mozlint import formatters
 
 
 def get_editor():
     return os.environ.get('EDITOR')
 
 
-def edit_issues(issues):
-    if not issues:
+def edit_issues(result):
+    if not result.issues:
         return
 
     editor = get_editor()
     if not editor:
         print('warning: could not find a default editor')
         return
 
     name = os.path.basename(editor)
@@ -36,18 +36,18 @@ def edit_issues(issues):
             # start with quickfix window opened
             '-c', 'copen',
             # running with -q seems to open an empty buffer in addition to the
             # first file, this removes that empty buffer
             '-c', '1bd',
         ]
 
         with tempfile.NamedTemporaryFile() as fh:
-            s = formatters.get('compact', summary=False)(issues)
+            s = formatters.get('compact', summary=False)(result)
             fh.write(s)
             fh.flush()
 
             cmd.extend(['-q', fh.name])
             subprocess.call(cmd)
 
     else:
-        for path, errors in issues.iteritems():
+        for path, errors in result.issues.iteritems():
             subprocess.call([editor, path])
--- a/python/mozlint/test/python.ini
+++ b/python/mozlint/test/python.ini
@@ -1,10 +1,11 @@
 [DEFAULT]
 subsuite = mozlint, os == "linux"
 skip-if = python == 3
 
 [test_cli.py]
+[test_editor.py]
 [test_formatters.py]
 [test_parser.py]
 [test_pathutils.py]
 [test_roller.py]
 [test_types.py]
new file mode 100644
--- /dev/null
+++ b/python/mozlint/test/test_editor.py
@@ -0,0 +1,94 @@
+# 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/.
+
+from __future__ import absolute_import
+
+import os
+import subprocess
+
+import mozunit
+import pytest
+
+from mozlint import editor
+from mozlint.result import ResultSummary, Issue
+
+here = os.path.abspath(os.path.dirname(__file__))
+
+
+@pytest.fixture
+def capture_commands(monkeypatch):
+
+    def inner(commands):
+
+        def fake_subprocess_call(*args, **kwargs):
+            commands.append(args[0])
+
+        monkeypatch.setattr(subprocess, 'call', fake_subprocess_call)
+
+    return inner
+
+
+@pytest.fixture
+def result():
+    result = ResultSummary()
+    result.issues['foo.py'].extend([
+        Issue(
+            linter='no-foobar',
+            path='foo.py',
+            lineno=1,
+            message="Oh no!",
+        ),
+        Issue(
+            linter='no-foobar',
+            path='foo.py',
+            lineno=3,
+            column=10,
+            message="To Yuma!",
+        ),
+    ])
+    return result
+
+
+def test_no_editor(monkeypatch, capture_commands, result):
+    commands = []
+    capture_commands(commands)
+
+    monkeypatch.delenv('EDITOR', raising=False)
+    editor.edit_issues(result)
+    assert commands == []
+
+
+def test_no_issues(monkeypatch, capture_commands, result):
+    commands = []
+    capture_commands(commands)
+
+    monkeypatch.setenv('EDITOR', 'generic')
+    result.issues = {}
+    editor.edit_issues(result)
+    assert commands == []
+
+
+def test_vim(monkeypatch, capture_commands, result):
+    commands = []
+    capture_commands(commands)
+
+    monkeypatch.setenv('EDITOR', 'vim')
+    editor.edit_issues(result)
+    assert len(commands) == 1
+    assert commands[0][0] == 'vim'
+
+
+def test_generic(monkeypatch, capture_commands, result):
+    commands = []
+    capture_commands(commands)
+
+    monkeypatch.setenv('EDITOR', 'generic')
+    editor.edit_issues(result)
+    assert len(commands) == len(result.issues)
+    assert all(c[0] == 'generic' for c in commands)
+    assert all('foo.py' in c for c in commands)
+
+
+if __name__ == '__main__':
+    mozunit.main()