|
32 | 32 |
|
33 | 33 | import unicodecsv |
34 | 34 |
|
| 35 | +import click |
35 | 36 | from click.testing import CliRunner |
36 | 37 |
|
37 | 38 | from commoncode.testcase import FileBasedTesting |
38 | 39 | from deltacode import cli |
39 | 40 | from deltacode import DeltaCode |
40 | 41 | from deltacode import utils |
41 | 42 |
|
| 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 | + |
42 | 111 |
|
43 | 112 | def load_csv(location): |
44 | 113 | """ |
@@ -204,3 +273,40 @@ def test_write_csv_1_file_moved_and_added(self): |
204 | 273 | cli.write_csv(delta, result_file) |
205 | 274 | expected_file = self.get_test_loc('cli/1_file_moved_and_added.csv') |
206 | 275 | check_csvs(result_file, expected_file) |
| 276 | + |
| 277 | + def test_json_output_option_selected(self): |
| 278 | + new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json') |
| 279 | + old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json') |
| 280 | + |
| 281 | + result_file = self.get_temp_file('json') |
| 282 | + |
| 283 | + result = run_scan_click(['-n', new_scan, '-o', old_scan, '-j', result_file]) |
| 284 | + |
| 285 | + expected_file = self.get_test_loc('cli/1_file_moved.json') |
| 286 | + |
| 287 | + assert result.exit_code == 0 |
| 288 | + check_json_scan(result_file, expected_file) |
| 289 | + |
| 290 | + def test_csv_output_option_selected(self): |
| 291 | + new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json') |
| 292 | + old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json') |
| 293 | + |
| 294 | + result_file = self.get_temp_file('.csv') |
| 295 | + |
| 296 | + result = run_scan_click(['-n', new_scan, '-o', old_scan, '-c', result_file]) |
| 297 | + |
| 298 | + expected_file = self.get_test_loc('cli/1_file_moved.csv') |
| 299 | + |
| 300 | + assert result.exit_code == 0 |
| 301 | + check_csvs(result_file, expected_file) |
| 302 | + |
| 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']) |
| 306 | + assert 'Usage: cli [OPTIONS]' in result.output |
| 307 | + |
| 308 | + result = run_scan_click([]) |
| 309 | + assert 'Usage: cli [OPTIONS]' in result.output |
| 310 | + |
| 311 | + result = run_scan_click(['-xyz']) |
| 312 | + assert 'Error: no such option: -x' in result.output |
0 commit comments