// Package trunk is where a catalogue check finds the trunk's copy of a list that may only shrink (novox/hq ADR 0529 clause 2): // the file as the catalogue's base commit holds it, to judge a change's list against. // // The checker fetches the change's own repository at its base commit beside the change, as it fetches a sibling, and says where // in MESH_CHECK_BESIDE; the copy is under the repository's name (the same convention the other repositories' tests use). With // MESH_CHECK_BESIDE unset, or set without the catalogue's copy in it, the comparison is skipped and the test says NOT COMPARED // WITH THE TRUNK, naming why: the checker that runs before R4g's delivery places siblings but not the change's own repository, // so R4g's own check cannot hold the copy (issue 1295 notes that a missing copy becomes a failure once that checker is gone). A trunk that holds no such file had // none: the list's first copy, which nothing was added to. package trunk import ( "errors" "os" "path/filepath" "testing" ) // Repository is the catalogue's name beside a check. const Repository = "mesh-catalog" // BesideEnv is where a merge check says the clones beside the one checked are. const BesideEnv = "MESH_CHECK_BESIDE" // Copy is the trunk's copy of the file at rel (a path from the catalogue's root), written to a file of the test's own: its path, // and whether the trunk holds one (false: its first copy). It skips the test, saying so, when no copy of the catalogue is beside. func Copy(t testing.TB, rel string) (string, bool) { t.Helper() raw, found, skip, err := Read(rel) switch { case skip != "": t.Skip(skip) case err != nil: t.Fatal(err) case !found: t.Logf("the trunk holds no %s: its first copy", rel) return "", false } path := filepath.Join(t.TempDir(), filepath.Base(rel)) if err := os.WriteFile(path, raw, 0o644); err != nil { t.Fatal(err) } return path, true } // Read is the trunk's copy of rel and whether the trunk holds one. skip is the words to skip the comparison with, when no trunk is // named; err is a trunk named and not there, or unreadable. func Read(rel string) (content []byte, found bool, skip string, err error) { beside := os.Getenv(BesideEnv) if beside == "" { return nil, false, "NOT COMPARED WITH THE TRUNK: " + BesideEnv + " is not set, so the trunk's copy of " + rel + " is not beside this run; the checker places it", nil } dir := filepath.Join(beside, Repository) if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) { return nil, false, "NOT COMPARED WITH THE TRUNK: " + Repository + " is not beside this check in " + BesideEnv + "=" + beside + ", so " + rel + " is not judged against the trunk's copy (the checker before R4g places no copy of the change's own repository)", nil } else if err != nil { return nil, false, "", err } raw, err := os.ReadFile(filepath.Join(dir, rel)) if errors.Is(err, os.ErrNotExist) { return nil, false, "", nil } if err != nil { return nil, false, "", err } return raw, true, "", nil }