-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenum_parameter.py
75 lines (60 loc) · 2.23 KB
/
enum_parameter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Copyright 2022 Inmanta
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contact: code@inmanta.com
"""
from enum import Enum
from typing import Container, Optional, Type, TypeVar, Union
from pytest_inmanta.test_parameter.parameter import DynamicDefault, TestParameter
E = TypeVar("E", bound=Enum)
class EnumTestParameter(TestParameter[E]):
"""
A test parameter that should contain an enumeration value. The option will store a string value
but it will only accepts values from the provided enum.
.. code-block:: python
inm_install_mode = EnumTestParameter(
argument="--install_mode",
environment_variable="INMANTA_INSTALL_MODE",
usage="Install mode for modules downloaded during this test",
enum=InstallMode,
default=InstallMode.release,
group=param_group,
)
"""
def __init__(
self,
argument: str,
environment_variable: str,
usage: str,
*,
enum: Type[E],
default: Optional[Union[E, DynamicDefault[E]]] = None,
key: Optional[str] = None,
group: Optional[str] = None,
legacy: Optional["EnumTestParameter[E]"] = None,
legacy_environment_variable: Optional[str] = None,
) -> None:
self.enum = enum
super().__init__(
argument,
environment_variable,
usage,
default=default,
key=key,
group=group,
legacy=legacy,
legacy_environment_variable=legacy_environment_variable,
)
@property
def choices(self) -> Optional[Container[str]]:
return [str(item.value) for item in self.enum]
def validate(self, raw_value: object) -> E:
return self.enum(raw_value)