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 #2

Merged
merged 7 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Tests

on:
push:
branches: [ main ]
pull_request:

jobs:
test_pyenv:
runs-on: ubuntu-latest
name: Test with pyenv
steps:
- name: Install python build tools
# Ref: https://github.com/pyenv/pyenv/wiki#suggested-build-environment
run: |
sudo apt-get update -y
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
- name: Install pyenv
run: |
git clone https://github.com/pyenv/pyenv.git "$HOME/.pyenv"
PYENV_ROOT="$HOME/.pyenv"
PYENV_BIN="$PYENV_ROOT/bin"
echo "$PYENV_BIN" >> $GITHUB_PATH
echo "PYENV_ROOT=$PYENV_ROOT" >> $GITHUB_ENV
- name: Check pyenv version
run: |
pyenv --version

- name: Cache pyenv installed version
uses: actions/cache@v4
with:
path: ~/.pyenv/versions
key: ${{ runner.os }}-pyenv-3.11.10-b

# Doing this beforehand should make the test itself faster
- name: Preinstall Python 3.11.10
run: |
pyenv install -v --skip-existing 3.11.10

- name: Checkout
uses: actions/checkout@v4

- name: Test with pytest
run: |
python3 -m pip install pytest
python3 -m pytest -v
2 changes: 1 addition & 1 deletion env_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, python_exe):

def _get_version(self):
res = run([self.python_exe, '--version'], check=True, stdout=PIPE)
m = re.match("Python\s+(\S+)$", res.stdout.decode('utf-8'))
m = re.match(r"Python\s+(\S+)$", res.stdout.decode('utf-8'))
if not m:
raise ValueError(
f"python --version output ({res.stdout}) not as expected"
Expand Down
51 changes: 51 additions & 0 deletions test_env_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from shutil import which
from subprocess import run, PIPE, STDOUT
import sys

import pytest

from env_cache import (EnvsManager, FixedPythonEnvMaker, PyenvEnvMaker)

@pytest.mark.skipif(sys.version_info.releaselevel != 'final', reason='pre-release Python')
def test_fixed_python(tmp_path):
vi = sys.version_info
version = f"{vi.major}.{vi.minor}.{vi.micro}"

mgr = EnvsManager(tmp_path, FixedPythonEnvMaker(sys.executable))
env = mgr.get_env(version, "flit_core==3.10.1")
assert env.is_relative_to(tmp_path)
env_py = env / 'bin' / 'python'
assert env_py.is_file()
r = run([env_py, '-c', 'import flit_core'], stdout=PIPE, stderr=STDOUT)
if r.stdout:
print(r.stdout)
assert r.returncode == 0

# Asking for an env with the same specifications should reuse it
env_again = mgr.get_env(version, "flit_core==3.10.1")
assert env_again == env


def test_fixed_python_wrong_version(tmp_path):
mgr = EnvsManager(tmp_path, FixedPythonEnvMaker(sys.executable))
# Assume this test will never run on Python 3.2.1
with pytest.raises(ValueError, match="not 3.2.1"):
mgr.get_env("3.2.1", "")


@pytest.mark.skipif(which('pyenv') is None, reason="pyenv not available")
def test_pyenv(tmp_path):
version = "3.11.10"
mgr = EnvsManager(tmp_path, PyenvEnvMaker())
env = mgr.get_env(version, "flit_core==3.10.1")
assert env.is_relative_to(tmp_path)
env_py = env / 'bin' / 'python'
assert env_py.is_file()
r = run([env_py, '-c', 'import flit_core'], stdout=PIPE, stderr=STDOUT)
if r.stdout:
print(r.stdout)
assert r.returncode == 0

# Asking for an env with the same specifications should reuse it
env_again = mgr.get_env(version, "flit_core==3.10.1")
assert env_again == env