|
| 1 | +"""Tests for ``charon_env``.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import charon_env |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +def test_load_charon_env_parses_export_lines(tmp_path: Path) -> None: |
| 12 | + """Export lines are parsed into a key/value mapping.""" |
| 13 | + env_file = tmp_path / "charon.env" |
| 14 | + env_file.write_text( |
| 15 | + "\nexport CHARON_OCI_REGISTRY=repo@sha256:abcdef0123456789\n" |
| 16 | + "export CHARON_TARGET=dev-npm-ga\n" |
| 17 | + 'export CHARON_PRODUCT_NAME="Test Product"\n', |
| 18 | + encoding="utf-8", |
| 19 | + ) |
| 20 | + env = charon_env.load_charon_env(env_file) |
| 21 | + assert env["CHARON_OCI_REGISTRY"] == "repo@sha256:abcdef0123456789" |
| 22 | + assert env["CHARON_TARGET"] == "dev-npm-ga" |
| 23 | + assert env["CHARON_PRODUCT_NAME"] == "Test Product" |
| 24 | + |
| 25 | + |
| 26 | +def test_load_charon_env_missing_file(tmp_path: Path) -> None: |
| 27 | + """Missing env files raise FileNotFoundError.""" |
| 28 | + with pytest.raises(FileNotFoundError): |
| 29 | + charon_env.load_charon_env(tmp_path / "missing.env") |
| 30 | + |
| 31 | + |
| 32 | +def test_split_oci_registries() -> None: |
| 33 | + """Registry references are split on percent signs.""" |
| 34 | + value = "quay.io/a@sha256:111111%quay.io/b@sha256:222222" |
| 35 | + assert charon_env.split_oci_registries(value) == [ |
| 36 | + "quay.io/a@sha256:111111", |
| 37 | + "quay.io/b@sha256:222222", |
| 38 | + ] |
| 39 | + |
| 40 | + |
| 41 | +def test_short_sha256_prefix() -> None: |
| 42 | + """Short hash uses the first six digest characters.""" |
| 43 | + assert charon_env.short_sha256_prefix("repo@sha256:0b15aad24f1b847") == "0b15aa" |
| 44 | + |
| 45 | + |
| 46 | +def test_source_repo() -> None: |
| 47 | + """Source repo strips the digest suffix.""" |
| 48 | + assert charon_env.source_repo("quay.io/org/app@sha256:abc") == "quay.io/org/app" |
| 49 | + |
| 50 | + |
| 51 | +def test_load_charon_env_skips_malformed_lines(tmp_path: Path) -> None: |
| 52 | + """Lines without ``=`` are ignored.""" |
| 53 | + env_file = tmp_path / "charon.env" |
| 54 | + env_file.write_text("export CHARON_TARGET=dev\nnot-a-variable\n", encoding="utf-8") |
| 55 | + env = charon_env.load_charon_env(env_file) |
| 56 | + assert env == {"CHARON_TARGET": "dev"} |
| 57 | + |
| 58 | + |
| 59 | +def test_short_sha256_prefix_requires_digest() -> None: |
| 60 | + """Registry references without a digest raise ValueError.""" |
| 61 | + with pytest.raises(ValueError, match="@sha256:"): |
| 62 | + charon_env.short_sha256_prefix("quay.io/org/app:latest") |
| 63 | + |
| 64 | + |
| 65 | +def test_split_oci_registries_ignores_empty_segments() -> None: |
| 66 | + """Trailing or duplicate ``%`` separators yield no empty entries.""" |
| 67 | + assert charon_env.split_oci_registries("quay.io/a@sha256:111111%") == [ |
| 68 | + "quay.io/a@sha256:111111", |
| 69 | + ] |
| 70 | + assert charon_env.split_oci_registries("%quay.io/a@sha256:111111%") == [ |
| 71 | + "quay.io/a@sha256:111111", |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +def test_load_charon_env_skips_comments_and_blank_lines(tmp_path: Path) -> None: |
| 76 | + """Comments and blank lines are ignored.""" |
| 77 | + env_file = tmp_path / "charon.env" |
| 78 | + env_file.write_text( |
| 79 | + "# comment\n\nexport CHARON_TARGET=dev\n# export CHARON_FOO=bar\n", |
| 80 | + encoding="utf-8", |
| 81 | + ) |
| 82 | + assert charon_env.load_charon_env(env_file) == {"CHARON_TARGET": "dev"} |
| 83 | + |
| 84 | + |
| 85 | +def test_load_charon_env_strips_single_quotes(tmp_path: Path) -> None: |
| 86 | + """Single-quoted values are unquoted.""" |
| 87 | + env_file = tmp_path / "charon.env" |
| 88 | + env_file.write_text("export CHARON_PRODUCT_NAME='Test Product'\n", encoding="utf-8") |
| 89 | + assert charon_env.load_charon_env(env_file)["CHARON_PRODUCT_NAME"] == "Test Product" |
| 90 | + |
| 91 | + |
| 92 | +def test_require_env_keys_raises_for_missing_key() -> None: |
| 93 | + """Missing required keys raise ValueError.""" |
| 94 | + with pytest.raises(ValueError, match="missing required charon env variable"): |
| 95 | + charon_env.require_env_keys({"CHARON_TARGET": "dev"}, "CHARON_PRODUCT_NAME") |
| 96 | + |
| 97 | + |
| 98 | +def test_require_oci_registries(tmp_path: Path) -> None: |
| 99 | + """Non-empty registry lists are returned from parsed env data.""" |
| 100 | + env_file = tmp_path / "charon.env" |
| 101 | + env_file.write_text( |
| 102 | + "export CHARON_OCI_REGISTRY=quay.io/a@sha256:111111%quay.io/b@sha256:222222\n", |
| 103 | + encoding="utf-8", |
| 104 | + ) |
| 105 | + env = charon_env.load_charon_env(env_file) |
| 106 | + assert charon_env.require_oci_registries(env) == [ |
| 107 | + "quay.io/a@sha256:111111", |
| 108 | + "quay.io/b@sha256:222222", |
| 109 | + ] |
| 110 | + |
| 111 | + |
| 112 | +def test_require_oci_registries_missing_key() -> None: |
| 113 | + """Missing ``CHARON_OCI_REGISTRY`` raises ValueError.""" |
| 114 | + with pytest.raises(ValueError, match="CHARON_OCI_REGISTRY is required"): |
| 115 | + charon_env.require_oci_registries({}) |
| 116 | + |
| 117 | + |
| 118 | +def test_require_oci_registries_empty_value() -> None: |
| 119 | + """Blank ``CHARON_OCI_REGISTRY`` values raise ValueError.""" |
| 120 | + with pytest.raises(ValueError, match="at least one registry"): |
| 121 | + charon_env.require_oci_registries({"CHARON_OCI_REGISTRY": ""}) |
| 122 | + |
| 123 | + |
| 124 | +def test_charon_config_path_uses_explicit_home(tmp_path: Path) -> None: |
| 125 | + """An explicit *home* overrides ``Path.home()``.""" |
| 126 | + assert charon_env.charon_config_path(home=tmp_path) == tmp_path / ".charon" / "charon.yaml" |
| 127 | + |
| 128 | + |
| 129 | +def test_install_charon_config(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 130 | + """Charon config is copied into ``$HOME/.charon/charon.yaml``.""" |
| 131 | + monkeypatch.setenv("HOME", str(tmp_path)) |
| 132 | + config_path = charon_env.charon_config_path() |
| 133 | + source = tmp_path / "input.yaml" |
| 134 | + source.write_text("charon-config\n", encoding="utf-8") |
| 135 | + dest = charon_env.install_charon_config(source) |
| 136 | + assert dest == config_path |
| 137 | + assert config_path.read_text(encoding="utf-8") == "charon-config\n" |
| 138 | + |
| 139 | + |
| 140 | +def test_install_charon_config_missing_source(tmp_path: Path) -> None: |
| 141 | + """Missing config sources raise FileNotFoundError.""" |
| 142 | + with pytest.raises(FileNotFoundError, match="charon config file not found"): |
| 143 | + charon_env.install_charon_config(tmp_path / "missing.yaml") |
0 commit comments