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/.
4
5import os
6import subprocess
7
8import mozunit
9import pytest
10
11from mozlint import editor
12from mozlint.result import ResultSummary, Issue
13
14here = os.path.abspath(os.path.dirname(__file__))
15
16
17@pytest.fixture
18def capture_commands(monkeypatch):
19    def inner(commands):
20        def fake_subprocess_call(*args, **kwargs):
21            commands.append(args[0])
22
23        monkeypatch.setattr(subprocess, "call", fake_subprocess_call)
24
25    return inner
26
27
28@pytest.fixture
29def result():
30    result = ResultSummary("/fake/root")
31    result.issues["foo.py"].extend(
32        [
33            Issue(
34                linter="no-foobar",
35                path="foo.py",
36                lineno=1,
37                message="Oh no!",
38            ),
39            Issue(
40                linter="no-foobar",
41                path="foo.py",
42                lineno=3,
43                column=10,
44                message="To Yuma!",
45            ),
46        ]
47    )
48    return result
49
50
51def test_no_editor(monkeypatch, capture_commands, result):
52    commands = []
53    capture_commands(commands)
54
55    monkeypatch.delenv("EDITOR", raising=False)
56    editor.edit_issues(result)
57    assert commands == []
58
59
60def test_no_issues(monkeypatch, capture_commands, result):
61    commands = []
62    capture_commands(commands)
63
64    monkeypatch.setenv("EDITOR", "generic")
65    result.issues = {}
66    editor.edit_issues(result)
67    assert commands == []
68
69
70def test_vim(monkeypatch, capture_commands, result):
71    commands = []
72    capture_commands(commands)
73
74    monkeypatch.setenv("EDITOR", "vim")
75    editor.edit_issues(result)
76    assert len(commands) == 1
77    assert commands[0][0] == "vim"
78
79
80def test_generic(monkeypatch, capture_commands, result):
81    commands = []
82    capture_commands(commands)
83
84    monkeypatch.setenv("EDITOR", "generic")
85    editor.edit_issues(result)
86    assert len(commands) == len(result.issues)
87    assert all(c[0] == "generic" for c in commands)
88    assert all("foo.py" in c for c in commands)
89
90
91if __name__ == "__main__":
92    mozunit.main()
93