Repository navigation
Expand file tree
/
Copy pathtest_runner.py
More file actions
213 lines (176 loc) · 5.88 KB
/
Copy pathtest_runner.py
File metadata and controls
213 lines (176 loc) · 5.88 KB
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""Tests for runner module."""
import sys
import pytest
from vscode_common_python_lsp.runner import (
CustomIO,
RunResult,
run_api,
run_module,
run_path,
)
class TestRunResult:
@pytest.mark.parametrize(
"args, expected_exit_code",
[
(("out", "err"), None),
(("out", "err", 1), 1),
],
)
def test_fields(self, args, expected_exit_code):
r = RunResult(*args)
assert r.stdout == "out"
assert r.stderr == "err"
assert r.exit_code == expected_exit_code
class TestCustomIO:
def test_write_and_read(self):
c = CustomIO("<test>")
c.write("hello")
assert c.get_value() == "hello"
def test_close_is_noop(self):
c = CustomIO("<test>")
c.close() # should not raise
c.write("still works")
assert c.get_value() == "still works"
class TestRunPath:
def test_simple_command(self, tmp_path):
result = run_path(
[sys.executable, "-c", "print('hello')"],
use_stdin=False,
cwd=str(tmp_path),
)
assert "hello" in result.stdout
assert result.exit_code == 0
def test_with_stdin(self, tmp_path):
result = run_path(
[sys.executable, "-c", "import sys; print(sys.stdin.read())"],
use_stdin=True,
cwd=str(tmp_path),
source="test input",
)
assert "test input" in result.stdout
def test_exit_code_captured(self, tmp_path):
result = run_path(
[sys.executable, "-c", "import sys; sys.exit(42)"],
use_stdin=False,
cwd=str(tmp_path),
)
assert result.exit_code == 42
def test_with_env(self, tmp_path):
import os
env = {**os.environ, "TEST_VAR": "hello_from_env"}
result = run_path(
[sys.executable, "-c", "import os; print(os.environ['TEST_VAR'])"],
use_stdin=False,
cwd=str(tmp_path),
env=env,
)
assert "hello_from_env" in result.stdout
class TestRunModule:
def test_basic_execution(self, tmp_path):
"""run_module can execute a stdlib module like json.tool."""
result = run_module(
"json.tool",
["json.tool"],
use_stdin=True,
cwd=str(tmp_path),
source='{"key": "value"}',
)
assert '"key"' in result.stdout
assert '"value"' in result.stdout
def test_captures_exit_code(self, tmp_path):
"""SystemExit exit codes are captured by run_module."""
result = run_module(
"json.tool",
["json.tool"],
use_stdin=True,
cwd=str(tmp_path),
source="not valid json",
)
assert result.exit_code is not None
assert result.exit_code != 0
def test_captures_stderr(self, tmp_path):
"""Stderr output is captured by run_module."""
result = run_module(
"json.tool",
["json.tool"],
use_stdin=True,
cwd=str(tmp_path),
source="not valid json",
)
# json.tool stores the error in exit_code (JSONDecodeError);
# verify the module ran and captured something meaningful
assert result.exit_code is not None
class TestRunApi:
def test_basic_callback(self, tmp_path):
"""run_api executes a callback and captures output."""
def _callback(argv, stdout, stderr, stdin=None):
stdout.write("api output")
result = run_api(
_callback,
["tool", "--arg"],
use_stdin=False,
cwd=str(tmp_path),
)
assert result.stdout == "api output"
assert result.stderr == ""
def test_callback_with_stdin(self, tmp_path):
"""run_api passes stdin to callback when use_stdin=True."""
def _callback(argv, stdout, stderr, stdin):
data = stdin.read()
stdout.write(f"got: {data}")
result = run_api(
_callback,
["tool"],
use_stdin=True,
cwd=str(tmp_path),
source="input data",
)
assert result.stdout == "got: input data"
def test_callback_receives_argv(self, tmp_path):
"""run_api passes argv to the callback."""
def _callback(argv, stdout, stderr, stdin=None):
stdout.write(",".join(argv))
result = run_api(
_callback,
["tool", "--flag", "value"],
use_stdin=False,
cwd=str(tmp_path),
)
assert result.stdout == "tool,--flag,value"
def test_system_exit_captured(self, tmp_path):
"""SystemExit is caught and exit code stored."""
def _callback(argv, stdout, stderr, stdin=None):
stdout.write("before exit")
raise SystemExit(1)
result = run_api(
_callback,
["tool"],
use_stdin=False,
cwd=str(tmp_path),
)
assert result.exit_code == 1
assert "before exit" in result.stdout
def test_captures_exit_code(self, tmp_path):
"""SystemExit exit codes are captured."""
def _callback(argv, stdout, stderr, stdin=None):
raise SystemExit(42)
result = run_api(
_callback,
["test"],
use_stdin=False,
cwd=str(tmp_path),
)
assert result.exit_code == 42
def test_captures_stderr(self, tmp_path):
"""Stderr output is captured separately."""
def _callback(argv, stdout, stderr, stdin=None):
stderr.write("error message")
result = run_api(
_callback,
["test"],
use_stdin=False,
cwd=str(tmp_path),
)
assert result.stderr == "error message"