Skip to content

Commit

Permalink
📦 Stop relying on in-tree pkg version helper
Browse files Browse the repository at this point in the history
Closes #57
  • Loading branch information
webknjaz committed Aug 25, 2024
1 parent 116222c commit c4f995b
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 143 deletions.
17 changes: 0 additions & 17 deletions octomachinery/utils/versiontools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Version tools set."""

import os
from typing import Callable, Optional, Union

from setuptools_scm import get_version
Expand All @@ -24,19 +23,3 @@ def get_version_from_scm_tag(
)
except LookupError:
return 'unknown'


def cut_local_version_on_upload(version):
"""Return empty local version if uploading to PyPI."""
is_pypi_upload = os.getenv('PYPI_UPLOAD') == 'true'
if is_pypi_upload:
return ''

# pylint: disable=import-outside-toplevel
import setuptools_scm.version # only available during setup time
return setuptools_scm.version.get_local_node_and_date(version)


def get_self_version():
"""Calculate the version of the dist itself."""
return get_version_from_scm_tag(local_scheme=cut_local_version_on_upload)
6 changes: 0 additions & 6 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ universal = 0

[metadata]
name = octomachinery
version = attr: octomachinery.utils.versiontools.get_self_version
url = https://octomachinery.dev
project_urls =
Chat: Matrix = https://matrix.to/#/#octomachinery:matrix.org
Expand Down Expand Up @@ -89,11 +88,6 @@ packages = find_namespace:
zip_safe = True
include_package_data = True

# These are required during `setup.py` run:
setup_requires =
setuptools_scm >= 1.15.0
setuptools_scm_git_archive >= 1.0

# These are required in actual runtime:
install_requires =
aiohttp
Expand Down
119 changes: 1 addition & 118 deletions tests/utils/versiontools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
import contextlib
import os
import pathlib
import subprocess
import tempfile

import pytest

import setuptools_scm.version

from octomachinery.utils.versiontools import (
cut_local_version_on_upload, get_self_version, get_version_from_scm_tag,
)
from octomachinery.utils.versiontools import get_version_from_scm_tag


@contextlib.contextmanager
Expand All @@ -34,118 +29,6 @@ def temporary_working_directory():
yield path


@pytest.fixture
def git_cmd():
"""Provide a Git command helper."""
git_cmd_ = ('git',)
# pylint: disable=unexpected-keyword-arg
return lambda *args: subprocess.check_output(git_cmd_ + args, text=True)


@pytest.fixture
def git_init_cmd(git_cmd):
"""Provide a Git init command helper."""
return lambda *args: git_cmd(*(('init',) + args))


@pytest.fixture
def git_config_cmd(git_cmd):
"""Provide a Git config command helper."""
return lambda *args: git_cmd(*(('config', '--local') + args))


@pytest.fixture
def git_commit_cmd(git_cmd):
"""Provide a Git commit command helper."""
return lambda *args: git_cmd(*(('commit', '--allow-empty') + args))


@pytest.fixture
def git_tag_cmd(git_cmd):
"""Provide a Git tag command helper."""
return lambda *args: git_cmd(*(('tag',) + args))


@pytest.fixture
def tmp_git_repo(temporary_working_directory, git_config_cmd, git_init_cmd):
"""Create a temporary Git repo and cd there, coming back upon cleanup."""
git_init_cmd()
git_config_cmd('user.name', 'Test User')
git_config_cmd('user.email', 'test.user@example.com')
yield temporary_working_directory


def test_get_self_version_in_git_repo(
monkeypatch,
tmp_git_repo,
git_cmd, git_commit_cmd, git_tag_cmd,
):
"""Check that get_self_version works properly in existing Git repo."""
assert get_self_version() == '0.1.dev0'

git_commit_cmd('-m', 'Test commit')
git_tag_cmd('v1.3.9')
assert get_self_version() == '1.3.9'

git_commit_cmd('-m', 'Test commit 2')
head_sha1_hash = git_cmd('rev-parse', '--short', 'HEAD').strip()
assert get_self_version() == f'1.3.10.dev1+g{head_sha1_hash}'

with monkeypatch.context() as mp_ctx:
mp_ctx.setenv('PYPI_UPLOAD', 'true')
assert get_self_version() == '1.3.10.dev1'


def test_get_self_version_outside_git_repo(temporary_working_directory):
"""Check that version is unknown outside of Git repo."""
assert get_self_version() == 'unknown'


def test_cut_local_version_on_upload(monkeypatch, tmp_git_repo):
"""Test that PEP440 local version isn't emitted when upload."""
scm_node = 'gfe99188'
try:
ver = setuptools_scm.version.ScmVersion(
'v1.1.4',
distance=3, node='gfe99188',
dirty=False, branch='master',
config=setuptools_scm.config.Configuration(),
)
except AttributeError:
# pylint: disable-next=no-value-for-parameter
ver = setuptools_scm.version.ScmVersion(
'v1.1.4',
distance=3, node='gfe99188',
dirty=False, branch='master',
)
assert cut_local_version_on_upload(ver) == f'+{scm_node}'

with monkeypatch.context() as mp_ctx:
mp_ctx.setenv('PYPI_UPLOAD', 'true')
assert cut_local_version_on_upload(ver) == ''


def test_get_version_from_scm_tag_in_git_repo(
monkeypatch,
tmp_git_repo,
git_cmd, git_commit_cmd, git_tag_cmd,
):
"""Check that get_version_from_scm_tag works properly in Git repo."""
assert get_self_version() == '0.1.dev0'

git_commit_cmd('-m', 'Test commit')
git_tag_cmd('v1.3.9')
assert get_version_from_scm_tag() == '1.3.9'

git_commit_cmd('-m', 'Test commit 2')
head_sha1_hash = git_cmd('rev-parse', '--short', 'HEAD').strip()
assert get_version_from_scm_tag() == f'1.3.10.dev1+g{head_sha1_hash}'

with monkeypatch.context() as mp_ctx:
mp_ctx.setenv('PYPI_UPLOAD', 'true')
assert get_version_from_scm_tag() == f'1.3.10.dev1+g{head_sha1_hash}'


def test_get_version_from_scm_tag_outside_git_repo(
temporary_working_directory,
):
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ usedevelop = false
skip_install = true
deps =
build >= 0.3.1, < 0.4.0
setenv =
PYPI_UPLOAD = true
commands =
{envpython} -c \
"import shutil; \
Expand Down

0 comments on commit c4f995b

Please sign in to comment.