mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-25 11:51:16 +01:00
test: download fixtures from pinned sources instead of committing them
Remove the sample documents, PDF and licence copies from tests/fixtures/. tests/fixtures/manifest.json lists each file's download URL (pinned to a commit for GitHub sources), size and SHA-256. tools/fetch_fixtures.py downloads them into the git-ignored tests/fixtures/files/ and verifies them. The fixture tests read from there and skip when it is absent.
This commit is contained in:
1 parent
e21d73aa0a
commit
bf72b93d23
10 files changed
+250
-34
No files matched your search
@@ -2,3 +2,4 @@
|
||||
/target/
|
||||
/.thoughts/
|
||||
__pycache__/
|
||||
/tests/fixtures/files/
|
||||
@@ -26,7 +26,11 @@ now). Licence: MIT.
|
||||
## Layout
|
||||
|
||||
- `src/header.rs`, `src/container.rs`: decoded layers (see `docs/format/`).
|
||||
- `tests/fixtures.rs`: smoke tests on committed open-licensed samples.
|
||||
- `tests/fixtures.rs`: smoke tests on open-licensed samples listed in
|
||||
`tests/fixtures/manifest.json`; they skip if `tests/fixtures/files/` is
|
||||
absent.
|
||||
- `tools/fetch_fixtures.py`: downloads the fixtures into the git-ignored
|
||||
`tests/fixtures/files/` and checks their SHA-256.
|
||||
- `tests/corpus.rs`: tests over the local `corpus/`; they skip if absent.
|
||||
- `tools/inventory.py`: corpus inventory (`python3 -I tools/inventory.py corpus/`).
|
||||
- `tools/root_values.py [--write]`: root style values that every corpus IDML
|
||||
@@ -45,6 +49,10 @@ the INDD version were probably re-saved after export; trust them less.
|
||||
## Commands
|
||||
|
||||
- `cargo test` (all tests), `cargo clippy --all-targets`, `cargo fmt`.
|
||||
- `python3 -I tools/fetch_fixtures.py`: fetch the fixture files before
|
||||
`cargo test`. To add a fixture, add its pinned URL, size and SHA-256 to
|
||||
`tests/fixtures/manifest.json` and its licence and source to
|
||||
`tests/fixtures/README.md`.
|
||||
- `cargo run -q -- info <file>`, `indd convert in.indd out.idml`,
|
||||
`indd objects <file>`, `indd dump <file> <uid>...`.
|
||||
- `python3 -I tools/compare.py [--detail TAG --show N]`: convert every
|
||||
|
||||
+3
-3
@@ -17,9 +17,9 @@ documents.
|
||||
- **Published source under compatible licences may be used**, with
|
||||
attribution. The list is in "Permitted references" below.
|
||||
- **The IDML side follows Adobe's published IDML specification.**
|
||||
- **Only redistributable samples are committed.** See
|
||||
`tests/fixtures/README.md`. Other samples stay in the git-ignored
|
||||
`corpus/` directory.
|
||||
- **No sample files are committed.** The open-licensed test fixtures
|
||||
are downloaded from their sources (`tests/fixtures/README.md`). Other
|
||||
samples stay in the git-ignored `corpus/` directory.
|
||||
|
||||
## Method
|
||||
|
||||
|
||||
@@ -117,8 +117,10 @@ version.
|
||||
- `tools/compare.py --all` also converts the files without a usable IDML.
|
||||
- With `--schemas` and `--jing`, every output is validated against the
|
||||
IDML RelaxNG schemas with Jing (`tools/validate.sh`).
|
||||
- `cargo test` runs smoke tests on the open-licensed samples in
|
||||
`tests/fixtures/` and, if `corpus/` exists, tests over the corpus.
|
||||
- `cargo test` runs smoke tests on the open-licensed samples listed in
|
||||
`tests/fixtures/manifest.json` (fetch them first with
|
||||
`python3 -I tools/fetch_fixtures.py`) and, if `corpus/` exists, tests
|
||||
over the corpus.
|
||||
|
||||
## Current numbers
|
||||
|
||||
@@ -170,6 +172,7 @@ template.
|
||||
|
||||
## Development
|
||||
|
||||
- `python3 -I tools/fetch_fixtures.py` downloads the test fixtures.
|
||||
- `cargo test`, `cargo clippy --all-targets`, `cargo fmt`.
|
||||
- `python3 -I tools/compare.py [--all] [--detail TAG --show N]
|
||||
[--schemas DIR --jing DIR]` after every change. The schemas and Jing
|
||||
@@ -181,5 +184,6 @@ template.
|
||||
|
||||
## Licence
|
||||
|
||||
MIT. Sample files in `tests/fixtures/` keep their own licences; see the
|
||||
README there.
|
||||
MIT. The repository contains no sample files. The test fixtures are
|
||||
downloaded from their sources and keep their own licences; see
|
||||
`tests/fixtures/README.md`.
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
Two sample files store their objects big-endian (byte order flag 2, see
|
||||
`header.md`): `opf-neddy-flyer` (InDesign 3.0) and `xmp-toolkit-bluesquare`
|
||||
(InDesign 4.0), both in `tests/fixtures/`. They are also the only samples
|
||||
from before InDesign 7.0, so some of their object layouts differ from the
|
||||
(InDesign 4.0), both test fixtures (`tests/fixtures/README.md`). They are
|
||||
also the only samples from before InDesign 7.0, so some of their object layouts differ from the
|
||||
rest of the corpus for reasons of version rather than byte order. Both
|
||||
kinds of difference are described here.
|
||||
|
||||
Neither file has an IDML. The facts below rest on the files' internal
|
||||
consistency, on the flyer's print PDF (in the fixture directory), and on
|
||||
consistency, on the flyer's print PDF (also a fixture), and on
|
||||
comparison with the layouts in the other documents of `docs/format/`.
|
||||
Implemented in `src/object.rs` (byte order) and `src/model/`.
|
||||
|
||||
|
||||
@@ -571,7 +571,7 @@ All 486 style names and 291 `NextStyle` values in the pairs match.
|
||||
|
||||
**Kind field.** The kind and the unidentified u16 after it can be read
|
||||
as one u32 in most files, because the second u16 is 0. The InDesign 7.5
|
||||
template in `tests/fixtures/scml-template/` (no IDML) has 1 there in 600
|
||||
template fixture `scml-template/scml.indt` (no IDML) has 1 there in 600
|
||||
of its styles: 151 have `00 00 01 00` before the flag and 449 have
|
||||
`01 00 01 00`. The 151 are all listed in the tree of the root character
|
||||
style group (chunk 0x28C2 = 0xCA0D, below) and the 449 in the tree of the
|
||||
|
||||
+46
-10
@@ -1,13 +1,31 @@
|
||||
//! Smoke tests on the redistributable sample files in `tests/fixtures/`.
|
||||
//! Smoke tests on the open-licensed sample files listed in
|
||||
//! `tests/fixtures/manifest.json`. `tools/fetch_fixtures.py` downloads them
|
||||
//! into the git-ignored `tests/fixtures/files/`. Each test passes without
|
||||
//! checking anything when that directory is absent.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use indd::{ByteOrder, Version, read_header};
|
||||
|
||||
fn fixture(rel: &str) -> String {
|
||||
format!("{}/tests/fixtures/{rel}", env!("CARGO_MANIFEST_DIR"))
|
||||
/// The fixture directory, or `None` (with a note) if it has not been fetched.
|
||||
fn fixtures() -> Option<PathBuf> {
|
||||
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/files");
|
||||
if root.is_dir() {
|
||||
Some(root)
|
||||
} else {
|
||||
eprintln!(
|
||||
"tests/fixtures/files/ not present; skipping \
|
||||
(fetch with `python3 -I tools/fetch_fixtures.py`)"
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixture_headers() {
|
||||
let Some(root) = fixtures() else {
|
||||
return;
|
||||
};
|
||||
let cases = [
|
||||
(
|
||||
"opf-neddy-flyer/Neddy_Flyer_HeatherRyan.indd",
|
||||
@@ -36,7 +54,7 @@ fn fixture_headers() {
|
||||
),
|
||||
];
|
||||
for (rel, order, major, minor) in cases {
|
||||
let h = read_header(fixture(rel)).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
let h = read_header(root.join(rel)).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
assert_eq!(&h.kind, b"DOCUMENT", "{rel}");
|
||||
assert_eq!(h.byte_order, order, "{rel}");
|
||||
assert_eq!(h.version, Version { major, minor }, "{rel}");
|
||||
@@ -45,12 +63,18 @@ fn fixture_headers() {
|
||||
|
||||
#[test]
|
||||
fn rejects_non_indd_file() {
|
||||
let err = read_header(fixture("opf-neddy-flyer/Neddy_Flyer_HeatherRyan.pdf")).unwrap_err();
|
||||
let Some(root) = fixtures() else {
|
||||
return;
|
||||
};
|
||||
let err = read_header(root.join("opf-neddy-flyer/Neddy_Flyer_HeatherRyan.pdf")).unwrap_err();
|
||||
assert!(matches!(err, indd::Error::NotIndd));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixture_containers() {
|
||||
let Some(root) = fixtures() else {
|
||||
return;
|
||||
};
|
||||
for rel in [
|
||||
"opf-neddy-flyer/Neddy_Flyer_HeatherRyan.indd",
|
||||
"scml-template/scml.indt",
|
||||
@@ -58,7 +82,7 @@ fn fixture_containers() {
|
||||
"lizdenys-minizine/indesign-minizine-template.indd",
|
||||
"xmp-toolkit-bluesquare/BlueSquare.indd",
|
||||
] {
|
||||
let bytes = std::fs::read(fixture(rel)).unwrap();
|
||||
let bytes = std::fs::read(root.join(rel)).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
let c = indd::Container::parse(&bytes).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
assert!(c.contig_start() < bytes.len(), "{rel}");
|
||||
let xmp = c.xmp().unwrap().unwrap_or_else(|| panic!("{rel}: no XMP"));
|
||||
@@ -72,6 +96,9 @@ fn fixture_containers() {
|
||||
|
||||
#[test]
|
||||
fn fixture_objects_read() {
|
||||
let Some(root) = fixtures() else {
|
||||
return;
|
||||
};
|
||||
for rel in [
|
||||
"opf-neddy-flyer/Neddy_Flyer_HeatherRyan.indd",
|
||||
"xmp-toolkit-bluesquare/BlueSquare.indd",
|
||||
@@ -79,7 +106,7 @@ fn fixture_objects_read() {
|
||||
"bootstrap3-template/bootstrap3-indesign-template.indd",
|
||||
"lizdenys-minizine/indesign-minizine-template.indd",
|
||||
] {
|
||||
let bytes = std::fs::read(fixture(rel)).unwrap();
|
||||
let bytes = std::fs::read(root.join(rel)).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
let c = indd::Container::parse(&bytes).unwrap();
|
||||
let db = c.database().unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
let mut total = 0;
|
||||
@@ -92,6 +119,9 @@ fn fixture_objects_read() {
|
||||
|
||||
#[test]
|
||||
fn big_endian_fixtures_convert() {
|
||||
let Some(root) = fixtures() else {
|
||||
return;
|
||||
};
|
||||
// InDesign 3.0 and 4.0. Neither has an IDML, so check values that can
|
||||
// be seen otherwise: the flyer's print PDF shows a letter-size page and
|
||||
// these headings; BlueSquare is a letter-size page with one blue square.
|
||||
@@ -124,7 +154,7 @@ fn big_endian_fixtures_convert() {
|
||||
][..],
|
||||
),
|
||||
] {
|
||||
let bytes = std::fs::read(fixture(rel)).unwrap();
|
||||
let bytes = std::fs::read(root.join(rel)).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
let mut out = Vec::new();
|
||||
indd::convert(&bytes, "test.indd", &mut out).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
assert_eq!(&out[..4], b"PK\x03\x04", "{rel}");
|
||||
@@ -137,12 +167,15 @@ fn big_endian_fixtures_convert() {
|
||||
|
||||
#[test]
|
||||
fn little_endian_fixtures_convert() {
|
||||
let Some(root) = fixtures() else {
|
||||
return;
|
||||
};
|
||||
for rel in [
|
||||
"scml-template/scml.indt",
|
||||
"bootstrap3-template/bootstrap3-indesign-template.indd",
|
||||
"lizdenys-minizine/indesign-minizine-template.indd",
|
||||
] {
|
||||
let bytes = std::fs::read(fixture(rel)).unwrap();
|
||||
let bytes = std::fs::read(root.join(rel)).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
let mut out = Vec::new();
|
||||
indd::convert(&bytes, "test.indd", &mut out).unwrap_or_else(|e| panic!("{rel}: {e}"));
|
||||
assert_eq!(&out[..4], b"PK\x03\x04", "{rel}");
|
||||
@@ -156,7 +189,10 @@ fn little_endian_fixtures_convert() {
|
||||
/// followed by a non-zero u16 (`docs/format/objects.md`, Styles).
|
||||
#[test]
|
||||
fn style_kind_is_a_u16() {
|
||||
let bytes = std::fs::read(fixture("scml-template/scml.indt")).unwrap();
|
||||
let Some(root) = fixtures() else {
|
||||
return;
|
||||
};
|
||||
let bytes = std::fs::read(root.join("scml-template/scml.indt")).unwrap();
|
||||
let mut out = Vec::new();
|
||||
indd::convert(&bytes, "test.indd", &mut out).unwrap();
|
||||
let text = String::from_utf8_lossy(&out);
|
||||
|
||||
Vendored
+36
-12
@@ -1,16 +1,40 @@
|
||||
# Test fixtures
|
||||
|
||||
Every file here is redistributable. Nothing else may be added to this
|
||||
directory: files without a licence that allows redistribution belong in the
|
||||
local, git-ignored `corpus/` directory.
|
||||
`tests/fixtures.rs` runs smoke tests on six open-licensed sample files:
|
||||
five InDesign documents and one print PDF. The files are not in the
|
||||
repository. `manifest.json` lists where to download each one, pinned to a
|
||||
commit where the source is a Git repository, with its size and SHA-256.
|
||||
|
||||
| Directory | File | InDesign version | Licence | Source |
|
||||
|---|---|---|---|---|
|
||||
| `opf-neddy-flyer` | `Neddy_Flyer_HeatherRyan.indd`, `.pdf` | 3.0 (big-endian) | CC0, Heather Ryan (waiver in the `.rtf` README) | [openpreserve/format-corpus](https://github.com/openpreserve/format-corpus/tree/master/desktop-publishing/InDesign) |
|
||||
| `lizdenys-minizine` | `indesign-minizine-template.indd` | 20.3 | CC0, Liz Denys | [lizdenys.com](https://lizdenys.com/journal/articles/indesign-minizine-template.html) |
|
||||
| `bootstrap3-template` | `bootstrap3-indesign-template.indd` | 9.2 | MIT, © 2014 Miix (see `LICENSE`) | [jeffing/bootstrap3-indesign-template](https://github.com/jeffing/bootstrap3-indesign-template) @ `59a7d9fbe360` |
|
||||
| `xmp-toolkit-bluesquare` | `BlueSquare.indd` | 4.0 (big-endian) | BSD 3-Clause, © Adobe (see `LICENSE`) | [adobe/XMP-Toolkit-SDK](https://github.com/adobe/XMP-Toolkit-SDK/tree/main/samples/testfiles) @ `7093513bd3ca` |
|
||||
| `scml-template` | `scml.indt` | 7.5 | MIT, © 2014 Scribe, Inc. (see `LICENSE`) | [scribenet/scr-scml-indesign-templates](https://github.com/scribenet/scr-scml-indesign-templates) @ `556f09acc5ab` |
|
||||
## Fetching
|
||||
|
||||
None of these has a matching IDML file. Use them for parser smoke tests:
|
||||
the file opens, the header is read, and the converter runs without error.
|
||||
```sh
|
||||
python3 -I tools/fetch_fixtures.py
|
||||
```
|
||||
|
||||
The script saves the files in `tests/fixtures/files/` (git-ignored),
|
||||
checks each one's size and SHA-256, and skips files that are already
|
||||
present and correct. It exits non-zero if any file fails.
|
||||
|
||||
Without `tests/fixtures/files/`, the fixture tests pass without checking
|
||||
anything and print a note. If the directory exists, every file must be
|
||||
present.
|
||||
|
||||
Never commit the downloaded files. Other samples stay in the local,
|
||||
git-ignored `corpus/` directory.
|
||||
|
||||
## Files
|
||||
|
||||
| Path under `files/` | InDesign version | Licence | Source |
|
||||
|---|---|---|---|
|
||||
| `opf-neddy-flyer/Neddy_Flyer_HeatherRyan.indd` | 3.0 (big-endian) | CC0, Heather Ryan (waiver in [the README next to it](https://github.com/openpreserve/format-corpus/blob/0bd37b0d10f397e0c2a7785befa2253f912b77fc/desktop-publishing/InDesign/Neddy_Flyer_README_HeatherRyan.md.rtf)) | [openpreserve/format-corpus](https://github.com/openpreserve/format-corpus/tree/0bd37b0d10f397e0c2a7785befa2253f912b77fc/desktop-publishing/InDesign) @ `0bd37b0d10f3` |
|
||||
| `opf-neddy-flyer/Neddy_Flyer_HeatherRyan.pdf` | Print PDF of the flyer | As above | As above |
|
||||
| `lizdenys-minizine/indesign-minizine-template.indd` | 20.3 | [CC0](https://creativecommons.org/publicdomain/zero/1.0/), Liz Denys | [lizdenys.com](https://lizdenys.com/journal/articles/indesign-minizine-template.html) (not versioned; the SHA-256 detects changes) |
|
||||
| `bootstrap3-template/bootstrap3-indesign-template.indd` | 9.2 | MIT, © 2014 Miix ([LICENSE](https://github.com/jeffing/bootstrap3-indesign-template/blob/59a7d9fbe360a495066a4dafb194ff59dde01c10/LICENSE)) | [jeffing/bootstrap3-indesign-template](https://github.com/jeffing/bootstrap3-indesign-template/tree/59a7d9fbe360a495066a4dafb194ff59dde01c10) @ `59a7d9fbe360` |
|
||||
| `xmp-toolkit-bluesquare/BlueSquare.indd` | 4.0 (big-endian) | BSD 3-Clause, © Adobe ([LICENSE](https://github.com/adobe/XMP-Toolkit-SDK/blob/7093513bd3caaad29da01db0f275d88a39d6bcc2/LICENSE)) | [adobe/XMP-Toolkit-SDK](https://github.com/adobe/XMP-Toolkit-SDK/tree/7093513bd3caaad29da01db0f275d88a39d6bcc2/samples/testfiles) @ `7093513bd3ca` |
|
||||
| `scml-template/scml.indt` | 7.5 | MIT, © 2014 Scribe, Inc. ([LICENSE](https://github.com/scribenet/scr-scml-indesign-templates/blob/556f09acc5ab48fa9957732c115f8eb78ca09717/LICENSE)) | [scribenet/scr-scml-indesign-templates](https://github.com/scribenet/scr-scml-indesign-templates/tree/556f09acc5ab48fa9957732c115f8eb78ca09717) @ `556f09acc5ab` |
|
||||
|
||||
None of the documents has a matching IDML file. The tests check that each
|
||||
file opens, its header and container are read, its objects are read, and
|
||||
the converter runs without error. For the two big-endian files they also
|
||||
check values that can be seen without an IDML (see
|
||||
`docs/format/big-endian.md`).
|
||||
Vendored
+53
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"comment": "Sample files for tests/fixtures.rs. Fetch them with `python3 -I tools/fetch_fixtures.py`; each `path` is relative to tests/fixtures/files/. Never commit the files.",
|
||||
"files": [
|
||||
{
|
||||
"path": "opf-neddy-flyer/Neddy_Flyer_HeatherRyan.indd",
|
||||
"url": "https://raw.githubusercontent.com/openpreserve/format-corpus/0bd37b0d10f397e0c2a7785befa2253f912b77fc/desktop-publishing/InDesign/Neddy_Flyer_HeatherRyan.indd",
|
||||
"size": 1503232,
|
||||
"sha256": "8f206d618c0161cfddf4c18aa3932ed99d9e500bf1260c2551e316b12a4e3ed2",
|
||||
"licence": "CC0-1.0",
|
||||
"source": "https://github.com/openpreserve/format-corpus/tree/0bd37b0d10f397e0c2a7785befa2253f912b77fc/desktop-publishing/InDesign"
|
||||
},
|
||||
{
|
||||
"path": "opf-neddy-flyer/Neddy_Flyer_HeatherRyan.pdf",
|
||||
"url": "https://raw.githubusercontent.com/openpreserve/format-corpus/0bd37b0d10f397e0c2a7785befa2253f912b77fc/desktop-publishing/InDesign/Neddy_Flyer_HeatherRyan.pdf",
|
||||
"size": 59106,
|
||||
"sha256": "6a3c9444d4905c8896a717be7c30ee7d20b3c319eb2d3d469393a0f0e3529243",
|
||||
"licence": "CC0-1.0",
|
||||
"source": "https://github.com/openpreserve/format-corpus/tree/0bd37b0d10f397e0c2a7785befa2253f912b77fc/desktop-publishing/InDesign"
|
||||
},
|
||||
{
|
||||
"path": "lizdenys-minizine/indesign-minizine-template.indd",
|
||||
"url": "https://lizdenys.com/uploads/zines/indesign-minizine-template/indesign-minizine-template.indd",
|
||||
"size": 1507328,
|
||||
"sha256": "189021fc8c1d051e9528f155348bbf63bc90eac856f33668180bdfa8972b8cd8",
|
||||
"licence": "CC0-1.0",
|
||||
"source": "https://lizdenys.com/journal/articles/indesign-minizine-template.html"
|
||||
},
|
||||
{
|
||||
"path": "bootstrap3-template/bootstrap3-indesign-template.indd",
|
||||
"url": "https://raw.githubusercontent.com/jeffing/bootstrap3-indesign-template/59a7d9fbe360a495066a4dafb194ff59dde01c10/bootstrap3-indesign-template.indd",
|
||||
"size": 2953216,
|
||||
"sha256": "712875094e04336b7b643678b53abba9b557e104a19bd8ba95abd19829bf6740",
|
||||
"licence": "MIT",
|
||||
"source": "https://github.com/jeffing/bootstrap3-indesign-template/tree/59a7d9fbe360a495066a4dafb194ff59dde01c10"
|
||||
},
|
||||
{
|
||||
"path": "xmp-toolkit-bluesquare/BlueSquare.indd",
|
||||
"url": "https://raw.githubusercontent.com/adobe/XMP-Toolkit-SDK/7093513bd3caaad29da01db0f275d88a39d6bcc2/samples/testfiles/BlueSquare.indd",
|
||||
"size": 757760,
|
||||
"sha256": "5e4d1db27034f91fdc915c5fed77767935ae57015963c83ada25033860e144e0",
|
||||
"licence": "BSD-3-Clause",
|
||||
"source": "https://github.com/adobe/XMP-Toolkit-SDK/tree/7093513bd3caaad29da01db0f275d88a39d6bcc2/samples/testfiles"
|
||||
},
|
||||
{
|
||||
"path": "scml-template/scml.indt",
|
||||
"url": "https://raw.githubusercontent.com/scribenet/scr-scml-indesign-templates/556f09acc5ab48fa9957732c115f8eb78ca09717/scml.indt",
|
||||
"size": 1187840,
|
||||
"sha256": "70acd6bc2a95003bafef085aab2bc9aeaa275069374f4e79332ea17e6d208ac2",
|
||||
"licence": "MIT",
|
||||
"source": "https://github.com/scribenet/scr-scml-indesign-templates/tree/556f09acc5ab48fa9957732c115f8eb78ca09717"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Download the test fixtures listed in tests/fixtures/manifest.json.
|
||||
|
||||
Each file is saved under tests/fixtures/files/ (git-ignored) and checked
|
||||
against the size and SHA-256 in the manifest. Files that are already
|
||||
present and match are not downloaded again. Exits non-zero if any file
|
||||
cannot be downloaded or does not match.
|
||||
|
||||
Usage: python3 -I tools/fetch_fixtures.py
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import urllib.request
|
||||
from pathlib import Path, PurePosixPath
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
MANIFEST = ROOT / "tests" / "fixtures" / "manifest.json"
|
||||
DEST = ROOT / "tests" / "fixtures" / "files"
|
||||
TIMEOUT = 60
|
||||
|
||||
|
||||
def sha256_of(path: Path) -> str:
|
||||
h = hashlib.sha256()
|
||||
with path.open("rb") as f:
|
||||
for chunk in iter(lambda: f.read(1 << 16), b""):
|
||||
h.update(chunk)
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
def matches(path: Path, size: int, sha256: str) -> bool:
|
||||
return path.is_file() and path.stat().st_size == size and sha256_of(path) == sha256
|
||||
|
||||
|
||||
def target(rel: str) -> Path:
|
||||
"""Resolve a manifest path, refusing anything outside DEST."""
|
||||
p = PurePosixPath(rel)
|
||||
if p.is_absolute() or ".." in p.parts or not p.parts:
|
||||
raise ValueError(f"bad path in manifest: {rel!r}")
|
||||
return DEST.joinpath(*p.parts)
|
||||
|
||||
|
||||
def download(url: str, path: Path, size: int, sha256: str) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=".part-")
|
||||
try:
|
||||
h = hashlib.sha256()
|
||||
n = 0
|
||||
req = urllib.request.Request(url, headers={"User-Agent": "indd-fetch-fixtures"})
|
||||
with os.fdopen(fd, "wb") as out, urllib.request.urlopen(req, timeout=TIMEOUT) as resp:
|
||||
while chunk := resp.read(1 << 16):
|
||||
n += len(chunk)
|
||||
if n > size:
|
||||
raise ValueError(f"more than the expected {size} bytes")
|
||||
h.update(chunk)
|
||||
out.write(chunk)
|
||||
if n != size:
|
||||
raise ValueError(f"got {n} bytes, expected {size}")
|
||||
if h.hexdigest() != sha256:
|
||||
raise ValueError(f"SHA-256 {h.hexdigest()}, expected {sha256}")
|
||||
os.replace(tmp, path)
|
||||
except BaseException:
|
||||
Path(tmp).unlink(missing_ok=True)
|
||||
raise
|
||||
|
||||
|
||||
def main() -> int:
|
||||
entries = json.loads(MANIFEST.read_text(encoding="utf-8"))["files"]
|
||||
failures = 0
|
||||
for e in entries:
|
||||
rel, size, sha256 = e["path"], e["size"], e["sha256"]
|
||||
try:
|
||||
path = target(rel)
|
||||
if matches(path, size, sha256):
|
||||
print(f"present {rel}")
|
||||
continue
|
||||
download(e["url"], path, size, sha256)
|
||||
print(f"fetched {rel}")
|
||||
except Exception as err:
|
||||
failures += 1
|
||||
print(f"FAILED {rel}: {err}", file=sys.stderr)
|
||||
print(f"{len(entries) - failures} of {len(entries)} fixtures ready in {DEST.relative_to(ROOT)}/")
|
||||
return 1 if failures else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user