Make CLI testing helper functions, data and strategies public
Marco Ricci

Marco Ricci commited on 2025-01-28 00:15:01
Zeige 1 geänderte Dateien mit 48 Einfügungen und 36 Löschungen.


Make these helpers public in the sense that they get picked up by the
API documentation generator.
... ...
@@ -2907,7 +2907,7 @@ Boo.
2907 2907
             assert f'FutureWarning: {THE_FUTURE}' in record_tuples[1][2]
2908 2908
             assert f'UserWarning: {JUST_TESTING}' in record_tuples[2][2]
2909 2909
 
2910
-    def _export_as_sh_helper(
2910
+    def export_as_sh_helper(
2911 2911
         self,
2912 2912
         config: Any,
2913 2913
     ) -> None:
... ...
@@ -2982,7 +2982,7 @@ Boo.
2982 2982
         settings settable via `--config` and settings requiring
2983 2983
         `--import`.
2984 2984
 
2985
-        The actual verification is done by [`_export_as_sh_helper`][].
2985
+        The actual verification is done by [`export_as_sh_helper`][].
2986 2986
 
2987 2987
         """
2988 2988
         config: _types.VaultConfig = {
... ...
@@ -2991,7 +2991,7 @@ Boo.
2991 2991
         }
2992 2992
         assert _types.clean_up_falsy_vault_config_values(config) is not None
2993 2993
         assert _types.is_vault_config(config)
2994
-        return self._export_as_sh_helper(config)
2994
+        return self.export_as_sh_helper(config)
2995 2995
 
2996 2996
     @hypothesis.given(
2997 2997
         global_config_importable=strategies.fixed_dictionaries(
... ...
@@ -3023,7 +3023,7 @@ Boo.
3023 3023
         Here, we check global-only configurations which only use
3024 3024
         settings requiring `--import`.
3025 3025
 
3026
-        The actual verification is done by [`_export_as_sh_helper`][].
3026
+        The actual verification is done by [`export_as_sh_helper`][].
3027 3027
 
3028 3028
         """
3029 3029
         config: _types.VaultConfig = {
... ...
@@ -3034,7 +3034,7 @@ Boo.
3034 3034
         assert _types.is_vault_config(config)
3035 3035
         if not config['global']:
3036 3036
             config.pop('global')
3037
-        return self._export_as_sh_helper(config)
3037
+        return self.export_as_sh_helper(config)
3038 3038
 
3039 3039
     @hypothesis.given(
3040 3040
         service_name=strategies.text(
... ...
@@ -3086,7 +3086,7 @@ Boo.
3086 3086
         settings settable via `--config` and settings requiring
3087 3087
         `--import`.
3088 3088
 
3089
-        The actual verification is done by [`_export_as_sh_helper`][].
3089
+        The actual verification is done by [`export_as_sh_helper`][].
3090 3090
 
3091 3091
         """
3092 3092
         config: _types.VaultConfig = {
... ...
@@ -3098,7 +3098,7 @@ Boo.
3098 3098
         }
3099 3099
         assert _types.clean_up_falsy_vault_config_values(config) is not None
3100 3100
         assert _types.is_vault_config(config)
3101
-        return self._export_as_sh_helper(config)
3101
+        return self.export_as_sh_helper(config)
3102 3102
 
3103 3103
     @hypothesis.given(
3104 3104
         service_name=strategies.text(
... ...
@@ -3147,7 +3147,7 @@ Boo.
3147 3147
         Here, we check service-only configurations which only use
3148 3148
         settings requiring `--import`.
3149 3149
 
3150
-        The actual verification is done by [`_export_as_sh_helper`][].
3150
+        The actual verification is done by [`export_as_sh_helper`][].
3151 3151
 
3152 3152
         """
3153 3153
         config: _types.VaultConfig = {
... ...
@@ -3157,7 +3157,7 @@ Boo.
3157 3157
         }
3158 3158
         assert _types.clean_up_falsy_vault_config_values(config) is not None
3159 3159
         assert _types.is_vault_config(config)
3160
-        return self._export_as_sh_helper(config)
3160
+        return self.export_as_sh_helper(config)
3161 3161
 
3162 3162
     @pytest.mark.parametrize(
3163 3163
         ['command_line', 'config', 'result_config'],
... ...
@@ -3920,9 +3920,9 @@ class TestCLITransition:
3920 3920
             ) == [DUMMY_SERVICE]
3921 3921
 
3922 3922
 
3923
-_known_services = (DUMMY_SERVICE, 'email', 'bank', 'work')
3923
+KNOWN_SERVICES = (DUMMY_SERVICE, 'email', 'bank', 'work')
3924 3924
 """Known service names.  Used for the [`ConfigManagementStateMachine`][]."""
3925
-_valid_properties = (
3925
+VALID_PROPERTIES = (
3926 3926
     'length',
3927 3927
     'repeat',
3928 3928
     'upper',
... ...
@@ -3935,37 +3935,44 @@ _valid_properties = (
3935 3935
 """Known vault properties.  Used for the [`ConfigManagementStateMachine`][]."""
3936 3936
 
3937 3937
 
3938
-def _build_reduced_vault_config_settings(
3938
+def build_reduced_vault_config_settings(
3939 3939
     config: _types.VaultConfigServicesSettings,
3940
-    keys_to_purge: frozenset[str],
3940
+    keys_to_prune: frozenset[str],
3941 3941
 ) -> _types.VaultConfigServicesSettings:
3942 3942
     """Return a service settings object with certain keys pruned.
3943 3943
 
3944 3944
     Args:
3945 3945
         config:
3946 3946
             The original service settings object.
3947
-        keys_to_purge:
3948
-            The keys to purge from the settings object.
3947
+        keys_to_prune:
3948
+            The keys to prune from the settings object.
3949 3949
 
3950 3950
     """
3951 3951
     config2 = copy.deepcopy(config)
3952
-    for key in keys_to_purge:
3952
+    for key in keys_to_prune:
3953 3953
         config2.pop(key, None)  # type: ignore[misc]
3954 3954
     return config2
3955 3955
 
3956 3956
 
3957
-_services_strategy = strategies.builds(
3958
-    _build_reduced_vault_config_settings,
3957
+SERVICES_STRATEGY = strategies.builds(
3958
+    build_reduced_vault_config_settings,
3959 3959
     tests.vault_full_service_config(),
3960 3960
     strategies.sets(
3961
-        strategies.sampled_from(_valid_properties),
3961
+        strategies.sampled_from(VALID_PROPERTIES),
3962 3962
         max_size=7,
3963 3963
     ),
3964 3964
 )
3965 3965
 """A hypothesis strategy to build incomplete service configurations."""
3966 3966
 
3967 3967
 
3968
-def _assemble_config(
3968
+def services_strategy() -> strategies.SearchStrategy[
3969
+    _types.VaultConfigServicesSettings
3970
+]:
3971
+    """Return a strategy to build incomplete service configurations."""
3972
+    return SERVICES_STRATEGY
3973
+
3974
+
3975
+def assemble_config(
3969 3976
     global_data: _types.VaultConfigGlobalSettings,
3970 3977
     service_data: list[tuple[str, _types.VaultConfigServicesSettings]],
3971 3978
 ) -> _types.VaultConfig:
... ...
@@ -3979,14 +3986,14 @@ def _assemble_config(
3979 3986
 
3980 3987
 
3981 3988
 @strategies.composite
3982
-def _draw_service_name_and_data(
3989
+def draw_service_name_and_data(
3983 3990
     draw: hypothesis.strategies.DrawFn,
3984 3991
     num_entries: int,
3985 3992
 ) -> tuple[tuple[str, _types.VaultConfigServicesSettings], ...]:
3986 3993
     """Draw a service name and settings, as a hypothesis strategy.
3987 3994
 
3988
-    Will draw service names from [`_known_services`][] and service
3989
-    settings via [`_services_strategy`][].
3995
+    Will draw service names from [`KNOWN_SERVICES`][] and service
3996
+    settings via [`services_strategy`][].
3990 3997
 
3991 3998
     Args:
3992 3999
         draw:
... ...
@@ -3998,7 +4005,7 @@ def _draw_service_name_and_data(
3998 4005
         A sequence of pairs of service names and service settings.
3999 4006
 
4000 4007
     """
4001
-    possible_services = list(_known_services)
4008
+    possible_services = list(KNOWN_SERVICES)
4002 4009
     selected_services: list[str] = []
4003 4010
     for _ in range(num_entries):
4004 4011
         selected_services.append(
... ...
@@ -4006,21 +4013,26 @@ def _draw_service_name_and_data(
4006 4013
         )
4007 4014
         possible_services.remove(selected_services[-1])
4008 4015
     return tuple(
4009
-        (service, draw(_services_strategy)) for service in selected_services
4016
+        (service, draw(services_strategy())) for service in selected_services
4010 4017
     )
4011 4018
 
4012 4019
 
4013
-_vault_full_config = strategies.builds(
4014
-    _assemble_config,
4015
-    _services_strategy,
4020
+VAULT_FULL_CONFIG = strategies.builds(
4021
+    assemble_config,
4022
+    services_strategy(),
4016 4023
     strategies.integers(
4017 4024
         min_value=2,
4018 4025
         max_value=4,
4019
-    ).flatmap(_draw_service_name_and_data),
4026
+    ).flatmap(draw_service_name_and_data),
4020 4027
 )
4021 4028
 """A hypothesis strategy to build full vault configurations."""
4022 4029
 
4023 4030
 
4031
+def vault_full_config() -> strategies.SearchStrategy[_types.VaultConfig]:
4032
+    """Return a strategy to build full vault configurations."""
4033
+    return VAULT_FULL_CONFIG
4034
+
4035
+
4024 4036
 @tests.hypothesis_settings_coverage_compatible
4025 4037
 class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
4026 4038
     """A state machine recording changes in the vault configuration.
... ...
@@ -4064,7 +4076,7 @@ class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
4064 4076
     @stateful.initialize(
4065 4077
         target=configuration,
4066 4078
         configs=strategies.lists(
4067
-            _vault_full_config,
4079
+            vault_full_config(),
4068 4080
             min_size=8,
4069 4081
             max_size=8,
4070 4082
         ),
... ...
@@ -4079,7 +4091,7 @@ class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
4079 4091
     @stateful.initialize(
4080 4092
         target=setting,
4081 4093
         configs=strategies.lists(
4082
-            _vault_full_config,
4094
+            vault_full_config(),
4083 4095
             min_size=4,
4084 4096
             max_size=4,
4085 4097
         ),
... ...
@@ -4114,7 +4126,7 @@ class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
4114 4126
         config=configuration,
4115 4127
         setting=setting.filter(bool),
4116 4128
         maybe_unset=strategies.sets(
4117
-            strategies.sampled_from(_valid_properties),
4129
+            strategies.sampled_from(VALID_PROPERTIES),
4118 4130
             max_size=3,
4119 4131
         ),
4120 4132
         overwrite=strategies.booleans(),
... ...
@@ -4169,7 +4181,7 @@ class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
4169 4181
             + [
4170 4182
                 f'--{key}={value}'
4171 4183
                 for key, value in setting.items()
4172
-                if key in _valid_properties
4184
+                if key in VALID_PROPERTIES
4173 4185
             ],
4174 4186
             catch_exceptions=False,
4175 4187
         )
... ...
@@ -4181,10 +4193,10 @@ class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
4181 4193
     @stateful.rule(
4182 4194
         target=configuration,
4183 4195
         config=configuration,
4184
-        service=strategies.sampled_from(_known_services),
4196
+        service=strategies.sampled_from(KNOWN_SERVICES),
4185 4197
         setting=setting.filter(bool),
4186 4198
         maybe_unset=strategies.sets(
4187
-            strategies.sampled_from(_valid_properties),
4199
+            strategies.sampled_from(VALID_PROPERTIES),
4188 4200
             max_size=3,
4189 4201
         ),
4190 4202
         overwrite=strategies.booleans(),
... ...
@@ -4242,7 +4254,7 @@ class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
4242 4254
             + [
4243 4255
                 f'--{key}={value}'
4244 4256
                 for key, value in setting.items()
4245
-                if key in _valid_properties
4257
+                if key in VALID_PROPERTIES
4246 4258
             ]
4247 4259
             + ['--', service],
4248 4260
             catch_exceptions=False,
4249 4261