Пример #1
0
 def test_get_output_from_cache_no_cache(self):
     dummy_hash = utils.calculate_hash("some_tool", [])
     cache_filename = '/cache/linter.%s/filename.txt' % dummy_hash
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename):
         self.assertIsNone(
             utils.get_output_from_cache('linter', dummy_hash, 'filename'))
Пример #2
0
 def test_get_output_from_cache_no_cache(self):
     cache_filename = '/cache/filename.txt'
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename), \
          mock.patch('os.path.exists', return_value=False):
         self.assertIsNone(
             utils.get_output_from_cache('linter', 'filename'))
Пример #3
0
 def test_get_output_from_cache_cache_is_expired(self):
     cache_filename = '/cache/filename.txt'
     self.fs.create_file(cache_filename)
     self.fs.create_file('filename')
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename):
         self.assertIsNone(
             utils.get_output_from_cache('linter', 'lint', {}, 'filename'))
Пример #4
0
 def test_get_output_from_cache_cache_is_expired(self):
     cache_filename = '/cache/filename.txt'
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename), \
          mock.patch('os.path.exists', return_value=True), \
          mock.patch('os.path.getmtime', side_effect=[2, 1]):
         self.assertIsNone(
             utils.get_output_from_cache('linter', 'filename'))
Пример #5
0
 def test_get_output_from_cache_cache_is_valid(self):
     cache_filename = '/cache/filename.txt'
     content = 'some_content'
     self.fs.create_file('filename')
     self.fs.create_file(cache_filename, contents=content)
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename):
         self.assertEqual(content,
                          utils.get_output_from_cache('linter', 'filename'))
Пример #6
0
 def test_get_output_from_cache_cache_is_expired(self):
     cache_filename = '/cache/filename.txt'
     self.fs.create_file(cache_filename)
     self.fs.create_file('filename')
     with mock.patch(
             'gitlint.utils._get_cache_filename',
             return_value=cache_filename):
         self.assertIsNone(
             utils.get_output_from_cache('linter', 'filename'))
Пример #7
0
 def test_get_output_from_cache_cache_is_valid(self):
     cache_filename = '/cache/filename.txt'
     content = 'some_content'
     self.fs.create_file('filename')
     self.fs.create_file(cache_filename, contents=content)
     with mock.patch(
             'gitlint.utils._get_cache_filename',
             return_value=cache_filename):
         self.assertEqual(content,
                          utils.get_output_from_cache('linter', 'filename'))
Пример #8
0
 def test_get_output_from_cache_cache_is_valid(self):
     dummy_hash = utils.calculate_hash("some_tool", [])
     cache_filename = '/cache/linter.%s/filename.txt' % dummy_hash
     content = 'some_content'
     self.fs.create_file('filename')
     self.fs.create_file(cache_filename, contents=content)
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename):
         self.assertEqual(
             content,
             utils.get_output_from_cache('linter', dummy_hash, 'filename'))
Пример #9
0
 def test_get_output_from_cache_cache_is_valid(self):
     cache_filename = '/cache/filename.txt'
     content = 'some_content'
     with mock.patch('gitlint.utils._get_cache_filename',
                     return_value=cache_filename), \
          mock.patch('os.path.exists', return_value=True), \
          mock.patch('os.path.getmtime', side_effect=[1, 2]), \
          mock.patch('io.open',
                     mock.mock_open(read_data=content),
                     create=True) as mock_open:
         self.assertEqual(
             content, utils.get_output_from_cache('linter', 'filename'))
         mock_open.assert_called_once_with(cache_filename)
Пример #10
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}}
Пример #11
0
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
        }
    }