Skip to content

Commit 1b262ea

Browse files
committed
Refactor cli tests #37
* Removed and replaced ScanCode-based functions and tests. Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent 977139f commit 1b262ea

2 files changed

Lines changed: 31 additions & 83 deletions

File tree

src/deltacode/cli.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ def write_json(deltacode, outfile):
7575

7676

7777
@click.command()
78-
# See https://github.com/nexB/scancode-toolkit/blob/develop/src/scancode/cli.py#L345
79-
# @click.command(name='broccoli')
8078
@click.help_option('-h', '--help')
8179
@click.option('-n', '--new', required=True, prompt=False, type=click.Path(exists=True, readable=True), help='Identify the path to the "new" scan file')
8280
@click.option('-o', '--old', required=True, prompt=False, type=click.Path(exists=True, readable=True), help='Identify the path to the "old" scan file')

tests/test_cli.py

Lines changed: 31 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,82 +32,13 @@
3232

3333
import unicodecsv
3434

35-
import click
3635
from click.testing import CliRunner
3736

3837
from commoncode.testcase import FileBasedTesting
3938
from deltacode import cli
4039
from deltacode import DeltaCode
4140
from deltacode import utils
4241

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-
11142

11243
def load_csv(location):
11344
"""
@@ -279,34 +210,53 @@ def test_json_output_option_selected(self):
279210
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
280211

281212
result_file = self.get_temp_file('json')
282-
283-
result = run_scan_click(['-n', new_scan, '-o', old_scan, '-j', result_file])
284-
285213
expected_file = self.get_test_loc('cli/1_file_moved.json')
286214

215+
runner = CliRunner()
216+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-j', result_file])
217+
287218
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
289229

290230
def test_csv_output_option_selected(self):
291231
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
292232
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
293233

294234
result_file = self.get_temp_file('.csv')
295-
296-
result = run_scan_click(['-n', new_scan, '-o', old_scan, '-c', result_file])
297-
298235
expected_file = self.get_test_loc('cli/1_file_moved.csv')
299236

237+
runner = CliRunner()
238+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-c', result_file])
239+
300240
assert result.exit_code == 0
301241
check_csvs(result_file, expected_file)
302242

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+
306247
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, [])
307254

308-
result = run_scan_click([])
309255
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'])
310261

311-
result = run_scan_click(['-xyz'])
312262
assert 'Error: no such option: -x' in result.output

0 commit comments

Comments
 (0)