Skip to content

Commit 65fe11b

Browse files
committed
Refactor pip list json tests
So they are resilient to new fields added to the json output.
1 parent 4ecf68e commit 65fe11b

File tree

1 file changed

+97
-51
lines changed

1 file changed

+97
-51
lines changed

tests/functional/test_list.py

+97-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
from pathlib import Path
4+
from typing import Any, Dict
45

56
import pytest
67

@@ -35,6 +36,22 @@ def simple_script(
3536
return script
3637

3738

39+
def subdict_in(subdict: Dict[Any, Any], d: Dict[Any, Any]) -> bool:
40+
"""Return true if all keys of subdict are in d and the correponding values match."""
41+
return all(k in d and d[k] == v for k, v in subdict.items())
42+
43+
44+
def subdict_in_list(subdict: Dict[Any, Any], items: Any) -> bool:
45+
"""
46+
Return true if at least one of the dictionaries in items contains all the keys
47+
of subdict and the corresponding values match.
48+
"""
49+
for item in items:
50+
if subdict_in(subdict, item):
51+
return True
52+
return False
53+
54+
3855
def test_basic_list(simple_script: PipTestEnvironment) -> None:
3956
"""
4057
Test default behavior of list command without format specifier.
@@ -97,7 +114,9 @@ def test_local_flag(simple_script: PipTestEnvironment) -> None:
97114
98115
"""
99116
result = simple_script.pip("list", "--local", "--format=json")
100-
assert {"name": "simple", "version": "1.0"} in json.loads(result.stdout)
117+
assert subdict_in_list(
118+
{"name": "simple", "version": "1.0"}, json.loads(result.stdout)
119+
)
101120

102121

103122
def test_local_columns_flag(simple_script: PipTestEnvironment) -> None:
@@ -139,8 +158,12 @@ def test_user_flag(script: PipTestEnvironment, data: TestData) -> None:
139158
script.pip("install", "-f", data.find_links, "--no-index", "simple==1.0")
140159
script.pip("install", "-f", data.find_links, "--no-index", "--user", "simple2==2.0")
141160
result = script.pip("list", "--user", "--format=json")
142-
assert {"name": "simple", "version": "1.0"} not in json.loads(result.stdout)
143-
assert {"name": "simple2", "version": "2.0"} in json.loads(result.stdout)
161+
assert not subdict_in_list(
162+
{"name": "simple", "version": "1.0"}, json.loads(result.stdout)
163+
)
164+
assert subdict_in_list(
165+
{"name": "simple2", "version": "2.0"}, json.loads(result.stdout)
166+
)
144167

145168

146169
@pytest.mark.network
@@ -191,13 +214,19 @@ def test_uptodate_flag(script: PipTestEnvironment, data: TestData) -> None:
191214
for item in json_output:
192215
if "editable_project_location" in item:
193216
item["editable_project_location"] = "<location>"
194-
assert {"name": "simple", "version": "1.0"} not in json_output # 3.0 is latest
195-
assert {
196-
"name": "pip-test-package",
197-
"version": "0.1.1",
198-
"editable_project_location": "<location>",
199-
} in json_output # editables included
200-
assert {"name": "simple2", "version": "3.0"} in json_output
217+
assert not subdict_in_list(
218+
{"name": "simple", "version": "1.0"},
219+
json_output,
220+
) # 3.0 is latest
221+
assert subdict_in_list(
222+
{
223+
"name": "pip-test-package",
224+
"version": "0.1.1",
225+
"editable_project_location": "<location>",
226+
},
227+
json_output,
228+
) # editables included
229+
assert subdict_in_list({"name": "simple2", "version": "3.0"}, json_output)
201230

202231

203232
@pytest.mark.network
@@ -267,30 +296,33 @@ def test_outdated_flag(script: PipTestEnvironment, data: TestData) -> None:
267296
for item in json_output:
268297
if "editable_project_location" in item:
269298
item["editable_project_location"] = "<location>"
270-
assert {
271-
"name": "simple",
272-
"version": "1.0",
273-
"latest_version": "3.0",
274-
"latest_filetype": "sdist",
275-
} in json_output
276-
assert (
299+
assert subdict_in_list(
300+
{
301+
"name": "simple",
302+
"version": "1.0",
303+
"latest_version": "3.0",
304+
"latest_filetype": "sdist",
305+
},
306+
json_output,
307+
)
308+
assert subdict_in_list(
277309
dict(
278310
name="simplewheel",
279311
version="1.0",
280312
latest_version="2.0",
281313
latest_filetype="wheel",
282-
)
283-
in json_output
314+
),
315+
json_output,
284316
)
285-
assert (
317+
assert subdict_in_list(
286318
dict(
287319
name="pip-test-package",
288320
version="0.1",
289321
latest_version="0.1.1",
290322
latest_filetype="sdist",
291323
editable_project_location="<location>",
292-
)
293-
in json_output
324+
),
325+
json_output,
294326
)
295327
assert "simple2" not in {p["name"] for p in json_output}
296328

@@ -358,7 +390,9 @@ def test_editables_flag(pip_test_package_script: PipTestEnvironment) -> None:
358390
"""
359391
result = pip_test_package_script.pip("list", "--editable", "--format=json")
360392
result2 = pip_test_package_script.pip("list", "--editable")
361-
assert {"name": "simple", "version": "1.0"} not in json.loads(result.stdout)
393+
assert not subdict_in_list(
394+
{"name": "simple", "version": "1.0"}, json.loads(result.stdout)
395+
)
362396
assert os.path.join("src", "pip-test-package") in result2.stdout
363397

364398

@@ -368,7 +402,9 @@ def test_exclude_editable_flag(pip_test_package_script: PipTestEnvironment) -> N
368402
Test the behavior of --editables flag in the list command
369403
"""
370404
result = pip_test_package_script.pip("list", "--exclude-editable", "--format=json")
371-
assert {"name": "simple", "version": "1.0"} in json.loads(result.stdout)
405+
assert subdict_in_list(
406+
{"name": "simple", "version": "1.0"}, json.loads(result.stdout)
407+
)
372408
assert "pip-test-package" not in {p["name"] for p in json.loads(result.stdout)}
373409

374410

@@ -516,7 +552,9 @@ def test_outdated_pre(script: PipTestEnvironment, data: TestData) -> None:
516552
wheelhouse_path,
517553
"--format=json",
518554
)
519-
assert {"name": "simple", "version": "1.0"} in json.loads(result.stdout)
555+
assert subdict_in_list(
556+
{"name": "simple", "version": "1.0"}, json.loads(result.stdout)
557+
)
520558
result = script.pip(
521559
"list",
522560
"--no-index",
@@ -525,12 +563,15 @@ def test_outdated_pre(script: PipTestEnvironment, data: TestData) -> None:
525563
"--outdated",
526564
"--format=json",
527565
)
528-
assert {
529-
"name": "simple",
530-
"version": "1.0",
531-
"latest_version": "1.1",
532-
"latest_filetype": "wheel",
533-
} in json.loads(result.stdout)
566+
assert subdict_in_list(
567+
{
568+
"name": "simple",
569+
"version": "1.0",
570+
"latest_version": "1.1",
571+
"latest_filetype": "wheel",
572+
},
573+
json.loads(result.stdout),
574+
)
534575
result_pre = script.pip(
535576
"list",
536577
"--no-index",
@@ -540,12 +581,15 @@ def test_outdated_pre(script: PipTestEnvironment, data: TestData) -> None:
540581
"--pre",
541582
"--format=json",
542583
)
543-
assert {
544-
"name": "simple",
545-
"version": "1.0",
546-
"latest_version": "2.0.dev0",
547-
"latest_filetype": "wheel",
548-
} in json.loads(result_pre.stdout)
584+
assert subdict_in_list(
585+
{
586+
"name": "simple",
587+
"version": "1.0",
588+
"latest_version": "2.0.dev0",
589+
"latest_filetype": "wheel",
590+
},
591+
json.loads(result_pre.stdout),
592+
)
549593

