| 1 |
# |
| 2 |
# subunit: extensions to Python unittest to get test results from subprocesses. |
| 3 |
# Copyright (C) 2026 Jelmer Vernooij <[email protected]> |
| 4 |
# |
| 5 |
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause |
| 6 |
# license at the users choice. A copy of both licenses are available in the |
| 7 |
# project source as Apache-2.0 and BSD. You may not use this file except in |
| 8 |
# compliance with one of these two licences. |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# license you chose for the specific language governing permissions and |
| 14 |
# limitations under that license. |
| 15 |
# |
| 16 |
|
| 17 |
"""Tests for subunit.filter_scripts.subunit_combine.""" |
| 18 |
|
| 19 |
import os |
| 20 |
import subprocess |
| 21 |
import sys |
| 22 |
import tempfile |
| 23 |
from io import BytesIO |
| 24 |
|
| 25 |
from testtools import TestCase |
| 26 |
from testtools.testresult.doubles import StreamResult |
| 27 |
|
| 28 |
from subunit import ByteStreamToStreamResult, StreamResultToBytes |
| 29 |
from subunit.filter_scripts.subunit_combine import ( |
| 30 |
_PrefixingStreamResult, |
| 31 |
_expand_argv, |
| 32 |
_read_id_list, |
| 33 |
_select_ids_for_command, |
| 34 |
combine, |
| 35 |
load_config, |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def _stream_with_tests(test_ids): |
| 40 |
"""Build a subunit v2 byte stream with an inprogress+success pair per id.""" |
| 41 |
buf = BytesIO() |
| 42 |
out = StreamResultToBytes(buf) |
| 43 |
for tid in test_ids: |
| 44 |
out.status(test_id=tid, test_status="inprogress") |
| 45 |
out.status(test_id=tid, test_status="success") |
| 46 |
return buf.getvalue() |
| 47 |
|
| 48 |
|
| 49 |
def _parse(data): |
| 50 |
"""Parse a subunit v2 byte stream into a list of (test_id, test_status).""" |
| 51 |
events = StreamResult() |
| 52 |
ByteStreamToStreamResult(BytesIO(data)).run(events) |
| 53 |
return [(ev[1], ev[2]) for ev in events._events if ev[0] == "status"] |
| 54 |
|
| 55 |
|
| 56 |
class TestPrefixingStreamResult(TestCase): |
| 57 |
def test_prefixes_test_id(self): |
| 58 |
target = StreamResult() |
| 59 |
result = _PrefixingStreamResult(target, "py/") |
| 60 |
result.status(test_id="foo", test_status="success") |
| 61 |
self.assertEqual([("status", "py/foo", "success")], [ev[:3] for ev in target._events]) |
| 62 |
|
| 63 |
def test_passes_through_none_test_id(self): |
| 64 |
target = StreamResult() |
| 65 |
result = _PrefixingStreamResult(target, "py/") |
| 66 |
result.status(file_name="stdout", file_bytes=b"hi") |
| 67 |
self.assertEqual([("status", None, None)], [ev[:3] for ev in target._events]) |
| 68 |
|
| 69 |
|
| 70 |
class TestLoadConfig(TestCase): |
| 71 |
def _write(self, text): |
| 72 |
fd, path = tempfile.mkstemp(suffix=".yaml") |
| 73 |
self.addCleanup(os.unlink, path) |
| 74 |
with os.fdopen(fd, "w") as f: |
| 75 |
f.write(text) |
| 76 |
return path |
| 77 |
|
| 78 |
def test_basic(self): |
| 79 |
path = self._write("commands:\n - prefix: 'a/'\n argv: ['echo', 'hi']\n - argv: ['true']\n") |
| 80 |
self.assertEqual( |
| 81 |
[{"prefix": "a/", "argv": ["echo", "hi"]}, {"argv": ["true"]}], |
| 82 |
load_config(path), |
| 83 |
) |
| 84 |
|
| 85 |
def test_rejects_non_mapping(self): |
| 86 |
path = self._write("- 1\n- 2\n") |
| 87 |
self.assertRaises(ValueError, load_config, path) |
| 88 |
|
| 89 |
def test_rejects_empty_commands(self): |
| 90 |
path = self._write("commands: []\n") |
| 91 |
self.assertRaises(ValueError, load_config, path) |
| 92 |
|
| 93 |
def test_rejects_missing_argv(self): |
| 94 |
path = self._write("commands:\n - prefix: 'a/'\n") |
| 95 |
self.assertRaises(ValueError, load_config, path) |
| 96 |
|
| 97 |
def test_rejects_non_string_prefix(self): |
| 98 |
path = self._write("commands:\n - argv: ['x']\n prefix: 42\n") |
| 99 |
self.assertRaises(ValueError, load_config, path) |
| 100 |
|
| 101 |
|
| 102 |
class TestCombine(TestCase): |
| 103 |
def _cat_cmd(self, data): |
| 104 |
"""A command that writes the given bytes to stdout and exits 0. |
| 105 |
|
| 106 |
We use python -c so this works on all platforms and without relying on |
| 107 |
shell quoting. |
| 108 |
""" |
| 109 |
src = "import sys; sys.stdout.buffer.write({!r})".format(data) |
| 110 |
return [sys.executable, "-c", src] |
| 111 |
|
| 112 |
def test_merges_streams_with_prefixes(self): |
| 113 |
stream_a = _stream_with_tests(["one", "two"]) |
| 114 |
stream_b = _stream_with_tests(["alpha"]) |
| 115 |
commands = [ |
| 116 |
{"prefix": "py/", "argv": self._cat_cmd(stream_a)}, |
| 117 |
{"prefix": "rs/", "argv": self._cat_cmd(stream_b)}, |
| 118 |
] |
| 119 |
output = BytesIO() |
| 120 |
rc = combine(commands, output) |
| 121 |
self.assertEqual(0, rc) |
| 122 |
self.assertEqual( |
| 123 |
[ |
| 124 |
("py/one", "inprogress"), |
| 125 |
("py/one", "success"), |
| 126 |
("py/two", "inprogress"), |
| 127 |
("py/two", "success"), |
| 128 |
("rs/alpha", "inprogress"), |
| 129 |
("rs/alpha", "success"), |
| 130 |
], |
| 131 |
_parse(output.getvalue()), |
| 132 |
) |
| 133 |
|
| 134 |
def test_no_prefix(self): |
| 135 |
stream = _stream_with_tests(["solo"]) |
| 136 |
commands = [{"argv": self._cat_cmd(stream)}] |
| 137 |
output = BytesIO() |
| 138 |
rc = combine(commands, output) |
| 139 |
self.assertEqual(0, rc) |
| 140 |
self.assertEqual( |
| 141 |
[("solo", "inprogress"), ("solo", "success")], |
| 142 |
_parse(output.getvalue()), |
| 143 |
) |
| 144 |
|
| 145 |
def test_nonzero_exit_propagates(self): |
| 146 |
stream = _stream_with_tests(["only"]) |
| 147 |
src = "import sys; sys.stdout.buffer.write({!r}); sys.exit(3)".format(stream) |
| 148 |
commands = [{"argv": [sys.executable, "-c", src]}] |
| 149 |
output = BytesIO() |
| 150 |
rc = combine(commands, output) |
| 151 |
self.assertEqual(1, rc) |
| 152 |
|
| 153 |
|
| 154 |
class TestCombineCommand(TestCase): |
| 155 |
def test_end_to_end(self): |
| 156 |
stream_a = _stream_with_tests(["a"]) |
| 157 |
stream_b = _stream_with_tests(["b"]) |
| 158 |
|
| 159 |
def cmd_for(data): |
| 160 |
src = "import sys; sys.stdout.buffer.write({!r})".format(data) |
| 161 |
return [sys.executable, "-c", src] |
| 162 |
|
| 163 |
import yaml |
| 164 |
|
| 165 |
config = { |
| 166 |
"commands": [ |
| 167 |
{"prefix": "py/", "argv": cmd_for(stream_a)}, |
| 168 |
{"prefix": "rs/", "argv": cmd_for(stream_b)}, |
| 169 |
] |
| 170 |
} |
| 171 |
fd, path = tempfile.mkstemp(suffix=".yaml") |
| 172 |
self.addCleanup(os.unlink, path) |
| 173 |
with os.fdopen(fd, "w") as f: |
| 174 |
yaml.safe_dump(config, f) |
| 175 |
|
| 176 |
ps = subprocess.Popen( |
| 177 |
[sys.executable, "-m", "subunit.filter_scripts.subunit_combine", path], |
| 178 |
stdout=subprocess.PIPE, |
| 179 |
stderr=subprocess.PIPE, |
| 180 |
) |
| 181 |
out, err = ps.communicate() |
| 182 |
self.assertEqual(0, ps.returncode, err) |
| 183 |
self.assertEqual( |
| 184 |
[ |
| 185 |
("py/a", "inprogress"), |
| 186 |
("py/a", "success"), |
| 187 |
("rs/b", "inprogress"), |
| 188 |
("rs/b", "success"), |
| 189 |
], |
| 190 |
_parse(out), |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
class TestExpandArgv(TestCase): |
| 195 |
def test_listopt_expanded_in_list_mode(self): |
| 196 |
cmd = {"argv": ["runner", "$LISTOPT", "$IDOPTION"], "list_option": "--list"} |
| 197 |
self.assertEqual( |
| 198 |
["runner", "--list"], |
| 199 |
_expand_argv(cmd, list_mode=True, test_ids=None, idfile_path=None), |
| 200 |
) |
| 201 |
|
| 202 |
def test_listopt_empty_when_not_listing(self): |
| 203 |
cmd = {"argv": ["runner", "$LISTOPT", "$IDOPTION"], "list_option": "--list"} |
| 204 |
self.assertEqual( |
| 205 |
["runner"], |
| 206 |
_expand_argv(cmd, list_mode=False, test_ids=None, idfile_path=None), |
| 207 |
) |
| 208 |
|
| 209 |
def test_idoption_expanded_with_idfile(self): |
| 210 |
cmd = { |
| 211 |
"argv": ["runner", "$IDOPTION"], |
| 212 |
"id_option": "--load-list $IDFILE", |
| 213 |
} |
| 214 |
self.assertEqual( |
| 215 |
["runner", "--load-list", "/tmp/ids"], |
| 216 |
_expand_argv(cmd, list_mode=False, test_ids=["a", "b"], idfile_path="/tmp/ids"), |
| 217 |
) |
| 218 |
|
| 219 |
def test_idlist_expanded(self): |
| 220 |
cmd = {"argv": ["runner", "$IDLIST"]} |
| 221 |
self.assertEqual( |
| 222 |
["runner", "a", "b", "c"], |
| 223 |
_expand_argv(cmd, list_mode=False, test_ids=["a", "b", "c"], idfile_path=None), |
| 224 |
) |
| 225 |
|
| 226 |
def test_idoption_empty_without_ids(self): |
| 227 |
cmd = { |
| 228 |
"argv": ["runner", "$IDOPTION"], |
| 229 |
"id_option": "--load-list $IDFILE", |
| 230 |
} |
| 231 |
self.assertEqual( |
| 232 |
["runner"], |
| 233 |
_expand_argv(cmd, list_mode=False, test_ids=None, idfile_path=None), |
| 234 |
) |
| 235 |
|
| 236 |
def test_literal_argv_entries_untouched(self): |
| 237 |
cmd = {"argv": ["runner", "--tag=$literal", "$IDLIST"]} |
| 238 |
self.assertEqual( |
| 239 |
["runner", "--tag=$literal", "x"], |
| 240 |
_expand_argv(cmd, list_mode=False, test_ids=["x"], idfile_path=None), |
| 241 |
) |
| 242 |
|
| 243 |
|
| 244 |
class TestSelectIdsForCommand(TestCase): |
| 245 |
def test_none_returns_none(self): |
| 246 |
self.assertIsNone(_select_ids_for_command({"prefix": "py/"}, None)) |
| 247 |
|
| 248 |
def test_filters_by_prefix_and_strips(self): |
| 249 |
ids = ["py/one", "rs/two", "py/three"] |
| 250 |
self.assertEqual(["one", "three"], _select_ids_for_command({"prefix": "py/"}, ids)) |
| 251 |
|
| 252 |
def test_no_prefix_returns_all(self): |
| 253 |
ids = ["one", "two"] |
| 254 |
self.assertEqual(ids, _select_ids_for_command({}, ids)) |
| 255 |
self.assertIsNot(ids, _select_ids_for_command({}, ids)) # must copy |
| 256 |
|
| 257 |
def test_no_matches_returns_empty(self): |
| 258 |
self.assertEqual([], _select_ids_for_command({"prefix": "py/"}, ["rs/a"])) |
| 259 |
|
| 260 |
|
| 261 |
class TestReadIdList(TestCase): |
| 262 |
def test_skips_blank_and_comments(self): |
| 263 |
fd, path = tempfile.mkstemp() |
| 264 |
self.addCleanup(os.unlink, path) |
| 265 |
with os.fdopen(fd, "w") as f: |
| 266 |
f.write("one\n\n# comment\ntwo # trailing\nthree\n") |
| 267 |
self.assertEqual(["one", "two", "three"], _read_id_list(path)) |
| 268 |
|
| 269 |
|
| 270 |
class TestCombineListAndFilter(TestCase): |
| 271 |
def _cmd_for(self, data): |
| 272 |
src = "import sys; sys.stdout.buffer.write({!r})".format(data) |
| 273 |
return [sys.executable, "-c", src] |
| 274 |
|
| 275 |
def _exists_stream(self, test_ids): |
| 276 |
buf = BytesIO() |
| 277 |
out = StreamResultToBytes(buf) |
| 278 |
for tid in test_ids: |
| 279 |
out.status(test_id=tid, test_status="exists") |
| 280 |
return buf.getvalue() |
| 281 |
|
| 282 |
def test_list_mode_substitutes_listopt(self): |
| 283 |
# Child emits ids only when --list is present: we simulate this by |
| 284 |
# embedding the argument directly in the command and asserting it's |
| 285 |
# what gets executed. |
| 286 |
list_stream = self._exists_stream(["foo", "bar"]) |
| 287 |
src = ( |
| 288 |
"import sys; args = sys.argv[1:]; assert args == ['--list'], args; sys.stdout.buffer.write({!r})" |
| 289 |
).format(list_stream) |
| 290 |
commands = [ |
| 291 |
{ |
| 292 |
"prefix": "py/", |
| 293 |
"argv": [sys.executable, "-c", src, "$LISTOPT"], |
| 294 |
"list_option": "--list", |
| 295 |
} |
| 296 |
] |
| 297 |
output = BytesIO() |
| 298 |
rc = combine(commands, output, list_mode=True) |
| 299 |
self.assertEqual(0, rc) |
| 300 |
self.assertEqual( |
| 301 |
[("py/foo", "exists"), ("py/bar", "exists")], |
| 302 |
_parse(output.getvalue()), |
| 303 |
) |
| 304 |
|
| 305 |
def test_listopt_absent_when_not_listing(self): |
| 306 |
# When list_mode is False, $LISTOPT should expand to nothing. |
| 307 |
run_stream = _stream_with_tests(["x"]) |
| 308 |
src = ("import sys; args = sys.argv[1:]; assert args == [], args; sys.stdout.buffer.write({!r})").format( |
| 309 |
run_stream |
| 310 |
) |
| 311 |
commands = [ |
| 312 |
{ |
| 313 |
"argv": [sys.executable, "-c", src, "$LISTOPT"], |
| 314 |
"list_option": "--list", |
| 315 |
} |
| 316 |
] |
| 317 |
output = BytesIO() |
| 318 |
rc = combine(commands, output) |
| 319 |
self.assertEqual(0, rc) |
| 320 |
|
| 321 |
def test_idoption_and_idfile_substituted(self): |
| 322 |
# The child asserts it received --load-list <file> with two ids in it. |
| 323 |
run_stream = _stream_with_tests(["one", "two"]) |
| 324 |
src = ( |
| 325 |
"import sys, pathlib; " |
| 326 |
"args = sys.argv[1:]; " |
| 327 |
"assert args[0] == '--load-list', args; " |
| 328 |
"contents = pathlib.Path(args[1]).read_text().splitlines(); " |
| 329 |
"assert contents == ['one', 'two'], contents; " |
| 330 |
"sys.stdout.buffer.write({!r})" |
| 331 |
).format(run_stream) |
| 332 |
commands = [ |
| 333 |
{ |
| 334 |
"prefix": "py/", |
| 335 |
"argv": [sys.executable, "-c", src, "$IDOPTION"], |
| 336 |
"id_option": "--load-list $IDFILE", |
| 337 |
} |
| 338 |
] |
| 339 |
output = BytesIO() |
| 340 |
rc = combine(commands, output, test_ids=["py/one", "py/two"]) |
| 341 |
self.assertEqual(0, rc) |
| 342 |
|
| 343 |
def test_idlist_substituted(self): |
| 344 |
run_stream = _stream_with_tests(["a"]) |
| 345 |
src = ( |
| 346 |
"import sys; args = sys.argv[1:]; assert args == ['a', 'b'], args; sys.stdout.buffer.write({!r})" |
| 347 |
).format(run_stream) |
| 348 |
commands = [ |
| 349 |
{ |
| 350 |
"prefix": "py/", |
| 351 |
"argv": [sys.executable, "-c", src, "$IDLIST"], |
| 352 |
} |
| 353 |
] |
| 354 |
output = BytesIO() |
| 355 |
rc = combine(commands, output, test_ids=["py/a", "py/b"]) |
| 356 |
self.assertEqual(0, rc) |
| 357 |
|
| 358 |
def test_command_with_no_matching_ids_is_skipped(self): |
| 359 |
# 'py/' receives ids; 'rs/' has no matching ids and must not run. |
| 360 |
py_stream = _stream_with_tests(["one"]) |
| 361 |
py_src = "import sys; sys.stdout.buffer.write({!r})".format(py_stream) |
| 362 |
rs_src = "import sys; sys.exit('must not run')" |
| 363 |
commands = [ |
| 364 |
{"prefix": "py/", "argv": [sys.executable, "-c", py_src]}, |
| 365 |
{"prefix": "rs/", "argv": [sys.executable, "-c", rs_src]}, |
| 366 |
] |
| 367 |
output = BytesIO() |
| 368 |
rc = combine(commands, output, test_ids=["py/one"]) |
| 369 |
self.assertEqual(0, rc) |
| 370 |
self.assertEqual( |
| 371 |
[("py/one", "inprogress"), ("py/one", "success")], |
| 372 |
_parse(output.getvalue()), |
| 373 |
) |
| 374 |
|
| 375 |
def test_cli_list_flag(self): |
| 376 |
list_stream = self._exists_stream(["foo"]) |
| 377 |
src = ( |
| 378 |
"import sys; args = sys.argv[1:]; assert args == ['--list'], args; sys.stdout.buffer.write({!r})" |
| 379 |
).format(list_stream) |
| 380 |
config = { |
| 381 |
"commands": [ |
| 382 |
{ |
| 383 |
"prefix": "py/", |
| 384 |
"argv": [sys.executable, "-c", src, "$LISTOPT"], |
| 385 |
"list_option": "--list", |
| 386 |
} |
| 387 |
] |
| 388 |
} |
| 389 |
fd, path = tempfile.mkstemp(suffix=".yaml") |
| 390 |
self.addCleanup(os.unlink, path) |
| 391 |
with os.fdopen(fd, "w") as f: |
| 392 |
import yaml |
| 393 |
|
| 394 |
yaml.safe_dump(config, f) |
| 395 |
ps = subprocess.Popen( |
| 396 |
[sys.executable, "-m", "subunit.filter_scripts.subunit_combine", "--list", path], |
| 397 |
stdout=subprocess.PIPE, |
| 398 |
stderr=subprocess.PIPE, |
| 399 |
) |
| 400 |
out, err = ps.communicate() |
| 401 |
self.assertEqual(0, ps.returncode, err) |
| 402 |
self.assertEqual([("py/foo", "exists")], _parse(out)) |
| 403 |
|
| 404 |
def test_cli_positional_ids(self): |
| 405 |
run_stream = _stream_with_tests(["a"]) |
| 406 |
src = ("import sys; args = sys.argv[1:]; assert args == ['a'], args; sys.stdout.buffer.write({!r})").format( |
| 407 |
run_stream |
| 408 |
) |
| 409 |
config = { |
| 410 |
"commands": [ |
| 411 |
{ |
| 412 |
"prefix": "py/", |
| 413 |
"argv": [sys.executable, "-c", src, "$IDLIST"], |
| 414 |
} |
| 415 |
] |
| 416 |
} |
| 417 |
fd, path = tempfile.mkstemp(suffix=".yaml") |
| 418 |
self.addCleanup(os.unlink, path) |
| 419 |
with os.fdopen(fd, "w") as f: |
| 420 |
import yaml |
| 421 |
|
| 422 |
yaml.safe_dump(config, f) |
| 423 |
ps = subprocess.Popen( |
| 424 |
[sys.executable, "-m", "subunit.filter_scripts.subunit_combine", path, "py/a"], |
| 425 |
stdout=subprocess.PIPE, |
| 426 |
stderr=subprocess.PIPE, |
| 427 |
) |
| 428 |
out, err = ps.communicate() |
| 429 |
self.assertEqual(0, ps.returncode, err) |
| 430 |
self.assertEqual( |
| 431 |
[("py/a", "inprogress"), ("py/a", "success")], |
| 432 |
_parse(out), |
| 433 |
) |
| 434 |
|
| 435 |
def test_cli_load_list(self): |
| 436 |
run_stream = _stream_with_tests(["one"]) |
| 437 |
src = ("import sys; args = sys.argv[1:]; assert args == ['one'], args; sys.stdout.buffer.write({!r})").format( |
| 438 |
run_stream |
| 439 |
) |
| 440 |
config = { |
| 441 |
"commands": [ |
| 442 |
{ |
| 443 |
"prefix": "py/", |
| 444 |
"argv": [sys.executable, "-c", src, "$IDLIST"], |
| 445 |
} |
| 446 |
] |
| 447 |
} |
| 448 |
cfg_fd, cfg_path = tempfile.mkstemp(suffix=".yaml") |
| 449 |
self.addCleanup(os.unlink, cfg_path) |
| 450 |
with os.fdopen(cfg_fd, "w") as f: |
| 451 |
import yaml |
| 452 |
|
| 453 |
yaml.safe_dump(config, f) |
| 454 |
list_fd, list_path = tempfile.mkstemp(suffix=".list") |
| 455 |
self.addCleanup(os.unlink, list_path) |
| 456 |
with os.fdopen(list_fd, "w") as f: |
| 457 |
f.write("py/one\n") |
| 458 |
ps = subprocess.Popen( |
| 459 |
[ |
| 460 |
sys.executable, |
| 461 |
"-m", |
| 462 |
"subunit.filter_scripts.subunit_combine", |
| 463 |
"--load-list", |
| 464 |
list_path, |
| 465 |
cfg_path, |
| 466 |
], |
| 467 |
stdout=subprocess.PIPE, |
| 468 |
stderr=subprocess.PIPE, |
| 469 |
) |
| 470 |
out, err = ps.communicate() |
| 471 |
self.assertEqual(0, ps.returncode, err) |
| 472 |
self.assertEqual( |
| 473 |
[("py/one", "inprogress"), ("py/one", "success")], |
| 474 |
_parse(out), |
| 475 |
) |