|
32 | 32 |
|
33 | 33 | import unicodecsv |
34 | 34 |
|
35 | | -import click |
36 | 35 | from click.testing import CliRunner |
37 | 36 |
|
38 | 37 | from commoncode.testcase import FileBasedTesting |
39 | 38 | from deltacode import cli |
40 | 39 | from deltacode import DeltaCode |
41 | 40 | from deltacode import utils |
42 | 41 |
|
43 | | -# NOTE: From https://github.com/nexB/scancode-toolkit/blob/develop/src/scancode/cli_test_utils.py#L96 |
44 | | -# NOTE: Do we need the references to monkeypatch or can we delete? |
45 | | -# NOTE: We'll need to revise the docstring. |
46 | | -def run_scan_click(options, monkeypatch=None, catch_exceptions=False): |
47 | | - """ |
48 | | - Run a scan as a Click-controlled subprocess |
49 | | - If monkeypatch is provided, a tty with a size (80, 43) is mocked. |
50 | | - Return a click.testing.Result object. |
51 | | - """ |
52 | | - # import click |
53 | | - # from click.testing import CliRunner |
54 | | - # from scancode import cli |
55 | | - |
56 | | - # NOTE: I don't think we need to use monkeypatch, do we? |
57 | | - # if monkeypatch: |
58 | | - # monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True) |
59 | | - # monkeypatch.setattr(click , 'get_terminal_size', lambda : (80, 43,)) |
60 | | - runner = CliRunner() |
61 | | - |
62 | | - return runner.invoke(cli.cli, options, catch_exceptions=catch_exceptions) |
63 | | - |
64 | | - |
65 | | -# NOTE: Based on https://github.com/nexB/scancode-toolkit/blob/develop/src/scancode/cli_test_utils.py#L46 |
66 | | -# NOTE: We'll need to revise the docstring. |
67 | | -# NOTE: I don't think we need to pass/use the 'strip_dates' parameter. |
68 | | -def check_json_scan(expected_file, result_file, regen=False, strip_dates=False): |
69 | | - """ |
70 | | - Check the scan result_file JSON results against the expected_file expected JSON |
71 | | - results. Removes references to test_dir for the comparison. If regen is True the |
72 | | - expected_file WILL BE overwritten with the results. This is convenient for |
73 | | - updating tests expectations. But use with caution. |
74 | | - """ |
75 | | - result = _load_json_result(result_file) |
76 | | - if strip_dates: |
77 | | - remove_dates(result) |
78 | | - if regen: |
79 | | - with open(expected_file, 'wb') as reg: |
80 | | - json.dump(result, reg, indent=2, separators=(',', ': ')) |
81 | | - expected = _load_json_result(expected_file) |
82 | | - if strip_dates: |
83 | | - remove_dates(expected) |
84 | | - |
85 | | - # NOTE: The following note comes from the original ScanCode code. |
86 | | - # NOTE we redump the JSON as a string for a more efficient comparison of |
87 | | - # failures |
88 | | - expected = json.dumps(expected, indent=2, sort_keys=True, separators=(',', ': ')) |
89 | | - result = json.dumps(result, indent=2, sort_keys=True, separators=(',', ': ')) |
90 | | - assert expected == result |
91 | | - |
92 | | - |
93 | | -# NOTE: Based on https://github.com/nexB/scancode-toolkit/blob/develop/src/scancode/cli_test_utils.py#L70 |
94 | | -# NOTE: We'll need to revise the docstring. |
95 | | -def _load_json_result(result_file): |
96 | | - """ |
97 | | - Load the result file as utf-8 JSON |
98 | | - Sort the results by location. [1/19/18 This line applies to the ScanCode test and should be deleted from this DeltaCode file.] |
99 | | - """ |
100 | | - with codecs.open(result_file, encoding='utf-8') as res: |
101 | | - scan_result = json.load(res, object_pairs_hook=OrderedDict) |
102 | | - |
103 | | - # NOTE: 1/19/18 Following used for ScanCode testing but not applicable to DeltaCode? |
104 | | - # if scan_result.get('scancode_version'): |
105 | | - # del scan_result['scancode_version'] |
106 | | - |
107 | | - # NOTE: 1/19/18 Is this line only for ScanCode output? |
108 | | - # scan_result['files'].sort(key=lambda x: x['path']) |
109 | | - return scan_result |
110 | | - |
111 | 42 |
|
112 | 43 | def load_csv(location): |
113 | 44 | """ |
@@ -279,34 +210,53 @@ def test_json_output_option_selected(self): |
279 | 210 | old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json') |
280 | 211 |
|
281 | 212 | result_file = self.get_temp_file('json') |
282 | | - |
283 | | - result = run_scan_click(['-n', new_scan, '-o', old_scan, '-j', result_file]) |
284 | | - |
285 | 213 | expected_file = self.get_test_loc('cli/1_file_moved.json') |
286 | 214 |
|
| 215 | + runner = CliRunner() |
| 216 | + result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-j', result_file]) |
| 217 | + |
287 | 218 | assert result.exit_code == 0 |
288 | | - check_json_scan(result_file, expected_file) |
| 219 | + |
| 220 | + json_result = json.load(open(result_file)) |
| 221 | + stats = {'unmodified': 7, 'removed': 0, 'added': 0, 'moved': 1, 'modified': 0} |
| 222 | + |
| 223 | + assert json_result.get('deltacode_stats') == stats |
| 224 | + |
| 225 | + moved_expected = {'category': 'moved', 'name': 'a4.py', 'path': 'b/a4.py', 'old_path': 'a/a4.py', 'type': 'file', 'size': 200} |
| 226 | + moved_result = [i for i in json_result.get('deltas') if i.get('category') == 'moved'][0] |
| 227 | + |
| 228 | + assert moved_result == moved_expected |
289 | 229 |
|
290 | 230 | def test_csv_output_option_selected(self): |
291 | 231 | new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json') |
292 | 232 | old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json') |
293 | 233 |
|
294 | 234 | result_file = self.get_temp_file('.csv') |
295 | | - |
296 | | - result = run_scan_click(['-n', new_scan, '-o', old_scan, '-c', result_file]) |
297 | | - |
298 | 235 | expected_file = self.get_test_loc('cli/1_file_moved.csv') |
299 | 236 |
|
| 237 | + runner = CliRunner() |
| 238 | + result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-c', result_file]) |
| 239 | + |
300 | 240 | assert result.exit_code == 0 |
301 | 241 | check_csvs(result_file, expected_file) |
302 | 242 |
|
303 | | - # NOTE: Based on https://github.com/nexB/scancode-toolkit/blob/develop/tests/scancode/test_cli.py#L233 |
304 | | - def test_usage_and_help(self): |
305 | | - result = run_scan_click(['--help']) |
| 243 | + def test_help(self): |
| 244 | + runner = CliRunner() |
| 245 | + result = runner.invoke(cli.cli, ['--help']) |
| 246 | + |
306 | 247 | assert 'Usage: cli [OPTIONS]' in result.output |
| 248 | + assert 'Identify the changes that need to be made' in result.output |
| 249 | + assert 'If no file option is selected' in result.output |
| 250 | + |
| 251 | + def test_empty(self): |
| 252 | + runner = CliRunner() |
| 253 | + result = runner.invoke(cli.cli, []) |
307 | 254 |
|
308 | | - result = run_scan_click([]) |
309 | 255 | assert 'Usage: cli [OPTIONS]' in result.output |
| 256 | + assert 'Error: Missing option "-n" / "--new".' in result.output |
| 257 | + |
| 258 | + def test_incorrect_flag(self): |
| 259 | + runner = CliRunner() |
| 260 | + result = runner.invoke(cli.cli, ['-xyz']) |
310 | 261 |
|
311 | | - result = run_scan_click(['-xyz']) |
312 | 262 | assert 'Error: no such option: -x' in result.output |
0 commit comments