|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +from unittest import mock, TestCase |
| 7 | + |
| 8 | +from azure.cli.core.mock import DummyCli |
| 9 | +from azure.cli.core.util import run_cmd |
| 10 | + |
| 11 | + |
| 12 | +class ArgcompleteScenarioTest(TestCase): |
| 13 | + |
| 14 | + def argcomplete_env(self, comp_line, comp_point): |
| 15 | + return { |
| 16 | + 'COMP_LINE': comp_line, |
| 17 | + 'COMP_POINT': comp_point, |
| 18 | + '_ARGCOMPLETE': '1', |
| 19 | + '_ARGCOMPLETE_IFS': ' ', |
| 20 | + '_ARGCOMPLETE_SUPPRESS_SPACE': '0', |
| 21 | + 'ARGCOMPLETE_USE_TEMPFILES': '1', |
| 22 | + '_ARGCOMPLETE_STDOUT_FILENAME': './argcomplete.out', } |
| 23 | + |
| 24 | + def test_completion(self): |
| 25 | + import os |
| 26 | + import sys |
| 27 | + |
| 28 | + from azure.cli.core.parser import AzCompletionFinder |
| 29 | + |
| 30 | + if sys.platform == 'win32': |
| 31 | + self.skipTest('Skip argcomplete test on Windows') |
| 32 | + |
| 33 | + run_cmd(['az'], env=self.argcomplete_env('az account sh', '13')) |
| 34 | + with open('argcomplete.out') as f: |
| 35 | + self.assertEqual(f.read(), 'show ') |
| 36 | + os.remove('argcomplete.out') |
| 37 | + |
| 38 | + run_cmd(['az'], capture_output=True, env=self.argcomplete_env('az account s', '12')) |
| 39 | + with open('argcomplete.out') as f: |
| 40 | + self.assertEqual(f.read(), 'set show') |
| 41 | + os.remove('argcomplete.out') |
| 42 | + |
| 43 | + run_cmd(['az'], env=self.argcomplete_env('az account szzz', '15')) |
| 44 | + with open('argcomplete.out') as f: |
| 45 | + self.assertFalse(f.read()) |
| 46 | + os.remove('argcomplete.out') |
| 47 | + |
| 48 | + class MockCompletionFinder(AzCompletionFinder): |
| 49 | + def __call__(self, *args, **kwargs): |
| 50 | + import sys |
| 51 | + return super().__call__(*args, exit_method=sys.exit, **kwargs) |
| 52 | + |
| 53 | + def dummy_completor(*args, **kwargs): |
| 54 | + return ['dummystorage'] |
| 55 | + |
| 56 | + cli = DummyCli() |
| 57 | + # argcomplete uses os._exit to exit, which is also used by pytest. Patch AzCompletionFinder to use sys.exit |
| 58 | + # There is no recording for this test case, as completer is mocked. |
| 59 | + with mock.patch('azure.cli.core.parser.AzCompletionFinder', MockCompletionFinder): |
| 60 | + with mock.patch('azure.cli.core.commands.parameters.get_resource_name_completion_list', lambda _: dummy_completor): |
| 61 | + env = self.argcomplete_env('az storage blob list -c test --account-name dumm', '48') |
| 62 | + with mock.patch.dict(os.environ, env): |
| 63 | + self.assertRaises(SystemExit, cli.invoke, ['az']) |
| 64 | + with open('argcomplete.out') as f: |
| 65 | + self.assertEqual(f.read(), 'dummystorage ') |
| 66 | + os.remove('argcomplete.out') |
0 commit comments