diff --git a/src/azure-cli-core/azure/cli/core/tests/test_argcomplete.py b/src/azure-cli-core/azure/cli/core/tests/test_argcomplete.py new file mode 100644 index 00000000000..52a24af9b3c --- /dev/null +++ b/src/azure-cli-core/azure/cli/core/tests/test_argcomplete.py @@ -0,0 +1,66 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- + +from unittest import mock, TestCase + +from azure.cli.core.mock import DummyCli +from azure.cli.core.util import run_cmd + + +class ArgcompleteScenarioTest(TestCase): + + def argcomplete_env(self, comp_line, comp_point): + return { + 'COMP_LINE': comp_line, + 'COMP_POINT': comp_point, + '_ARGCOMPLETE': '1', + '_ARGCOMPLETE_IFS': ' ', + '_ARGCOMPLETE_SUPPRESS_SPACE': '0', + 'ARGCOMPLETE_USE_TEMPFILES': '1', + '_ARGCOMPLETE_STDOUT_FILENAME': './argcomplete.out', } + + def test_completion(self): + import os + import sys + + from azure.cli.core.parser import AzCompletionFinder + + if sys.platform == 'win32': + self.skipTest('Skip argcomplete test on Windows') + + run_cmd(['az'], env=self.argcomplete_env('az account sh', '13')) + with open('argcomplete.out') as f: + self.assertEqual(f.read(), 'show ') + os.remove('argcomplete.out') + + run_cmd(['az'], capture_output=True, env=self.argcomplete_env('az account s', '12')) + with open('argcomplete.out') as f: + self.assertEqual(f.read(), 'set show') + os.remove('argcomplete.out') + + run_cmd(['az'], env=self.argcomplete_env('az account szzz', '15')) + with open('argcomplete.out') as f: + self.assertFalse(f.read()) + os.remove('argcomplete.out') + + class MockCompletionFinder(AzCompletionFinder): + def __call__(self, *args, **kwargs): + import sys + return super().__call__(*args, exit_method=sys.exit, **kwargs) + + def dummy_completor(*args, **kwargs): + return ['dummystorage'] + + cli = DummyCli() + # argcomplete uses os._exit to exit, which is also used by pytest. Patch AzCompletionFinder to use sys.exit + # There is no recording for this test case, as completer is mocked. + with mock.patch('azure.cli.core.parser.AzCompletionFinder', MockCompletionFinder): + with mock.patch('azure.cli.core.commands.parameters.get_resource_name_completion_list', lambda _: dummy_completor): + env = self.argcomplete_env('az storage blob list -c test --account-name dumm', '48') + with mock.patch.dict(os.environ, env): + self.assertRaises(SystemExit, cli.invoke, ['az']) + with open('argcomplete.out') as f: + self.assertEqual(f.read(), 'dummystorage ') + os.remove('argcomplete.out') diff --git a/src/azure-cli-core/setup.py b/src/azure-cli-core/setup.py index b1f6f20ab9e..56f3ae9d349 100644 --- a/src/azure-cli-core/setup.py +++ b/src/azure-cli-core/setup.py @@ -43,7 +43,7 @@ ] DEPENDENCIES = [ - 'argcomplete~=3.3.0', + 'argcomplete~=3.5.2', 'azure-cli-telemetry==1.1.0.*', 'azure-mgmt-core>=1.2.0,<2', 'cryptography', diff --git a/src/azure-cli/requirements.py3.Darwin.txt b/src/azure-cli/requirements.py3.Darwin.txt index f1dc5f4816b..b94c0ed60b5 100644 --- a/src/azure-cli/requirements.py3.Darwin.txt +++ b/src/azure-cli/requirements.py3.Darwin.txt @@ -1,6 +1,6 @@ antlr4-python3-runtime==4.13.1 applicationinsights==0.11.9 -argcomplete==3.3.0 +argcomplete==3.5.2 asn1crypto==0.24.0 azure-appconfiguration==1.7.0 azure-batch==14.2.0 diff --git a/src/azure-cli/requirements.py3.Linux.txt b/src/azure-cli/requirements.py3.Linux.txt index 414d1efb627..06472a5a8a4 100644 --- a/src/azure-cli/requirements.py3.Linux.txt +++ b/src/azure-cli/requirements.py3.Linux.txt @@ -1,6 +1,6 @@ antlr4-python3-runtime==4.13.1 applicationinsights==0.11.9 -argcomplete==3.3.0 +argcomplete==3.5.2 asn1crypto==0.24.0 azure-appconfiguration==1.7.0 azure-batch==14.2.0 diff --git a/src/azure-cli/requirements.py3.windows.txt b/src/azure-cli/requirements.py3.windows.txt index cfceeb79197..1d35afbd724 100644 --- a/src/azure-cli/requirements.py3.windows.txt +++ b/src/azure-cli/requirements.py3.windows.txt @@ -1,6 +1,6 @@ antlr4-python3-runtime==4.13.1 applicationinsights==0.11.9 -argcomplete==3.3.0 +argcomplete==3.5.2 asn1crypto==0.24.0 azure-appconfiguration==1.7.0 azure-batch==14.2.0