Skip to content

Commit 977139f

Browse files
committed
Add initial cli tests #37
Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent 2a5e600 commit 977139f

3 files changed

Lines changed: 177 additions & 0 deletions

File tree

src/deltacode/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ 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')
7880
@click.help_option('-h', '--help')
7981
@click.option('-n', '--new', required=True, prompt=False, type=click.Path(exists=True, readable=True), help='Identify the path to the "new" scan file')
8082
@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/data/cli/1_file_moved.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"deltacode_version": "0.0.1.beta",
3+
"deltacode_stats": {
4+
"added": 0,
5+
"modified": 0,
6+
"moved": 1,
7+
"removed": 0,
8+
"unmodified": 7
9+
},
10+
"deltas": [
11+
{
12+
"category": "moved",
13+
"path": "b/a4.py",
14+
"old_path": "a/a4.py",
15+
"name": "a4.py",
16+
"type": "file",
17+
"size": 200
18+
},
19+
{
20+
"category": "unmodified",
21+
"path": "a/a3.py",
22+
"name": "a3.py",
23+
"type": "file",
24+
"size": 200
25+
},
26+
{
27+
"category": "unmodified",
28+
"path": "b/b4.py",
29+
"name": "b4.py",
30+
"type": "file",
31+
"size": 200
32+
},
33+
{
34+
"category": "unmodified",
35+
"path": "a/a2.py",
36+
"name": "a2.py",
37+
"type": "file",
38+
"size": 200
39+
},
40+
{
41+
"category": "unmodified",
42+
"path": "b/b2.py",
43+
"name": "b2.py",
44+
"type": "file",
45+
"size": 200
46+
},
47+
{
48+
"category": "unmodified",
49+
"path": "b/b1.py",
50+
"name": "b1.py",
51+
"type": "file",
52+
"size": 200
53+
},
54+
{
55+
"category": "unmodified",
56+
"path": "b/b3.py",
57+
"name": "b3.py",
58+
"type": "file",
59+
"size": 200
60+
},
61+
{
62+
"category": "unmodified",
63+
"path": "a/a1.py",
64+
"name": "a1.py",
65+
"type": "file",
66+
"size": 200
67+
}
68+
]
69+
}

tests/test_cli.py

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

3333
import unicodecsv
3434

35+
import click
3536
from click.testing import CliRunner
3637

3738
from commoncode.testcase import FileBasedTesting
3839
from deltacode import cli
3940
from deltacode import DeltaCode
4041
from deltacode import utils
4142

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+
42111

43112
def load_csv(location):
44113
"""
@@ -204,3 +273,40 @@ def test_write_csv_1_file_moved_and_added(self):
204273
cli.write_csv(delta, result_file)
205274
expected_file = self.get_test_loc('cli/1_file_moved_and_added.csv')
206275
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

Comments
 (0)