summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/util/editor.py
blob: 7365bd4e4d62957bfa156474e481efe8bd8e697c (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import os
import sys

import pytest

from llnl.util.filesystem import set_executable

import spack.util.editor as ed

pytestmark = [
    pytest.mark.usefixtures("working_env"),
    pytest.mark.not_on_windows("editor not implemented on windows"),
]


# env vars that control the editor
EDITOR_VARS = ["SPACK_EDITOR", "VISUAL", "EDITOR"]


@pytest.fixture(scope="module", autouse=True)
def clean_env_vars():
    """Unset all editor env vars before tests."""
    for var in EDITOR_VARS:
        if var in os.environ:
            del os.environ[var]


@pytest.fixture(autouse=True)
def working_editor_test_env(working_env):
    """Don't leak environent variables between functions here."""


# parameterized fixture for editor var names
@pytest.fixture(params=EDITOR_VARS)
def editor_var(request):
    return request.param


def _make_exe(tmpdir_factory, name, contents=None):
    if sys.platform == "win32":
        name += ".exe"
    path = str(tmpdir_factory.mktemp("%s_exe" % name).join(name))
    if contents is not None:
        with open(path, "w") as f:
            f.write("#!/bin/sh\n%s\n" % contents)
        set_executable(path)
    return path


@pytest.fixture(scope="session")
def good_exe(tmpdir_factory):
    return _make_exe(tmpdir_factory, "good", "exit 0")


@pytest.fixture(scope="session")
def bad_exe(tmpdir_factory):
    return _make_exe(tmpdir_factory, "bad", "exit 1")


@pytest.fixture(scope="session")
def nosuch_exe(tmpdir_factory):
    return _make_exe(tmpdir_factory, "nosuch")


@pytest.fixture(scope="session")
def vim_exe(tmpdir_factory):
    return _make_exe(tmpdir_factory, "vim", "exit 0")


@pytest.fixture(scope="session")
def gvim_exe(tmpdir_factory):
    return _make_exe(tmpdir_factory, "gvim", "exit 0")


def test_find_exe_from_env_var(good_exe):
    os.environ["EDITOR"] = good_exe
    assert ed._find_exe_from_env_var("EDITOR") == (good_exe, [good_exe])


def test_find_exe_from_env_var_with_args(good_exe):
    os.environ["EDITOR"] = good_exe + " a b c"
    assert ed._find_exe_from_env_var("EDITOR") == (good_exe, [good_exe, "a", "b", "c"])


def test_find_exe_from_env_var_bad_path(nosuch_exe):
    os.environ["EDITOR"] = nosuch_exe
    assert ed._find_exe_from_env_var("FOO") == (None, [])


def test_editor_gvim_special_case(gvim_exe):
    os.environ["EDITOR"] = gvim_exe

    def assert_exec(exe, args):
        assert exe == gvim_exe
        assert args == [gvim_exe, "-f", "/path/to/file"]
        return 0

    assert ed.editor("/path/to/file", exec_fn=assert_exec)

    os.environ["EDITOR"] = gvim_exe + " -f"
    assert ed.editor("/path/to/file", exec_fn=assert_exec)


def test_editor_precedence(good_exe, gvim_exe, vim_exe, bad_exe):
    """Ensure we prefer editor variables in order of precedence."""
    os.environ["SPACK_EDITOR"] = good_exe
    os.environ["VISUAL"] = gvim_exe
    os.environ["EDITOR"] = vim_exe
    correct_exe = good_exe

    def assert_callback(exe, args):
        result = ed.executable(exe, args)
        if result == 0:
            assert exe == correct_exe
        return result

    ed.editor(exec_fn=assert_callback)

    os.environ["SPACK_EDITOR"] = bad_exe
    correct_exe = gvim_exe
    ed.editor(exec_fn=assert_callback)

    os.environ["VISUAL"] = bad_exe
    correct_exe = vim_exe
    ed.editor(exec_fn=assert_callback)


def test_find_exe_from_env_var_no_editor():
    if "FOO" in os.environ:
        os.environ.unset("FOO")
    assert ed._find_exe_from_env_var("FOO") == (None, [])


def test_editor(editor_var, good_exe):
    os.environ[editor_var] = good_exe

    def assert_exec(exe, args):
        assert exe == good_exe
        assert args == [good_exe, "/path/to/file"]
        return 0

    ed.editor("/path/to/file", exec_fn=assert_exec)


def test_editor_visual_bad(good_exe, bad_exe):
    os.environ["VISUAL"] = bad_exe
    os.environ["EDITOR"] = good_exe

    def assert_exec(exe, args):
        if exe == bad_exe:
            raise OSError()

        assert exe == good_exe
        assert args == [good_exe, "/path/to/file"]
        return 0

    ed.editor("/path/to/file", exec_fn=assert_exec)


def test_editor_no_visual(good_exe):
    os.environ["EDITOR"] = good_exe

    def assert_exec(exe, args):
        assert exe == good_exe
        assert args == [good_exe, "/path/to/file"]
        return 0

    ed.editor("/path/to/file", exec_fn=assert_exec)


def test_editor_no_visual_with_args(good_exe):
    # editor has extra args in the var (e.g., emacs -nw)
    os.environ["EDITOR"] = good_exe + " -nw --foo"

    def assert_exec(exe, args):
        assert exe == good_exe
        assert args == [good_exe, "-nw", "--foo", "/path/to/file"]
        return 0

    ed.editor("/path/to/file", exec_fn=assert_exec)


def test_editor_both_bad(nosuch_exe, vim_exe):
    os.environ["VISUAL"] = nosuch_exe
    os.environ["EDITOR"] = nosuch_exe

    os.environ["PATH"] = "%s%s%s" % (os.path.dirname(vim_exe), os.pathsep, os.environ["PATH"])

    def assert_exec(exe, args):
        assert exe == vim_exe
        assert args == [vim_exe, "/path/to/file"]
        return 0

    ed.editor("/path/to/file", exec_fn=assert_exec)


def test_no_editor():
    os.environ["PATH"] = ""

    def assert_exec(exe, args):
        assert False

    with pytest.raises(EnvironmentError, match=r"No text editor found.*"):
        ed.editor("/path/to/file", exec_fn=assert_exec)

    def assert_exec(exe, args):
        return False

    with pytest.raises(EnvironmentError, match=r"No text editor found.*"):
        ed.editor("/path/to/file", exec_fn=assert_exec)


def test_exec_fn_executable(editor_var, good_exe, bad_exe):
    """Make sure editor() works with ``ed.executable`` as well as execv"""
    os.environ[editor_var] = good_exe
    assert ed.editor(exec_fn=ed.executable)

    os.environ[editor_var] = bad_exe
    with pytest.raises(EnvironmentError, match=r"No text editor found.*"):
        ed.editor(exec_fn=ed.executable)