550594

551595
def test_outdated_formats(script: PipTestEnvironment, data: TestData) -> None:
@@ -598,14 +642,16 @@ def test_outdated_formats(script: PipTestEnvironment, data: TestData) -> None:
598642
"--format=json",
599643
)
600644
data = json.loads(result.stdout)
601-
assert data == [
645+
assert len(data) == 1 # type: ignore
646+
assert subdict_in_list(
602647
{
603648
"name": "simple",
604649
"version": "1.0",
605650
"latest_version": "1.1",
606651
"latest_filetype": "wheel",
607-
}
608-
]
652+
},
653+
data,
654+
)
609655

610656

611657
def test_not_required_flag(script: PipTestEnvironment, data: TestData) -> None:
@@ -634,8 +680,8 @@ def test_list_json(simple_script: PipTestEnvironment) -> None:
634680
"""
635681
result = simple_script.pip("list", "--format=json")
636682
data = json.loads(result.stdout)
637-
assert {"name": "simple", "version": "1.0"} in data
638-
assert {"name": "simple2", "version": "3.0"} in data
683+
assert subdict_in_list({"name": "simple", "version": "1.0"}, data)
684+
assert subdict_in_list({"name": "simple2", "version": "3.0"}, data)
639685

640686

641687
def test_list_path(tmpdir: Path, script: PipTestEnvironment, data: TestData) -> None:
@@ -644,12 +690,12 @@ def test_list_path(tmpdir: Path, script: PipTestEnvironment, data: TestData) ->
644690
"""
645691
result = script.pip("list", "--path", tmpdir, "--format=json")
646692
json_result = json.loads(result.stdout)
647-
assert {"name": "simple", "version": "2.0"} not in json_result
693+
assert not subdict_in_list({"name": "simple", "version": "2.0"}, json_result)
648694

