File size: 15,391 Bytes
88d205f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Unit tests for the Code Analyzer service
"""

import unittest
from unittest.mock import patch, MagicMock, mock_open
import os
import sys
import json
from pathlib import Path

# Add the project root directory to the Python path
project_root = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(project_root))

from src.services.code_analyzer import CodeAnalyzer


class TestCodeAnalyzer(unittest.TestCase):
    """Test cases for the CodeAnalyzer class"""

    def setUp(self):
        """Set up test fixtures"""
        self.analyzer = CodeAnalyzer()
        self.test_repo_path = "/test/repo"
    
    @patch('os.path.exists')
    @patch('subprocess.run')
    def test_analyze_python_code(self, mock_run, mock_exists):
        """Test analyze_python_code method"""
        # Set up the mocks
        mock_exists.return_value = True
        
        # Mock the subprocess.run result
        mock_process = MagicMock()
        mock_process.returncode = 0
        mock_process.stdout = json.dumps({
            "messages": [
                {
                    "type": "convention",
                    "module": "test_module",
                    "obj": "",
                    "line": 10,
                    "column": 0,
                    "path": "test.py",
                    "symbol": "missing-docstring",
                    "message": "Missing module docstring",
                    "message-id": "C0111"
                }
            ]
        })
        mock_run.return_value = mock_process
        
        # Mock the file discovery
        with patch.object(self.analyzer, '_find_files', return_value=['/test/repo/test.py']):
            # Call the method
            result = self.analyzer.analyze_python_code(self.test_repo_path)
            
            # Verify the result
            self.assertEqual(len(result['issues']), 1)
            self.assertEqual(result['issue_count'], 1)
            self.assertEqual(result['issues'][0]['type'], 'convention')
            self.assertEqual(result['issues'][0]['file'], 'test.py')
            self.assertEqual(result['issues'][0]['line'], 10)
            self.assertEqual(result['issues'][0]['message'], 'Missing module docstring')
    
    @patch('os.path.exists')
    @patch('subprocess.run')
    def test_analyze_javascript_code(self, mock_run, mock_exists):
        """Test analyze_javascript_code method"""
        # Set up the mocks
        mock_exists.return_value = True
        
        # Mock the subprocess.run result
        mock_process = MagicMock()
        mock_process.returncode = 0
        mock_process.stdout = json.dumps([
            {
                "filePath": "/test/repo/test.js",
                "messages": [
                    {
                        "ruleId": "semi",
                        "severity": 2,
                        "message": "Missing semicolon.",
                        "line": 5,
                        "column": 20,
                        "nodeType": "ExpressionStatement"
                    }
                ],
                "errorCount": 1,
                "warningCount": 0,
                "fixableErrorCount": 1,
                "fixableWarningCount": 0
            }
        ])
        mock_run.return_value = mock_process
        
        # Mock the file discovery
        with patch.object(self.analyzer, '_find_files', return_value=['/test/repo/test.js']):
            # Call the method
            result = self.analyzer.analyze_javascript_code(self.test_repo_path)
            
            # Verify the result
            self.assertEqual(len(result['issues']), 1)
            self.assertEqual(result['issue_count'], 1)
            self.assertEqual(result['issues'][0]['type'], 'error')
            self.assertEqual(result['issues'][0]['file'], 'test.js')
            self.assertEqual(result['issues'][0]['line'], 5)
            self.assertEqual(result['issues'][0]['message'], 'Missing semicolon.')
    
    @patch('os.path.exists')
    @patch('subprocess.run')
    def test_analyze_typescript_code(self, mock_run, mock_exists):
        """Test analyze_typescript_code method"""
        # Set up the mocks
        mock_exists.return_value = True
        
        # Mock the subprocess.run results
        # First for ESLint
        eslint_process = MagicMock()
        eslint_process.returncode = 0
        eslint_process.stdout = json.dumps([
            {
                "filePath": "/test/repo/test.ts",
                "messages": [
                    {
                        "ruleId": "@typescript-eslint/no-unused-vars",
                        "severity": 1,
                        "message": "'x' is defined but never used.",
                        "line": 3,
                        "column": 7,
                        "nodeType": "Identifier"
                    }
                ],
                "errorCount": 0,
                "warningCount": 1,
                "fixableErrorCount": 0,
                "fixableWarningCount": 0
            }
        ])
        
        # Then for TSC
        tsc_process = MagicMock()
        tsc_process.returncode = 2  # Error code for TypeScript compiler
        tsc_process.stderr = "test.ts(10,15): error TS2339: Property 'foo' does not exist on type 'Bar'."
        
        # Set up the mock to return different values on consecutive calls
        mock_run.side_effect = [eslint_process, tsc_process]
        
        # Mock the file discovery
        with patch.object(self.analyzer, '_find_files', return_value=['/test/repo/test.ts']):
            # Call the method
            result = self.analyzer.analyze_typescript_code(self.test_repo_path)
            
            # Verify the result
            self.assertEqual(len(result['issues']), 2)  # One from ESLint, one from TSC
            self.assertEqual(result['issue_count'], 2)
            
            # Check the ESLint issue
            eslint_issue = next(issue for issue in result['issues'] if issue['source'] == 'eslint')
            self.assertEqual(eslint_issue['type'], 'warning')
            self.assertEqual(eslint_issue['file'], 'test.ts')
            self.assertEqual(eslint_issue['line'], 3)
            self.assertEqual(eslint_issue['message'], "'x' is defined but never used.")
            
            # Check the TSC issue
            tsc_issue = next(issue for issue in result['issues'] if issue['source'] == 'tsc')
            self.assertEqual(tsc_issue['type'], 'error')
            self.assertEqual(tsc_issue['file'], 'test.ts')
            self.assertEqual(tsc_issue['line'], 10)
            self.assertEqual(tsc_issue['message'], "Property 'foo' does not exist on type 'Bar'.")
    
    @patch('os.path.exists')
    @patch('subprocess.run')
    def test_analyze_java_code(self, mock_run, mock_exists):
        """Test analyze_java_code method"""
        # Set up the mocks
        mock_exists.return_value = True
        
        # Mock the subprocess.run result
        mock_process = MagicMock()
        mock_process.returncode = 0
        mock_process.stdout = """
