Skip to content

Commit 9dbf8ac

Browse files
committed
Use click to test TTY cli code
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent fd90791 commit 9dbf8ac

File tree

6 files changed

+76
-40
lines changed

6 files changed

+76
-40
lines changed

tests/test_cmd.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from attributecode import cmd
3030
from attributecode import Error
3131

32-
from testing_utils import run_about_command_test
32+
from testing_utils import run_about_command_test_click
3333
from testing_utils import get_test_loc
3434

3535

@@ -250,16 +250,17 @@ def check_about_stdout(options, expected_loc, regen=False):
250250
command success and that the stdout is equal to the `expected_loc` test file
251251
content.
252252
"""
253-
stdout, _stderr = run_about_command_test(options)
253+
result = run_about_command_test_click(options)
254254
if regen:
255255
expected_file = get_test_loc(expected_loc, must_exists=False)
256256
with open(expected_file, 'wb') as ef:
257-
ef.write(stdout)
257+
ef.write(result.output_bytes)
258258

259259
expected_file = get_test_loc(expected_loc, must_exists=True)
260-
expected = open(expected_file, 'rb').read()
261-
# we do not keep ends to ignore LF/CRF differences across OSes
262-
assert expected.splitlines(False) == stdout.splitlines(False)
260+
with open(expected_file, 'rb') as ef:
261+
expected = ef.read()
262+
263+
assert expected.splitlines(False) == result.output_bytes.splitlines(False)
263264

264265

265266
def test_about_help_text(regen=False):

tests/testdata/test_cmd/help/about_attrib_help.txt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@ Usage: about attrib [OPTIONS] LOCATION OUTPUT
22

33
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.
44

5-
LOCATION: Path to a file, directory or .zip archive containing .ABOUT
6-
files.
5+
LOCATION: Path to a file, directory or .zip archive containing .ABOUT files.
76

87
OUTPUT: Path where to write the attribution document.
98

109
Options:
11-
--template TEMPLATE_FILE_PATH Path to an optional custom attribution
12-
template to generate the attribution
13-
document.
10+
--template TEMPLATE_FILE_PATH Path to an optional custom attribution template
11+
to generate the attribution document.
1412
--variable <key>=<value> Add variable(s) as key=value for use in a
1513
custom attribution template.
16-
--inventory PATH Path to an optional JSON or CSV inventory
17-
file listing the subset of .ABOUT files paths
18-
to consider when generating the attribution
14+
--inventory PATH Path to an optional JSON or CSV inventory file
15+
listing the subset of .ABOUT files paths to
16+
consider when generating the attribution
1917
document.
2018
--mapping Use the default file mapping.config
2119
(./attributecode/mapping.config) with mapping
2220
between input keys and ABOUT field names.
23-
--mapping-file FILE Use a custom mapping file with mapping
24-
between input keys and ABOUT field names.
21+
--mapping-file FILE Use a custom mapping file with mapping between
22+
input keys and ABOUT field names.
2523
-q, --quiet Do not print error or warning messages.
2624
--verbose Show all error and warning messages.
2725
-h, --help Show this message and exit.

tests/testdata/test_cmd/help/about_gen_help.txt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,33 @@ Usage: about gen [OPTIONS] LOCATION OUTPUT
88
OUTPUT: Path to a directory where ABOUT files are generated.
99

1010
Options:
11-
--fetch-license KEY Fetch license data and texts from a a
12-
DejaCode License Library API. Create
13-
<license>.LICENSE files from the text of
14-
each license key side-by-side with the
15-
generated .ABOUT file. Also enhance the
16-
.ABOUT file with other data such name and
17-
category.
11+
--fetch-license KEY Fetch license data and texts from a a DejaCode
12+
License Library API. Create <license>.LICENSE
13+
files from the text of each license key side-
14+
by-side with the generated .ABOUT file. Also
15+
enhance the .ABOUT file with other data such
16+
name and category.
1817

19-
The following additional options
20-
are required:
18+
The following additional
19+
options are required:
2120

22-
api_url - URL to the DejaCode
23-
License Library API endpoint
21+
api_url - URL to the
22+
DejaCode License Library API endpoint
2423

25-
api_key -
26-
DejaCode API key
24+
api_key
25+
- DejaCode API key
2726
Example syntax:
2827

2928
about gen
3029
--fetch-license 'api_url' 'api_key'
3130
--license-notice-text-location PATH
32-
Copy the 'license_file' from the directory
33-
to the generated location.
31+
Copy the 'license_file' from the directory to
32+
the generated location.
3433
--mapping Use the default file mapping.config
35-
(./attributecode/mapping.config) with
36-
mapping between input keys and ABOUT field
37-
names.
38-
--mapping-file FILE Use a custom mapping file with mapping
34+
(./attributecode/mapping.config) with mapping
3935
between input keys and ABOUT field names.
36+
--mapping-file FILE Use a custom mapping file with mapping between
37+
input keys and ABOUT field names.
4038
-q, --quiet Do not print error or warning messages.
4139
--verbose Show all error and warning messages.
4240
-h, --help Show this message and exit.

tests/testdata/test_cmd/help/about_help.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Usage: about [OPTIONS] COMMAND [ARGS]...
33
Generate licensing attribution and credit notices from .ABOUT files and
44
inventories.
55

6-
Read, write and collect provenance and license inventories from .ABOUT
7-
files to and from JSON or CSV files.
6+
Read, write and collect provenance and license inventories from .ABOUT files
7+
to and from JSON or CSV files.
88

99
Use about <command> --help for help on a command.
1010

tests/testdata/test_cmd/help/about_inventory_help.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Options:
1111
key=value e.g. "license_expression=gpl-2.0
1212
-f, --format [json|csv] Set OUTPUT inventory file format. [default: csv]
1313
--mapping Use the default file mapping.config
14-
(./attributecode/mapping.config) with mapping
15-
between input keys and ABOUT field names.
16-
--mapping-file FILE Use a custom mapping file with mapping between
14+
(./attributecode/mapping.config) with mapping between
1715
input keys and ABOUT field names.
16+
--mapping-file FILE Use a custom mapping file with mapping between input
17+
keys and ABOUT field names.
1818
-q, --quiet Do not print error or warning messages.
1919
--verbose Show all error and warning messages.
2020
-h, --help Show this message and exit.

tests/testing_utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,42 @@ def run_about_command_test(options, expected_rc=0):
171171
).format(**locals())
172172
assert rc == expected_rc, error
173173
return stdout, stderr
174+
175+
176+
def run_about_command_test_click(options, expected_rc=0, monkeypatch=None, ):
177+
"""
178+
Run an "about" command as a Click-controlled subprocess with the `options`
179+
list of options. Return a click.testing.Result object.
180+
181+
If monkeypatch is provided, a tty with a size (80, 43) is mocked.
182+
"""
183+
import click
184+
from click.testing import CliRunner
185+
from attributecode import cmd
186+
if monkeypatch:
187+
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
188+
monkeypatch.setattr(click , 'get_terminal_size', lambda : (80, 43,))
189+
runner = CliRunner()
190+
191+
result = runner.invoke(cmd.cli, options, catch_exceptions=False)
192+
193+
output = result.output
194+
if result.exit_code != expected_rc:
195+
opts = get_opts(options)
196+
error = '''
197+
Failure to run: about %(opts)s
198+
output:
199+
%(output)s
200+
''' % locals()
201+
assert result.exit_code == expected_rc, error
202+
return result
203+
204+
205+
def get_opts(options):
206+
try:
207+
return ' '.join(options)
208+
except:
209+
try:
210+
return b' '.join(options)
211+
except:
212+
return b' '.join(map(repr, options))

0 commit comments

Comments
 (0)