Skip to content

Commit 59c9b1a

Browse files
test(ENG-13681): fold duplicated cases together
The suite had grown several tests per behaviour: three separate fallback-to-builtins tests, four on one lookup failure, two each on the entry key set and the built-in entry fields, and a repository-scoping pair that asserted what the precedence tests already prove. Merged them into parametrized cases and dropped the redundant ones. Every behaviour still has a test, including the failure paths; there is just one test per behaviour now instead of three. Also removed the narrative comments inside the tests, leaving the docstrings to say what each one covers. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent a04a9cd commit 59c9b1a

6 files changed

Lines changed: 84 additions & 203 deletions

File tree

‎cloudsmith_cli/cli/tests/commands/test_credential_helper.py‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ def _cache_dir(tmp_path, monkeypatch):
244244
[
245245
# 200 → records built and cached
246246
(200, True, True),
247-
# 404 → [] but NOT cached (org not found - probably a typo, must
248-
# fail loudly every time rather than replay a cached failure)
247+
# 404 → [] but NOT cached
249248
(404, False, False),
250249
# 403 → [] but NOT cached (may succeed after auth)
251250
(403, False, False),
@@ -472,8 +471,6 @@ def test_get_custom_domains_bearer_credential_sends_authorization_header(
472471
content_type="application/json",
473472
)
474473

475-
# A previously configured API key must not leak into a bearer-authenticated
476-
# request: initialise_api clears the class-default X-Api-Key when switching.
477474
initialise_api(
478475
host=API_HOST,
479476
credential=CredentialResult(api_key="k_stale", source_name="test"),
@@ -749,10 +746,7 @@ def test_is_cloudsmith_domain(
749746
@pytest.mark.parametrize(
750747
"credential",
751748
[
752-
# no credential at all
753749
None,
754-
# a credential carrying no usable key — truthy as an object, so the
755-
# guard has to look at api_key, not just the credential
756750
CredentialResult(api_key="", source_name="test"),
757751
],
758752
)
@@ -850,7 +844,6 @@ def test_custom_domain_scope(repository, expected):
850844
[
851845
("repository", DomainScope.REPOSITORY),
852846
("organization", DomainScope.ORGANIZATION),
853-
# A hand-edited config must not break every wrapped run.
854847
("nonsense", DomainScope.ORGANIZATION),
855848
(None, DomainScope.ORGANIZATION),
856849
("", DomainScope.ORGANIZATION),

‎cloudsmith_cli/cli/tests/commands/test_credential_helper_install.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ def _fake_get_format_domains(*_a, **kwargs):
429429
)
430430
assert captured["cache_only"] is True, "a dry run must not query the API"
431431
assert not (docker_dir / "config.json").exists()
432-
# The preview names every host the real install would write.
433432
assert any("docker.acme.com" in a and "would set" in a for a in actions)
434433

435434
else: # discovery_raises — graceful failure guard

‎cloudsmith_cli/cli/tests/commands/test_custom_domain_precedence.py‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -121,32 +121,6 @@ def test_inactive_domains_are_never_chosen(enabled, validated):
121121
assert select_custom_domain([inactive], domain_type=DomainType.DOWNLOAD) is None
122122

123123

124-
def test_naming_a_repository_is_enough_to_be_scoped_to_it():
125-
"""The repository alone binds the domain.
126-
127-
The server refuses to save a repository and its `repository_only` flag
128-
apart, so the flag can only ever restate the repository.
129-
"""
130-
repo_bound = _domain("dl-prod.acme.com", repository="prod")
131-
132-
chosen = select_custom_domain(
133-
[repo_bound], domain_type=DomainType.DOWNLOAD, repository="prod"
134-
)
135-
136-
assert chosen is repo_bound
137-
138-
139-
def test_a_domain_bound_elsewhere_is_never_chosen():
140-
"""A repository-scoped host can never stand in for another repository."""
141-
bound_elsewhere = _domain("dl-staging.acme.com", repository="staging")
142-
143-
chosen = select_custom_domain(
144-
[bound_elsewhere], domain_type=DomainType.DOWNLOAD, repository="prod"
145-
)
146-
147-
assert chosen is None
148-
149-
150124
def test_a_domain_bound_to_another_repository_is_never_used():
151125
"""A repository-scoped host serves only the repository it belongs to."""
152126
other = _repo_domain("dl-staging.acme.com", repository="staging")

‎cloudsmith_cli/cli/tests/commands/test_default_domains.py‎

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,26 @@ def no_trusted_config(tmp_path, monkeypatch):
6666
monkeypatch.setattr(cli_config.ConfigReader, "config_searchpath", [str(empty)])
6767

6868

69-
def test_load_returns_builtins_when_no_config(tmp_path, no_trusted_config):
70-
"""With no config file present, the built-in table is used."""
71-
assert load_default_domains(config_path=tmp_path / "absent.ini") == list(
72-
BUILTIN_DOMAINS
73-
)
69+
@pytest.mark.parametrize(
70+
"body",
71+
[
72+
None,
73+
"[default]\napi_host = https://api.cloudsmith.io\n",
74+
"this is not valid ini [[[",
75+
],
76+
ids=["absent", "no-domains-section", "malformed"],
77+
)
78+
def test_a_config_declaring_no_table_falls_back_to_builtins(
79+
tmp_path, no_trusted_config, body
80+
):
81+
"""Only a readable [domains] section replaces the built-in table."""
82+
config = tmp_path / "config.ini"
83+
if body is None:
84+
config = tmp_path / "absent.ini"
85+
else:
86+
config.write_text(body, encoding="utf-8")
87+
88+
assert load_default_domains(config_path=config) == list(BUILTIN_DOMAINS)
7489

7590

7691
def test_explicit_config_without_a_table_falls_back_to_a_trusted_one(
@@ -116,7 +131,6 @@ def test_config_section_replaces_builtins_wholesale(tmp_path, no_trusted_config)
116131
indexed = _by_host(domains)
117132
assert indexed["packages.internal.example.com"].backend_kind == (BackendKind.PYTHON)
118133
assert indexed["packages.internal.example.com"].format_label == "python"
119-
# An empty value means "no single format".
120134
assert indexed["cdn.internal.example.com"].backend_kind is None
121135
assert indexed["cdn.internal.example.com"].format_label is None
122136
assert indexed["cdn.internal.example.com"].domain_type is DomainType.DOWNLOAD
@@ -172,26 +186,6 @@ def test_config_label_without_backend_kind_is_formatless(tmp_path, no_trusted_co
172186
assert domain.domain_type is DomainType.DOWNLOAD
173187

174188

175-
def test_config_without_domains_section_falls_back_to_builtins(
176-
tmp_path, no_trusted_config
177-
):
178-
"""A config file that has no [domains] section changes nothing."""
179-
config = tmp_path / "config.ini"
180-
config.write_text(
181-
"[default]\napi_host = https://api.cloudsmith.io\n", encoding="utf-8"
182-
)
183-
184-
assert load_default_domains(config_path=config) == list(BUILTIN_DOMAINS)
185-
186-
187-
def test_unreadable_config_falls_back_to_builtins(tmp_path, no_trusted_config):
188-
"""A malformed config must not break the command."""
189-
config = tmp_path / "config.ini"
190-
config.write_text("this is not valid ini [[[", encoding="utf-8")
191-
192-
assert load_default_domains(config_path=config) == list(BUILTIN_DOMAINS)
193-
194-
195189
def test_trusted_lookup_skips_a_config_without_a_domains_section(tmp_path, monkeypatch):
196190
"""The table comes from the first trusted config that declares one.
197191
@@ -340,8 +334,6 @@ def test_untrusted_cwd_config_is_not_honoured(tmp_path, monkeypatch):
340334
a Cloudsmith host and harvest a token.
341335
"""
342336
monkeypatch.chdir(tmp_path)
343-
# Pin the trusted lookup to "nothing found" so the developer's real
344-
# config.ini cannot influence what this test observes.
345337
monkeypatch.setattr(
346338
"cloudsmith_cli.credential_helpers.default_domains._trusted_domains",
347339
lambda: None,
@@ -467,14 +459,13 @@ def test_builtin_domains_with_a_backend_kind_are_native_api():
467459
assert domain.domain_type is DomainType.NATIVE_API, domain.host
468460

469461

470-
def test_builtin_host_for_type_resolves_download_and_upload():
471-
"""The download/upload hosts are looked up by type, not by constant."""
462+
def test_builtin_host_for_type_resolves_the_format_less_hosts():
463+
"""The download and upload hosts are looked up by type, not by constant.
464+
465+
NATIVE_API covers many hosts, so there is no single one to return.
466+
"""
472467
assert builtin_host_for_type(DomainType.DOWNLOAD) == "dl.cloudsmith.io"
473468
assert builtin_host_for_type(DomainType.UPLOAD) == "upload.cloudsmith.io"
474-
475-
476-
def test_builtin_host_for_type_rejects_ambiguous_native_api():
477-
"""NATIVE_API covers many hosts, so there is no single one to return."""
478469
with pytest.raises(ValueError):
479470
builtin_host_for_type(DomainType.NATIVE_API)
480471

@@ -483,25 +474,20 @@ def test_builtin_host_for_type_rejects_ambiguous_native_api():
483474
"backend_kind,expected",
484475
[
485476
(BackendKind.MAVEN, DomainType.NATIVE_API),
486-
# DEB is 0 and therefore falsy: a truthiness check here would
487-
# misclassify it as a download host.
488477
(BackendKind.DEB, DomainType.NATIVE_API),
489478
(None, DomainType.DOWNLOAD),
490479
],
480+
ids=["maven", "deb-is-zero-so-falsy", "no-kind"],
491481
)
492482
def test_domain_type_for_backend_kind(backend_kind, expected):
493483
"""A kind implies native API; its absence implies a download host."""
494484
assert domain_type_for_backend_kind(backend_kind) is expected
495485

496486

497-
def test_builtin_host_resolves_backend_kind():
498-
"""builtin_host maps a backend kind to its built-in service host."""
487+
def test_builtin_host_resolves_a_backend_kind_to_its_own_host():
488+
"""Formats served only via the CDN have no dedicated built-in host."""
499489
assert builtin_host(BackendKind.MAVEN) == "maven.cloudsmith.io"
500490
assert builtin_host(BackendKind.PYTHON) == "python.cloudsmith.io"
501-
502-
503-
def test_builtin_host_rejects_kind_without_dedicated_host():
504-
"""Formats served only via the CDN have no dedicated built-in host."""
505491
with pytest.raises(ValueError):
506492
builtin_host(BackendKind.DEB)
507493

0 commit comments

Comments
 (0)