Skip to content

Commit

Permalink
Use a dummy repository to run the upgrade_python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentClarret committed Oct 16, 2023
1 parent 5ba30fc commit a0c49c2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
46 changes: 46 additions & 0 deletions ddev/tests/cli/meta/scripts/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# (C) Datadog, Inc. 2023-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import pytest

from ddev.repo.core import Repository


@pytest.fixture
def fake_repo(tmp_path_factory, config_file, ddev):
repo_path = tmp_path_factory.mktemp('integrations-core')
repo = Repository('integrations-core', str(repo_path))

config_file.model.repos['core'] = str(repo.path)
config_file.save()

write_file(repo_path / 'ddev' / 'src' / 'ddev' / 'repo', 'constants.py', """# (C) Datadog, Inc. 2022-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
CONFIG_DIRECTORY = '.ddev'
NOT_SHIPPABLE = frozenset(['datadog_checks_dev', 'datadog_checks_tests_helper', 'ddev'])
FULL_NAMES = {
'core': 'integrations-core',
'extras': 'integrations-extras',
'marketplace': 'marketplace',
'agent': 'datadog-agent',
}
# This is automatically maintained
PYTHON_VERSION = '3.9'
""")

write_file(repo_path / 'dummy', 'hatch.toml', """[env.collectors.datadog-checks]
[[envs.default.matrix]]
python = ["2.7", "3.9"]
""")

yield repo


def write_file(folder, file, content):
folder.mkdir(exist_ok=True, parents=True)
file_path = folder / file
file_path.write_text(content)
25 changes: 14 additions & 11 deletions ddev/tests/cli/meta/scripts/test_upgrade_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,42 @@
# Licensed under a 3-clause BSD style license (see LICENSE)
from collections import defaultdict

from ddev.repo.constants import PYTHON_VERSION


def test_upgrade_python(ddev, repository):
major, minor = PYTHON_VERSION.split('.')
new_version = f'{major}.{int(minor) + 1}'
def test_upgrade_python(fake_repo, ddev):
new_version = "3.11"
old_version = "3.9"

changes = defaultdict(list)
for entry in repository.path.iterdir():
for entry in fake_repo.path.iterdir():
config_file = entry / 'hatch.toml'
if not config_file.is_file():
continue

for i, line in enumerate(config_file.read_text().splitlines()):
if line.startswith('python = [') and PYTHON_VERSION in line:
if line.startswith('python = [') and old_version in line:
changes[config_file].append(i)

minimum_changes = sum(map(len, changes.values()))

constant_file = repository.path / 'ddev' / 'src' / 'ddev' / 'repo' / 'constants.py'
constant_file = fake_repo.path / 'ddev' / 'src' / 'ddev' / 'repo' / 'constants.py'
contents = constant_file.read_text()

assert f'PYTHON_VERSION = {PYTHON_VERSION!r}' in contents
assert f'PYTHON_VERSION = {old_version!r}' in contents
assert f'PYTHON_VERSION = {new_version!r}' not in contents

result = ddev('meta', 'scripts', 'upgrade-python', new_version)

assert result.exit_code == 0, result.output
assert result.output.startswith('Python upgrades\n\nPassed: ')
assert result.output == 'Python upgrades\n\nPassed: 2\n'

passed = int(result.output.partition('Passed:')[2].strip())
assert passed >= minimum_changes

contents = constant_file.read_text()
assert f'PYTHON_VERSION = {PYTHON_VERSION!r}' not in contents
assert f'PYTHON_VERSION = {old_version!r}' not in contents
assert f'PYTHON_VERSION = {new_version!r}' in contents

hatch_file = fake_repo.path / 'dummy' / 'hatch.toml'
contents = hatch_file.read_text()
assert f'python = ["2.7", "{old_version}"]' not in contents
assert f'python = ["2.7", "{new_version}"]' in contents

0 comments on commit a0c49c2

Please sign in to comment.