Skip to content

Commit b917eb5

Browse files
committed
#318 Support different JSON format
* Add test code Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 03b40a2 commit b917eb5

File tree

6 files changed

+44
-63
lines changed

6 files changed

+44
-63
lines changed

src/attributecode/util.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf8 -*-
33
# ============================================================================
4-
# Copyright (c) 2013-2017 nexB Inc. http://www.nexb.com/ - All rights reserved.
4+
# Copyright (c) 2013-2018 nexB Inc. http://www.nexb.com/ - All rights reserved.
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
77
# You may obtain a copy of the License at
@@ -427,10 +427,13 @@ def load_json(location, use_mapping=False, mapping_file=None):
427427
else:
428428
updated_results = [results]
429429
else:
430-
updated_results = results
431-
if use_mapping:
432-
results = apply_mapping(updated_results, mapping_file)
433-
return results
430+
updated_results = sorted(results)
431+
432+
if use_mapping or mapping_file:
433+
about_ordered_list = apply_mapping(updated_results, mapping_file)
434+
else:
435+
about_ordered_list = updated_results
436+
return about_ordered_list
434437

435438

436439
def have_network_connection():

tests/test_util.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,33 @@ def test_load_json_with_mapping(self):
301301
result = util.load_json(test_file, use_mapping=True)
302302
assert expected == result
303303

304+
def test_load_non_list_json_with_mapping(self):
305+
test_file = get_test_loc('load/not_a_list_need_mapping.json')
306+
mapping_file = get_test_loc('custom-mapping-file/mapping.config')
307+
expected = [OrderedDict(
308+
[('about_file_path', '/load/this.ABOUT'),
309+
('version', '0.11.0'),
310+
('about_resource', '.'),
311+
('name', 'AboutCode'),
312+
('about_resource_path', '.'),
313+
])
314+
]
315+
result = util.load_json(test_file, use_mapping=False, mapping_file=mapping_file)
316+
assert expected == result
317+
318+
def test_load_non_list_json(self):
319+
test_file = get_test_loc('load/not_a_list.json')
320+
expected = [OrderedDict(
321+
[('about_file_path', '/load/this.ABOUT'),
322+
('version', '0.11.0'),
323+
('about_resource', '.'),
324+
('name', 'AboutCode'),
325+
('about_resource_path', '.'),
326+
])
327+
]
328+
result = util.load_json(test_file)
329+
assert expected == result
330+
304331
def test_get_about_file_path_from_csv_using_mapping(self):
305332
test_file = get_test_loc('util/about.csv')
306333
expected = ['about.ABOUT']

tests/testdata/custom-mapping-file/mapping.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#
3232

3333
# Essential Fields
34-
about_file_path: about_file
34+
about_file_path: path
3535
name: Component
3636

3737
# Optional Fields

tests/testdata/load/not_a_list.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"path": "/load/this.ABOUT",
2+
"about_file_path": "/load/this.ABOUT",
33
"about_resource_path": ".",
44
"about_resource": ".",
55
"name": "AboutCode",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"path": "/load/this.ABOUT",
3+
"about_resource_path": ".",
4+
"about_resource": ".",
5+
"name": "AboutCode",
6+
"version": "0.11.0"
7+
}

tests/testdata/load/scancode_result.json

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,12 @@
88
"type": "directory",
99
"name": "test",
1010
"base_name": "test",
11-
"extension": "",
12-
"size": 0,
13-
"date": null,
14-
"sha1": null,
15-
"md5": null,
16-
"mime_type": null,
17-
"file_type": null,
18-
"programming_language": null,
19-
"is_binary": false,
20-
"is_text": false,
21-
"is_archive": false,
22-
"is_media": false,
23-
"is_source": false,
24-
"is_script": false,
25-
"licenses": [],
26-
"copyrights": [],
27-
"emails": [],
28-
"urls": [],
29-
"files_count": 1,
30-
"dirs_count": 0,
31-
"size_count": 173,
32-
"scan_errors": []
3311
},
3412
{
3513
"path": "test/ScanCode.ABOUT",
3614
"type": "file",
3715
"name": "ScanCode.ABOUT",
3816
"base_name": "ScanCode",
39-
"extension": ".ABOUT",
40-
"size": 173,
41-
"date": "2018-03-14",
42-
"sha1": "96f9a9baa6d9114a190a68f4364341d5b16a62cd",
43-
"md5": "ef3544d674fa0fe93daf435dacdf6857",
44-
"mime_type": "text/plain",
45-
"file_type": "ASCII text",
46-
"programming_language": "ActionScript 3",
47-
"is_binary": false,
48-
"is_text": true,
49-
"is_archive": false,
50-
"is_media": false,
51-
"is_source": true,
52-
"is_script": false,
53-
"licenses": [],
54-
"copyrights": [
55-
{
56-
"statements": [
57-
"Copyright (c) 2017 nexB Inc. owner nexB Inc."
58-
],
59-
"holders": [
60-
"nexB Inc. owner nexB Inc."
61-
],
62-
"authors": [],
63-
"start_line": 5,
64-
"end_line": 7
65-
}
66-
],
67-
"emails": [],
68-
"urls": [],
69-
"files_count": 0,
70-
"dirs_count": 0,
71-
"size_count": 0,
72-
"scan_errors": []
7317
}
7418
]
7519
}

0 commit comments

Comments
 (0)