|
| 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 format 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_format.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 format as format_command |
| 26 | +from click.testing import CliRunner |
| 27 | + |
| 28 | + |
| 29 | +class FormatCliTest(unittest.TestCase): |
| 30 | + """Tests for the format command.""" |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + self.runner = CliRunner() |
| 34 | + self.mock_local_butler = self.enterContext( |
| 35 | + patch('casp.commands.format.local_butler', autospec=True)) |
| 36 | + self.mock_subprocess_run = self.enterContext( |
| 37 | + patch('subprocess.run', autospec=True)) |
| 38 | + |
| 39 | + def test_format_success_no_dir(self): |
| 40 | + """Tests successful execution of `casp format` without a directory.""" |
| 41 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 42 | + result = self.runner.invoke(format_command.cli) |
| 43 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 44 | + self.mock_local_butler.build_command.assert_called_once_with('format') |
| 45 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 46 | + |
| 47 | + def test_format_success_with_path_option(self): |
| 48 | + """Tests `casp format --path <some_dir>`.""" |
| 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(format_command.cli, ['--path', 'test_dir']) |
| 53 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 54 | + self.mock_local_butler.build_command.assert_called_once_with( |
| 55 | + 'format', path='test_dir') |
| 56 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 57 | + |
| 58 | + def test_format_success_with_path(self): |
| 59 | + """Tests `casp format <some_dir>` (positional argument).""" |
| 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(format_command.cli, ['test_dir']) |
| 64 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 65 | + self.mock_local_butler.build_command.assert_called_once_with( |
| 66 | + 'format', path='test_dir') |
| 67 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 68 | + |
| 69 | + def test_butler_not_found(self): |
| 70 | + """Tests when `butler.py` is not found.""" |
| 71 | + self.mock_local_butler.build_command.side_effect = FileNotFoundError |
| 72 | + result = self.runner.invoke(format_command.cli) |
| 73 | + self.assertNotEqual(0, result.exit_code) |
| 74 | + self.assertIn('butler.py not found', result.output) |
| 75 | + self.mock_subprocess_run.assert_not_called() |
| 76 | + |
| 77 | + def test_subprocess_run_fails(self): |
| 78 | + """Tests when `subprocess.run` fails.""" |
| 79 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 80 | + self.mock_subprocess_run.side_effect = subprocess.CalledProcessError( |
| 81 | + 1, 'cmd') |
| 82 | + result = self.runner.invoke(format_command.cli) |
| 83 | + self.assertNotEqual(0, result.exit_code) |
| 84 | + self.assertIn('Error running butler.py format', result.output) |
| 85 | + |
| 86 | + def test_python_not_found(self): |
| 87 | + """Tests when `python` command is not found.""" |
| 88 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 89 | + self.mock_subprocess_run.side_effect = FileNotFoundError |
| 90 | + result = self.runner.invoke(format_command.cli) |
| 91 | + self.assertNotEqual(0, result.exit_code) |
| 92 | + self.assertIn('python not found in PATH', result.output) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + unittest.main() |
0 commit comments