|
| 1 | +# (C) Datadog, Inc. 2026-present |
| 2 | +# All rights reserved |
| 3 | +# Licensed under a 3-clause BSD style license (see LICENSE) |
| 4 | +import shlex |
| 5 | +import sys |
| 6 | +from pathlib import Path, PurePosixPath |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from ddev.plugin.external.hatch.environment_collector import DatadogChecksEnvironmentCollector, shell_quote |
| 11 | + |
| 12 | + |
| 13 | +def tokenize(command): |
| 14 | + """Tokenize a generated command the way a POSIX shell does (Hatch runs scripts with ``shell=True``).""" |
| 15 | + return shlex.split(command.replace('{verbosity:flag:-1}', '-q')) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def fake_checkout(tmp_path): |
| 20 | + """Build a fake in-core checkout, returning `(integrations_core, integration_root)`.""" |
| 21 | + |
| 22 | + def _make(*extra_dirs, whitespace=False): |
| 23 | + base = tmp_path / 'Jane Doe' if whitespace else tmp_path |
| 24 | + integrations_core = base / 'integrations-core' |
| 25 | + (integrations_core / 'datadog_checks_base').mkdir(parents=True) |
| 26 | + for extra_dir in extra_dirs: |
| 27 | + (integrations_core / extra_dir).mkdir() |
| 28 | + integration_root = integrations_core / 'fake_integration' |
| 29 | + integration_root.mkdir() |
| 30 | + return integrations_core, integration_root |
| 31 | + |
| 32 | + return _make |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize( |
| 36 | + 'features, version, local_path, expected', |
| 37 | + [ |
| 38 | + pytest.param(['deps'], '', None, 'datadog-checks-base[deps]', id='pypi_with_deps'), |
| 39 | + pytest.param([], '', None, 'datadog-checks-base[deps]', id='pypi_empty_features_defaults_to_deps'), |
| 40 | + pytest.param(['deps', 'http'], '', None, 'datadog-checks-base[deps,http]', id='pypi_multi_features'), |
| 41 | + pytest.param(['deps'], '1.2.3', None, 'datadog-checks-base[deps]==1.2.3', id='pypi_with_version'), |
| 42 | + pytest.param( |
| 43 | + ['deps'], |
| 44 | + '', |
| 45 | + PurePosixPath('/repo') / 'integrations-core' / 'datadog_checks_base', |
| 46 | + f"{PurePosixPath('/repo') / 'integrations-core' / 'datadog_checks_base'}[deps]", |
| 47 | + id='local_path_with_deps', |
| 48 | + ), |
| 49 | + pytest.param( |
| 50 | + ['deps', 'http'], |
| 51 | + '', |
| 52 | + PurePosixPath('/repo') / 'integrations-core' / 'datadog_checks_base', |
| 53 | + f"{PurePosixPath('/repo') / 'integrations-core' / 'datadog_checks_base'}[deps,http]", |
| 54 | + id='local_path_multi_features', |
| 55 | + ), |
| 56 | + ], |
| 57 | +) |
| 58 | +def test_format_base_package(monkeypatch, features, version, local_path, expected): |
| 59 | + # Pin to a POSIX target: a native `Path` would render backslashes on a Windows host, |
| 60 | + # which `shlex.quote` treats as unsafe and wraps in quotes, breaking the raw string comparison. |
| 61 | + monkeypatch.setattr(sys, 'platform', 'linux') |
| 62 | + |
| 63 | + assert DatadogChecksEnvironmentCollector.format_base_package(features, version=version, local_path=local_path) == ( |
| 64 | + expected |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize('whitespace', [False, True], ids=['plain_checkout', 'whitespace_checkout']) |
| 69 | +def test_base_package_install_command_uses_absolute_path_in_core(fake_checkout, monkeypatch, tmp_path, whitespace): |
| 70 | + monkeypatch.delenv('DDEV_TEST_BASE_PACKAGE_VERSION', raising=False) |
| 71 | + |
| 72 | + integrations_core, integration_root = fake_checkout(whitespace=whitespace) |
| 73 | + monkeypatch.chdir(tmp_path) |
| 74 | + |
| 75 | + collector = DatadogChecksEnvironmentCollector(integration_root, {}) |
| 76 | + command = collector.base_package_install_command(features=None) |
| 77 | + |
| 78 | + assert tokenize(command)[-1] == f"{integrations_core / 'datadog_checks_base'}[deps]" |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize('whitespace', [False, True], ids=['plain_checkout', 'whitespace_checkout']) |
| 82 | +def test_test_package_install_command_uses_absolute_path_in_core(fake_checkout, whitespace): |
| 83 | + integrations_core, integration_root = fake_checkout('datadog_checks_dev', whitespace=whitespace) |
| 84 | + |
| 85 | + collector = DatadogChecksEnvironmentCollector(integration_root, {}) |
| 86 | + command = collector.test_package_install_command |
| 87 | + |
| 88 | + assert tokenize(command)[-1] == str(integrations_core / 'datadog_checks_dev') |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize( |
| 92 | + 'has_local_ruff_config, expected_relative_to', |
| 93 | + [ |
| 94 | + pytest.param(True, 'self', id='local_ruff_config_returns_integration_root'), |
| 95 | + pytest.param(False, 'parent', id='no_local_ruff_config_returns_repo_root'), |
| 96 | + ], |
| 97 | +) |
| 98 | +def test_ruff_settings_dir_uses_absolute_path( |
| 99 | + fake_checkout, monkeypatch, tmp_path, has_local_ruff_config, expected_relative_to |
| 100 | +): |
| 101 | + integrations_core, integration_root = fake_checkout() |
| 102 | + |
| 103 | + pyproject = '[project]\nname = "fake"\n' |
| 104 | + if has_local_ruff_config: |
| 105 | + pyproject += '\n[tool.ruff]\nline-length = 120\n' |
| 106 | + (integration_root / 'pyproject.toml').write_text(pyproject) |
| 107 | + |
| 108 | + monkeypatch.chdir(tmp_path) |
| 109 | + |
| 110 | + collector = DatadogChecksEnvironmentCollector(integration_root, {}) |
| 111 | + settings_dir = collector.ruff_settings_dir() |
| 112 | + |
| 113 | + expected = str(integration_root) if expected_relative_to == 'self' else str(integrations_core) |
| 114 | + assert settings_dir == expected |
| 115 | + assert settings_dir not in ('.', '..') |
| 116 | + |
| 117 | + |
| 118 | +def test_format_base_package_quotes_local_path_with_whitespace(): |
| 119 | + local_path = Path('/Users/Jane Doe/integrations-core/datadog_checks_base') |
| 120 | + |
| 121 | + token = DatadogChecksEnvironmentCollector.format_base_package(['deps'], local_path=local_path) |
| 122 | + command = DatadogChecksEnvironmentCollector.uv_install_command('-e', token) |
| 123 | + |
| 124 | + assert tokenize(command)[-1] == f'{local_path}[deps]' |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parametrize( |
| 128 | + 'platform, path, expected', |
| 129 | + [ |
| 130 | + pytest.param('linux', '/Users/Jane Doe/integrations-core', "'/Users/Jane Doe/integrations-core'", id='linux'), |
| 131 | + pytest.param('darwin', '/Users/Jane Doe/integrations-core', "'/Users/Jane Doe/integrations-core'", id='macos'), |
| 132 | + pytest.param( |
| 133 | + 'win32', r'C:\Users\Jane Doe\integrations-core', r'"C:\Users\Jane Doe\integrations-core"', id='win' |
| 134 | + ), |
| 135 | + ], |
| 136 | +) |
| 137 | +def test_shell_quote_matches_target_shell(monkeypatch, platform, path, expected): |
| 138 | + monkeypatch.setattr(sys, 'platform', platform) |
| 139 | + |
| 140 | + assert shell_quote(path) == expected |
| 141 | + |
| 142 | + |
| 143 | +def test_shell_quote_windows_groups_whitespace_path_into_one_token(monkeypatch): |
| 144 | + monkeypatch.setattr(sys, 'platform', 'win32') |
| 145 | + path = r'C:\Users\Jane Doe\integrations-core\datadog_checks_base' |
| 146 | + |
| 147 | + quoted = shell_quote(path) |
| 148 | + |
| 149 | + # cmd.exe treats a double-quoted span as a single argument; the path is recovered intact, spaces and all. |
| 150 | + assert quoted == f'"{path}"' |
| 151 | + assert quoted[1:-1] == path |
| 152 | + |
| 153 | + |
| 154 | +def test_format_base_package_double_quotes_local_path_on_windows(monkeypatch): |
| 155 | + monkeypatch.setattr(sys, 'platform', 'win32') |
| 156 | + local_path = Path('/Users/Jane Doe/integrations-core/datadog_checks_base') |
| 157 | + |
| 158 | + token = DatadogChecksEnvironmentCollector.format_base_package(['deps'], local_path=local_path) |
| 159 | + |
| 160 | + assert token == f'"{local_path}"[deps]' |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.parametrize( |
| 164 | + 'script_name, config_flag', |
| 165 | + [ |
| 166 | + pytest.param('style', '--config', id='ruff_style'), |
| 167 | + pytest.param('fmt', '--config', id='ruff_fmt'), |
| 168 | + pytest.param('typing', '--config-file', id='mypy_typing'), |
| 169 | + ], |
| 170 | +) |
| 171 | +def test_lint_commands_survive_whitespace_checkout(fake_checkout, monkeypatch, tmp_path, script_name, config_flag): |
| 172 | + integrations_core, integration_root = fake_checkout(whitespace=True) |
| 173 | + monkeypatch.chdir(tmp_path) |
| 174 | + |
| 175 | + collector = DatadogChecksEnvironmentCollector(integration_root, {'check-types': True}) |
| 176 | + scripts = collector.get_initial_config()['lint']['scripts'] |
| 177 | + |
| 178 | + for command in scripts[script_name]: |
| 179 | + tokens = tokenize(command) |
| 180 | + if config_flag == '--config-file': |
| 181 | + # mypy's config path is built with `Path.__truediv__`, which uses the native separator. |
| 182 | + config_path = str(integrations_core / 'pyproject.toml') |
| 183 | + assert f'--config-file={config_path}' in tokens |
| 184 | + else: |
| 185 | + # ruff's config path is always joined with `/`, regardless of platform. |
| 186 | + config_path = f'{integrations_core}/pyproject.toml' |
| 187 | + assert config_path == tokens[tokens.index('--config') + 1] |
0 commit comments