Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Add example project for offline builds #340

Merged
merged 8 commits into from
Nov 22, 2023
57 changes: 57 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import shutil
import sys
from pathlib import Path

import pytest

from helpers import chdir, check_run


if sys.version_info >= (3, 9):
removesuffix = str.removesuffix
else:
def removesuffix(s: str, suffix: str) -> str:
if not s.endswith(suffix):
return s
return s[:-len(suffix)]


def _src_name(src):
if isinstance(src, Path):
return src.name

return removesuffix(src, '.git').rsplit('/', 1)[1]


# pylint: disable=redefined-outer-name
# would trigger on any fixture which uses another fixture defined in the same file

@pytest.fixture(scope="session", ids=_src_name, params=(
Path(__file__).parent / 'fixtures' / 'minimal-package',
Path(__file__).parent.parent, # bork's source tree
) + tuple(pytest.param(url, marks=pytest.mark.network) for url in (
"https://github.com/astronouth7303/ppb-mutant.git",
"https://github.com/duckinator/emanate.git",
"https://github.com/ppb/ppb-vector.git",
)))
def project_src(request, tmp_path_factory):
"""Provide a source tree, which is cached over the whole pytest run.

This fixture is parameterized over all the source trees that are built during tests,
making any test (or fixture) that uses it parameterized itself.
"""
if isinstance(request.param, Path):
return request.param

url = request.param
dest = tmp_path_factory.mktemp(_src_name(url))
check_run(['git', 'clone', '--depth', '1', url, dest])
return dest


@pytest.fixture(scope="function")
def project(project_src, tmp_path):
"""Provide a fresh copy of a source tree, which is discarded after the current test."""
shutil.copytree(project_src, tmp_path, dirs_exist_ok=True)
with chdir(tmp_path):
yield tmp_path
8 changes: 8 additions & 0 deletions tests/fixtures/minimal-package/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build-system]
# Specify the required build system.
requires = ["setuptools >= 61", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "test-project-please-ignore"
version = "0"
2 changes: 2 additions & 0 deletions tests/fixtures/minimal-package/src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""Test module, please ignore. 🥺
"""
13 changes: 12 additions & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import subprocess
import sys
import tarfile
import zipfile

from contextlib import contextmanager
from pathlib import Path

def check_tgz(path):
assert tarfile.is_tarfile(path)
Expand Down Expand Up @@ -37,3 +39,12 @@ def python_check(*args):

def bork_check(*args):
return python_check("-m", "bork", *args)


@contextmanager
def chdir(path):
cwd = Path.cwd()

os.chdir(path)
yield
os.chdir(cwd)
53 changes: 25 additions & 28 deletions tests/test_cmd_download.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import os

import pytest
from helpers import bork_check, python_check, check_zipfile, check_tgz
from helpers import chdir, bork_check, python_check, check_zipfile, check_tgz


@pytest.mark.network
def test_download(tmpdir):
os.chdir(tmpdir)

# Download a pyz file from GitHub, saved to ./downloads, and check
# that it can be run with Python.
bork_check("download", "gh:duckinator/emanate", "v7.0.0")
python_check("downloads/emanate-7.0.0.pyz", "--help")

# Download a pyz file for a specific version from GitHub, saved to ./bin
bork_check("download", "gh:duckinator/emanate", "v7.0.0", "--directory", "bin/")
python_check("bin/emanate-7.0.0.pyz", "--help")

# Download a .tar.gz file from GitHub, saved to ./downloads
bork_check("download",
"gh:ppb/pursuedpybear", "v0.6.0",
"--files", "*.tar.gz",
"--directory", "downloads")
assert check_tgz("downloads/ppb-0.6.0.tar.gz")

# Download a .whl file from PyPi, and verify it's uncorrupted.
bork_check("download", "pypi:emanate", "v6.0.0", "--files", "*.whl")
assert check_zipfile("downloads/emanate-6.0.0-py3-none-any.whl")

# Download a .whl file from PyPi's test instance, and verify it's uncorrupted.
bork_check("download", "pypi-test:whaledo", "1.0.1", "--files", "*.whl")
assert check_zipfile("downloads/whaledo-1.0.1-py3-none-any.whl")
with chdir(tmpdir):
# Download a pyz file from GitHub, saved to ./downloads, and check
# that it can be run with Python.
bork_check("download", "gh:duckinator/emanate", "v7.0.0")
python_check("downloads/emanate-7.0.0.pyz", "--help")

# Download a pyz file for a specific version from GitHub, saved to ./bin
bork_check("download", "gh:duckinator/emanate", "v7.0.0", "--directory", "bin/")
python_check("bin/emanate-7.0.0.pyz", "--help")

# Download a .tar.gz file from GitHub, saved to ./downloads
bork_check("download",
"gh:ppb/pursuedpybear", "v0.6.0",
"--files", "*.tar.gz",
"--directory", "downloads")
assert check_tgz("downloads/ppb-0.6.0.tar.gz")

# Download a .whl file from PyPi, and verify it's uncorrupted.
bork_check("download", "pypi:emanate", "v6.0.0", "--files", "*.whl")
assert check_zipfile("downloads/emanate-6.0.0-py3-none-any.whl")

# Download a .whl file from PyPi's test instance, and verify it's uncorrupted.
bork_check("download", "pypi-test:whaledo", "1.0.1", "--files", "*.whl")
assert check_zipfile("downloads/whaledo-1.0.1-py3-none-any.whl")
37 changes: 5 additions & 32 deletions tests/test_repos.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
import os
from pathlib import Path

import pytest
from helpers import bork_check, check_run
from helpers import bork_check

# pylint: disable=unused-argument
# the `project` feature is used implicitly, as it changes the working directory

def _repo_test(tmpdir, repo):
os.chdir(tmpdir)
check_run(['git', 'clone', '--depth', '1', repo, str(Path(tmpdir, 'repo'))])
os.chdir('repo')
@pytest.mark.slow
def test_repo(project):
bork_check('clean')
bork_check('build')
bork_check('release', '--dry-run')


@pytest.mark.network
@pytest.mark.slow
def test_repo_ppb_vector(tmpdir):
_repo_test(tmpdir, 'https://github.com/ppb/ppb-vector.git')


@pytest.mark.network
@pytest.mark.slow
def test_repo_emanate(tmpdir):
_repo_test(tmpdir, 'https://github.com/duckinator/emanate.git')


@pytest.mark.network
@pytest.mark.slow
def test_repo_bork(tmpdir):
_repo_test(tmpdir, 'https://github.com/duckinator/bork.git')


@pytest.mark.network
@pytest.mark.slow
def test_repo_ppb_mutant(tmpdir):
_repo_test(tmpdir, 'https://github.com/astronouth7303/ppb-mutant.git')