Tests: use time machine to “pin” Anki dependencies

Packages anki and aqt do not pin their requirements,
which breaks anki <= 2.1.49 because of protobuf major version bump.

While it's possible to use requirements.txt as constraints,
it might not be the best idea:
  * requirements.txt can’t be used as constraints
    without some careful sed’ding to remove hashes and extras;
  * its location changes between versions;
  * you can’t get this file for a pre-release version of Anki
    since these don't get git tags;
  * it’s rarely updated anyway and hence won’t reliably
    produce the environment that actual Anki uses.

So use a pypi-timemachine, which prevents pip from using packages
that were uploaded after a specified date.
While the code is a tad complex, it feels slightly less hacky,
and is likely to be producing more accurate environments.
This commit is contained in:
oakkitten 2022-06-07 23:04:13 +01:00
parent 5774ef7346
commit 863cef5e64
2 changed files with 45 additions and 23 deletions

20
tox-install-command Normal file
View File

@ -0,0 +1,20 @@
#!/bin/bash
set -eux
trap '[[ -v SERVER_PID ]] && pkill -P $SERVER_PID' EXIT
print_first_group() { perl -snle 'm/$re/; print $1; exit 0' -- -re="$1"; }
envname="$1"
toxworkdir="$2"
packages=("${@:3}")
version=$(print_first_group 'anki([\d\.a-z]+)' <<< "$envname")
upload_time=$(curl https://pypi.org/pypi/anki/json \
| jq --arg v "$version" -r '.releases[$v][0].upload_time_iso_8601')
cutoff_time=$(date --utc -d "$upload_time +1 hour" '+%Y-%m-%dT%H:%M:%S')
coproc SERVER { "$toxworkdir"/.tox/bin/python -um pypi_timemachine "$cutoff_time"; }
index_url=$(print_first_group '(http\S+)' <&"${SERVER[0]}")
python -m pip install --index-url "$index_url" "anki==$version" "$AQT==$version"
python -m pip install "${packages[@]}"

48
tox.ini
View File

@ -42,42 +42,44 @@
# QT_DEBUG_PLUGINS=1
# ANKIDEV=1
# Note: pypi packages anki and aqt do not pin their dependencies.
# To tests against historically accurate dependencies, we use a “time machine”
# that prevents pip from using packages that were uploaded after a specified date.
[tox]
minversion = 3.24
skipsdist = true
skip_install = true
envlist = py38-anki{45,46,47,48,49},py39-anki{50qt5,50qt6}
requires =
pypi-timemachine
envlist =
py38-anki2.1.{45,46,47,48,49}
py39-anki2.1.50-qt{5,6}
[testenv:.tox]
install_command =
python -m pip install {packages}
[testenv]
install_command =
bash tox-install-command {envname} {toxworkdir} {packages}
commands =
env HOME={envtmpdir}/home xvfb-run python -m pytest {posargs}
setenv =
anki50qt6: DISABLE_QT5_COMPAT=1
DISABLE_QT5_COMPAT=1
!qt{5,6}: AQT=aqt
qt5: AQT=aqt[qt5]
qt6: AQT=aqt[qt6]
allowlist_externals =
bash
env
xvfb-run
deps =
pytest==7.1.1
pytest-forked==1.4.0
pytest-anki @ git+https://github.com/oakkitten/pytest-anki.git@a0d27aa5
anki45: anki==2.1.45
anki45: aqt==2.1.45
anki46: anki==2.1.46
anki46: aqt==2.1.46
anki47: anki==2.1.47
anki47: aqt==2.1.47
anki48: anki==2.1.48
anki48: aqt==2.1.48
anki49: anki==2.1.49
anki49: aqt==2.1.49
anki50qt5: anki==2.1.50
anki50qt5: aqt[qt5]==2.1.50
anki50qt6: anki==2.1.50
anki50qt6: aqt[qt6]==2.1.50