Fix miscellaneous type checking issues in the tests
Marco Ricci

Marco Ricci commited on 2024-06-30 21:37:17
Zeige 2 geänderte Dateien mit 20 Einfügungen und 7 Löschungen.

... ...
@@ -11,6 +11,7 @@ import json
11 11
 import os
12 12
 from typing import Any, TYPE_CHECKING, TypedDict
13 13
 
14
+import click.testing
14 15
 import derivepassphrase
15 16
 import derivepassphrase.cli
16 17
 import derivepassphrase.types
... ...
@@ -5,6 +5,7 @@
5 5
 from __future__ import annotations
6 6
 
7 7
 import base64
8
+from collections.abc import Callable
8 9
 import json
9 10
 import os
10 11
 import socket
... ...
@@ -131,7 +132,7 @@ SINGLES: dict[tuple[str, ...], SingleConfiguration] = {
131 132
     ('--import', '-'): SingleConfiguration(False, b'{"services": {}}', True),
132 133
 }
133 134
 INTERESTING_OPTION_COMBINATIONS: list[OptionCombination] = []
134
-config: OptionCombination | SingleConfiguration
135
+config: IncompatibleConfiguration | SingleConfiguration
135 136
 for opt, config in INCOMPATIBLE.items():
136 137
     for opt2 in config.other_options:
137 138
         INTERESTING_OPTION_COMBINATIONS.extend([
... ...
@@ -180,7 +181,7 @@ class TestCLI:
180 181
             f'program died unexpectedly with exit code {result.exit_code}'
181 182
         )
182 183
         assert not result.stderr_bytes, (
183
-            f'program barfed on stderr: {result.stderr_bytes}'
184
+            f'program barfed on stderr: {result.stderr_bytes!r}'
184 185
         )
185 186
         for c in charset:
186 187
             assert c not in result.stdout, (
... ...
@@ -198,7 +199,7 @@ class TestCLI:
198 199
             f'program died unexpectedly with exit code {result.exit_code}'
199 200
         )
200 201
         assert not result.stderr_bytes, (
201
-            f'program barfed on stderr: {result.stderr_bytes}'
202
+            f'program barfed on stderr: {result.stderr_bytes!r}'
202 203
         )
203 204
         passphrase = result.stdout.rstrip('\r\n')
204 205
         for i in range(len(passphrase) - 1):
... ...
@@ -422,13 +423,17 @@ class TestCLI:
422 423
         self, monkeypatch: Any,
423 424
     ) -> None:
424 425
         runner = click.testing.CliRunner(mix_stderr=False)
426
+        # `isolated_config` validates the configuration.  So, to pass an
427
+        # actual broken configuration, we must open the configuration file
428
+        # ourselves afterwards, inside the context.
425 429
         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner,
426 430
                                    config={'services': {}}):
427 431
             with open(cli._config_filename(), 'wt') as outfile:
428 432
                 print('This string is not valid JSON.', file=outfile)
433
+            dname = os.path.dirname(cli._config_filename())
429 434
             result = runner.invoke(
430 435
                 cli.derivepassphrase,
431
-                ['--import', os.path.dirname(cli._config_filename())],
436
+                ['--import', os.fsdecode(dname)],
432 437
                 catch_exceptions=False)
433 438
             assert result.exit_code > 0, (
434 439
                 'program unexpectedly succeeded'
... ...
@@ -504,7 +509,7 @@ class TestCLI:
504 509
                                    config={'services': {}}):
505 510
             dname = os.path.dirname(cli._config_filename())
506 511
             result = runner.invoke(cli.derivepassphrase,
507
-                                   ['--export', dname],
512
+                                   ['--export', os.fsdecode(dname)],
508 513
                                    input=b'null', catch_exceptions=False)
509 514
             assert result.exit_code > 0, (
510 515
                 'program unexpectedly succeeded'
... ...
@@ -579,6 +584,7 @@ contents go here
579 584
             result = runner.invoke(cli.derivepassphrase, ['--notes', 'sv'],
580 585
                                    catch_exceptions=False)
581 586
             assert result.exit_code != 0, 'program unexpectedly succeeded'
587
+            assert result.stderr_bytes is not None
582 588
             assert b'user aborted request' in result.stderr_bytes, (
583 589
                 'expected error message missing'
584 590
             )
... ...
@@ -648,7 +654,7 @@ contents go here
648 654
     ])
649 655
     def test_225_store_config_fail(
650 656
         self, monkeypatch: Any, command_line: list[str],
651
-        input: bytes, err_text: str,
657
+        input: bytes, err_text: bytes,
652 658
     ) -> None:
653 659
         runner = click.testing.CliRunner(mix_stderr=False)
654 660
         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner,
... ...
@@ -660,6 +666,7 @@ contents go here
660 666
                                    ['--config'] + command_line,
661 667
                                    catch_exceptions=False, input=input)
662 668
             assert result.exit_code != 0, 'program unexpectedly succeeded?!'
669
+            assert result.stderr_bytes is not None
663 670
             assert err_text in result.stderr_bytes, (
664 671
                 'expected error message missing'
665 672
             )
... ...
@@ -678,6 +685,7 @@ contents go here
678 685
                                    ['--key', '--config'],
679 686
                                    catch_exceptions=False)
680 687
             assert result.exit_code != 0, 'program unexpectedly succeeded'
688
+            assert result.stderr_bytes is not None
681 689
             assert b'custom error message' in result.stderr_bytes, (
682 690
                 'expected error message missing'
683 691
             )
... ...
@@ -687,6 +695,7 @@ contents go here
687 695
         result = runner.invoke(cli.derivepassphrase, [],
688 696
                                catch_exceptions=False)
689 697
         assert result.exit_code != 0, 'program unexpectedly succeeded'
698
+        assert result.stderr_bytes is not None
690 699
         assert b'SERVICE is required' in result.stderr_bytes, (
691 700
             'expected error message missing'
692 701
         )
... ...
@@ -696,6 +705,7 @@ contents go here
696 705
         result = runner.invoke(cli.derivepassphrase, [DUMMY_SERVICE],
697 706
                                catch_exceptions=False)
698 707
         assert result.exit_code != 0, 'program unexpectedly succeeded'
708
+        assert result.stderr_bytes is not None
699 709
         assert b'no passphrase or key given' in result.stderr_bytes, (
700 710
             'expected error message missing'
701 711
         )
... ...
@@ -887,7 +897,9 @@ Boo.
887 897
     @tests.skip_if_no_agent
888 898
     @pytest.mark.parametrize(['conn_hint'],
889 899
                              [('none',), ('socket',), ('client',)])
890
-    def test_227_get_suitable_ssh_keys(self, monkeypatch, conn_hint):
900
+    def test_227_get_suitable_ssh_keys(
901
+        self, monkeypatch: Any, conn_hint: str,
902
+    ) -> None:
891 903
         monkeypatch.setattr(ssh_agent_client.SSHAgentClient,
892 904
                             'list_keys', tests.list_keys)
893 905
         hint: ssh_agent_client.SSHAgentClient | socket.socket | None
894 906