Exemple #1
0
    def __init__(self, details=False, kind=PYLINT):
        self.details = details
        self.kind = kind

        if kind == PYLINT:
            self.analyzer = PyLint()
        else:
            self.analyzer = Flake8()
Exemple #2
0
    def test_analyze_error(self, check_output_mock):
        """Test whether an exception is thrown in case of errors"""

        check_output_mock.side_effect = subprocess.CalledProcessError(
            -1, "command", output=b'output')

        pylint = PyLint()
        kwargs = {
            'module_path': os.path.join(self.repo_path, ANALYZER_TEST_FILE),
            'details': False
        }
        with self.assertRaises(GraalError):
            _ = pylint.analyze(**kwargs)
Exemple #3
0
class ModuleAnalyzer:
    """Class to evaluate code quality in a Python project

    :params details: if enable, it returns fine-grained results
    :param kind: the analyzer kind (e.g., PYLINT, FLAKE8)
    """
    def __init__(self, details=False, kind=PYLINT):
        self.details = details
        self.kind = kind

        if kind == PYLINT:
            self.analyzer = PyLint()
        else:
            self.analyzer = Flake8()

    def analyze(self, module_path, worktree_path):
        """Analyze the content of a module

        :param module_path: module path
        :param worktree_path: worktree path

        :returns a dict containing the results of the analysis, like the one below
        {
          'warnings': [..]
        }
        """
        kwargs = {'module_path': module_path, 'details': self.details}
        if self.kind == FLAKE8:
            kwargs['worktree_path'] = worktree_path
        analysis = self.analyzer.analyze(**kwargs)

        return analysis
Exemple #4
0
    def test_analyze_no_details(self):
        """Test whether pylint returns the expected fields data"""

        pylint = PyLint()
        kwargs = {
            'module_path': os.path.join(self.repo_path, ANALYZER_TEST_FILE),
            'details': False
        }
        result = pylint.analyze(**kwargs)

        self.assertNotIn('modules', result)
        self.assertIn('quality', result)
        self.assertTrue(type(result['quality']), str)
        self.assertIn('num_modules', result)
        self.assertTrue(type(result['num_modules']), int)
        self.assertIn('warnings', result)
        self.assertTrue(type(result['warnings']), int)
Exemple #5
0
    def test_analyze_details(self):
        """Test whether pylint returns the expected fields data"""

        pylint = PyLint()
        kwargs = {
            'module_path': os.path.join(self.repo_path, "perceval"),
            'details': True
        }
        result = pylint.analyze(**kwargs)

        self.assertIn('quality', result)
        self.assertTrue(type(result['quality']), str)
        self.assertIn('num_modules', result)
        self.assertTrue(type(result['num_modules']), int)
        self.assertIn('warnings', result)
        self.assertTrue(type(result['warnings']), int)
        self.assertIn('modules', result)
        self.assertTrue(type(result['modules']), dict)

        first_key = list(result['modules'].keys())[0]
        for md in result['modules'].get(first_key):
            self.assertTrue(type(md), str)