Skip to content

Commit 3f8a84d

Browse files
committed
[tests] Add tests for repository level analysis via lizard
Signed-off-by: inishchith <[email protected]>
1 parent 3a59457 commit 3f8a84d

File tree

3 files changed

+132
-11
lines changed

3 files changed

+132
-11
lines changed

graal/backends/core/analyzers/lizard.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def __analyze_repository(self, repository_path, details):
100100
}
101101
analysis_result.append(result)
102102

103+
# TODO: implement details option
104+
103105
return analysis_result
104106

105107
def analyze(self, **kwargs):

tests/test_cocom.py

Lines changed: 107 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
import tempfile
2828
import unittest.mock
2929

30+
from graal.graal import GraalError
3031
from graal.graal import GraalCommandArgumentParser
3132
from graal.backends.core.analyzers.cloc import Cloc
3233
from graal.backends.core.analyzers.lizard import Lizard
33-
from graal.backends.core.cocom import (CATEGORY_COCOM,
34+
from graal.backends.core.cocom import (CATEGORY_COCOM_LIZARD_FILE,
35+
CATEGORY_COCOM_LIZARD_REPOSITORY,
3436
CoCom,
3537
FileAnalyzer,
38+
RepositoryAnalyzer,
3639
CoComCommand)
3740
from perceval.utils import DEFAULT_DATETIME
3841
from test_graal import TestCaseGraal
@@ -80,15 +83,13 @@ def test_initialization(self):
8083
self.assertEqual(cc.worktreepath, os.path.join(self.worktree_path, os.path.split(cc.gitpath)[1]))
8184
self.assertEqual(cc.origin, 'http://example.com')
8285
self.assertEqual(cc.tag, 'test')
83-
self.assertEqual(cc.file_analyzer.details, False)
8486

8587
cc = CoCom('http://example.com', self.git_path, self.worktree_path, details=True, tag='test')
8688
self.assertEqual(cc.uri, 'http://example.com')
8789
self.assertEqual(cc.gitpath, self.git_path)
8890
self.assertEqual(cc.worktreepath, os.path.join(self.worktree_path, os.path.split(cc.gitpath)[1]))
8991
self.assertEqual(cc.origin, 'http://example.com')
9092
self.assertEqual(cc.tag, 'test')
91-
self.assertEqual(cc.file_analyzer.details, True)
9293

9394
# When tag is empty or None it will be set to the value in uri
9495
cc = CoCom('http://example.com', self.git_path, self.worktree_path)
@@ -99,8 +100,8 @@ def test_initialization(self):
99100
self.assertEqual(cc.origin, 'http://example.com')
100101
self.assertEqual(cc.tag, 'http://example.com')
101102

102-
def test_fetch(self):
103-
"""Test whether commits are properly processed"""
103+
def test_fetch_lizard_file(self):
104+
"""Test whether commits are properly processed via file level"""
104105

105106
cc = CoCom('http://example.com', self.git_path, self.worktree_path, in_paths=['perceval/backends/core/github.py'])
106107
commits = [commit for commit in cc.fetch()]
@@ -110,7 +111,7 @@ def test_fetch(self):
110111

111112
for commit in commits:
112113
self.assertEqual(commit['backend_name'], 'CoCom')
113-
self.assertEqual(commit['category'], CATEGORY_COCOM)
114+
self.assertEqual(commit['category'], CATEGORY_COCOM_LIZARD_FILE)
114115
self.assertEqual(commit['data']['analysis'][0]['file_path'],
115116
'perceval/backends/core/github.py')
116117
self.assertTrue('Author' in commit['data'])
@@ -119,10 +120,36 @@ def test_fetch(self):
119120
self.assertFalse('parents' in commit['data'])
120121
self.assertFalse('refs' in commit['data'])
121122

123+
def test_fetch_lizard_repository(self):
124+
"""Test whether commits are properly processed via repository level"""
125+
126+
cc = CoCom('http://example.com', self.git_path, self.worktree_path)
127+
commits = [commit for commit in cc.fetch(category="code_complexity_lizard_repository")]
128+
129+
self.assertEqual(len(commits), 6)
130+
self.assertFalse(os.path.exists(cc.worktreepath))
131+
132+
for commit in commits:
133+
self.assertEqual(commit['backend_name'], 'CoCom')
134+
self.assertEqual(commit['category'], CATEGORY_COCOM_LIZARD_REPOSITORY)
135+
self.assertTrue('Author' in commit['data'])
136+
self.assertTrue('Commit' in commit['data'])
137+
self.assertFalse('files' in commit['data'])
138+
self.assertFalse('parents' in commit['data'])
139+
self.assertFalse('refs' in commit['data'])
140+
141+
def test_fetch_unknown(self):
142+
"""Test whether commits are properly processed"""
143+
144+
cc = CoCom('http://example.com', self.git_path, self.worktree_path)
145+
146+
with self.assertRaises(GraalError):
147+
_ = cc.fetch(category="unknown")
148+
122149
def test_fetch_analysis(self):
123150
"""Test whether commits have properly set values"""
124151

125-
cc = CoCom('http://example.com', self.git_path, self.worktree_path)
152+
cc = CoCom('http://example.com', self.git_path, self.worktree_path, details=True)
126153
commits = [commit for commit in cc.fetch()]
127154

128155
self.assertEqual(len(commits), 6)
@@ -141,6 +168,46 @@ def test_fetch_analysis(self):
141168
self.assertEqual(deleted_file_commit['data']['analysis'][0]['avg_tokens'], None)
142169
self.assertEqual(deleted_file_commit['data']['analysis'][0]['num_funs'], None)
143170
self.assertEqual(deleted_file_commit['data']['analysis'][0]['tokens'], None)
171+
self.assertEqual(deleted_file_commit['data']['analysis'][0]['funs'], [])
172+
173+
def test_metadata_category(self):
174+
"""Test metadata_category"""
175+
item = {
176+
"Author": "Nishchith Shetty <[email protected]>",
177+
"AuthorDate": "Tue Feb 26 22:06:31 2019 +0530",
178+
"Commit": "Nishchith Shetty <[email protected]>",
179+
"CommitDate": "Tue Feb 26 22:06:31 2019 +0530",
180+
"analysis": [],
181+
"analyzer": "lizard_file",
182+
"commit": "5866a479587e8b548b0cb2d591f3a3f5dab04443",
183+
"message": "[copyright] Update copyright dates"
184+
}
185+
self.assertEqual(CoCom.metadata_category(item), CATEGORY_COCOM_LIZARD_FILE)
186+
187+
item = {
188+
"Author": "Nishchith Shetty <[email protected]>",
189+
"AuthorDate": "Tue Feb 26 22:06:31 2019 +0530",
190+
"Commit": "Nishchith Shetty <[email protected]>",
191+
"CommitDate": "Tue Feb 26 22:06:31 2019 +0530",
192+
"analysis": [],
193+
"analyzer": "lizard_repository",
194+
"commit": "5866a479587e8b548b0cb2d591f3a3f5dab04443",
195+
"message": "[copyright] Update copyright dates"
196+
}
197+
self.assertEqual(CoCom.metadata_category(item), CATEGORY_COCOM_LIZARD_REPOSITORY)
198+
199+
item = {
200+
"Author": "Nishchith Shetty <[email protected]>",
201+
"AuthorDate": "Tue Feb 26 22:06:31 2019 +0530",
202+
"Commit": "Nishchith Shetty <[email protected]>",
203+
"CommitDate": "Tue Feb 26 22:06:31 2019 +0530",
204+
"analysis": [],
205+
"analyzer": "unknown",
206+
"commit": "5866a479587e8b548b0cb2d591f3a3f5dab04443",
207+
"message": "[copyright] Update copyright dates"
208+
}
209+
with self.assertRaises(GraalError):
210+
_ = CoCom.metadata_category(item)
144211

145212

146213
class TestFileAnalyzer(TestCaseAnalyzer):
@@ -206,6 +273,39 @@ def test_analyze_functions(self):
206273
self.assertIn('end', fd)
207274

208275

276+
class TestRepositoryAnalyzer(TestCaseAnalyzer):
277+
"""RepositoryAnalyzer tests"""
278+
279+
def test_init(self):
280+
"""Test initialization"""
281+
282+
repository_analyzer = RepositoryAnalyzer()
283+
284+
self.assertIsInstance(repository_analyzer, RepositoryAnalyzer)
285+
self.assertIsInstance(repository_analyzer.lizard, Lizard)
286+
self.assertFalse(repository_analyzer.details)
287+
288+
repository_analyzer = RepositoryAnalyzer(details=True)
289+
290+
self.assertIsInstance(repository_analyzer, RepositoryAnalyzer)
291+
self.assertIsInstance(repository_analyzer.lizard, Lizard)
292+
self.assertTrue(repository_analyzer.details)
293+
294+
def test_analyze(self):
295+
"""Test whether the analyze method works"""
296+
297+
repository_path = self.tmp_data_path
298+
repository_analyzer = RepositoryAnalyzer()
299+
analysis = repository_analyzer.analyze(repository_path)
300+
301+
file_analysis = analysis[0]
302+
303+
self.assertIn('num_funs', file_analysis)
304+
self.assertIn('ccn', file_analysis)
305+
self.assertIn('loc', file_analysis)
306+
self.assertIn('tokens', file_analysis)
307+
308+
209309
class TestCoComCommand(unittest.TestCase):
210310
"""CoComCommand tests"""
211311

tests/test_lizard.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
class TestLizard(TestCaseAnalyzer):
3434
"""Lizard tests"""
3535

36-
def test_analyze_no_details(self):
37-
"""Test whether lizard returns the expected fields data"""
36+
def test_analyze_file_no_details(self):
37+
"""Test whether lizard returns the expected fields data for files"""
3838

3939
lizard = Lizard()
4040
kwargs = {'file_path': os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE),
@@ -57,8 +57,8 @@ def test_analyze_no_details(self):
5757
self.assertIn('tokens', result)
5858
self.assertTrue(type(result['tokens']), int)
5959

60-
def test_analyze_details(self):
61-
"""Test whether lizard returns the expected fields data"""
60+
def test_analyze_file_details(self):
61+
"""Test whether lizard returns the expected fields data for files"""
6262

6363
lizard = Lizard()
6464
kwargs = {'file_path': os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE),
@@ -100,6 +100,25 @@ def test_analyze_details(self):
100100
self.assertIn('end', fd)
101101
self.assertTrue(type(fd['end']), int)
102102

103+
def test_analyze_repository(self):
104+
"""Test whether lizard returns the expected fields data for repository"""
105+
106+
lizard = Lizard()
107+
kwargs = {'repository_path': self.tmp_data_path,
108+
'repository_level': True,
109+
'details': False}
110+
result = lizard.analyze(**kwargs)
111+
result = result[0]
112+
113+
self.assertIn('ccn', result)
114+
self.assertTrue(type(result['ccn']), int)
115+
self.assertIn('num_funs', result)
116+
self.assertTrue(type(result['num_funs']), int)
117+
self.assertIn('loc', result)
118+
self.assertTrue(type(result['loc']), int)
119+
self.assertIn('tokens', result)
120+
self.assertTrue(type(result['tokens']), int)
121+
103122

104123
if __name__ == "__main__":
105124
unittest.main()

0 commit comments

Comments
 (0)