From 28beb7d2e774fe74005b9f78324634fc692d176d Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Sat, 5 Dec 2026 22:45:20 +0100 Subject: [PATCH] test(compat): compare whole files, and replay rows of text files Rows of text files failed the matrix while the clients behaved as they should (found with the rows of tpl-MNI152NLin2009cSym's migration, whose CerebA lookup table gets an n/a row): * the scenario reported the first 200 bytes of each file, and the tests compared them with the whole file. It now also reports each file's sha256, which the tests compare; * the DataLad Archive wrote made-up bytes for every old file of the rows, while S3 serves the real bytes of text files. It now holds the bytes S3 serves before the table, and the tests expect those. The sample table also removes the CerebA table (n/a), so that the matrix runs a text file. Co-Authored-By: Claude Opus 5.5 --- CHANGES.rst | 3 ++- tests/compat/README.md | 8 +++++--- tests/compat/datalad_archive.py | 20 +++++++++++++++----- tests/compat/runtime/scenario.py | 5 +++++ tests/compat/sample-renames.tsv | 1 + tests/compat/test_compat.py | 14 ++++++++------ 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index ebac8fcc..9e3ba6ef 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -41,7 +41,8 @@ TemplateFlow 2.0 file names: the client reads the v2 skeleton and resolves old n in earlier releases. * MAINT: A compatibility matrix (``tests/compat``, workflow *Compatibility*) runs the released clients and this one against an Archive before and after a renames table, - in S3 and DataLad homes. It replays a proposed table before it is merged. + in S3 and DataLad homes. It replays a proposed table before it is merged, text files + included. * MAINT: The documentation builds on CircleCI again: Python 3.13, the ``doc`` extra instead of a conda environment, and pandoc for the notebooks. diff --git a/tests/compat/README.md b/tests/compat/README.md index c5998e9f..9212cd54 100644 --- a/tests/compat/README.md +++ b/tests/compat/README.md @@ -9,9 +9,9 @@ breaks no released client that reads it through S3. ```text test_compat.py (pytest, the branch installed) ├── archive.py the fixture Archive: names from the bundled v2 skeleton, made-up -│ content, annexed files as MD5E symlinks; skeletons built by the -│ real build-skeletons.py (templateflow/templateflow), before and -│ after the table +│ content for annexed files (MD5E symlinks), the bundled content of +│ text files; skeletons built by the real build-skeletons.py +│ (templateflow/templateflow), before and after the table ├── datalad_archive.py a one-template git-annex Archive for DataLad homes ├── server.py local S3 (404 answers as S3 sends them) and skeleton URLs └── client processes one uv environment per client (25.0.4, 25.1.2, branch) @@ -82,3 +82,5 @@ does not exercise (no removed files, say) are skipped. The sample table renames `tpl-MNI152Lin` files as the 2.0 rules do: `res-01` to `res-1` (entities kept), entity order with `res` (kept), `PET` to `pet` and `desc-head` to `label-head` (entities changed), one removed file (`n/a`) and one purged. +It also removes a text file, the CerebA lookup table of `tpl-MNI152NLin2009cSym` (`n/a`), +as that template's migration does. Results compare the whole file (its sha256). diff --git a/tests/compat/datalad_archive.py b/tests/compat/datalad_archive.py index 84036e87..e03a9de1 100644 --- a/tests/compat/datalad_archive.py +++ b/tests/compat/datalad_archive.py @@ -17,7 +17,7 @@ from functools import partial from pathlib import Path -from archive import GIT_ENV, KEYWORDS, content +from archive import GIT_ENV, KEYWORDS TEXT = ('template_description.json', 'LICENSE', 'CHANGES') @@ -41,13 +41,23 @@ def run(cwd: Path, *args: str, bin_dir: Path) -> str: class DataladArchive: - def __init__(self, work: Path, rows: list[tuple[str, str]], text: dict[str, bytes], bin_dir): + def __init__( + self, + work: Path, + rows: list[tuple[str, str]], + text: dict[str, bytes], + files: dict[str, bytes], + bin_dir, + ): """The rows of the :func:`busiest` template; ``text`` the Archive's text files; - ``bin_dir`` the folder with ``git-annex``.""" + ``files`` the bytes of the rows' old paths, as S3 serves them before the table (made + up for annexed files, real for text files); ``bin_dir`` the folder with + ``git-annex``.""" self.run = partial(run, bin_dir=bin_dir) self.template = template = busiest(rows) self.root = work / 'origin' / 'templateflow' self.rows = [(o, n) for o, n in rows if o.startswith(f'{template}/')] + self.contents = {old: files[old] for old, _ in self.rows} self.repo = self.root / template self.repo.mkdir(parents=True) self.run(self.root, 'git', 'init', '-q', '-b', 'master') @@ -59,8 +69,8 @@ def __init__(self, work: Path, rows: list[tuple[str, str]], text: dict[str, byte for name in TEXT: if f'{self.template}/{name}' in text: (self.repo / name).write_bytes(text[f'{self.template}/{name}']) - for old, _ in self.rows: - (self.root / old).write_bytes(content(old)) + for old, data in self.contents.items(): + (self.root / old).write_bytes(data) self.run(self.repo, 'git', 'annex', 'add', '-q', '.') self.run( self.repo, diff --git a/tests/compat/runtime/scenario.py b/tests/compat/runtime/scenario.py index eb874eba..2dda2341 100644 --- a/tests/compat/runtime/scenario.py +++ b/tests/compat/runtime/scenario.py @@ -18,6 +18,7 @@ Only uses the API that every client in the matrix has (25.0 and later). """ +import hashlib import json import sys import warnings @@ -62,6 +63,10 @@ def op_get(path): Path(p).read_bytes()[:200].decode(errors='replace') if Path(p).is_file() else None for p in paths ], + 'sha256': [ + hashlib.sha256(Path(p).read_bytes()).hexdigest() if Path(p).is_file() else None + for p in paths + ], 'warnings': [ f'{w.category.__name__}: {w.message}' for w in caught diff --git a/tests/compat/sample-renames.tsv b/tests/compat/sample-renames.tsv index 92accffe..8ccb1b7f 100644 --- a/tests/compat/sample-renames.tsv +++ b/tests/compat/sample-renames.tsv @@ -5,4 +5,5 @@ tpl-MNI152Lin/tpl-MNI152Lin_desc-SPM12_res-02_T1w.nii.gz tpl-MNI152Lin/tpl-MNI15 tpl-MNI152Lin/tpl-MNI152Lin_desc-SPM12_res-02_PET.nii.gz tpl-MNI152Lin/tpl-MNI152Lin_res-2_desc-SPM12_pet.nii.gz tpl-MNI152Lin/tpl-MNI152Lin_res-01_desc-head_mask.nii.gz tpl-MNI152Lin/tpl-MNI152Lin_res-1_label-head_mask.nii.gz tpl-MNI152Lin/tpl-MNI152Lin_desc-SPM12_res-02_SPECT.nii.gz n/a +tpl-MNI152NLin2009cSym/tpl-MNI152NLin2009cSym_atlas-CerebA_dseg.tsv n/a tpl-MNI152Lin/tpl-MNI152Lin_desc-SPM12_res-02_epi.nii.gz purge diff --git a/tests/compat/test_compat.py b/tests/compat/test_compat.py index 0e55ada1..6ac670ba 100644 --- a/tests/compat/test_compat.py +++ b/tests/compat/test_compat.py @@ -7,6 +7,7 @@ from __future__ import annotations +import hashlib import json import os import shutil @@ -194,7 +195,7 @@ def assert_found(result: dict, path: str, expected: bytes): assert result['error'] is None, result assert result['single'], result assert result['paths'] == [path], result - assert result['contents'][0] == expected.decode(), result + assert result['sha256'][0] == hashlib.sha256(expected).hexdigest(), result # Pinned clients: no update, the bundled skeleton ###################################### @@ -334,8 +335,9 @@ def datalad_archive(tmp_path, fixture, environments) -> DataladArchive: for path, data in fixture.files['before'].items() if path.count('/') == 1 and path.split('/', 1)[1] in TEXT } + files = {old: fixture.content('before', old) for old, _ in fixture.rows} bin_dir = environments('branch', datalad=True).parent - return DataladArchive(tmp_path / 'datalad', fixture.rows, text, bin_dir) + return DataladArchive(tmp_path / 'datalad', fixture.rows, text, files, bin_dir) def datalad_rows(archive_): @@ -348,7 +350,7 @@ def install_and_update(client, datalad_archive) -> None: renamed_, removed_ = datalad_rows(datalad_archive) found = client.get(*(old for old, _ in renamed_ + removed_)) for old, _ in renamed_ + removed_: - assert_found(found[old], old, archive.content(old)) + assert_found(found[old], old, datalad_archive.contents[old]) datalad_archive.apply() client.update() @@ -361,10 +363,10 @@ def test_branch_datalad(fixture, server, client_for, datalad_archive): renamed_, removed_ = datalad_rows(datalad_archive) found = client.get(*(old for old, _ in renamed_ + removed_)) for old, new in renamed_: - assert_found(found[old], new, archive.content(old)) + assert_found(found[old], new, datalad_archive.contents[old]) assert bool(found[old]['warnings']) is entities_change(old, new), found[old] for old, _ in removed_: - assert_found(found[old], old, archive.content(old)) + assert_found(found[old], old, datalad_archive.contents[old]) assert any('removed from the Archive' in w for w in found[old]['warnings']) @@ -378,7 +380,7 @@ def test_released_datalad(fixture, server, client_for, datalad_archive, name): need(rows, 'renames that keep the entities') found = client.get(*(old for old, _ in rows)) for old, new in rows: - assert_found(found[old], new, archive.content(old)) + assert_found(found[old], new, datalad_archive.contents[old]) @pytest.mark.xfail(