Skip to content

Commit 7309796

Browse files
committed
Limit output to changes by default #41
* Add command option: '-a/--all'. * Pass 'all' parameter to 'write_csv()', 'write_json()' and 'utils.deltas()'. * Fix failing tests, modify/add new tests for '-a/--all' option. Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent 4bf4000 commit 7309796

4 files changed

Lines changed: 119 additions & 29 deletions

File tree

src/deltacode/cli.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939

4040

4141
# FIXME: update the function argument delta to deltacode
42-
def write_csv(delta, result_file):
42+
def write_csv(delta, result_file, all):
4343
"""
4444
Using the DeltaCode object, create a .csv file
45-
containing the primary information from the Delta objects.
45+
containing the primary information from the Delta objects. Omit all Delta
46+
objects whose 'category' is 'unmodified' unless the user selects the
47+
'-a'/'--all' option.
4648
"""
4749
with open(result_file, 'wb') as out:
4850
csv_out = csv.writer(out)
@@ -55,18 +57,23 @@ def write_csv(delta, result_file):
5557
f.old_file.size if f.category == 'removed' else f.new_file.size,
5658
f.old_file.path if f.category == 'moved' else '')
5759
for d in delta.deltas for f in delta.deltas.get(d)]:
58-
csv_out.writerow(row)
60+
if all is True:
61+
csv_out.writerow(row)
62+
elif row[0] != 'unmodified':
63+
csv_out.writerow(row)
5964

6065

61-
def write_json(deltacode, outfile):
66+
def write_json(deltacode, outfile, all):
6267
"""
6368
Using the DeltaCode object, create a .json file
64-
containing the primary information from the Delta objects.
69+
containing the primary information from the Delta objects. Omit all Delta
70+
objects whose 'category' is 'unmodified' unless the user selects the
71+
'-a'/'--all' option.
6572
"""
6673
results = OrderedDict([
6774
('deltacode_version', __version__),
6875
('deltacode_stats', deltacode.get_stats()),
69-
('deltas', deltas(deltacode)),
76+
('deltas', deltas(deltacode, all)),
7077
])
7178

