|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# ----------------------------------------------------------------------------- |
| 6 | + |
| 7 | +import unittest |
| 8 | +from unittest import mock |
| 9 | +import os |
| 10 | + |
| 11 | +from knack.util import CLIError, CommandResultItem |
| 12 | + |
| 13 | +from azdev.operations.latest_index import generate_latest_index, verify_latest_index |
| 14 | + |
| 15 | + |
| 16 | +class LatestIndexTestCase(unittest.TestCase): |
| 17 | + |
| 18 | + @mock.patch('azdev.operations.latest_index.py_cmd') |
| 19 | + @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) |
| 20 | + @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) |
| 21 | + def test_generate_with_explicit_repo_path(self, _, __, mock_py_cmd): |
| 22 | + mock_py_cmd.return_value = CommandResultItem('generated', exit_code=0, error=None) |
| 23 | + |
| 24 | + generate_latest_index(cli_path='/fake/azure-cli') |
| 25 | + |
| 26 | + self.assertTrue(mock_py_cmd.called) |
| 27 | + command = mock_py_cmd.call_args.args[0] |
| 28 | + self.assertIn('generate_latest_indices.py generate', command) |
| 29 | + self.assertEqual(os.path.abspath('/fake/azure-cli'), mock_py_cmd.call_args.kwargs['cwd']) |
| 30 | + self.assertFalse(mock_py_cmd.call_args.kwargs['is_module']) |
| 31 | + |
| 32 | + @mock.patch('azdev.operations.latest_index.py_cmd') |
| 33 | + @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) |
| 34 | + @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) |
| 35 | + @mock.patch('azdev.operations.latest_index.get_cli_repo_path', return_value='/configured/azure-cli') |
| 36 | + def test_verify_uses_configured_repo_path(self, _, __, ___, mock_py_cmd): |
| 37 | + mock_py_cmd.return_value = CommandResultItem('verified', exit_code=0, error=None) |
| 38 | + |
| 39 | + verify_latest_index() |
| 40 | + |
| 41 | + self.assertEqual('/configured/azure-cli', mock_py_cmd.call_args.kwargs['cwd']) |
| 42 | + |
| 43 | + @mock.patch('azdev.operations.latest_index.py_cmd') |
| 44 | + @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) |
| 45 | + @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) |
| 46 | + def test_verify_non_zero_exit_is_propagated(self, _, __, mock_py_cmd): |
| 47 | + # simulate bytes output as returned by py_cmd on failure |
| 48 | + mock_py_cmd.return_value = CommandResultItem(b'stale\r\n', exit_code=1, error='mismatch') |
| 49 | + |
| 50 | + with self.assertRaises(SystemExit) as ex: |
| 51 | + verify_latest_index(cli_path='/fake/azure-cli') |
| 52 | + |
| 53 | + self.assertEqual(1, ex.exception.code) |
| 54 | + |
| 55 | + def test_non_latest_profile_is_rejected(self): |
| 56 | + with self.assertRaises(CLIError): |
| 57 | + generate_latest_index(cli_path='/fake/azure-cli', profile='2019-03-01-hybrid') |
| 58 | + |
| 59 | + def test_all_profiles_flag_is_rejected(self): |
| 60 | + with self.assertRaises(CLIError): |
| 61 | + verify_latest_index(cli_path='/fake/azure-cli', all_profiles=True) |
| 62 | + |
| 63 | + @mock.patch('azdev.operations.latest_index.display') |
| 64 | + @mock.patch('azdev.operations.latest_index.py_cmd') |
| 65 | + @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) |
| 66 | + @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) |
| 67 | + def test_bytes_output_decoded_and_hint_replaced(self, _, __, mock_py_cmd, mock_display): |
| 68 | + raw = b'files are out of date\r\nRun:\r\n python scripts/generate_latest_indices.py generate\r\n' |
| 69 | + mock_py_cmd.return_value = CommandResultItem(raw, exit_code=1, error='mismatch') |
| 70 | + |
| 71 | + with self.assertRaises(SystemExit): |
| 72 | + verify_latest_index(cli_path='/fake/azure-cli') |
| 73 | + |
| 74 | + displayed = mock_display.call_args.args[0] |
| 75 | + self.assertNotIn("b'", displayed) |
| 76 | + self.assertNotIn('\\r\\n', displayed) |
| 77 | + self.assertNotIn('python scripts/generate_latest_indices.py generate', displayed) |
| 78 | + self.assertIn('azdev latest-index generate', displayed) |
0 commit comments