<?xml version="1.0" encoding="UTF-8"?>
<pmd version="6.55.0" timestamp="2023-06-01T12:00:00.000">
<file name="/test/repo/Test.java">
<violation beginline="10" endline="10" begincolumn="5" endcolumn="20" rule="UnusedLocalVariable" ruleset="Best Practices" class="Test" method="main" variable="unusedVar" externalInfoUrl="https://pmd.github.io/pmd-6.55.0/pmd_rules_java_bestpractices.html#unusedlocalvariable" priority="3">
Avoid unused local variables such as 'unusedVar'.
</violation>
</file>
</pmd>
        """
        mock_run.return_value = mock_process
        
        # Mock the file discovery
        with patch.object(self.analyzer, '_find_files', return_value=['/test/repo/Test.java']):
            # Call the method
            result = self.analyzer.analyze_java_code(self.test_repo_path)
            
            # Verify the result
            self.assertEqual(len(result['issues']), 1)
            self.assertEqual(result['issue_count'], 1)
            self.assertEqual(result['issues'][0]['type'], 'warning')  # Priority 3 maps to warning
            self.assertEqual(result['issues'][0]['file'], 'Test.java')
            self.assertEqual(result['issues'][0]['line'], 10)
            self.assertEqual(result['issues'][0]['message'], "Avoid unused local variables such as 'unusedVar'.")
    
    @patch('os.path.exists')
    @patch('subprocess.run')
    def test_analyze_go_code(self, mock_run, mock_exists):
        """Test analyze_go_code method"""
        # Set up the mocks
        mock_exists.return_value = True
        
        # Mock the subprocess.run result
        mock_process = MagicMock()
        mock_process.returncode = 0
        mock_process.stdout = json.dumps({
            "Issues": [
                {
                    "FromLinter": "gosimple",
                    "Text": "S1000: should use a simple channel send/receive instead of select with a single case",
                    "Pos": {
                        "Filename": "test.go",
                        "Line": 15,
                        "Column": 2
                    },
                    "Severity": "warning"
                }
            ]
        })
        mock_run.return_value = mock_process
        
        # Call the method
        result = self.analyzer.analyze_go_code(self.test_repo_path)
        
        # Verify the result
        self.assertEqual(len(result['issues']), 1)
        self.assertEqual(result['issue_count'], 1)
        self.assertEqual(result['issues'][0]['type'], 'warning')
        self.assertEqual(result['issues'][0]['file'], 'test.go')
        self.assertEqual(result['issues'][0]['line'], 15)
        self.assertEqual(result['issues'][0]['message'], 'S1000: should use a simple channel send/receive instead of select with a single case')
    
    @patch('os.path.exists')
    @patch('subprocess.run')
    def test_analyze_rust_code(self, mock_run, mock_exists):
        """Test analyze_rust_code method"""
        # Set up the mocks
        mock_exists.return_value = True
        
        # Mock the subprocess.run result
        mock_process = MagicMock()
        mock_process.returncode = 0
        mock_process.stdout = json.dumps({
            "reason": "compiler-message",
            "message": {
                "rendered": "warning: unused variable: `x`\n --> src/main.rs:2:9\n  |\n2 |     let x = 5;\n  |         ^ help: if this is intentional, prefix it with an underscore: `_x`\n  |\n  = note: `#[warn(unused_variables)]` on by default\n\n",
                "children": [],
                "code": {
                    "code": "unused_variables",
                    "explanation": null
                },
                "level": "warning",
                "message": "unused variable: `x`",
                "spans": [
                    {
                        "byte_end": 26,
                        "byte_start": 25,
                        "column_end": 10,
                        "column_start": 9,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": "help: if this is intentional, prefix it with an underscore: `_x`",
                        "line_end": 2,
                        "line_start": 2,
                        "suggested_replacement": "_x",
                        "suggestion_applicability": "MachineApplicable",
                        "text": [
                            {
                                "highlight_end": 10,
                                "highlight_start": 9,
                                "text": "    let x = 5;"
                            }
                        ]
                    }
                ]
            }
        })
        mock_run.return_value = mock_process
        
        # Call the method
        result = self.analyzer.analyze_rust_code(self.test_repo_path)
        
        # Verify the result
        self.assertEqual(len(result['issues']), 1)
        self.assertEqual(result['issue_count'], 1)
        self.assertEqual(result['issues'][0]['type'], 'warning')
        self.assertEqual(result['issues'][0]['file'], 'src/main.rs')
        self.assertEqual(result['issues'][0]['line'], 2)
        self.assertEqual(result['issues'][0]['message'], 'unused variable: `x`')
    
    def test_analyze_code(self):
        """Test analyze_code method"""
        # Mock the language-specific analysis methods
        self.analyzer.analyze_python_code = MagicMock(return_value={
            'issues': [{'type': 'convention', 'file': 'test.py', 'line': 10, 'message': 'Test issue'}],
            'issue_count': 1
        })
        self.analyzer.analyze_javascript_code = MagicMock(return_value={
            'issues': [{'type': 'error', 'file': 'test.js', 'line': 5, 'message': 'Test issue'}],
            'issue_count': 1
        })
        
        # Call the method
        result = self.analyzer.analyze_code(self.test_repo_path, ['Python', 'JavaScript'])
        
        # Verify the result
        self.assertEqual(len(result), 2)  # Two languages
        self.assertIn('Python', result)
        self.assertIn('JavaScript', result)
        self.assertEqual(result['Python']['issue_count'], 1)
        self.assertEqual(result['JavaScript']['issue_count'], 1)
        
        # Verify the method calls
        self.analyzer.analyze_python_code.assert_called_once_with(self.test_repo_path)
        self.analyzer.analyze_javascript_code.assert_called_once_with(self.test_repo_path)
    
    @patch('os.walk')
    def test_find_files(self, mock_walk):
        """Test _find_files method"""
        # Set up the mock
        mock_walk.return_value = [
            ('/test/repo', ['dir1'], ['file1.py', 'file2.js']),
            ('/test/repo/dir1', [], ['file3.py'])
        ]
        
        # Call the method
        python_files = self.analyzer._find_files(self.test_repo_path, '.py')
        
        # Verify the result
        self.assertEqual(len(python_files), 2)
        self.assertIn('/test/repo/file1.py', python_files)
        self.assertIn('/test/repo/dir1/file3.py', python_files)
    
    @patch('os.path.exists')
    def test_check_tool_availability(self, mock_exists):
        """Test _check_tool_availability method"""
        # Set up the mock
        mock_exists.side_effect = [True, False]  # First tool exists, second doesn't
        
        # Call the method
        result1 = self.analyzer._check_tool_availability('tool1')
        result2 = self.analyzer._check_tool_availability('tool2')
        
        # Verify the result
        self.assertTrue(result1)
        self.assertFalse(result2)
    
    @patch('subprocess.run')
    def test_run_command(self, mock_run):
        """Test _run_command method"""
        # Set up the mock
        mock_process = MagicMock()
        mock_process.returncode = 0
        mock_process.stdout = "Test output"
        mock_run.return_value = mock_process
        
        # Call the method
        returncode, output = self.analyzer._run_command(['test', 'command'])
        
        # Verify the result
        self.assertEqual(returncode, 0)
        self.assertEqual(output, "Test output")
        mock_run.assert_called_once()


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