649695
script.pip_install_local("--target", tmpdir, "simple==2.0")
650696
result = script.pip("list", "--path", tmpdir, "--format=json")
651697
json_result = json.loads(result.stdout)
652-
assert {"name": "simple", "version": "2.0"} in json_result
698+
assert subdict_in_list({"name": "simple", "version": "2.0"}, json_result)
653699

654700

655701
@pytest.mark.incompatible_with_test_venv
@@ -665,11 +711,11 @@ def test_list_path_exclude_user(
665711

666712
result = script.pip("list", "--user", "--format=json")
667713
json_result = json.loads(result.stdout)
668-
assert {"name": "simple2", "version": "3.0"} in json_result
714+
assert subdict_in_list({"name": "simple2", "version": "3.0"}, json_result)
669715

670716
result = script.pip("list", "--path", tmpdir, "--format=json")
671717
json_result = json.loads(result.stdout)
672-
assert {"name": "simple", "version": "1.0"} in json_result
718+
assert subdict_in_list({"name": "simple", "version": "1.0"}, json_result)
673719

674720

675721
def test_list_path_multiple(
@@ -688,12 +734,12 @@ def test_list_path_multiple(
688734

689735
result = script.pip("list", "--path", path1, "--format=json")
690736
json_result = json.loads(result.stdout)
691-
assert {"name": "simple", "version": "2.0"} in json_result
737+
assert subdict_in_list({"name": "simple", "version": "2.0"}, json_result)
692738

693739
result = script.pip("list", "--path", path1, "--path", path2, "--format=json")
694740
json_result = json.loads(result.stdout)
695-
assert {"name": "simple", "version": "2.0"} in json_result
696-
assert {"name": "simple2", "version": "3.0"} in json_result
741+
assert subdict_in_list({"name": "simple", "version": "2.0"}, json_result)
742+
assert subdict_in_list({"name": "simple2", "version": "3.0"}, json_result)
697743

698744

699745
def test_list_skip_work_dir_pkg(script: PipTestEnvironment) -> None:
@@ -708,7 +754,7 @@ def test_list_skip_work_dir_pkg(script: PipTestEnvironment) -> None:
708754
# List should not include package simple when run from package directory
709755
result = script.pip("list", "--format=json", cwd=pkg_path)
710756
json_result = json.loads(result.stdout)
711-
assert {"name": "simple", "version": "1.0"} not in json_result
757+
assert not subdict_in_list({"name": "simple", "version": "1.0"}, json_result)
712758

713759

714760
def test_list_include_work_dir_pkg(script: PipTestEnvironment) -> None:
@@ -727,7 +773,7 @@ def test_list_include_work_dir_pkg(script: PipTestEnvironment) -> None:
727773
# when the package directory is in PYTHONPATH
728774
result = script.pip("list", "--format=json", cwd=pkg_path)
729775
json_result = json.loads(result.stdout)
730-
assert {"name": "simple", "version": "1.0"} in json_result
776+
assert subdict_in_list({"name": "simple", "version": "1.0"}, json_result)
731777

732778

733779
@pytest.mark.usefixtures("with_wheel")

0 commit comments

Comments
 (0)