|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for the lint command. |
| 15 | +
|
| 16 | + For running all the tests, use (from the root of the project): |
| 17 | + python -m unittest discover -s cli/casp/src/casp/tests -p test_lint.py -v |
| 18 | +""" |
| 19 | + |
| 20 | +from pathlib import Path |
| 21 | +import subprocess |
| 22 | +import unittest |
| 23 | +from unittest.mock import patch |
| 24 | + |
| 25 | +from casp.commands import lint as lint_command |
| 26 | +from click.testing import CliRunner |
| 27 | + |
| 28 | + |
| 29 | +class LintCliTest(unittest.TestCase): |
| 30 | + """Tests for the lint command.""" |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + self.runner = CliRunner() |
| 34 | + self.mock_local_butler = self.enterContext( |
| 35 | + patch('casp.commands.lint.local_butler', autospec=True)) |
| 36 | + self.mock_subprocess_run = self.enterContext( |
| 37 | + patch('subprocess.run', autospec=True)) |
| 38 | + |
| 39 | + def test_lint_success_no_args(self): |
| 40 | + """Tests successful execution of `casp lint` without arguments.""" |
| 41 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 42 | + result = self.runner.invoke(lint_command.cli) |
| 43 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 44 | + self.mock_local_butler.build_command.assert_called_once_with('lint') |
| 45 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 46 | + |
| 47 | + def test_lint_success_with_path_arg(self): |
| 48 | + """Tests `casp lint <some_dir>` (positional argument).""" |
| 49 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 50 | + with self.runner.isolated_filesystem(): |
| 51 | + Path('test_dir').mkdir() |
| 52 | + result = self.runner.invoke(lint_command.cli, ['test_dir']) |
| 53 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 54 | + self.mock_local_butler.build_command.assert_called_once_with( |
| 55 | + 'lint', path='test_dir') |
| 56 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 57 | + |
| 58 | + def test_lint_success_with_path_option(self): |
| 59 | + """Tests `casp lint --path <some_dir>`.""" |
| 60 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 61 | + with self.runner.isolated_filesystem(): |
| 62 | + Path('test_dir').mkdir() |
| 63 | + result = self.runner.invoke(lint_command.cli, ['--path', 'test_dir']) |
| 64 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 65 | + self.mock_local_butler.build_command.assert_called_once_with( |
| 66 | + 'lint', path='test_dir') |
| 67 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 68 | + |
| 69 | + def test_lint_success_with_type_check(self): |
| 70 | + """Tests `casp lint --type-check`.""" |
| 71 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 72 | + result = self.runner.invoke(lint_command.cli, ['--type-check']) |
| 73 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 74 | + self.mock_local_butler.build_command.assert_called_once_with( |
| 75 | + 'lint', type_check=None) |
| 76 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 77 | + |
| 78 | + def test_lint_success_with_path_and_type_check(self): |
| 79 | + """Tests `casp lint <some_dir> --type-check`.""" |
| 80 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 81 | + with self.runner.isolated_filesystem(): |
| 82 | + Path('test_dir').mkdir() |
| 83 | + result = self.runner.invoke(lint_command.cli, |
| 84 | + ['test_dir', '--type-check']) |
| 85 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 86 | + self.mock_local_butler.build_command.assert_called_once_with( |
| 87 | + 'lint', path='test_dir', type_check=None) |
| 88 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 89 | + |
| 90 | + def test_butler_not_found(self): |
| 91 | + """Tests when `butler.py` is not found.""" |
| 92 | + self.mock_local_butler.build_command.side_effect = FileNotFoundError |
| 93 | + result = self.runner.invoke(lint_command.cli) |
| 94 | + self.assertNotEqual(0, result.exit_code) |
| 95 | + self.assertIn('butler.py not found', result.output) |
| 96 | + self.mock_subprocess_run.assert_not_called() |
| 97 | + |
| 98 | + def test_subprocess_run_fails(self): |
| 99 | + """Tests when `subprocess.run` fails.""" |
| 100 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 101 | + self.mock_subprocess_run.side_effect = subprocess.CalledProcessError( |
| 102 | + 1, 'cmd') |
| 103 | + result = self.runner.invoke(lint_command.cli) |
| 104 | + self.assertNotEqual(0, result.exit_code) |
| 105 | + self.assertIn('Error running butler.py lint', result.output) |
| 106 | + |
| 107 | + def test_python_not_found(self): |
| 108 | + """Tests when `python` command is not found.""" |
| 109 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 110 | + self.mock_subprocess_run.side_effect = FileNotFoundError |
| 111 | + result = self.runner.invoke(lint_command.cli) |
| 112 | + self.assertNotEqual(0, result.exit_code) |
| 113 | + self.assertIn('python not found in PATH', result.output) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + unittest.main() |
0 commit comments