MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / _write_profile

Function _write_profile

tests/lib/test_credentials.py:126–155  ·  view source on GitHub ↗

Test helper: lay out ``configs/ .json`` and optionally ``credentials/ .json`` under ``config_dir``. Accepts either the new nested ``{"authentication": {...}}`` shape or a legacy flat shape (``{"type": "workload_identity", ...}``) for backwards compatibility with the

(
    config_dir: pathlib.Path,
    profile: str,
    config: Dict[str, Any],
    credentials: Optional[Dict[str, Any]] = None,
)

Source from the content-addressed store, hash-verified

124
125
126def _write_profile(
127 config_dir: pathlib.Path,
128 profile: str,
129 config: Dict[str, Any],
130 credentials: Optional[Dict[str, Any]] = None,
131) -> None:
132 """Test helper: lay out ``configs/<profile>.json`` and optionally
133 ``credentials/<profile>.json`` under ``config_dir``.
134
135 Accepts either the new nested ``{"authentication": {...}}`` shape or a
136 legacy flat shape (``{"type": "workload_identity", ...}``) for backwards
137 compatibility with the tests that predate the schema migration. Legacy
138 inputs are translated via :func:`_migrate_legacy_config` before being
139 written to disk.
140
141 Prepends ``"type": "oauth_token"`` to the credentials dict unless the
142 caller already supplied a ``type`` key (so negative tests can override).
143 """
144 if "type" in config and "authentication" not in config:
145 config = _migrate_legacy_config(config)
146 (config_dir / "configs").mkdir(parents=True, exist_ok=True)
147 (config_dir / "configs" / f"{profile}.json").write_text(json.dumps(config))
148 if credentials is not None:
149 if "type" not in credentials:
150 credentials = {"type": "oauth_token", **credentials}
151 (config_dir / "credentials").mkdir(parents=True, exist_ok=True)
152 creds_path = config_dir / "credentials" / f"{profile}.json"
153 creds_path.write_text(json.dumps(credentials))
154 # Match the 0o600 invariant the real credentials reader now enforces.
155 creds_path.chmod(0o600)
156
157
158# --------------------------------------------------------------------------- #

Calls 1

_migrate_legacy_configFunction · 0.85

Tested by

no test coverage detected