|
| 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 bootstrap 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 bootstrap_test.py -v |
| 18 | +""" |
| 19 | + |
| 20 | +import subprocess |
| 21 | +import unittest |
| 22 | +from unittest.mock import patch |
| 23 | + |
| 24 | +from casp.commands import bootstrap |
| 25 | +from click.testing import CliRunner |
| 26 | + |
| 27 | + |
| 28 | +class BootstrapCliTest(unittest.TestCase): |
| 29 | + """Tests for the bootstrap command.""" |
| 30 | + |
| 31 | + def setUp(self): |
| 32 | + self.runner = CliRunner() |
| 33 | + self.mock_local_butler = self.enterContext( |
| 34 | + patch('casp.commands.bootstrap.local_butler', autospec=True)) |
| 35 | + self.mock_subprocess_run = self.enterContext( |
| 36 | + patch('subprocess.run', autospec=True)) |
| 37 | + |
| 38 | + def test_bootstrap_success(self): |
| 39 | + """Tests successful execution of `casp bootstrap`.""" |
| 40 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 41 | + result = self.runner.invoke(bootstrap.cli) |
| 42 | + self.assertEqual(0, result.exit_code, msg=result.output) |
| 43 | + self.mock_local_butler.build_command.assert_called_once_with( |
| 44 | + 'bootstrap', None) |
| 45 | + self.mock_subprocess_run.assert_called_once_with(['cmd'], check=True) |
| 46 | + |
| 47 | + def test_butler_not_found(self): |
| 48 | + """Tests when `butler.py` is not found.""" |
| 49 | + self.mock_local_butler.build_command.side_effect = FileNotFoundError |
| 50 | + result = self.runner.invoke(bootstrap.cli) |
| 51 | + self.assertNotEqual(0, result.exit_code) |
| 52 | + self.assertIn('butler.py not found', result.output) |
| 53 | + self.mock_subprocess_run.assert_not_called() |
| 54 | + |
| 55 | + def test_subprocess_run_fails(self): |
| 56 | + """Tests when `subprocess.run` fails.""" |
| 57 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 58 | + self.mock_subprocess_run.side_effect = subprocess.CalledProcessError( |
| 59 | + 1, 'cmd') |
| 60 | + result = self.runner.invoke(bootstrap.cli) |
| 61 | + self.assertNotEqual(0, result.exit_code) |
| 62 | + self.assertIn('Error running butler.py bootstrap', result.output) |
| 63 | + |
| 64 | + def test_python_not_found(self): |
| 65 | + """Tests when `python` command is not found.""" |
| 66 | + self.mock_local_butler.build_command.return_value = ['cmd'] |
| 67 | + self.mock_subprocess_run.side_effect = FileNotFoundError |
| 68 | + result = self.runner.invoke(bootstrap.cli) |
| 69 | + self.assertNotEqual(0, result.exit_code) |
| 70 | + self.assertIn('python not found in PATH', result.output) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + unittest.main() |
0 commit comments