Skip to content

Commit 4cfeaf4

Browse files
authored
Merge pull request #434 from nexB/425-json-output-change
#425: JSON outputs no longer pretty-printed by default; added format …
2 parents 99f0a0d + 2b258ad commit 4cfeaf4

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

etc/release/release.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ function test_scan {
5050
# minimal test: update when new scans are available
5151
./scancode --quiet -lcip apache-2.0.LICENSE test_scan.json
5252
echo "TEST JSON passed: ./scancode --quiet -lcip apache-2.0.LICENSE test_scan.json"
53+
./scancode --quiet -lcip --format json-pp apache-2.0.LICENSE test_scan.json
54+
echo "TEST JSON-PP passed: ./scancode --quiet -lcip --format json-pp apache-2.0.LICENSE test_scan.json"
5355
./scancode --quiet -lcip --format html apache-2.0.LICENSE test_scan.html
5456
echo "TEST HTML passed: ./scancode --quiet -lcip --format html apache-2.0.LICENSE test_scan.html"
5557
./scancode --quiet -lcip --format html-app apache-2.0.LICENSE test_scan_app.html

src/scancode/cli.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class ScanCommand(utils.BaseCommand):
245245
Try 'scancode --help' for help on options and arguments.'''
246246

247247

248-
formats = ('json', 'html', 'html-app', 'spdx-tv', 'spdx-rdf')
248+
formats = ('json', 'json-pp', 'html', 'html-app', 'spdx-tv', 'spdx-rdf')
249249

250250
def validate_formats(ctx, param, value):
251251
value_lower = value.lower()
@@ -659,7 +659,7 @@ def save_results(files_count, scanned_files, format, input, output_file):
659659
except HtmlAppAssetCopyError:
660660
echo_stderr('\nFailed to create HTML app.', fg='red')
661661

662-
elif format == 'json':
662+
elif format == 'json' or format == 'json-pp':
663663
import simplejson as json
664664

665665
meta = OrderedDict()
@@ -668,7 +668,10 @@ def save_results(files_count, scanned_files, format, input, output_file):
668668
meta['files_count'] = files_count
669669
# TODO: add scanning options to meta
670670
meta['files'] = scanned_files
671-
output_file.write(unicode(json.dumps(meta, indent=2 * ' ', iterable_as_array=True, encoding='utf-8')))
671+
if format == 'json-pp':
672+
output_file.write(unicode(json.dumps(meta, indent=2 * ' ', iterable_as_array=True, encoding='utf-8')))
673+
else:
674+
output_file.write(unicode(json.dumps(meta, separators=(',', ':'), iterable_as_array=True, encoding='utf-8')))
672675
output_file.write('\n')
673676

674677
elif format == 'spdx-tv' or format == 'spdx-rdf':
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Copyright © 2000 ACME, Inc., All Rights Reserved */

tests/scancode/test_cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ def _load_json_result(result_file):
8585
return scan_result
8686

8787

88+
def test_json_pretty_print_option(monkeypatch):
89+
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
90+
test_dir = test_env.get_test_loc('json-option', copy=True)
91+
runner = CliRunner()
92+
result_file = test_env.get_temp_file('json')
93+
result = runner.invoke(cli.scancode, ['--copyright', '--format', 'json-pp', test_dir, result_file], catch_exceptions=True)
94+
assert result.exit_code == 0
95+
assert 'Scanning done' in result.output
96+
assert 'copyright_acme_c-c.c' in result.output
97+
assert os.path.exists(result_file)
98+
assert len(open(result_file).read()) > 10
99+
assert len(open(result_file).readlines()) > 1
100+
101+
102+
def test_json_output_option_is_minified(monkeypatch):
103+
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
104+
test_dir = test_env.get_test_loc('json-option', copy=True)
105+
runner = CliRunner()
106+
result_file = test_env.get_temp_file('json')
107+
result = runner.invoke(cli.scancode, ['--copyright', '--format', 'json', test_dir, result_file], catch_exceptions=True)
108+
assert result.exit_code == 0
109+
assert 'Scanning done' in result.output
110+
assert 'copyright_acme_c-c.c' in result.output
111+
assert os.path.exists(result_file)
112+
assert len(open(result_file).read()) > 10
113+
assert len(open(result_file).readlines()) == 1
114+
115+
88116
def test_package_option_detects_packages(monkeypatch):
89117
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
90118
test_dir = test_env.get_test_loc('package', copy=True)

0 commit comments

Comments
 (0)