Skip to content

Commit df1d195

Browse files
committed
[tests] Add tests for scancode_cli analyzer
Add tests for scancode_cli analyzer which also required updating tests for in-place implementation of colic backend. Signed-off-by: inishchith <[email protected]>
1 parent c647f48 commit df1d195

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed

graal/backends/core/analyzers/scancode.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ def __analyze_scancode(self, file_path):
6969
subprocess._cleanup()
7070

7171
licenses_raw = json.loads(msg)
72-
if 'files' not in licenses_raw:
73-
return result
74-
75-
result['licenses'] = licenses_raw['files'][0]['licenses']
72+
if 'files' in licenses_raw:
73+
result['licenses'] = licenses_raw['files'][0]['licenses']
7674

7775
return result
7876

@@ -103,8 +101,6 @@ def __analyze_scancode_cli(self, file_paths):
103101
output_json = json.loads(output_content)[1:]
104102
outputs_json.append(output_json)
105103
output_content = ''
106-
else:
107-
continue
108104
else:
109105
output_content += line
110106

tests/test_colic.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@
3333
from graal.backends.core.analyzers.scancode import ScanCode
3434
from graal.backends.core.colic import (CATEGORY_COLIC_NOMOS,
3535
CATEGORY_COLIC_SCANCODE,
36+
CATEGORY_COLIC_SCANCODE_CLI,
3637
NOMOS,
3738
SCANCODE,
39+
SCANCODE_CLI,
3840
CoLic,
3941
LicenseAnalyzer,
4042
CoLicCommand)
4143
from perceval.utils import DEFAULT_DATETIME
4244
from test_graal import TestCaseGraal
4345
from base_analyzer import (ANALYZER_TEST_FILE,
4446
TestCaseAnalyzer)
45-
from utils import NOMOS_PATH, SCANCODE_PATH
47+
from utils import NOMOS_PATH, SCANCODE_PATH, SCANCODE_CLI_PATH
4648

4749

4850
class TestCoLicBackend(TestCaseGraal):
@@ -122,7 +124,7 @@ def test_fetch_nomossa(self):
122124

123125
cl = CoLic('http://example.com', self.git_path, NOMOS_PATH, self.worktree_path,
124126
in_paths=['perceval/backends/core/github.py'])
125-
commits = [commit for commit in cl.fetch()]
127+
commits = [commit for commit in cl.fetch(category=CATEGORY_COLIC_NOMOS)]
126128

127129
self.assertEqual(len(commits), 1)
128130
self.assertFalse(os.path.exists(cl.worktreepath))
@@ -159,6 +161,36 @@ def test_fetch_scancode(self):
159161
self.assertFalse('parents' in commit['data'])
160162
self.assertFalse('refs' in commit['data'])
161163

164+
def test_fetch_scancode_cli(self):
165+
"""Test whether commits are properly processed"""
166+
167+
cl = CoLic('http://example.com', self.git_path, SCANCODE_CLI_PATH, self.worktree_path,
168+
in_paths=['perceval/backends/core/github.py'])
169+
commits = [commit for commit in cl.fetch(category=CATEGORY_COLIC_SCANCODE_CLI)]
170+
171+
self.assertEqual(len(commits), 1)
172+
self.assertFalse(os.path.exists(cl.worktreepath))
173+
174+
for commit in commits:
175+
self.assertEqual(commit['backend_name'], 'CoLic')
176+
self.assertEqual(commit['category'], CATEGORY_COLIC_SCANCODE_CLI)
177+
self.assertEqual(commit['data']['analysis']['files'][0]['file_path'],
178+
'perceval/backends/core/github.py')
179+
self.assertTrue('Author' in commit['data'])
180+
self.assertTrue('Commit' in commit['data'])
181+
self.assertFalse('files' in commit['data'])
182+
self.assertFalse('parents' in commit['data'])
183+
self.assertFalse('refs' in commit['data'])
184+
185+
def test_fetch_unknown(self):
186+
"""Test whether commits are properly processed"""
187+
188+
cl = CoLic('http://example.com', self.git_path, SCANCODE_CLI_PATH, self.worktree_path,
189+
in_paths=['perceval/backends/core/github.py'])
190+
191+
with self.assertRaises(GraalError):
192+
_ = cl.fetch(category="unknown")
193+
162194
def test_metadata_category(self):
163195
"""Test metadata_category"""
164196

@@ -186,6 +218,18 @@ def test_metadata_category(self):
186218
}
187219
self.assertEqual(CoLic.metadata_category(item), CATEGORY_COLIC_NOMOS)
188220

