Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/azure-cli-core/azure/cli/core/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DummyCli(AzCli):
"""A dummy CLI instance can be used to facilitate automation"""
def __init__(self, commands_loader_cls=None, random_config_dir=False, **kwargs):
import os
from unittest.mock import patch

from azure.cli.core import MainCommandsLoader
from azure.cli.core.commands import AzCliCommandInvoker
Expand All @@ -26,6 +27,10 @@ def __init__(self, commands_loader_cls=None, random_config_dir=False, **kwargs):

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.
Copy link
Contributor Author

@bebound bebound Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.config_dir = os.environ.get('{}CONFIG_DIR'.format(env_var_prefix), default_config_dir)

https://github.com/microsoft/knack/blob/e0c14114aea5e4416c70a77623e403773aba73a8/knack/config.py#L44

self.env_patch = patch.dict(os.environ, {'AZURE_CONFIG_DIR': config_dir})
self.env_patch.start()

# In recording mode, copy login credentials from global config dir to the dummy config dir
if os.getenv(ENV_VAR_TEST_LIVE, '').lower() == 'true':
Expand All @@ -38,12 +43,10 @@ def __init__(self, commands_loader_cls=None, random_config_dir=False, **kwargs):
shutil.copy(os.path.join(GLOBAL_CONFIG_DIR, file), config_dir)
except FileNotFoundError:
pass
else:
config_dir = GLOBAL_CONFIG_DIR

super(DummyCli, self).__init__(
cli_name='az',
config_dir=config_dir,
config_dir=GLOBAL_CONFIG_DIR,
Copy link
Member

@jiasli jiasli Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what it looks like before #25689. As we are already overriding GLOBAL_CONFIG_DIR with environment variable AZURE_CONFIG_DIR, setting config_dir is unnecessary.

config_env_var_prefix=ENV_VAR_PREFIX,
commands_loader_cls=commands_loader_cls or MainCommandsLoader,
parser_cls=AzCliCommandParser,
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli-testsdk/azure/cli/testsdk/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ 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):
Expand Down