-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
150 lines (113 loc) · 4.26 KB
/
commands.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Functions representing the CLI apps, in a form more suitable for testing.
If the tests were to invoke the apps as imported from `plumbum.cmd`,
measuring coverage would be tricky.
If they invoked the apps via directly imported forms,
capturing stdout would be tricky and repetitive.
Each version herein returns the stdout as a `str` for convenience in tests.
"""
import io
import sys
from typing import cast
try:
from typing import TypeAlias
except ImportError:
from typing import Any as TypeAlias
from plumbum.cli import Application as _Application
from nt2.ui import (
JSONToNestedText as _JSONToNestedText, NestedTextToJSON as _NestedTextToJSON,
NestedTextToTOML as _NestedTextToTOML, NestedTextToYAML as _NestedTextToYAML,
TOMLToNestedText as _TOMLToNestedText, YAMLToNestedText as _YAMLToNestedText
)
Application: TypeAlias = _Application
JSONToNestedText = cast(Application, _JSONToNestedText)
NestedTextToJSON = cast(Application, _NestedTextToJSON)
NestedTextToTOML = cast(Application, _NestedTextToTOML)
NestedTextToYAML = cast(Application, _NestedTextToYAML)
TOMLToNestedText = cast(Application, _TOMLToNestedText)
YAMLToNestedText = cast(Application, _YAMLToNestedText)
def _run_app(app_class: Application, *cli_args, **cli_kwargs) -> str:
"""
Invoke `app_class` with given flags, and return stdout content.
Args:
app_class: A `plumbum.cli.Application` to invoke.
cli_args: Positional arguments to pass to `app_class`.
cli_kwargs: Named arguments to pass to `app_class`, using *internal* names.
Returns:
The content of (fake) stdout after invoking `app_class`.
Raises:
Exception: Unexpected problem during invocation.
"""
sys_stdout = sys.stdout
fake_stdout = io.StringIO()
try:
sys.stdout = fake_stdout
app, main_result = app_class.invoke(*cli_args, **cli_kwargs)
except Exception as e: # pragma: no cover
raise e
else:
output = fake_stdout.getvalue()
finally:
sys.stdout = sys_stdout
fake_stdout.close()
return output
def json2nt(*cli_args, **cli_kwargs) -> str:
"""
Invoke `JSONToNestedText` in a test-friendly way.
Args:
cli_args: Positional arguments.
cli_kwargs: Named options, using *internal* names.
Returns:
The content of (fake) stdout after invoking `JSONToNestedText`.
"""
return _run_app(JSONToNestedText, *cli_args, **cli_kwargs)
def nt2json(*cli_args, **cli_kwargs) -> str:
"""
Invoke `NestedTextToJSON` in a test-friendly way.
Args:
cli_args: Positional arguments.
cli_kwargs: Named options, using *internal* names.
Returns:
The content of (fake) stdout after invoking `NestedTextToJSON`.
"""
return _run_app(NestedTextToJSON, *cli_args, **cli_kwargs)
def nt2yaml(*cli_args, **cli_kwargs) -> str:
"""
Invoke `NestedTextToYAML` in a test-friendly way.
Args:
cli_args: Positional arguments.
cli_kwargs: Named options, using *internal* names.
Returns:
The content of (fake) stdout after invoking `NestedTextToYAML`.
"""
return _run_app(NestedTextToYAML, *cli_args, **cli_kwargs)
def yaml2nt(*cli_args, **cli_kwargs) -> str:
"""
Invoke `YAMLToNestedText` in a test-friendly way.
Args:
cli_args: Positional arguments.
cli_kwargs: Named options, using *internal* names.
Returns:
The content of (fake) stdout after invoking `YAMLToNestedText`.
"""
return _run_app(YAMLToNestedText, *cli_args, **cli_kwargs)
def nt2toml(*cli_args, **cli_kwargs) -> str:
"""
Invoke `NestedTextToTOML` in a test-friendly way.
Args:
cli_args: Positional arguments.
cli_kwargs: Named options, using *internal* names.
Returns:
The content of (fake) stdout after invoking `NestedTextToTOML`.
"""
return _run_app(NestedTextToTOML, *cli_args, **cli_kwargs)
def toml2nt(*cli_args, **cli_kwargs) -> str:
"""
Invoke `TOMLToNestedText` in a test-friendly way.
Args:
cli_args: Positional arguments.
cli_kwargs: Named options, using *internal* names.
Returns:
The content of (fake) stdout after invoking `TOMLToNestedText`.
"""
return _run_app(TOMLToNestedText, *cli_args, **cli_kwargs)