Skip to content

Commit 4bf4000

Browse files
authored
Merge pull request #44 from nexB/37-add-cli-tests
Add cli tests #37
2 parents 2a5e600 + 36dd66a commit 4bf4000

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,81 @@ def test_write_csv_1_file_moved_and_added(self):
204204
cli.write_csv(delta, result_file)
205205
expected_file = self.get_test_loc('cli/1_file_moved_and_added.csv')
206206
check_csvs(result_file, expected_file)
207+
208+
def test_json_output_option_selected(self):
209+
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
210+
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
211+
212+
result_file = self.get_temp_file('json')
213+
expected_file = self.get_test_loc('cli/1_file_moved.json')
214+
215+
runner = CliRunner()
216+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-j', result_file])
217+
218+
assert result.exit_code == 0
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 = [d for d in json_result.get('deltas') if d.get('category') == 'moved'].pop()
227+
228+
assert moved_result == moved_expected
229+
230+
def test_csv_output_option_selected(self):
231+
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
232+
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
233+
234+
result_file = self.get_temp_file('.csv')
235+
expected_file = self.get_test_loc('cli/1_file_moved.csv')
236+
237+
runner = CliRunner()
238+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-c', result_file])
239+
240+
assert result.exit_code == 0
241+
check_csvs(result_file, expected_file)
242+
243+
def test_no_output_option_selected(self):
244+
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
245+
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
246+
247+
runner = CliRunner()
248+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan])
249+
250+
assert result.exit_code == 0
251+
252+
assert '"added": 0' in result.output
253+
assert '"modified": 0' in result.output
254+
assert '"moved": 1' in result.output
255+
assert '"removed": 0' in result.output
256+
assert '"unmodified": 7' in result.output
257+
258+
assert '"category": "moved"' in result.output
259+
assert '"path": "b/a4.py"' in result.output
260+
assert '"old_path": "a/a4.py"' in result.output
261+
assert '"name": "a4.py"' in result.output
262+
assert '"type": "file"' in result.output
263+
assert '"size": 200' in result.output
264+
265+
def test_help(self):
266+
runner = CliRunner()
267+
result = runner.invoke(cli.cli, ['--help'])
268+
269+
assert 'Usage: cli [OPTIONS]' in result.output
270+
assert 'Identify the changes that need to be made' in result.output
271+
assert 'If no file option is selected' in result.output
272+
273+
def test_empty(self):
274+
runner = CliRunner()
275+
result = runner.invoke(cli.cli, [])
276+
277+
assert 'Usage: cli [OPTIONS]' in result.output
278+
assert 'Error: Missing option "-n" / "--new".' in result.output
279+
280+
def test_incorrect_flag(self):
281+
runner = CliRunner()
282+
result = runner.invoke(cli.cli, ['-xyz'])
283+
284+
assert 'Error: no such option: -x' in result.output

0 commit comments

Comments
 (0)