27
27
import tempfile
28
28
import unittest .mock
29
29
30
+ from graal .graal import GraalError
30
31
from graal .graal import GraalCommandArgumentParser
31
32
from graal .backends .core .analyzers .cloc import Cloc
32
33
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 ,
34
36
CoCom ,
35
37
FileAnalyzer ,
38
+ RepositoryAnalyzer ,
36
39
CoComCommand )
37
40
from perceval .utils import DEFAULT_DATETIME
38
41
from test_graal import TestCaseGraal
@@ -80,15 +83,13 @@ def test_initialization(self):
80
83
self .assertEqual (cc .worktreepath , os .path .join (self .worktree_path , os .path .split (cc .gitpath )[1 ]))
81
84
self .assertEqual (cc .origin , 'http://example.com' )
82
85
self .assertEqual (cc .tag , 'test' )
83
- self .assertEqual (cc .file_analyzer .details , False )
84
86
85
87
cc = CoCom ('http://example.com' , self .git_path , self .worktree_path , details = True , tag = 'test' )
86
88
self .assertEqual (cc .uri , 'http://example.com' )
87
89
self .assertEqual (cc .gitpath , self .git_path )
88
90
self .assertEqual (cc .worktreepath , os .path .join (self .worktree_path , os .path .split (cc .gitpath )[1 ]))
89
91
self .assertEqual (cc .origin , 'http://example.com' )
90
92
self .assertEqual (cc .tag , 'test' )
91
- self .assertEqual (cc .file_analyzer .details , True )
92
93
93
94
# When tag is empty or None it will be set to the value in uri
94
95
cc = CoCom ('http://example.com' , self .git_path , self .worktree_path )
@@ -99,8 +100,8 @@ def test_initialization(self):
99
100
self .assertEqual (cc .origin , 'http://example.com' )
100
101
self .assertEqual (cc .tag , 'http://example.com' )
101
102
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 """
104
105
105
106
cc = CoCom ('http://example.com' , self .git_path , self .worktree_path , in_paths = ['perceval/backends/core/github.py' ])
106
107
commits = [commit for commit in cc .fetch ()]
@@ -110,7 +111,7 @@ def test_fetch(self):
110
111
111
112
for commit in commits :
112
113
self .assertEqual (commit ['backend_name' ], 'CoCom' )
113
- self .assertEqual (commit ['category' ], CATEGORY_COCOM )
114
+ self .assertEqual (commit ['category' ], CATEGORY_COCOM_LIZARD_FILE )
114
115
self .assertEqual (commit ['data' ]['analysis' ][0 ]['file_path' ],
115
116
'perceval/backends/core/github.py' )
116
117
self .assertTrue ('Author' in commit ['data' ])
@@ -119,10 +120,36 @@ def test_fetch(self):
119
120
self .assertFalse ('parents' in commit ['data' ])
120
121
self .assertFalse ('refs' in commit ['data' ])
121
122
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
+
122
149
def test_fetch_analysis (self ):
123
150
"""Test whether commits have properly set values"""
124
151
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 )
126
153
commits = [commit for commit in cc .fetch ()]
127
154
128
155
self .assertEqual (len (commits ), 6 )
@@ -141,6 +168,46 @@ def test_fetch_analysis(self):
141
168
self .assertEqual (deleted_file_commit ['data' ]['analysis' ][0 ]['avg_tokens' ], None )
142
169
self .assertEqual (deleted_file_commit ['data' ]['analysis' ][0 ]['num_funs' ], None )
143
170
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 )
144
211
145
212
146
213
class TestFileAnalyzer (TestCaseAnalyzer ):
@@ -206,6 +273,39 @@ def test_analyze_functions(self):
206
273
self .assertIn ('end' , fd )
207
274
208
275
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
+
209
309
class TestCoComCommand (unittest .TestCase ):
210
310
"""CoComCommand tests"""
211
311
0 commit comments