Modularize test helpers to not unnecessarily manipulate config files
Marco Ricci

Marco Ricci commited on 2024-09-11 21:00:34
Zeige 2 geänderte Dateien mit 42 Einfügungen und 61 Löschungen.


Rename the `isolated_config` to `isolated_vault_config`, which sets the
`derivepassphrase vault` configuration files, and avoid config file
manipulation in "vanilla" `isolated_config`.  Update tests to use
`isolated_config` where feasible.
... ...
@@ -545,7 +545,6 @@ def phrase_from_key(key: bytes) -> bytes:
545 545
 def isolated_config(
546 546
     monkeypatch: pytest.MonkeyPatch,
547 547
     runner: click.testing.CliRunner,
548
-    config: Any,
549 548
 ) -> Iterator[None]:
550 549
     prog_name = cli.PROG_NAME
551 550
     env_name = prog_name.replace(' ', '_').upper() + '_PATH'
... ...
@@ -554,6 +553,16 @@ def isolated_config(
554 553
         monkeypatch.setenv('USERPROFILE', os.getcwd())
555 554
         monkeypatch.delenv(env_name, raising=False)
556 555
         os.makedirs(os.path.dirname(cli._config_filename()), exist_ok=True)
556
+        yield
557
+
558
+
559
+@contextlib.contextmanager
560
+def isolated_vault_config(
561
+    monkeypatch: pytest.MonkeyPatch,
562
+    runner: click.testing.CliRunner,
563
+    config: Any,
564
+) -> Iterator[None]:
565
+    with isolated_config(monkeypatch=monkeypatch, runner=runner):
557 566
         with open(cli._config_filename(), 'w', encoding='UTF-8') as outfile:
558 567
             json.dump(config, outfile)
559 568
         yield
... ...
@@ -205,7 +205,6 @@ class TestCLI:
205 205
         with tests.isolated_config(
206 206
             monkeypatch=monkeypatch,
207 207
             runner=runner,
208
-            config={'services': {}},
209 208
         ):
210 209
             _result = runner.invoke(
211 210
                 cli.derivepassphrase_vault, ['--help'], catch_exceptions=False
... ...
@@ -231,7 +230,6 @@ class TestCLI:
231 230
         with tests.isolated_config(
232 231
             monkeypatch=monkeypatch,
233 232
             runner=runner,
234
-            config={'services': {}},
235 233
         ):
236 234
             _result = runner.invoke(
237 235
                 cli.derivepassphrase_vault,
... ...
@@ -254,7 +252,6 @@ class TestCLI:
254 252
         with tests.isolated_config(
255 253
             monkeypatch=monkeypatch,
256 254
             runner=runner,
257
-            config={'services': {}},
258 255
         ):
259 256
             _result = runner.invoke(
260 257
                 cli.derivepassphrase_vault,
... ...
@@ -303,7 +300,7 @@ class TestCLI:
303 300
         config: _types.VaultConfig,
304 301
     ) -> None:
305 302
         runner = click.testing.CliRunner(mix_stderr=False)
306
-        with tests.isolated_config(
303
+        with tests.isolated_vault_config(
307 304
             monkeypatch=monkeypatch, runner=runner, config=config
308 305
         ):
309 306
             monkeypatch.setattr(
... ...
@@ -330,7 +327,7 @@ class TestCLI:
330 327
         self, monkeypatch: pytest.MonkeyPatch
331 328
     ) -> None:
332 329
         runner = click.testing.CliRunner(mix_stderr=False)
333
-        with tests.isolated_config(
330
+        with tests.isolated_vault_config(
334 331
             monkeypatch=monkeypatch,
335 332
             runner=runner,
336 333
             config={'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS}},
... ...
@@ -404,7 +401,7 @@ class TestCLI:
404 401
         )
405 402
         monkeypatch.setattr(ssh_agent.SSHAgentClient, 'sign', sign)
406 403
         runner = click.testing.CliRunner(mix_stderr=False)
407
-        with tests.isolated_config(
404
+        with tests.isolated_vault_config(
408 405
             monkeypatch=monkeypatch, runner=runner, config=config
409 406
         ):
410 407
             _result = runner.invoke(
... ...
@@ -425,7 +422,7 @@ class TestCLI:
425 422
         monkeypatch: pytest.MonkeyPatch,
426 423
     ) -> None:
427 424
         runner = click.testing.CliRunner(mix_stderr=False)
428
-        with tests.isolated_config(
425
+        with tests.isolated_vault_config(
429 426
             monkeypatch=monkeypatch,
430 427
             runner=runner,
431 428
             config={
... ...
@@ -474,7 +471,6 @@ class TestCLI:
474 471
         with tests.isolated_config(
475 472
             monkeypatch=monkeypatch,
476 473
             runner=runner,
477
-            config={'services': {}},
478 474
         ):
479 475
             for value in '-42', 'invalid':
480 476
                 _result = runner.invoke(
... ...
@@ -506,7 +502,7 @@ class TestCLI:
506 502
     ) -> None:
507 503
         monkeypatch.setattr(cli, '_prompt_for_passphrase', tests.auto_prompt)
508 504
         runner = click.testing.CliRunner(mix_stderr=False)
509
-        with tests.isolated_config(
505
+        with tests.isolated_vault_config(
510 506
             monkeypatch=monkeypatch,
511 507
             runner=runner,
512 508
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -532,7 +528,7 @@ class TestCLI:
532 528
                     empty_stderr=True
533 529
                 ), 'expected clean exit'
534 530
         if check_success:
535
-            with tests.isolated_config(
531
+            with tests.isolated_vault_config(
536 532
                 monkeypatch=monkeypatch,
537 533
                 runner=runner,
538 534
                 config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -567,7 +563,6 @@ class TestCLI:
567 563
         with tests.isolated_config(
568 564
             monkeypatch=monkeypatch,
569 565
             runner=runner,
570
-            config={'services': {}},
571 566
         ):
572 567
             _result = runner.invoke(
573 568
                 cli.derivepassphrase_vault,
... ...
@@ -585,9 +580,7 @@ class TestCLI:
585 580
         monkeypatch: pytest.MonkeyPatch,
586 581
     ) -> None:
587 582
         runner = click.testing.CliRunner(mix_stderr=False)
588
-        with tests.isolated_config(
589
-            monkeypatch=monkeypatch, runner=runner, config={'services': {}}
590
-        ):
583
+        with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
591 584
             _result = runner.invoke(
592 585
                 cli.derivepassphrase_vault,
593 586
                 ['--import', '-'],
... ...
@@ -604,9 +597,7 @@ class TestCLI:
604 597
         monkeypatch: pytest.MonkeyPatch,
605 598
     ) -> None:
606 599
         runner = click.testing.CliRunner(mix_stderr=False)
607
-        with tests.isolated_config(
608
-            monkeypatch=monkeypatch, runner=runner, config={'services': {}}
609
-        ):
600
+        with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
610 601
             _result = runner.invoke(
611 602
                 cli.derivepassphrase_vault,
612 603
                 ['--import', '-'],
... ...
@@ -623,12 +614,11 @@ class TestCLI:
623 614
         monkeypatch: pytest.MonkeyPatch,
624 615
     ) -> None:
625 616
         runner = click.testing.CliRunner(mix_stderr=False)
626
-        # `isolated_config` validates the configuration.  So, to pass an
627
-        # actual broken configuration, we must open the configuration file
628
-        # ourselves afterwards, inside the context.
629
-        with tests.isolated_config(
630
-            monkeypatch=monkeypatch, runner=runner, config={'services': {}}
631
-        ):
617
+        # `isolated_vault_config` validates the configuration.  So, to
618
+        # pass an actual broken configuration, we must open the
619
+        # configuration file ourselves afterwards, inside the context.
620
+        # We also might as well use `isolated_config` instead.
621
+        with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
632 622
             with open(
633 623
                 cli._config_filename(), 'w', encoding='UTF-8'
634 624
             ) as outfile:
... ...
@@ -649,9 +639,7 @@ class TestCLI:
649 639
         monkeypatch: pytest.MonkeyPatch,
650 640
     ) -> None:
651 641
         runner = click.testing.CliRunner(mix_stderr=False)
652
-        with tests.isolated_config(
653
-            monkeypatch=monkeypatch, runner=runner, config={'services': {}}
654
-        ):
642
+        with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
655 643
             with contextlib.suppress(FileNotFoundError):
656 644
                 os.remove(cli._config_filename())
657 645
             _result = runner.invoke(
... ...
@@ -667,7 +655,7 @@ class TestCLI:
667 655
         monkeypatch: pytest.MonkeyPatch,
668 656
     ) -> None:
669 657
         runner = click.testing.CliRunner(mix_stderr=False)
670
-        with tests.isolated_config(
658
+        with tests.isolated_vault_config(
671 659
             monkeypatch=monkeypatch, runner=runner, config={}
672 660
         ):
673 661
             _result = runner.invoke(
... ...
@@ -686,9 +674,7 @@ class TestCLI:
686 674
         monkeypatch: pytest.MonkeyPatch,
687 675
     ) -> None:
688 676
         runner = click.testing.CliRunner(mix_stderr=False)
689
-        with tests.isolated_config(
690
-            monkeypatch=monkeypatch, runner=runner, config={'services': {}}
691
-        ):
677
+        with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
692 678
             with contextlib.suppress(FileNotFoundError):
693 679
                 os.remove(cli._config_filename())
694 680
             os.makedirs(cli._config_filename())
... ...
@@ -708,9 +694,7 @@ class TestCLI:
708 694
         monkeypatch: pytest.MonkeyPatch,
709 695
     ) -> None:
710 696
         runner = click.testing.CliRunner(mix_stderr=False)
711
-        with tests.isolated_config(
712
-            monkeypatch=monkeypatch, runner=runner, config={'services': {}}
713
-        ):
697
+        with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
714 698
             dname = os.path.dirname(cli._config_filename())
715 699
             _result = runner.invoke(
716 700
                 cli.derivepassphrase_vault,
... ...
@@ -728,9 +712,7 @@ class TestCLI:
728 712
         monkeypatch: pytest.MonkeyPatch,
729 713
     ) -> None:
730 714
         runner = click.testing.CliRunner(mix_stderr=False)
731
-        with tests.isolated_config(
732
-            monkeypatch=monkeypatch, runner=runner, config={'services': {}}
733
-        ):
715
+        with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
734 716
             with contextlib.suppress(FileNotFoundError):
735 717
                 shutil.rmtree('.derivepassphrase')
736 718
             with open('.derivepassphrase', 'w', encoding='UTF-8') as outfile:
... ...
@@ -755,7 +737,7 @@ class TestCLI:
755 737
 contents go here
756 738
 """
757 739
         runner = click.testing.CliRunner(mix_stderr=False)
758
-        with tests.isolated_config(
740
+        with tests.isolated_vault_config(
759 741
             monkeypatch=monkeypatch,
760 742
             runner=runner,
761 743
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -779,7 +761,7 @@ contents go here
779 761
         self, monkeypatch: pytest.MonkeyPatch
780 762
     ) -> None:
781 763
         runner = click.testing.CliRunner(mix_stderr=False)
782
-        with tests.isolated_config(
764
+        with tests.isolated_vault_config(
783 765
             monkeypatch=monkeypatch,
784 766
             runner=runner,
785 767
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -800,7 +782,7 @@ contents go here
800 782
         self, monkeypatch: pytest.MonkeyPatch
801 783
     ) -> None:
802 784
         runner = click.testing.CliRunner(mix_stderr=False)
803
-        with tests.isolated_config(
785
+        with tests.isolated_vault_config(
804 786
             monkeypatch=monkeypatch,
805 787
             runner=runner,
806 788
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -824,7 +806,7 @@ contents go here
824 806
         self, monkeypatch: pytest.MonkeyPatch
825 807
     ) -> None:
826 808
         runner = click.testing.CliRunner(mix_stderr=False)
827
-        with tests.isolated_config(
809
+        with tests.isolated_vault_config(
828 810
             monkeypatch=monkeypatch,
829 811
             runner=runner,
830 812
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -890,7 +872,7 @@ contents go here
890 872
         result_config: Any,
891 873
     ) -> None:
892 874
         runner = click.testing.CliRunner(mix_stderr=False)
893
-        with tests.isolated_config(
875
+        with tests.isolated_vault_config(
894 876
             monkeypatch=monkeypatch,
895 877
             runner=runner,
896 878
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -933,7 +915,7 @@ contents go here
933 915
         err_text: str,
934 916
     ) -> None:
935 917
         runner = click.testing.CliRunner(mix_stderr=False)
936
-        with tests.isolated_config(
918
+        with tests.isolated_vault_config(
937 919
             monkeypatch=monkeypatch,
938 920
             runner=runner,
939 921
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -957,7 +939,7 @@ contents go here
957 939
         monkeypatch: pytest.MonkeyPatch,
958 940
     ) -> None:
959 941
         runner = click.testing.CliRunner(mix_stderr=False)
960
-        with tests.isolated_config(
942
+        with tests.isolated_vault_config(
961 943
             monkeypatch=monkeypatch,
962 944
             runner=runner,
963 945
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -983,7 +965,7 @@ contents go here
983 965
         monkeypatch: pytest.MonkeyPatch,
984 966
     ) -> None:
985 967
         runner = click.testing.CliRunner(mix_stderr=False)
986
-        with tests.isolated_config(
968
+        with tests.isolated_vault_config(
987 969
             monkeypatch=monkeypatch,
988 970
             runner=runner,
989 971
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -1004,7 +986,7 @@ contents go here
1004 986
         monkeypatch: pytest.MonkeyPatch,
1005 987
     ) -> None:
1006 988
         runner = click.testing.CliRunner(mix_stderr=False)
1007
-        with tests.isolated_config(
989
+        with tests.isolated_vault_config(
1008 990
             monkeypatch=monkeypatch,
1009 991
             runner=runner,
1010 992
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -1027,7 +1009,7 @@ contents go here
1027 1009
         try_race_free_implementation: bool,
1028 1010
     ) -> None:
1029 1011
         runner = click.testing.CliRunner(mix_stderr=False)
1030
-        with tests.isolated_config(
1012
+        with tests.isolated_vault_config(
1031 1013
             monkeypatch=monkeypatch,
1032 1014
             runner=runner,
1033 1015
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -1051,7 +1033,7 @@ contents go here
1051 1033
         monkeypatch: pytest.MonkeyPatch,
1052 1034
     ) -> None:
1053 1035
         runner = click.testing.CliRunner(mix_stderr=False)
1054
-        with tests.isolated_config(
1036
+        with tests.isolated_vault_config(
1055 1037
             monkeypatch=monkeypatch,
1056 1038
             runner=runner,
1057 1039
             config={'global': {'phrase': 'abc'}, 'services': {}},
... ...
@@ -1078,7 +1060,6 @@ contents go here
1078 1060
         with tests.isolated_config(
1079 1061
             monkeypatch=monkeypatch,
1080 1062
             runner=runner,
1081
-            config={'services': {}},
1082 1063
         ):
1083 1064
             _result = runner.invoke(
1084 1065
                 cli.derivepassphrase_vault, [], catch_exceptions=False
... ...
@@ -1095,7 +1076,6 @@ contents go here
1095 1076
         with tests.isolated_config(
1096 1077
             monkeypatch=monkeypatch,
1097 1078
             runner=runner,
1098
-            config={'services': {}},
1099 1079
         ):
1100 1080
             _result = runner.invoke(
1101 1081
                 cli.derivepassphrase_vault,
... ...
@@ -1115,7 +1095,6 @@ contents go here
1115 1095
         with tests.isolated_config(
1116 1096
             monkeypatch=monkeypatch,
1117 1097
             runner=runner,
1118
-            config={'services': {}},
1119 1098
         ):
1120 1099
             os.remove('.derivepassphrase/settings.json')
1121 1100
             os.rmdir('.derivepassphrase')
... ...
@@ -1155,7 +1134,6 @@ contents go here
1155 1134
         with tests.isolated_config(
1156 1135
             monkeypatch=monkeypatch,
1157 1136
             runner=runner,
1158
-            config={'services': {}},
1159 1137
         ):
1160 1138
             _save_config = cli._save_config
1161 1139
 
... ...
@@ -1188,7 +1166,6 @@ contents go here
1188 1166
         with tests.isolated_config(
1189 1167
             monkeypatch=monkeypatch,
1190 1168
             runner=runner,
1191
-            config={'services': {}},
1192 1169
         ):
1193 1170
             _save_config = cli._save_config
1194 1171
 
... ...
@@ -1294,7 +1271,7 @@ contents go here
1294 1271
         warning_message: str,
1295 1272
     ) -> None:
1296 1273
         runner = click.testing.CliRunner(mix_stderr=False)
1297
-        with tests.isolated_config(
1274
+        with tests.isolated_vault_config(
1298 1275
             monkeypatch=monkeypatch,
1299 1276
             runner=runner,
1300 1277
             config={'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS.copy()}},
... ...
@@ -1318,7 +1295,7 @@ class TestCLIUtils:
1318 1295
     ) -> None:
1319 1296
         runner = click.testing.CliRunner()
1320 1297
         with (
1321
-            tests.isolated_config(
1298
+            tests.isolated_vault_config(
1322 1299
                 monkeypatch=monkeypatch, runner=runner, config={}
1323 1300
             ),
1324 1301
             pytest.raises(ValueError, match='Invalid vault config'),
... ...
@@ -1498,7 +1475,7 @@ Boo.
1498 1475
     ) -> None:
1499 1476
         runner = click.testing.CliRunner(mix_stderr=False)
1500 1477
         for start_config in [config, result_config]:
1501
-            with tests.isolated_config(
1478
+            with tests.isolated_vault_config(
1502 1479
                 monkeypatch=monkeypatch, runner=runner, config=start_config
1503 1480
             ):
1504 1481
                 _result = runner.invoke(
... ...
@@ -1575,7 +1552,6 @@ class TestCLITransition:
1575 1552
         with tests.isolated_config(
1576 1553
             monkeypatch=monkeypatch,
1577 1554
             runner=runner,
1578
-            config={'services': {}},
1579 1555
         ):
1580 1556
             _result = runner.invoke(
1581 1557
                 cli.derivepassphrase, ['--help'], catch_exceptions=False
... ...
@@ -1592,7 +1568,6 @@ class TestCLITransition:
1592 1568
         with tests.isolated_config(
1593 1569
             monkeypatch=monkeypatch,
1594 1570
             runner=runner,
1595
-            config={'services': {}},
1596 1571
         ):
1597 1572
             _result = runner.invoke(
1598 1573
                 cli.derivepassphrase,
... ...
@@ -1611,7 +1586,6 @@ class TestCLITransition:
1611 1586
         with tests.isolated_config(
1612 1587
             monkeypatch=monkeypatch,
1613 1588
             runner=runner,
1614
-            config={'services': {}},
1615 1589
         ):
1616 1590
             _result = runner.invoke(
1617 1591
                 cli.derivepassphrase,
... ...
@@ -1630,7 +1604,6 @@ class TestCLITransition:
1630 1604
         with tests.isolated_config(
1631 1605
             monkeypatch=monkeypatch,
1632 1606
             runner=runner,
1633
-            config={'services': {}},
1634 1607
         ):
1635 1608
             _result = runner.invoke(
1636 1609
                 cli.derivepassphrase,
... ...
@@ -1682,7 +1655,6 @@ class TestCLITransition:
1682 1655
         with tests.isolated_config(
1683 1656
             monkeypatch=monkeypatch,
1684 1657
             runner=runner,
1685
-            config={'services': {}},
1686 1658
         ):
1687 1659
             _result = runner.invoke(
1688 1660
                 cli.derivepassphrase,
1689 1661