221+
item = {
222+
"Author": "Valerio Cosentino <[email protected]>",
223+
"AuthorDate": "Fri May 18 18:26:48 2018 +0200",
224+
"Commit": "Valerio Cosentino <[email protected]>",
225+
"CommitDate": "Fri May 18 18:26:48 2018 +0200",
226+
"analysis": [],
227+
"analyzer": "scancode_cli",
228+
"commit": "075f0c6161db5a3b1c8eca45e08b88469bb148b9",
229+
"message": "[perceval] first commit"
230+
}
231+
self.assertEqual(CoLic.metadata_category(item), CATEGORY_COLIC_SCANCODE_CLI)
232+
189233
item = {
190234
"Author": "Valerio Cosentino <[email protected]>",
191235
"AuthorDate": "Fri May 18 18:26:48 2018 +0200",
@@ -218,6 +262,10 @@ def test_init(self):
218262
self.assertIsInstance(license_analyzer, LicenseAnalyzer)
219263
self.assertIsInstance(license_analyzer.analyzer, ScanCode)
220264

265+
license_analyzer = LicenseAnalyzer(SCANCODE_CLI_PATH, SCANCODE_CLI)
266+
self.assertIsInstance(license_analyzer, LicenseAnalyzer)
267+
self.assertIsInstance(license_analyzer.analyzer, ScanCode)
268+
221269
with self.assertRaises(GraalError):
222270
_ = LicenseAnalyzer("/tmp/analyzer", SCANCODE)
223271

@@ -236,6 +284,12 @@ def test_analyze(self):
236284

237285
self.assertIn('licenses', analysis)
238286

287+
file_paths = [os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE)]
288+
license_analyzer = LicenseAnalyzer(SCANCODE_CLI_PATH, kind=SCANCODE_CLI)
289+
analysis = license_analyzer.analyze(file_paths)
290+
291+
self.assertIn('licenses', analysis['files'][0])
292+
239293

240294
class TestCoLicCommand(unittest.TestCase):
241295
"""CoLicCommand tests"""

tests/test_scancode.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
from graal.backends.core.analyzers.scancode import ScanCode
3333
from graal.graal import GraalError
34-
from utils import SCANCODE_PATH
34+
from utils import SCANCODE_PATH, SCANCODE_CLI_PATH
3535

3636

3737
class TestScanCode(TestCaseAnalyzer):
@@ -43,18 +43,38 @@ def test_init(self):
4343
scancode = ScanCode(exec_path=SCANCODE_PATH)
4444
self.assertEqual(scancode.exec_path, SCANCODE_PATH)
4545

46+
scancode_cli = ScanCode(exec_path=SCANCODE_CLI_PATH)
47+
self.assertEqual(scancode_cli.exec_path, SCANCODE_CLI_PATH)
48+
4649
with self.assertRaises(GraalError):
4750
_ = ScanCode("/tmp/invalid")
4851

49-
def test_analyze(self):
50-
"""Test whether nomos returns the expected fields data"""
52+
def test_analyze_scancode(self):
53+
"""Test whether scancode returns the expected fields data"""
5154

5255
scancode = ScanCode(exec_path=SCANCODE_PATH)
5356
kwargs = {'file_path': os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE)}
5457
result = scancode.analyze(**kwargs)
5558

5659
self.assertIn('licenses', result)
5760

61+
def test_analyze_scancode_cli(self):
62+
"""Test whether scancode_cli returns the expected fields data"""
63+
64+
scancode_cli = ScanCode(exec_path=SCANCODE_CLI_PATH, cli=True)
65+
kwargs = {'file_paths': [os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE)]}
66+
result = scancode_cli.analyze(**kwargs)
67+
68+
self.assertIn('licenses', result['files'][0])
69+
70+
def test_analyze_scancode_cli_error(self):
71+
"""Test whether an exception is thrown in case of error"""
72+
73+
scancode_cli = ScanCode(exec_path=SCANCODE_CLI_PATH, cli=True)
74+
kwargs = {'file_paths': os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE)}
75+
with self.assertRaises(GraalError):
76+
_ = scancode_cli.analyze(**kwargs)
77+
5878
@unittest.mock.patch('subprocess.check_output')
5979
def test_analyze_error(self, check_output_mock):
6080
"""Test whether an exception is thrown in case of errors"""

tests/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
#
2323

2424
NOMOS_PATH = "/home/travis/build/chaoss/grimoirelab-graal/exec/fossology/src/nomos/agent/nomossa"
25-
SCANCODE_PATH = "/home/travis/build/chaoss/grimoirelab-graal/exec/scancode-toolkit-3.0.0/scancode"
25+
SCANCODE_PATH = "/home/travis/build/chaoss/grimoirelab-graal/exec/scancode-toolkit/scancode"
26+
SCANCODE_CLI_PATH = "/home/travis/build/chaoss/grimoirelab-graal/exec/scancode-toolkit/etc/scripts/scancli.py"

0 commit comments

Comments
 (0)