7279
# TODO: add toggle for pretty printing
@@ -80,7 +87,8 @@ def write_json(deltacode, outfile):
8087
@click.option('-o', '--old', required=True, prompt=False, type=click.Path(exists=True, readable=True), help='Identify the path to the "old" scan file')
8188
@click.option('-c', '--csv-file', prompt=False, type=click.Path(exists=False), help='Identify the path to the .csv output file')
8289
@click.option('-j', '--json-file', prompt=False, default='-', type=click.File(mode='wb', lazy=False), help='Identify the path to the .json output file')
83-
def cli(new, old, csv_file, json_file):
90+
@click.option('-a', '--all', is_flag=True, help="Include unmodified files as well as all changed files in the .json or .csv output. If not selected, only changed files are included.")
91+
def cli(new, old, csv_file, json_file, all):
8492
"""
8593
Identify the changes that need to be made to the 'old'
8694
scan file (-o or -old) in order to generate the 'new' scan file (-n or
@@ -93,7 +101,7 @@ def cli(new, old, csv_file, json_file):
93101

94102
# output to csv
95103
if csv_file:
96-
write_csv(deltacode, csv_file)
104+
write_csv(deltacode, csv_file, all)
97105
# generate JSON output
98106
else:
99-
write_json(deltacode, json_file)
107+
write_json(deltacode, json_file, all)

src/deltacode/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@
3131
from commoncode import paths
3232

3333

34-
def deltas(deltacode):
34+
def deltas(deltacode, all):
3535
"""
36-
Return a generator of Delta dictionaries for JSON serialized ouput.
36+
Return a generator of Delta dictionaries for JSON serialized ouput. Omit
37+
all Delta objects whose 'category' is 'unmodified' unless the user selects
38+
the '-a'/'--all' option.
3739
"""
3840
for category, deltas in deltacode.deltas.iteritems():
3941
for delta in deltas:
40-
yield delta.to_dict()
42+
if all is True:
43+
yield delta.to_dict()
44+
elif delta.category != 'unmodified':
45+
yield delta.to_dict()
4146

4247

4348
class AlignmentException(Exception):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Type of delta,Path,Name,Type,Size,Old Path
2+
moved,b/a4.py,a4.py,file,200,a/a4.py

tests/test_cli.py

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_write_csv_added(self):
8181

8282
delta = DeltaCode(new_scan, old_scan)
8383
result_file = self.get_temp_file('.csv')
84-
cli.write_csv(delta, result_file)
84+
cli.write_csv(delta, result_file, True)
8585
expected_file = self.get_test_loc('cli/added1.csv')
8686
check_csvs(result_file, expected_file)
8787

@@ -91,7 +91,7 @@ def test_write_csv_modified(self):
9191

9292
delta = DeltaCode(new_scan, old_scan)
9393
result_file = self.get_temp_file('.csv')
94-
cli.write_csv(delta, result_file)
94+
cli.write_csv(delta, result_file, True)
9595
expected_file = self.get_test_loc('cli/modified1.csv')
9696
check_csvs(result_file, expected_file)
9797

@@ -101,7 +101,7 @@ def test_write_csv_removed(self):
101101

102102
delta = DeltaCode(new_scan, old_scan)
103103
result_file = self.get_temp_file('.csv')
104-
cli.write_csv(delta, result_file)
104+
cli.write_csv(delta, result_file, True)
105105
expected_file = self.get_test_loc('cli/removed1.csv')
106106
check_csvs(result_file, expected_file)
107107

@@ -111,7 +111,7 @@ def test_write_csv_renamed(self):
111111

112112
delta = DeltaCode(new_scan, old_scan)
113113
result_file = self.get_temp_file('.csv')
114-
cli.write_csv(delta, result_file)
114+
cli.write_csv(delta, result_file, True)
115115
expected_file = self.get_test_loc('cli/renamed1.csv')
116116
check_csvs(result_file, expected_file)
117117

@@ -121,7 +121,7 @@ def test_write_csv_modified_new_license_added(self):
121121

122122
delta = DeltaCode(new_scan, old_scan)
123123
result_file = self.get_temp_file('.csv')
124-
cli.write_csv(delta, result_file)
124+
cli.write_csv(delta, result_file, True)
125125
expected_file = self.get_test_loc('cli/modified_new_license_added.csv')
126126
check_csvs(result_file, expected_file)
127127

@@ -131,7 +131,7 @@ def test_write_csv_modified_new_license_added_low_score(self):
131131

132132
delta = DeltaCode(new_scan, old_scan)
133133
result_file = self.get_temp_file('.csv')
134-
cli.write_csv(delta, result_file)
134+
cli.write_csv(delta, result_file, True)
135135
expected_file = self.get_test_loc('cli/modified_new_license_added_low_score.csv')
136136
check_csvs(result_file, expected_file)
137137

@@ -141,7 +141,7 @@ def test_write_csv_license_info_removed(self):
141141

142142
delta = DeltaCode(new_scan, old_scan)
143143
result_file = self.get_temp_file('.csv')
144-
cli.write_csv(delta, result_file)
144+
cli.write_csv(delta, result_file, True)
145145
expected_file = self.get_test_loc('cli/license_info_removed.csv')
146146
check_csvs(result_file, expected_file)
147147

@@ -151,7 +151,7 @@ def test_write_csv_license_info_added(self):
151151

152152
delta = DeltaCode(new_scan, old_scan)
153153
result_file = self.get_temp_file('.csv')
154-
cli.write_csv(delta, result_file)
154+
cli.write_csv(delta, result_file, True)
155155
expected_file = self.get_test_loc('cli/license_info_added.csv')
156156
check_csvs(result_file, expected_file)
157157

@@ -161,7 +161,7 @@ def test_write_csv_license_info_removed_below_cutoff_score(self):
161161

162162
delta = DeltaCode(new_scan, old_scan)
163163
result_file = self.get_temp_file('.csv')
164-
cli.write_csv(delta, result_file)
164+
cli.write_csv(delta, result_file, True)
165165
expected_file = self.get_test_loc('cli/license_info_removed_below_cutoff_score.csv')
166166
check_csvs(result_file, expected_file)
167167

@@ -171,7 +171,7 @@ def test_write_csv_license_info_added_below_cutoff_score(self):
171171

172172
delta = DeltaCode(new_scan, old_scan)
173173
result_file = self.get_temp_file('.csv')
174-
cli.write_csv(delta, result_file)
174+
cli.write_csv(delta, result_file, True)
175175
expected_file = self.get_test_loc('cli/license_info_added_below_cutoff_score.csv')
176176
check_csvs(result_file, expected_file)
177177

@@ -181,7 +181,7 @@ def test_write_csv_1_file_moved(self):
181181

182182
delta = DeltaCode(new_scan, old_scan)
183183
result_file = self.get_temp_file('.csv')
184-
cli.write_csv(delta, result_file)
184+
cli.write_csv(delta, result_file, True)
185185
expected_file = self.get_test_loc('cli/1_file_moved.csv')
186186
check_csvs(result_file, expected_file)
187187

@@ -191,7 +191,7 @@ def test_write_csv_1_file_moved_and_1_copy(self):
191191

192192
delta = DeltaCode(new_scan, old_scan)
193193
result_file = self.get_temp_file('.csv')
194-
cli.write_csv(delta, result_file)
194+
cli.write_csv(delta, result_file, True)
195195
expected_file = self.get_test_loc('cli/1_file_moved_and_1_copy.csv')
196196
check_csvs(result_file, expected_file)
197197

@@ -201,16 +201,41 @@ def test_write_csv_1_file_moved_and_added(self):
201201

202202
delta = DeltaCode(new_scan, old_scan)
203203
result_file = self.get_temp_file('.csv')
204-
cli.write_csv(delta, result_file)
204+
cli.write_csv(delta, result_file, True)
205205
expected_file = self.get_test_loc('cli/1_file_moved_and_added.csv')
206206
check_csvs(result_file, expected_file)
207207

208-
def test_json_output_option_selected(self):
208+
def test_json_output_option_selected_all_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+
214+
runner = CliRunner()
215+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-j', result_file, '-a'])
216+
217+
assert result.exit_code == 0
218+
219+
json_result = json.load(open(result_file))
220+
stats = {'unmodified': 7, 'removed': 0, 'added': 0, 'moved': 1, 'modified': 0}
221+
222+
assert json_result.get('deltacode_stats') == stats
223+
224+
moved_expected = {'category': 'moved', 'name': 'a4.py', 'path': 'b/a4.py', 'old_path': 'a/a4.py', 'type': 'file', 'size': 200}
225+
moved_result = [d for d in json_result.get('deltas') if d.get('category') == 'moved'].pop()
226+
227+
assert moved_result == moved_expected
228+
229+
unmodified_expected = {'category': 'unmodified', 'name': 'a3.py', 'path': 'a/a3.py', 'type': 'file', 'size': 200}
230+
unmodified_result = [d for d in json_result.get('deltas') if d.get('category') == 'unmodified' and d.get('path') == 'a/a3.py'].pop()
231+
232+
assert unmodified_result == unmodified_expected
233+
234+
def test_json_output_option_selected_all_not_selected(self):
209235
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
210236
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
211237

212238
result_file = self.get_temp_file('json')
213-
expected_file = self.get_test_loc('cli/1_file_moved.json')
214239

215240
runner = CliRunner()
216241
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-j', result_file])
@@ -227,20 +252,65 @@ def test_json_output_option_selected(self):
227252

228253
assert moved_result == moved_expected
229254

230-
def test_csv_output_option_selected(self):
255+
unmodified_result = [d for d in json_result.get('deltas') if d.get('category') == 'unmodified']
256+
257+
assert len(unmodified_result) == 0
258+
259+
def test_csv_output_option_selected_all_selected(self):
231260
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
232261
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
233262

234263
result_file = self.get_temp_file('.csv')
235264
expected_file = self.get_test_loc('cli/1_file_moved.csv')
236265

266+
runner = CliRunner()
267+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-c', result_file, '-a'])
268+
269+
assert result.exit_code == 0
270+
check_csvs(result_file, expected_file)
271+
272+
def test_csv_output_option_selected_all_not_selected(self):
273+
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
274+
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
275+
276+
result_file = self.get_temp_file('.csv')
277+
expected_file = self.get_test_loc('cli/1_file_moved_all_not_selected.csv')
278+
237279
runner = CliRunner()
238280
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-c', result_file])
239281

240282
assert result.exit_code == 0
241283
check_csvs(result_file, expected_file)
242284

243-
def test_no_output_option_selected(self):
285+
def test_no_output_option_selected_all_selected(self):
286+
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
287+
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
288+
289+
runner = CliRunner()
290+
result = runner.invoke(cli.cli, ['-n', new_scan, '-o', old_scan, '-a'])
291+
292+
assert result.exit_code == 0
293+
294+
assert '"added": 0' in result.output
295+
assert '"modified": 0' in result.output
296+
assert '"moved": 1' in result.output
297+
assert '"removed": 0' in result.output
298+
assert '"unmodified": 7' in result.output
299+
300+
assert '"category": "moved"' in result.output
301+
assert '"path": "b/a4.py"' in result.output
302+
assert '"old_path": "a/a4.py"' in result.output
303+
assert '"name": "a4.py"' in result.output
304+
assert '"type": "file"' in result.output
305+
assert '"size": 200' in result.output
306+
307+
assert '"category": "unmodified"' in result.output
308+
assert '"path": "a/a3.py"' in result.output
309+
assert '"name": "a3.py"' in result.output
310+
assert '"type": "file"' in result.output
311+
assert '"size": 200' in result.output
312+
313+
def test_no_output_option_selected_all_not_selected(self):
244314
new_scan = self.get_test_loc('deltacode/scan_1_file_moved_new.json')
245315
old_scan = self.get_test_loc('deltacode/scan_1_file_moved_old.json')
246316

@@ -262,13 +332,18 @@ def test_no_output_option_selected(self):
262332
assert '"type": "file"' in result.output
263333
assert '"size": 200' in result.output
264334

335+
assert '"category": "unmodified"' not in result.output
336+
assert '"path": "a/a3.py"' not in result.output
337+
assert '"name": "a3.py"' not in result.output
338+
265339
def test_help(self):
266340
runner = CliRunner()
267341
result = runner.invoke(cli.cli, ['--help'])
268342

269343
assert 'Usage: cli [OPTIONS]' in result.output
270344
assert 'Identify the changes that need to be made' in result.output
271345
assert 'If no file option is selected' in result.output
346+
assert 'Include unmodified files' in result.output
272347

273348
def test_empty(self):
274349
runner = CliRunner()

0 commit comments

Comments
 (0)