diff --git a/src/azure-cli-core/azure/cli/core/mock.py b/src/azure-cli-core/azure/cli/core/mock.py index 7fa63bc390e..5fe20116b22 100644 --- a/src/azure-cli-core/azure/cli/core/mock.py +++ b/src/azure-cli-core/azure/cli/core/mock.py @@ -25,12 +25,14 @@ def __init__(self, commands_loader_cls=None, random_config_dir=False, **kwargs): from knack.completion import ARGCOMPLETE_ENV_NAME from knack.util import ensure_dir + env_patch = None + if random_config_dir: config_dir = os.path.join(GLOBAL_CONFIG_DIR, 'dummy_cli_config_dir', random_string()) # Knack prioritizes the AZURE_CONFIG_DIR env over the config_dir param, and other functions may call # get_config_dir directly. We need to set the env to make sure the config_dir is used. - self.env_patch = patch.dict(os.environ, {'AZURE_CONFIG_DIR': config_dir}) - self.env_patch.start() + env_patch = patch.dict(os.environ, {'AZURE_CONFIG_DIR': config_dir}) + env_patch.start() # Always copy command index to avoid initializing it again files_to_copy = ['commandIndex.json'] @@ -61,6 +63,9 @@ def __init__(self, commands_loader_cls=None, random_config_dir=False, **kwargs): help_cls=AzCliHelp, invocation_cls=AzCliCommandInvoker) + if random_config_dir: + env_patch.stop() + self.data['headers'] = {} # the x-ms-client-request-id is generated before a command is to execute self.data['command'] = 'unknown' self.data['completer_active'] = ARGCOMPLETE_ENV_NAME in os.environ diff --git a/src/azure-cli-testsdk/azure/cli/testsdk/base.py b/src/azure-cli-testsdk/azure/cli/testsdk/base.py index b1826eed909..d82186a2ffe 100644 --- a/src/azure-cli-testsdk/azure/cli/testsdk/base.py +++ b/src/azure-cli-testsdk/azure/cli/testsdk/base.py @@ -142,7 +142,6 @@ def tearDown(self): if self.random_config_dir: from azure.cli.core.util import rmtree_with_retry rmtree_with_retry(self.cli_ctx.config.config_dir) - self.cli_ctx.env_patch.stop() super(ScenarioTest, self).tearDown() def create_random_name(self, prefix, length):