|
| 1 | +import json |
1 | 2 | import pytest |
2 | 3 | import re |
3 | 4 |
|
| 5 | +from io import StringIO |
4 | 6 | from itertools import zip_longest |
5 | 7 |
|
6 | 8 | from manage import list_command |
@@ -315,6 +317,39 @@ def test_format_csv_empty(assert_log): |
315 | 317 | assert_log(assert_log.end_of_log()) |
316 | 318 |
|
317 | 319 |
|
| 320 | +@pytest.fixture(params=["populated", "empty"]) |
| 321 | +def json_format_installs(request): |
| 322 | + if request.param == "empty": |
| 323 | + return [], [] |
| 324 | + prefix = r"C:\Python" |
| 325 | + installs = [ |
| 326 | + {"id": "first", "prefix": Path(prefix)}, |
| 327 | + {"id": "second", "display-name": "Python\nPreview"}, |
| 328 | + ] |
| 329 | + expected = [{**installs[0], "prefix": prefix}, installs[1]] |
| 330 | + return installs, expected |
| 331 | + |
| 332 | + |
| 333 | +def test_format_json(monkeypatch, json_format_installs): |
| 334 | + output = StringIO() |
| 335 | + monkeypatch.setattr(list_command.LOGGER, "print_console", output) |
| 336 | + installs, expected = json_format_installs |
| 337 | + |
| 338 | + list_command.format_json(None, installs) |
| 339 | + |
| 340 | + assert json.loads(output.getvalue()) == {"versions": expected} |
| 341 | + |
| 342 | + |
| 343 | +def test_format_json_lines(monkeypatch, json_format_installs): |
| 344 | + output = StringIO() |
| 345 | + monkeypatch.setattr(list_command.LOGGER, "print_console", output) |
| 346 | + installs, expected = json_format_installs |
| 347 | + |
| 348 | + list_command.format_json_lines(None, installs) |
| 349 | + |
| 350 | + assert [json.loads(line) for line in output.getvalue().splitlines()] == expected |
| 351 | + |
| 352 | + |
318 | 353 | def test_csv_exclude(): |
319 | 354 | result = list(list_command._csv_filter_and_expand([ |
320 | 355 | dict(a=1, b=2), |
|
0 commit comments