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_main_file_changed_and_still_valid_with_commit(self):
lint_response = {
self.filename: {
'comments': []
}
}
self.lint.return_value = lint_response
self.assertEqual(
0,
gitlint.main(
['git-lint', '--last-commit'], stdout=self.stdout, stderr=None))
self.assertIn('OK', self.stdout.getvalue())
self.assert_mocked_calls(commit='abcd' * 10)
def test_main_file_with_skipped_and_error(self):
lint_response = {
self.filename: {
'skipped': ['skipped1'],
'error': ['error1'],
'comments': []
}
}
self.lint.return_value = lint_response
self.assertEqual(4,
gitlint.main([], stdout=self.stdout, stderr=None))
self.assertNotIn('OK', self.stdout.getvalue())
self.assertIn('skipped1', self.stdout.getvalue())
self.assertIn('error1', self.stdout.getvalue())
self.assert_mocked_calls()
def test_main_file_changed_and_still_valid(self):
lint_response = {
self.filename: {
'comments': []
}
}
self.lint.return_value = lint_response
self.assertEqual(
0, gitlint.main([], stdout=self.stdout, stderr=None))
self.assertIn('OK', self.stdout.getvalue())
self.assert_mocked_calls()
def test_main_with_invalid_files(self):
with mock.patch('gitlint.find_invalid_filenames',
return_value=[('foo.txt', 'does not exist')]):
self.assertEqual(2,
gitlint.main(['git-lint', 'foo.txt'],
stdout=None, stderr=self.stderr))
self.assertIn('does not exist', self.stderr.getvalue())
def lint():
"""Returns the response and ouput of git-lint."""
out = io.StringIO()
response = gitlint.main([], stdout=out, stderr=out)
return response, out.getvalue()
def test_main_file_changed_and_now_invalid(self):
lint_response = {
self.filename: {
'comments': [
{
'line': 3,
'message': 'error'
}
]
}
}
self.lint.return_value = lint_response
self.assertEqual(1,
gitlint.main([], stdout=self.stdout, stderr=None))
self.assertIn('line 3: error', self.stdout.getvalue())
self.assert_mocked_calls()
def test_main_not_in_repo(self):
self.git_repository_root.return_value = None
self.assertEqual(
128, gitlint.main([], stdout=None, stderr=self.stderr))
self.assertIn('Not a git repository', self.stderr.getvalue())
self.assertEqual(1,
gitlint.main(['git-lint', '--force'],
stdout=self.stdout, stderr=None))
self.assertIn('line 3: error', self.stdout.getvalue())
self.git_modified_files.assert_called_once_with(
self.root, tracked_only=False, commit=None)
self.lint.assert_called_once_with(
self.filename, None, self.git_lint_config)
self.reset_mock_calls()
self.stdout = io.StringIO()
self.assertEqual(1,
gitlint.main(['git-lint', '-f'],
stdout=self.stdout, stderr=None))
self.assertIn('line 3: error', self.stdout.getvalue())
self.git_modified_files.assert_called_once_with(
self.root, tracked_only=False, commit=None)
self.lint.assert_called_once_with(
self.filename, None, self.git_lint_config)
def test_main_with_valid_files_relative(self):
lint_response = {
self.filename: {
'comments': []
},
self.filename2: {
'comments': []
},
}
self.lint.return_value = lint_response
with mock.patch('gitlint.find_invalid_filenames', return_value=[]), \
mock.patch('os.getcwd', return_value=self.root):
self.assertEqual(
0, gitlint.main(['git-lint', 'bar/../changed.py', './foo.txt'],
stdout=self.stdout, stderr=self.stderr))
self.assertIn('OK', self.stdout.getvalue())
self.assertEqual('', self.stderr.getvalue())
self.git_modified_files.assert_called_once_with(
self.root, tracked_only=False, commit=None)
expected_calls = [mock.call(self.filename, ' M', commit=None),
mock.call(self.filename2, None, commit=None)]
self.assertEqual(expected_calls,
self.git_modified_lines.call_args_list)
expected_calls = [
mock.call(self.filename,
[3, 14],
self.git_lint_config),
mock.call(self.filename2,
[3, 14],
def test_main_file_changed_and_still_valid_tracked_only(self):
lint_response = {
self.filename: {
'comments': []
}
}
self.lint.return_value = lint_response
self.assertEqual(
0,
gitlint.main(['git-lint', '-t'], stdout=self.stdout, stderr=None))
self.assertIn('OK', self.stdout.getvalue())
self.assert_mocked_calls(tracked_only=True)
self.reset_mock_calls()
self.stdout = io.StringIO()
self.stderr = io.StringIO()
self.assertEqual(0, gitlint.main(['git-lint', '--tracked'],
stdout=self.stdout, stderr=None))
self.assertIn('OK', self.stdout.getvalue())
self.assert_mocked_calls(tracked_only=True)