diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 09:12:28 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 09:12:28 +0000 |
| commit | d9c8402180749df4452dc37febace4d933b8407d (patch) | |
| tree | 377a7b89203c266d00a714845b50d27febbda4c7 /tests/world.rs | |
| parent | 4cb2f4c7b68dceaed8f7ffbd8f0db3cfbf3f1ddc (diff) | |
| download | aur-build-d9c8402180749df4452dc37febace4d933b8407d.tar.gz aur-build-d9c8402180749df4452dc37febace4d933b8407d.zip | |
pkginfo: .PKGINFO/.BUILDINFO parsing gets a real home, and a parser bug fix (§10.1)
Ingest decides what the index claims about a package and what the world
snapshot records by reading two text files out of a tar inside a zstd stream.
That code was living in src/shims — the test harness — which meant production
depended on the harness for its view of a package's own metadata. Moved to
src/pkginfo.rs, with the shim now delegating to it so the harness and the
production path cannot disagree about what a package says about itself.
Still no libalpm: tar + zstd + eighty lines of key = value parsing. Unknown keys
are ignored rather than rejected, because makepkg adds fields over time and a new
one must not stop us publishing.
**The parser had a real bug.** A .BUILDINFO installed block puts its first entry
on the same line as the key:
installed = glibc 2.39-1 x86_64 -
gmp 6.3.0-1 x86_64 -
The old code skipped the header line, so the first dependency was silently
dropped from every snapshot — exactly the kind of gap that makes staleness
quietly wrong. The header line is parsed now.
tests/world.rs builds real .pkg.tar.zst files and asserts the fields come out of
the archive: repeated keys stay lists, an archive with no .PKGINFO is refused
rather than defaulted from the filename, and the world snapshot contains our own
packages and none of glibc/pacman/binutils. The empty-snapshot case for a first
build is asserted too — nothing of ours existed yet, and recording that honestly
beats inventing a world.
Split pkgbases produce one approval, one release and N index rows each with its
own artifact, asserted directly here. 143 tests pass, clippy clean.
Diffstat (limited to 'tests/world.rs')
| -rw-r--r-- | tests/world.rs | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/tests/world.rs b/tests/world.rs new file mode 100644 index 0000000..8229d62 --- /dev/null +++ b/tests/world.rs @@ -0,0 +1,226 @@ +//! `.PKGINFO` / `.BUILDINFO` parsing and the world snapshot (§10.1, I12). +//! +//! The world snapshot is the whole basis of staleness: "would rebuilding this +//! change what a consumer gets?" only means something if we recorded, at build +//! time, the exact set of our packages the build saw. Two properties matter and +//! are tested here — that the record comes from the archive rather than from +//! anything the build asserted about itself, and that it is intersected with our +//! own index **at ingest time**, because an unfiltered `.BUILDINFO` would make +//! every package permanently stale (Arch never stops moving) and the Attention +//! page would be noise forever. + +mod common; + +use aur_build::db; +use aur_build::pipeline; +use aur_build::pkginfo; +use common::*; + +/// Build a real `.pkg.tar.zst`: zstd-compressed tar with the two metadata files +/// inside, exactly as makepkg lays them out. `None` for either means the file is +/// absent from the archive. +fn make_archive(dir: &std::path::Path, name: &str, pkginfo: Option<&str>, buildinfo: Option<&str>) -> std::path::PathBuf { + let path = dir.join(format!("{name}.pkg.tar.zst")); + let enc = std::io::BufWriter::new(std::fs::File::create(&path).unwrap()); + let mut enc = zstd::Encoder::new(enc, 3).unwrap(); + { + let mut ar = tar::Builder::new(&mut enc); + let mut add = |rel: String, body: &str| { + let mut h = tar::Header::new_gnu(); + h.set_size(body.len() as u64); + h.set_mode(0o644); + h.set_mtime(0); + ar.append_data(&mut h, rel, std::io::Cursor::new(body.as_bytes())).unwrap(); + }; + if let Some(p) = pkginfo { + add(format!("{name}/.PKGINFO"), p); + } + if let Some(b) = buildinfo { + add(format!("{name}/.BUILDINFO"), b); + } + add(format!("{name}/usr/bin/{name}"), "#!/bin/sh\necho hi\n"); + ar.finish().unwrap(); + } + enc.finish().unwrap(); + path +} + +const PKGINFO: &str = "\ +pkgname = plain-make +pkgbase = plain-make +pkgver = 1.0-1 +pkgarch = x86_64 +pkgdesc = a synthetic package +url = https://example.invalid/plain-make +license = MIT +license = Apache +depend = glibc +depend = openssl>=1.1 +makedepend = cmake +provides = make-thing +conflict = other-make +"; + +const BUILDINFO: &str = "\ +format = 2 +pkgname = plain-make +pkgbase = plain-make +pkgver = 1.0-1 +packager = builduser +builddate = 1700000000 +installed = glibc 2.39-1 x86_64 - + gmp 6.3.0-1 x86_64 - + plain-make 0.9-1 x86_64 - +sigfile = /dev/null +"; + +#[test] +fn pkginfo_fields_are_parsed_from_the_archive_itself() { + let dir = tempfile::tempdir().unwrap(); + let path = make_archive(dir.path(), "plain-make", Some(PKGINFO), Some(BUILDINFO)); + let bytes = std::fs::read(&path).unwrap(); + + let text = pkginfo::pkginfo_text(&bytes).expect(".PKGINFO is in the archive"); + assert!(text.contains("pkgname = plain-make")); + let meta = pkginfo::parse_pkginfo(&text); + assert_eq!(meta.name, "plain-make"); + assert_eq!(meta.version, "1.0-1"); + assert_eq!(meta.arch, "x86_64"); + assert_eq!(meta.desc, "a synthetic package"); + // Repeated keys are lists, not last-one-wins. + assert_eq!(meta.license, vec!["MIT", "Apache"]); + assert_eq!(meta.depends, vec!["glibc", "openssl>=1.1"]); + assert_eq!(meta.makedepends, vec!["cmake"]); + assert_eq!(meta.provides, vec!["make-thing"]); + assert_eq!(meta.conflicts, vec!["other-make"]); +} + +#[test] +fn the_buildinfo_installed_block_is_parsed_line_by_line() { + let dir = tempfile::tempdir().unwrap(); + let path = make_archive(dir.path(), "plain-make", Some(PKGINFO), Some(BUILDINFO)); + let bytes = std::fs::read(&path).unwrap(); + let text = pkginfo::buildinfo_text(&bytes).unwrap(); + let installed = pkginfo::parse_installed(&text); + assert_eq!( + installed.iter().map(|i| (i.name.clone(), i.version.clone())).collect::<Vec<_>>(), + vec![ + ("glibc".to_string(), "2.39-1".to_string()), + ("gmp".to_string(), "6.3.0-1".to_string()), + ("plain-make".to_string(), "0.9-1".to_string()), + ], + "the installed block must come through exactly" + ); + // The block ends at the first unindented line; `sigfile` is not an install. + assert!(!installed.iter().any(|i| i.name == "sigfile")); +} + +#[test] +fn a_package_without_a_pkginfo_is_refused_not_defaulted() { + // Ingest decides what the index claims about a package. Guessing a name from + // the filename would let a mislabeled artifact into a signed repo. + let dir = tempfile::tempdir().unwrap(); + let path = make_archive(dir.path(), "mystery", None, Some(BUILDINFO)); + let bytes = std::fs::read(&path).unwrap(); + assert!(pkginfo::pkginfo_text(&bytes).is_err(), "an archive with no .PKGINFO must not parse"); + // And a truncated archive is an error too, not an empty success. + assert!(pkginfo::pkginfo_text(b"not even a zstd stream").is_err()); +} + +#[test] +fn the_world_snapshot_is_intersected_with_our_index_at_ingest_time() { + // §10.1 / I12. The fixture's build root contains glibc, pacman, binutils and + // plain-make; only plain-make is ours, and only plain-make may be recorded. + let (env, fx) = env_with_fixture("plain-make"); + // The first build has no world of ours to record — nothing we publish exists + // yet. That is the honest answer, not an empty lie about glibc. + publish(&env, "plain-make"); + let first = world_of_last_build(&env); + assert!(first.is_empty(), "nothing of ours existed for the first build: {first:?}"); + + // Now plain-make is in the index, so a later build records it and nothing + // else. A real chroot with our repo enabled has the previous version of the + // package installed; the manifest says the same. + let mut manifest = fx.manifest_json(); + manifest["installed"] = serde_json::json!([ + { "name": "glibc", "version": "2.39-1", "arch": "x86_64" }, + { "name": "pacman", "version": "6.1.0-1", "arch": "x86_64" }, + { "name": "binutils", "version": "2.42-1", "arch": "x86_64" }, + { "name": "plain-make", "version": "1.0-1", "arch": "x86_64" } + ]); + std::fs::write(&fx.manifest, serde_json::to_string(&manifest).unwrap()).unwrap(); + fx.bump(&[("pkgver", "3.0")]); + publish(&env, "plain-make"); + let members = world_of_last_build(&env); + assert!(members.iter().any(|m| m.pkgbase == "plain-make"), "our own package belongs in the world: {members:?}"); + for foreign in ["glibc", "pacman", "binutils"] { + assert!( + !members.iter().any(|m| m.pkgbase == foreign), + "{foreign} is not ours and must not be in the snapshot — an unfiltered world makes every package permanently stale" + ); + } + // And the snapshot is attached to the build, not computed later. + assert_eq!( + env.query_i64("SELECT count(*) FROM build WHERE state='succeeded' AND world_snapshot_id IS NULL", &[]), + Some(0), + "I12: a succeeded build must have a recorded world" + ); +} + +#[test] +fn a_split_pkgbase_is_one_approval_and_many_index_rows() { + // §3: one recipe, several packages, one human decision. Every package the + // build produced gets its own index entry and its own artifact. + let (env, _fx) = env_with_fixture("split-pkgbase"); + publish(&env, "split-pkgbase"); + let rows: Vec<String> = env + .db + .read(|c| { + let mut st = c.prepare( + "SELECT p.name, p.version, p.artifact_id FROM package p + JOIN release r ON r.id = p.release_id + WHERE p.state='in_index' ORDER BY p.name", + )?; + let out = st + .query_map([], |r| Ok(format!("{} {} art={:?}", r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<i64>>(2)?)))? + .filter_map(|x| x.ok()) + .collect(); + Ok(out) + }) + .unwrap(); + assert!(rows.len() >= 2, "a split pkgbase should produce several index rows: {rows:?}"); + assert!( + rows.iter().all(|r| r.contains("art=Some")), + "every index row needs its own artifact: {rows:?}" + ); + // One approval, however many packages came out of it. + assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), Some(1)); + assert_eq!(env.query_i64("SELECT count(*) FROM release", &[]), Some(1)); +} + +fn publish(env: &Env, name: &str) { + aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); + let c = aur_build::review::candidates::pending_candidates(&env.db).unwrap().remove(0); + let dd = aur_build::review::diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); + let c = aur_build::review::candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + aur_build::review::approve::approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap(); + let c = aur_build::review::candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); + pipeline::run(&env.cfg, &env.db, &id).unwrap(); +} + +fn world_of_last_build(env: &Env) -> Vec<db::WorldMember> { + let json: String = env + .db + .read(|c| { + Ok(c.query_row( + "SELECT w.members_json FROM world_snapshot w + JOIN build b ON b.world_snapshot_id = w.id + ORDER BY b.rowid DESC LIMIT 1", + [], + |r| r.get(0), + )?) + }) + .unwrap(); + serde_json::from_str(&json).unwrap() +} |
