コード例 #1
0
    def test_save_output_in_cache(self):
        output = 'Some content'
        with mock.patch('gitlint.utils._get_cache_filename',
                        return_value='/cache/filename.txt'):
            utils.save_output_in_cache('linter', 'filename', output)

            with open(utils._get_cache_filename('linter', 'filename')) as f:
                self.assertEqual(output, f.read())
コード例 #2
0
ファイル: test_utils.py プロジェクト: sk-/git-lint
    def test_save_output_in_cache(self):
        output = 'Some content'
        with mock.patch(
                'gitlint.utils._get_cache_filename',
                return_value='/cache/filename.txt'):
            utils.save_output_in_cache('linter', 'filename', output)

            with open(utils._get_cache_filename('linter', 'filename')) as f:
                self.assertEqual(output, f.read())
コード例 #3
0
ファイル: test_utils.py プロジェクト: stianvi/git-lint
 def test_save_output_in_cache(self):
     output = 'Some content'
     cache_filename = '/cache/filename.txt'
     mock_file = mock.MagicMock()
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename), \
          mock.patch('gitlint.utils._open_for_write',
                     mock.mock_open(mock_file)) as mock_open:
         utils.save_output_in_cache('linter', 'filename', output)
         mock_open.assert_called_once_with(cache_filename)
         mock_file().write.assert_called_once_with(output)
コード例 #4
0
    def test_save_output_in_cache(self):
        dummy_hash = utils.calculate_hash("some_tool", [])
        output = 'Some content'
        with mock.patch('gitlint.utils._get_cache_filename',
                        return_value='/cache/linter.%s/filename.txt' %
                        dummy_hash):
            utils.save_output_in_cache('linter', dummy_hash, 'filename',
                                       output)

            with open(
                    utils._get_cache_filename('linter', dummy_hash,
                                              'filename')) as f:
                self.assertEqual(output, f.read())
コード例 #5
0
def lint_command(name, program, arguments, fatal_exits, filter_regex, filename, lines):
    """Executes a lint program and filter the output.

    Executes the lint tool 'program' with arguments 'arguments' over the file
    'filename' returning only those lines matching the regular expression
    'filter_regex'.

    Args:
      name: string: the name of the linter.
      program: string: lint program.
      arguments: list[string]: extra arguments for the program.
      fatal_exits: list[int]: report error if linter exit code is in the list.
      filter_regex: string: regular expression to filter lines.
      filename: string: filename to lint.
      lines: list[int]|None: list of lines that we want to capture. If None,
        then all lines will be captured.

    Returns: dict: a dict with the extracted info from the message.
    """
    linter_hash = utils.calculate_hash(program, arguments)
    output = utils.get_output_from_cache(name, linter_hash, filename)

    if output is None:
        call_arguments = [program] + arguments + [filename]
        try:
            output = subprocess.check_output(
                call_arguments, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as error:
            if error.returncode in fatal_exits:
                return {
                    filename: {
                        'error': [('"%s" returned error code %i.%sOutput:%s%s') %
                                  (' '.join(call_arguments), error.returncode, os.linesep,
                                   error.output, os.linesep)]
                    }
                }
            else:
                output = error.output
        except OSError:
            return {
                filename: {
                    'error': [('Could not execute "%s".%sMake sure all ' +
                               'required programs are installed') %
                              (' '.join(call_arguments), os.linesep)]
                }
            }
        output = output.decode('utf-8')
        utils.save_output_in_cache(name, linter_hash, filename, output)

    output_lines = output.split(os.linesep)

    if lines is None:
        lines_regex = r'\d+'
    else:
        lines_regex = '|'.join(map(str, lines))
    lines_regex = '(%s)' % lines_regex

    groups = ('line', 'column', 'message', 'severity', 'message_id')
    filtered_lines = utils.filter_lines(
        output_lines,
        filter_regex.format(lines=lines_regex, filename=re.escape(filename)),
        groups=groups)

    result = []
    for data in filtered_lines:
        comment = dict(p for p in zip(groups, data) if p[1] is not None)
        if 'line' in comment:
            comment['line'] = int(comment['line'])
        if 'column' in comment:
            comment['column'] = int(comment['column'])
        if 'severity' in comment:
            comment['severity'] = comment['severity'].title()
        result.append(comment)

    return {filename: {'comments': result}}
コード例 #6
0
ファイル: linters.py プロジェクト: MyklClason/git-lint
def lint_command(name, program, arguments, filter_regex, filename, lines):
    """Executes a lint program and filter the output.

    Executes the lint tool 'program' with arguments 'arguments' over the file
    'filename' returning only those lines matching the regular expression
    'filter_regex'.

    Args:
      name: string: the name of the linter.
      program: string: lint program.
      arguments: list[string]: extra arguments for the program.
      filter_regex: string: regular expression to filter lines.
      filename: string: filename to lint.
      lines: list[int]|None: list of lines that we want to capture. If None,
        then all lines will be captured.

    Returns: dict: a dict with the extracted info from the message.
    """
    output = utils.get_output_from_cache(name, filename)

    if output is None:
        call_arguments = [program] + arguments + [filename]
        try:
            output = subprocess.check_output(call_arguments,
                                             stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as error:
            output = error.output
        except OSError:
            return {
                filename: {
                    'error': [('Could not execute "%s".%sMake sure all ' +
                               'required programs are installed') %
                              (' '.join(call_arguments), os.linesep)]
                }
            }
        output = output.decode('utf-8')
        utils.save_output_in_cache(name, filename, output)

    output_lines = output.split(os.linesep)

    if lines is None:
        lines_regex = r'\d+'
    else:
        lines_regex = '|'.join(map(str, lines))
    lines_regex = '(%s)' % lines_regex

    groups = ('line', 'column', 'message', 'severity', 'message_id')
    filtered_lines = utils.filter_lines(output_lines,
                                        filter_regex.format(lines=lines_regex,
                                                            filename=filename),
                                        groups=groups)

    result = []
    for data in filtered_lines:
        comment = dict(p for p in zip(groups, data) if p[1] is not None)
        if 'line' in comment:
            comment['line'] = int(comment['line'])
        if 'column' in comment:
            comment['column'] = int(comment['column'])
        if 'severity' in comment:
            comment['severity'] = comment['severity'].title()
        result.append(comment)

    return {
        filename: {
            'comments': result
        }
    }