summaryrefslogtreecommitdiff
path: root/tests/test_editor.py
blob: 0ec6f5f43f43523739b477959207631184c284f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
from os.path import join
from unittest.mock import patch

from alembic import util
from alembic.testing import combinations
from alembic.testing import expect_raises_message
from alembic.testing.fixtures import TestBase


class TestHelpers(TestBase):
    def common(self, cb, is_posix=True):
        with patch("alembic.util.editor.check_call") as check_call, patch(
            "alembic.util.editor.exists"
        ) as exists, patch(
            "alembic.util.editor.is_posix",
            new=is_posix,
        ), patch(
            "os.pathsep", new=":" if is_posix else ";"
        ):
            cb(check_call, exists)

    @combinations((True,), (False,))
    def test_edit_with_user_editor(self, posix):
        def go(check_call, exists):
            test_environ = {"EDITOR": "myvim", "PATH": "/usr/bin"}
            executable = join("/usr/bin", "myvim")
            if not posix:
                executable += ".exe"

            exists.side_effect = lambda fname: fname == executable
            util.open_in_editor("myfile", test_environ)
            check_call.assert_called_with([executable, "myfile"])

        self.common(go, posix)

    @combinations(("EDITOR",), ("VISUAL",))
    def test_edit_with_user_editor_exists(self, key):
        def go(check_call, exists):
            test_environ = {key: "myvim", "PATH": "/usr/bin"}
            exists.side_effect = lambda fname: fname == "myvim"
            util.open_in_editor("myfile", test_environ)
            check_call.assert_called_with(["myvim", "myfile"])

        self.common(go)

    @combinations((True,), (False,))
    def test_edit_with_user_editor_precedence(self, with_path):
        def go(check_call, exists):
            test_environ = {
                "EDITOR": "myvim",
                "VISUAL": "myvisual",
                "PATH": "/usr/bin",
            }
            exes = ["myvim", "myvisual"]
            if with_path:
                exes = [join("/usr/bin", n) for n in exes]
            exists.side_effect = lambda fname: fname in exes
            util.open_in_editor("myfile", test_environ)
            check_call.assert_called_with([exes[0], "myfile"])

        self.common(go)

    def test_edit_with_user_editor_abs(self):
        def go(check_call, exists):
            test_environ = {"EDITOR": "/foo/myvim", "PATH": "/usr/bin"}
            exists.side_effect = lambda fname: fname == "/usr/bin/foo/myvim"
            with expect_raises_message(util.CommandError, "EDITOR"):
                util.open_in_editor("myfile", test_environ)

        self.common(go)

    def test_edit_with_default_editor(self):
        def go(check_call, exists):
            test_environ = {"PATH": os.pathsep.join(["/usr/bin", "/bin"])}
            executable = join("/bin", "vim")

            exists.side_effect = lambda fname: fname == executable
            util.open_in_editor("myfile", test_environ)
            check_call.assert_called_with([executable, "myfile"])

        self.common(go)

    def test_edit_with_default_editor_windows(self):
        def go(check_call, exists):
            test_environ = {
                "PATH": os.pathsep.join(
                    [r"C:\Windows\System32", r"C:\Users\user\bin"]
                )
            }
            executable = join(r"C:\Users\user\bin", "notepad.exe")

            exists.side_effect = lambda fname: fname == executable
            util.open_in_editor("myfile", test_environ)
            check_call.assert_called_with([executable, "myfile"])

        self.common(go, False)

    def test_edit_with_missing_editor(self):
        def go(check_call, exists):
            test_environ = {}
            exists.return_value = False
            with expect_raises_message(util.CommandError, "EDITOR"):
                util.open_in_editor("myfile", test_environ)

        self.common(go)