tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_editor.py (2145B)


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