From 4e3ca2d8e0cbaf65fe368bea165fe0d915297a15 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 11 Nov 2024 15:39:54 +0000 Subject: [PATCH 1/7] Add some basic tests --- env_cache.py | 2 +- test_env_cache.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test_env_cache.py diff --git a/env_cache.py b/env_cache.py index a97fc9d..6e4ac12 100644 --- a/env_cache.py +++ b/env_cache.py @@ -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" diff --git a/test_env_cache.py b/test_env_cache.py new file mode 100644 index 0000000..9d7bf94 --- /dev/null +++ b/test_env_cache.py @@ -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 From 47c27c23b49a27790f41f854b472c06bc88a57d8 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 11 Nov 2024 16:12:30 +0000 Subject: [PATCH 2/7] Add Github actions workflow to run tests --- .github/workflows/tests.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..ee61b64 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,25 @@ +name: Tests + +on: + push: + branches: [ main ] + pull_request: + +jobs: + test_pyenv: + runs-on: ubuntu-latest + name: install pyenv + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: setup pyenv + uses: "gabrielfalcao/pyenv-action@v18" + with: + default: 3.12.7 + versions: 3.11.10 + + - name: Test with pytest + run: | + python3 -m pip install pytest + python3 -m pytest -v From 175747d7e99b59f0ebb5923432e1ce410ac9dd84 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 11 Nov 2024 16:18:09 +0000 Subject: [PATCH 3/7] Different way of setting up pyenv --- .github/workflows/tests.yml | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ee61b64..e06ba28 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,17 +8,29 @@ on: jobs: test_pyenv: runs-on: ubuntu-latest - name: install pyenv + 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 tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev ffmpeg + - 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: Checkout uses: actions/checkout@v4 - - name: setup pyenv - uses: "gabrielfalcao/pyenv-action@v18" - with: - default: 3.12.7 - versions: 3.11.10 - - name: Test with pytest run: | python3 -m pip install pytest From f2b3bba9ef2aae6f5c9a59a97bf9ebe523d83562 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 11 Nov 2024 16:27:36 +0000 Subject: [PATCH 4/7] Preinstall & cache Python 3.11.10 for tests --- .github/workflows/tests.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e06ba28..4ce45ab 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,6 +28,17 @@ jobs: run: | pyenv --version + - name: Cache pyenv installed version + uses: actions/cache@v4 + with: + path: ~/.pyenv/versions + key: ${{ runner.os }}-pyenv-3.11.10 + + # Doing this beforehand should make the test itself faster + - name: Preinstall Python 3.11.10 + run: | + pyenv install 3.11.10 + - name: Checkout uses: actions/checkout@v4 From 02e912974551befef2c5831e90fa9d57f6fb39eb Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 11 Nov 2024 16:42:49 +0000 Subject: [PATCH 5/7] Show details of pyenv install process --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4ce45ab..fa5aa55 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: # Doing this beforehand should make the test itself faster - name: Preinstall Python 3.11.10 run: | - pyenv install 3.11.10 + pyenv install -v 3.11.10 - name: Checkout uses: actions/checkout@v4 From 840d3bd821904bb4c109c6c70ff32db62ec93de3 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 11 Nov 2024 16:48:09 +0000 Subject: [PATCH 6/7] Don't install tk-dev for CI --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fa5aa55..ae20c4d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,7 +16,7 @@ jobs: 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 tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev ffmpeg + libncursesw5-dev xz-utils libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev ffmpeg - name: Install pyenv run: | git clone https://github.com/pyenv/pyenv.git "$HOME/.pyenv" @@ -32,7 +32,7 @@ jobs: uses: actions/cache@v4 with: path: ~/.pyenv/versions - key: ${{ runner.os }}-pyenv-3.11.10 + key: ${{ runner.os }}-pyenv-3.11.10-b # Doing this beforehand should make the test itself faster - name: Preinstall Python 3.11.10 From 9e1e480f65b1606bcf3910f4fc5822d5c8159435 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 11 Nov 2024 16:52:16 +0000 Subject: [PATCH 7/7] Don't install ffmpeg for CI --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ae20c4d..4c6a032 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,7 +16,7 @@ jobs: 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 ffmpeg + 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" @@ -37,7 +37,7 @@ jobs: # Doing this beforehand should make the test itself faster - name: Preinstall Python 3.11.10 run: | - pyenv install -v 3.11.10 + pyenv install -v --skip-existing 3.11.10 - name: Checkout uses: actions/checkout@v4