|
| 1 | +# (C) Datadog, Inc. 2024-present |
| 2 | +# All rights reserved |
| 3 | +# Licensed under a 3-clause BSD style license (see LICENSE) |
| 4 | +from collections.abc import Generator |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ddev.config.file import DDEV_TOML, ConfigFileWithOverrides |
| 9 | +from ddev.utils.fs import Path |
| 10 | +from ddev.utils.toml import dump_toml_data, load_toml_file |
| 11 | +from tests.helpers.git import ClonedRepo |
| 12 | +from tests.helpers.runner import CliRunner, Result |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def repo_with_ddev_tool_config(repository_as_cwd: ClonedRepo) -> Generator[ClonedRepo, None, None]: |
| 17 | + pyproject_path = repository_as_cwd.path / "pyproject.toml" |
| 18 | + pyproject = load_toml_file(pyproject_path) |
| 19 | + pyproject["tool"]["ddev"] = {"repo": "integrations-core"} |
| 20 | + dump_toml_data(pyproject, pyproject_path) |
| 21 | + |
| 22 | + yield repository_as_cwd |
| 23 | + |
| 24 | + |
| 25 | +def test_create_new_overrides_config( |
| 26 | + ddev: CliRunner, config_file: ConfigFileWithOverrides, helpers, repo_with_ddev_tool_config: ClonedRepo |
| 27 | +): |
| 28 | + temp_dir = repo_with_ddev_tool_config.path |
| 29 | + |
| 30 | + result = ddev("config", "override") |
| 31 | + local_path = str(temp_dir).replace("\\", "\\\\") |
| 32 | + |
| 33 | + expected_output = helpers.dedent( |
| 34 | + f""" |
| 35 | + Local repo configuration added in {config_file.pretty_overrides_path} |
| 36 | +
|
| 37 | + Local config content: |
| 38 | + repo = "core" |
| 39 | +
|
| 40 | + [repos] |
| 41 | + core = "{local_path}" |
| 42 | + """ |
| 43 | + ) |
| 44 | + # Reload new values |
| 45 | + config_file.load() |
| 46 | + |
| 47 | + assert result.exit_code == 0, result.output |
| 48 | + assert result.output == expected_output |
| 49 | + |
| 50 | + # Verify the config was actually created |
| 51 | + assert config_file.overrides_path.exists() |
| 52 | + assert config_file.overrides_model.raw_data["repos"]["core"] == str(config_file.overrides_path.parent) |
| 53 | + assert config_file.overrides_model.raw_data["repo"] == "core" |
| 54 | + |
| 55 | + |
| 56 | +def test_update_existing_local_config( |
| 57 | + ddev: CliRunner, config_file: ConfigFileWithOverrides, helpers, repo_with_ddev_tool_config: ClonedRepo |
| 58 | +): |
| 59 | + ddev_path = repo_with_ddev_tool_config.path / DDEV_TOML |
| 60 | + existing_config = helpers.dedent( |
| 61 | + """ |
| 62 | + [orgs.default] |
| 63 | + api_key = "test_key" |
| 64 | +
|
| 65 | + [repos] |
| 66 | + core = "/old/path" |
| 67 | + """ |
| 68 | + ) |
| 69 | + ddev_path.write_text(existing_config) |
| 70 | + |
| 71 | + result = ddev("config", "override") |
| 72 | + local_path = str(ddev_path.parent).replace("\\", "\\\\") |
| 73 | + |
| 74 | + assert result.exit_code == 0, result.output |
| 75 | + assert result.output == helpers.dedent( |
| 76 | + f""" |
| 77 | + Local config file already exists. Updating... |
| 78 | + Local repo configuration added in {config_file.pretty_overrides_path} |
| 79 | +
|
| 80 | + Local config content: |
| 81 | + repo = "core" |
| 82 | +
|
| 83 | + [orgs.default] |
| 84 | + api_key = "*****" |
| 85 | +
|
| 86 | + [repos] |
| 87 | + core = "{local_path}" |
| 88 | + """ |
| 89 | + ) |
| 90 | + |
| 91 | + # Verify the config was updated correctly |
| 92 | + config_file.load() |
| 93 | + assert config_file.overrides_model.raw_data["repos"]["core"] == str(repo_with_ddev_tool_config.path) |
| 94 | + assert config_file.overrides_model.raw_data["repo"] == "core" |
| 95 | + assert config_file.overrides_model.raw_data["orgs"]["default"]["api_key"] == "test_key" |
| 96 | + |
| 97 | + |
| 98 | +def assert_valid_local_config( |
| 99 | + config_file: ConfigFileWithOverrides, repo_path: Path, result: Result, expected_output: str |
| 100 | +): |
| 101 | + assert result.exit_code == 0 |
| 102 | + assert "The current repo could not be inferred" in result.output |
| 103 | + assert "What repo are you trying to override?" in result.output |
| 104 | + assert expected_output in result.output |
| 105 | + assert config_file.overrides_model.raw_data["repos"]["extras"] == str(repo_path) |
| 106 | + assert config_file.overrides_model.raw_data["repo"] == "extras" |
| 107 | + |
| 108 | + |
| 109 | +def test_not_in_repo_ask_user(ddev: CliRunner, config_file: ConfigFileWithOverrides, helpers, overrides_config: Path): |
| 110 | + result = ddev("config", "override", input="extras") |
| 111 | + extras_path = str(config_file.overrides_path.parent).replace("\\", "\\\\") |
| 112 | + |
| 113 | + expected_output = helpers.dedent( |
| 114 | + f""" |
| 115 | + Local config file already exists. Updating... |
| 116 | + Local repo configuration added in {config_file.pretty_overrides_path} |
| 117 | +
|
| 118 | + Local config content: |
| 119 | + repo = "extras" |
| 120 | +
|
| 121 | + [repos] |
| 122 | + extras = "{extras_path}" |
| 123 | + """ |
| 124 | + ) |
| 125 | + # Reload new values |
| 126 | + config_file.load() |
| 127 | + assert_valid_local_config(config_file, overrides_config.parent, result, expected_output) |
| 128 | + |
| 129 | + |
| 130 | +def test_pyproject_not_found_ask_user( |
| 131 | + ddev: CliRunner, config_file: ConfigFileWithOverrides, helpers, repository_as_cwd: ClonedRepo |
| 132 | +): |
| 133 | + (repository_as_cwd.path / "pyproject.toml").unlink() |
| 134 | + result = ddev("config", "override", input="extras") |
| 135 | + extras_path = str(config_file.overrides_path.parent).replace("\\", "\\\\") |
| 136 | + |
| 137 | + expected_output = helpers.dedent( |
| 138 | + f""" |
| 139 | + Local repo configuration added in {config_file.pretty_overrides_path} |
| 140 | +
|
| 141 | + Local config content: |
| 142 | + repo = "extras" |
| 143 | +
|
| 144 | + [repos] |
| 145 | + extras = "{extras_path}" |
| 146 | + """ |
| 147 | + ) |
| 148 | + # Reload new values |
| 149 | + config_file.load() |
| 150 | + assert_valid_local_config(config_file, config_file.overrides_path.parent, result, expected_output) |
| 151 | + |
| 152 | + |
| 153 | +def test_misconfigured_pyproject_fails( |
| 154 | + ddev: CliRunner, config_file: ConfigFileWithOverrides, helpers, repository_as_cwd: ClonedRepo |
| 155 | +): |
| 156 | + # Setup wrongly configured pyproject.toml |
| 157 | + pyproject_path = repository_as_cwd.path / "pyproject.toml" |
| 158 | + pyproject = load_toml_file(pyproject_path) |
| 159 | + pyproject["tool"]["ddev"] = {"repo": "wrong-repo"} |
| 160 | + dump_toml_data(pyproject, pyproject_path) |
| 161 | + |
| 162 | + result = ddev("config", "override") |
| 163 | + assert result.exit_code == 1 |
| 164 | + assert "Invalid ddev metadata found in pyproject.toml" in result.output |
| 165 | + assert "[tool.ddev.repo] is 'wrong-repo': Input should be 'integrations-core'" in result.output |
0 commit comments