Skip to content

Commit 4edb2a4

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 4edb2a4

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

tests/test_colic.py

Lines changed: 46 additions & 1 deletion
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):
@@ -159,6 +161,27 @@ 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+
162185
def test_metadata_category(self):
163186
"""Test metadata_category"""
164187

@@ -186,6 +209,18 @@ def test_metadata_category(self):
186209
}
187210
self.assertEqual(CoLic.metadata_category(item), CATEGORY_COLIC_NOMOS)
188211

212+
item = {
213+
"Author": "Valerio Cosentino <[email protected]>",
214+
"AuthorDate": "Fri May 18 18:26:48 2018 +0200",
215+
"Commit": "Valerio Cosentino <[email protected]>",
216+
"CommitDate": "Fri May 18 18:26:48 2018 +0200",
217+
"analysis": [],
218+
"analyzer": "scancode_cli",
219+
"commit": "075f0c6161db5a3b1c8eca45e08b88469bb148b9",
220+
"message": "[perceval] first commit"
221+
}
222+
self.assertEqual(CoLic.metadata_category(item), CATEGORY_COLIC_SCANCODE_CLI)
223+
189224
item = {
190225
"Author": "Valerio Cosentino <[email protected]>",
191226
"AuthorDate": "Fri May 18 18:26:48 2018 +0200",
@@ -218,6 +253,10 @@ def test_init(self):
218253
self.assertIsInstance(license_analyzer, LicenseAnalyzer)
219254
self.assertIsInstance(license_analyzer.analyzer, ScanCode)
220255

256+
license_analyzer = LicenseAnalyzer(SCANCODE_CLI_PATH, SCANCODE_CLI)
257+
self.assertIsInstance(license_analyzer, LicenseAnalyzer)
258+
self.assertIsInstance(license_analyzer.analyzer, ScanCode)
259+
221260
with self.assertRaises(GraalError):
222261
_ = LicenseAnalyzer("/tmp/analyzer", SCANCODE)
223262

@@ -236,6 +275,12 @@ def test_analyze(self):
236275

237276
self.assertIn('licenses', analysis)
238277

278+
file_paths = [os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE)]
279+
license_analyzer = LicenseAnalyzer(SCANCODE_CLI_PATH, kind=SCANCODE_CLI)
280+
analysis = license_analyzer.analyze(file_paths)
281+
282+
self.assertIn('licenses', analysis)
283+
239284

240285
class TestCoLicCommand(unittest.TestCase):
241286
"""CoLicCommand tests"""

tests/test_scancode.py

Lines changed: 15 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,30 @@ 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)
69+
5870
@unittest.mock.patch('subprocess.check_output')
5971
def test_analyze_error(self, check_output_mock):
6072
"""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)