Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ def _fake_list(*_a, **_kw):
]

with patch(
"cloudsmith_cli.credential_helpers.custom_domains.list_custom_domains",
"cloudsmith_cli.core.api.orgs.list_custom_domains",
_fake_list,
):
result = get_custom_domains(
Expand Down
21 changes: 14 additions & 7 deletions cloudsmith_cli/cli/tests/test_startup_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
HEAVY_PREFIXES = ("mcp", "httpx", "cloudsmith_api", "requests")


def modules_loaded_by_cli_import():
def modules_loaded_by_import(module_name="cloudsmith_cli.cli.commands.main"):
code = (
"import json, sys\n"
"import cloudsmith_cli.cli.commands.main\n"
f"import {module_name}\n"
"print(json.dumps(sorted(sys.modules)))\n"
)
result = subprocess.run(
Expand All @@ -27,22 +27,29 @@ def modules_loaded_by_cli_import():
return json.loads(result.stdout)


def test_cli_import_does_not_load_heavy_modules():
modules = modules_loaded_by_cli_import()
heavy = [
def heavy_modules_in(modules):
return [
name
for name in modules
if any(name == p or name.startswith(p + ".") for p in HEAVY_PREFIXES)
]
assert heavy == []


def test_cli_import_does_not_load_heavy_modules():
assert heavy_modules_in(modules_loaded_by_import()) == []


def test_cli_import_does_not_load_command_modules():
package = "cloudsmith_cli.cli.commands."
allowed = {package + "main", package + "registry"}
loaded = [
name
for name in modules_loaded_by_cli_import()
for name in modules_loaded_by_import()
if name.startswith(package) and name not in allowed
]
assert loaded == []


def test_docker_helper_import_does_not_load_heavy_modules():
modules = modules_loaded_by_import("cloudsmith_cli.credential_helpers.docker")
assert heavy_modules_in(modules) == []
7 changes: 4 additions & 3 deletions cloudsmith_cli/credential_helpers/custom_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
from pathlib import Path

from ..cli.config import get_default_config_path
from ..core.api.exceptions import ApiException
from ..core.api.init import initialise_api
from ..core.api.orgs import list_custom_domains
from ..core.cache_utils import atomic_write_json
from ..core.credentials.models import CredentialResult
from .default_domains import DomainType, domain_type_from_server
Expand Down Expand Up @@ -305,6 +302,10 @@ def get_custom_domains(

logger.debug("Fetching custom domains from API for %s", org)

from ..core.api.exceptions import ApiException
from ..core.api.init import initialise_api
from ..core.api.orgs import list_custom_domains

if configure_api:
initialise_api(host=api_host, credential=credential)

Expand Down
Loading