1
1
import json
2
2
import os
3
3
from pathlib import Path
4
+ from typing import Any , Dict
4
5
5
6
import pytest
6
7
@@ -35,6 +36,22 @@ def simple_script(
35
36
return script
36
37
37
38
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
+
38
55
def test_basic_list (simple_script : PipTestEnvironment ) -> None :
39
56
"""
40
57
Test default behavior of list command without format specifier.
@@ -97,7 +114,9 @@ def test_local_flag(simple_script: PipTestEnvironment) -> None:
97
114
98
115
"""
99
116
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
+ )
101
120
102
121
103
122
def test_local_columns_flag (simple_script : PipTestEnvironment ) -> None :
@@ -139,8 +158,12 @@ def test_user_flag(script: PipTestEnvironment, data: TestData) -> None:
139
158
script .pip ("install" , "-f" , data .find_links , "--no-index" , "simple==1.0" )
140
159
script .pip ("install" , "-f" , data .find_links , "--no-index" , "--user" , "simple2==2.0" )
141
160
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
+ )
144
167
145
168
146
169
@pytest .mark .network
@@ -191,13 +214,19 @@ def test_uptodate_flag(script: PipTestEnvironment, data: TestData) -> None:
191
214
for item in json_output :
192
215
if "editable_project_location" in item :
193
216
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 )
201
230
202
231
203
232
@pytest .mark .network
@@ -267,30 +296,33 @@ def test_outdated_flag(script: PipTestEnvironment, data: TestData) -> None:
267
296
for item in json_output :
268
297
if "editable_project_location" in item :
269
298
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 (
277
309
dict (
278
310
name = "simplewheel" ,
279
311
version = "1.0" ,
280
312
latest_version = "2.0" ,
281
313
latest_filetype = "wheel" ,
282
- )
283
- in json_output
314
+ ),
315
+ json_output ,
284
316
)
285
- assert (
317
+ assert subdict_in_list (
286
318
dict (
287
319
name = "pip-test-package" ,
288
320
version = "0.1" ,
289
321
latest_version = "0.1.1" ,
290
322
latest_filetype = "sdist" ,
291
323
editable_project_location = "<location>" ,
292
- )
293
- in json_output
324
+ ),
325
+ json_output ,
294
326
)
295
327
assert "simple2" not in {p ["name" ] for p in json_output }
296
328
@@ -358,7 +390,9 @@ def test_editables_flag(pip_test_package_script: PipTestEnvironment) -> None:
358
390
"""
359
391
result = pip_test_package_script .pip ("list" , "--editable" , "--format=json" )
360
392
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
+ )
362
396
assert os .path .join ("src" , "pip-test-package" ) in result2 .stdout
363
397
364
398
@@ -368,7 +402,9 @@ def test_exclude_editable_flag(pip_test_package_script: PipTestEnvironment) -> N
368
402
Test the behavior of --editables flag in the list command
369
403
"""
370
404
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
+ )
372
408
assert "pip-test-package" not in {p ["name" ] for p in json .loads (result .stdout )}
373
409
374
410
@@ -516,7 +552,9 @@ def test_outdated_pre(script: PipTestEnvironment, data: TestData) -> None:
516
552
wheelhouse_path ,
517
553
"--format=json" ,
518
554
)
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
+ )
520
558
result = script .pip (
521
559
"list" ,
522
560
"--no-index" ,
@@ -525,12 +563,15 @@ def test_outdated_pre(script: PipTestEnvironment, data: TestData) -> None:
525
563
"--outdated" ,
526
564
"--format=json" ,
527
565
)
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
+ )
534
575
result_pre = script .pip (
535
576
"list" ,
536
577
"--no-index" ,
@@ -540,12 +581,15 @@ def test_outdated_pre(script: PipTestEnvironment, data: TestData) -> None:
540
581
"--pre" ,
541
582
"--format=json" ,
542
583
)
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
+ )
549
593
550
594
551
595
def test_outdated_formats (script : PipTestEnvironment , data : TestData ) -> None :
@@ -598,14 +642,16 @@ def test_outdated_formats(script: PipTestEnvironment, data: TestData) -> None:
598
642
"--format=json" ,
599
643
)
600
644
data = json .loads (result .stdout )
601
- assert data == [
645
+ assert len (data ) == 1 # type: ignore
646
+ assert subdict_in_list (
602
647
{
603
648
"name" : "simple" ,
604
649
"version" : "1.0" ,
605
650
"latest_version" : "1.1" ,
606
651
"latest_filetype" : "wheel" ,
607
- }
608
- ]
652
+ },
653
+ data ,
654
+ )
609
655
610
656
611
657
def test_not_required_flag (script : PipTestEnvironment , data : TestData ) -> None :
@@ -634,8 +680,8 @@ def test_list_json(simple_script: PipTestEnvironment) -> None:
634
680
"""
635
681
result = simple_script .pip ("list" , "--format=json" )
636
682
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 )
639
685
640
686
641
687
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) ->
644
690
"""
645
691
result = script .pip ("list" , "--path" , tmpdir , "--format=json" )
646
692
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 )
648
694
649
695
script .pip_install_local ("--target" , tmpdir , "simple==2.0" )
650
696
result = script .pip ("list" , "--path" , tmpdir , "--format=json" )
651
697
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 )
653
699
654
700
655
701
@pytest .mark .incompatible_with_test_venv
@@ -665,11 +711,11 @@ def test_list_path_exclude_user(
665
711
666
712
result = script .pip ("list" , "--user" , "--format=json" )
667
713
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 )
669
715
670
716
result = script .pip ("list" , "--path" , tmpdir , "--format=json" )
671
717
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 )
673
719
674
720
675
721
def test_list_path_multiple (
@@ -688,12 +734,12 @@ def test_list_path_multiple(
688
734
689
735
result = script .pip ("list" , "--path" , path1 , "--format=json" )
690
736
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 )
692
738
693
739
result = script .pip ("list" , "--path" , path1 , "--path" , path2 , "--format=json" )
694
740
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 )
697
743
698
744
699
745
def test_list_skip_work_dir_pkg (script : PipTestEnvironment ) -> None :
@@ -708,7 +754,7 @@ def test_list_skip_work_dir_pkg(script: PipTestEnvironment) -> None:
708
754
# List should not include package simple when run from package directory
709
755
result = script .pip ("list" , "--format=json" , cwd = pkg_path )
710
756
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 )
712
758
713
759
714
760
def test_list_include_work_dir_pkg (script : PipTestEnvironment ) -> None :
@@ -727,7 +773,7 @@ def test_list_include_work_dir_pkg(script: PipTestEnvironment) -> None:
727
773
# when the package directory is in PYTHONPATH
728
774
result = script .pip ("list" , "--format=json" , cwd = pkg_path )
729
775
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 )
731
777
732
778
733
779
@pytest .mark .usefixtures ("with_wheel" )
0 commit comments