Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_lint_all_empty_lint(self):
linter1 = functools.partial(linters.lint_command, 'l1', 'linter1',
['-f'], '^Line {lines}:')
linter2 = functools.partial(linters.lint_command, 'l2', 'linter2', [],
'^ line {lines}:')
config = {'.txt': [linter1, linter2]}
outputs = [b'', b'']
with mock.patch('subprocess.check_output',
side_effect=outputs) as check_output, \
mock.patch('os.path.getmtime', side_effect=[1, 0, 1, 0]):
filename = 'foo.txt'
self.assertEqual({
filename: {
'comments': []
}
}, linters.lint(filename, lines=[4, 5], config=config))
expected_calls = [
mock.call(
['linter1', '-f', 'foo.txt'], stderr=subprocess.STDOUT),
mock.call(['linter2', 'foo.txt'], stderr=subprocess.STDOUT)
]
self.assertEqual(expected_calls, check_output.call_args_list)
mock.patch('os.path.getmtime', side_effect=[1, 0, 1, 0]):
filename = 'foo.txt'
self.assertEqual({
filename: {
'comments': [
{
'line': 4,
'message': '4'
},
{
'line': 5,
'message': '5'
},
],
},
}, linters.lint(filename, lines=[4, 5], config=config))
expected_calls = [
mock.call(
['linter1', '-f', 'foo.txt'], stderr=subprocess.STDOUT),
mock.call(['linter2', 'foo.txt'], stderr=subprocess.STDOUT)
]
self.assertEqual(expected_calls, check_output.call_args_list)
def test_lint_two_missing_programs(self):
linter1 = functools.partial(linters.missing_requirements_command, 'l1',
['p1', 'p2'], 'Install p1 and p2')
config = {'.txt': [linter1, linter1]}
output = linters.lint('foo.txt', lines=[4, 5], config=config)
self.assertEqual(2, len(output['foo.txt']['skipped']))
output['foo.txt']['skipped'] = []
self.assertEqual({'foo.txt': {'skipped': []}}, output)
def test_lint_missing_programs(self):
linter1 = functools.partial(linters.missing_requirements_command, 'l1',
['p1', 'p2'], 'Install p1 and p2')
config = {'.txt': [linter1]}
output = linters.lint('foo.txt', lines=[4, 5], config=config)
self.assertEqual(1, len(output['foo.txt']['skipped']))
output['foo.txt']['skipped'] = []
self.assertEqual({'foo.txt': {'skipped': []}}, output)
'line': 4,
'column': 1,
'message': '4.b',
},
{
'line': 4,
'column': 10,
'message': '4.a',
},
{
'line': 5,
'message': '5',
},
],
},
}, linters.lint(filename, lines=None, config=config))
def test_lint_extension_not_defined(self):
config = {}
output = linters.lint('foo.txt', lines=[4, 5], config=config)
self.assertEqual(1, len(output['foo.txt']['skipped']))
output['foo.txt']['skipped'] = []
self.assertEqual({'foo.txt': {'skipped': []}}, output)
config = {'.txt': [linter1, linter2]}
outputs = [b'', os.linesep.join([' line 4: 4']).encode('utf-8')]
with mock.patch('subprocess.check_output',
side_effect=outputs) as check_output, \
mock.patch('os.path.getmtime', side_effect=[1, 0, 1, 0]):
filename = 'foo.txt'
self.assertEqual({
filename: {
'comments': [
{
'line': 4,
'message': '4'
},
],
},
}, linters.lint(filename, lines=[4, 5], config=config))
expected_calls = [
mock.call(
['linter1', '-f', 'foo.txt'], stderr=subprocess.STDOUT),
mock.call(['linter2', 'foo.txt'], stderr=subprocess.STDOUT)
]
self.assertEqual(expected_calls, check_output.call_args_list)
def process_file(vcs, commit, force, gitlint_config, file_data):
"""Lint the file
Returns:
The results from the linter.
"""
filename, extra_data = file_data
if force:
modified_lines = None
else:
modified_lines = vcs.modified_lines(
filename, extra_data, commit=commit)
result = linters.lint(filename, modified_lines, gitlint_config)
result = result[filename]
return filename, result