I6 npm-test-files: the files of an npm package that leave its digest are the indexer's setting npm-test-files, no pattern left in code

Builds: design 47 H2 H-A-598, H-A-573
This commit is contained in:
2026-11-25 17:24:26 +01:00
parent 13eccb33b3
commit 336d2abaa8
7 changed files with 83 additions and 17 deletions
@@ -358,6 +358,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
Packages: awaitedStore{b: packages, js: js, name: index.Bucket(cred.Module, index.PackagesState)}, Files: filesVia(calls.Call), GoMod: goModVia(calls.Call),
Heard: heardStore, Runs: awaitedStore{b: runs}, Graph: graph,
// How long a reservation is kept and how long its gathering waits at most (design 47 H2 §805, H-A-424).
NpmTestFiles: listSetting("npm-test-files", logger.Printf),
ReservedKeep: durationSetting("reserved-keep", index.DefaultReservedKeep, logger.Printf),
ReservedSettle: durationSetting("reserved-settle", index.DefaultReservedSettle, logger.Printf),
// How long a take-in waits on another part before it says so, and before it raises take-in-waiting (H2 §803, H-A-461 (2)).
@@ -684,3 +685,19 @@ func durationSetting(key string, def time.Duration, logf func(string, ...any)) t
}
return d
}
// listSetting is the setting key read as a list of comma-separated words; one that is not given is said and is no words.
func listSetting(key string, logf func(string, ...any)) []string {
v, ok := stdio.Setting(key)
if !ok {
logf("the setting %s is not given: no file of an npm package is left out of its digest but node_modules", key)
return nil
}
var out []string
for _, w := range strings.Split(v, ",") {
if w = strings.TrimSpace(w); w != "" {
out = append(out, w)
}
}
return out
}
@@ -386,11 +386,11 @@ func Packages(module string, files map[string]string) (requires string, packages
// (design 47 H2 H-A-546 (1)). It refuses a listing with no package.json at that directory.
//
// Its one package is the npm name, its digest the hex sha256 of the sorted lines "<path relative to it> <object>" of every
// file under it but node_modules and the test files of `node --test`'s default patterns, which the Node build module's unit
// step runs (H-A-547): a file named `*.test.*`, `*-test.*`, `*_test.*` or `test.*`, and everything under a directory named
// test. The requirements' digest is the hex sha256 of the lines "package.json <object>" and "package-lock.json <object>"
// (the second absent with no lock file), as H-A-525 has it for go.mod and go.sum.
func NpmPackage(name string, files map[string]string) (requires string, packages []meshcatalog.PackagesByModuleValuePackagesItem, err error) {
// file under it but node_modules and the test files the indexer's setting npm-test-files names (H-A-598), the patterns of
// `node --test`'s defaults which the Node build module's unit step runs (H-A-547) as the setting's default has them. The
// requirements' digest is the hex sha256 of the lines "package.json <object>" and "package-lock.json <object>" (the second
// absent with no lock file), as H-A-525 has it for go.mod and go.sum.
func NpmPackage(name string, files map[string]string, testFiles []string) (requires string, packages []meshcatalog.PackagesByModuleValuePackagesItem, err error) {
pj, ok := files["package.json"]
if !ok {
return "", nil, fmt.Errorf("the npm package %s has no package.json at its directory", name)
@@ -402,7 +402,7 @@ func NpmPackage(name string, files map[string]string) (requires string, packages
sum := sha256.Sum256([]byte(req))
var lines []string
for p, object := range files {
if npmLeftOut(p) {
if npmLeftOut(p, testFiles) {
continue
}
lines = append(lines, p+" "+object)
@@ -413,15 +413,43 @@ func NpmPackage(name string, files map[string]string) (requires string, packages
}
// npmLeftOut is whether a file of an npm package, by its path relative to the package, leaves its digest: under
// node_modules or a directory named test, or a test file by `node --test`'s default patterns (H-A-547).
func npmLeftOut(p string) bool {
dir, file := path.Split(p)
// node_modules, or matching one of the setting npm-test-files' patterns (H-A-547, H-A-598).
func npmLeftOut(p string, testFiles []string) bool {
dir, _ := path.Split(p)
for _, e := range strings.Split(strings.TrimSuffix(dir, "/"), "/") {
if e == "node_modules" || e == "test" {
if e == "node_modules" {
return true
}
}
return strings.HasPrefix(file, "test.") || strings.Contains(file, ".test.") || strings.Contains(file, "-test.") || strings.Contains(file, "_test.")
for _, pattern := range testFiles {
if globMatch(strings.Split(pattern, "/"), strings.Split(p, "/")) {
return true
}
}
return false
}
// globMatch is whether the path's segments match the pattern's: a segment by path.Match, `**` any number of segments
// (none included).
func globMatch(pattern, segments []string) bool {
if len(pattern) == 0 {
return len(segments) == 0
}
if pattern[0] == "**" {
for i := 0; i <= len(segments); i++ {
if globMatch(pattern[1:], segments[i:]) {
return true
}
}
return false
}
if len(segments) == 0 {
return false
}
if ok, err := path.Match(pattern[0], segments[0]); err != nil || !ok {
return false
}
return globMatch(pattern[1:], segments[1:])
}
// ignoredDir is whether the go tool takes no package from dir: an element named testdata, or starting with _ or .
@@ -13,6 +13,9 @@ const sdk = "git.novox.be/novox/mesh-sdk/go"
// The indexer's one computation (ADR 0593 §2): what moved out of mesh-sdk computes what it computed, the import form
// moves only with what a module imports, and a package's digest is its own files.
// npmTestFiles are the setting npm-test-files' default, as the indexer's manifest gives it (H-A-573, H-A-598).
var npmTestFiles = []string{"**/*.test.*", "**/*-test.*", "**/*_test.*", "**/test.*", "**/test/**"}
func TestTheFingerprintComputation(t *testing.T) {
t.Run("the moved computation gives the values it gave in mesh-sdk", movedComputationGivesTheValuesItGaveInMeshSDK)
t.Run("the import form moves only with what the module imports", importFormMovesOnlyWithWhatTheModuleImports)
@@ -222,7 +225,7 @@ func packageDigestIsItsOwnFiles(t *testing.T) {
npm := map[string]string{"package.json": "p1", "package-lock.json": "l1", "src/stdio.ts": "a", "dist/stdio.js": "b",
"src/stdio.test.ts": "t1", "src/a-test.ts": "t2", "src/a_test.mjs": "t3", "test.js": "t4", "test/x.ts": "t5",
"node_modules/x/index.js": "n"}
npmReq, npmPkgs, err := NpmPackage("@novox/mesh-sdk", npm)
npmReq, npmPkgs, err := NpmPackage("@novox/mesh-sdk", npm, npmTestFiles)
if err != nil || len(npmPkgs) != 1 || npmPkgs[0].Package != "@novox/mesh-sdk" || npmPkgs[0].Digest == "" || npmReq == "" {
t.Fatalf("NpmPackage = %q, %+v, %v; want one package by its npm name", npmReq, npmPkgs, err)
}
@@ -231,7 +234,7 @@ func packageDigestIsItsOwnFiles(t *testing.T) {
moves bool
}{{"src/stdio.ts", true}, {"dist/stdio.js", true}, {"package.json", true}, {"src/stdio.test.ts", false}, {"src/a-test.ts", false},
{"src/a_test.mjs", false}, {"test.js", false}, {"test/x.ts", false}, {"node_modules/x/index.js", false}} {
r, ps, _ := NpmPackage("@novox/mesh-sdk", with(npm, c.file, "zz"))
r, ps, _ := NpmPackage("@novox/mesh-sdk", with(npm, c.file, "zz"), npmTestFiles)
if moved := ps[0].Digest != npmPkgs[0].Digest; moved != c.moves {
t.Errorf("changing %s of the npm package moved its digest %v, want %v", c.file, moved, c.moves)
}
@@ -239,10 +242,18 @@ func packageDigestIsItsOwnFiles(t *testing.T) {
t.Error("a changed package.json left the npm package's requirements' digest")
}
}
if r2, _, _ := NpmPackage("@novox/mesh-sdk", with(npm, "package-lock.json", "l2")); r2 == npmReq {
// H-A-598: the patterns are the setting's; given one, a file it matches leaves the digest and another file moves it.
one := []string{"**/*.spec.*"}
_, base, _ := NpmPackage("@novox/mesh-sdk", with(npm, "src/a.spec.ts", "s"), one)
_, specMoved, _ := NpmPackage("@novox/mesh-sdk", with(npm, "src/a.spec.ts", "s2"), one)
_, otherMoved, _ := NpmPackage("@novox/mesh-sdk", with(npm, "src/stdio.test.ts", "t9"), one)
if specMoved[0].Digest != base[0].Digest || otherMoved[0].Digest == base[0].Digest {
t.Error("with the setting npm-test-files given one pattern, a file it matches moved the digest or another did not")
}
if r2, _, _ := NpmPackage("@novox/mesh-sdk", with(npm, "package-lock.json", "l2"), npmTestFiles); r2 == npmReq {
t.Error("a changed lock file left the npm package's requirements' digest")
}
if _, _, err := NpmPackage("@novox/mesh-sdk", map[string]string{"index.js": "x"}); err == nil {
if _, _, err := NpmPackage("@novox/mesh-sdk", map[string]string{"index.js": "x"}, npmTestFiles); err == nil {
t.Error("a listing with no package.json was taken as an npm package")
}
}
@@ -186,6 +186,9 @@ type Indexer struct {
// first reservation its gathering settles at the latest: the settings reserved-keep and reserved-settle (design 47 H2
// §805, H-A-424); zero takes the default.
ReservedKeep, ReservedSettle time.Duration
// NpmTestFiles are the patterns of the files that leave an npm package's digest, the setting npm-test-files (design 47 H2
// H-A-598); none leaves only node_modules.
NpmTestFiles []string
// WaitingLine is how long a take-in waits on another part before it says so and keeps its waiting-on, and WaitingBound
// how long before it raises take-in-waiting: the settings take-in-waiting-line and take-in-waiting-bound (design 47 H2
// §803, H-A-461 (2)); zero takes the default.
@@ -459,7 +459,7 @@ func (ix *Indexer) npmPackagesIn(ctx context.Context, repository, commit string,
if _, ok := files["package.json"]; !ok {
continue
}
requires, pkgs, err := fingerprint.NpmPackage(name, files)
requires, pkgs, err := fingerprint.NpmPackage(name, files, ix.NpmTestFiles)
if err != nil {
ix.logf("%s at %s: %v; it is not published", repository, commit, err)
break
@@ -226,7 +226,7 @@ func TestARepositorysPackagesAreThoseInTheMergesTree(t *testing.T) {
if err := ix.publish(ctx, "novox/mesh-sdk", merge, held, false, TrunkBranch); err != nil {
t.Fatal(err)
}
_, want, _ := fingerprint.NpmPackage(npmName, listing)
_, want, _ := fingerprint.NpmPackage(npmName, listing, nil)
got, ok := ix.publishedAll(ctx)[npmName]
if !ok || got.Repository != "novox/mesh-sdk" || got.Dir != "ts" || got.Commit != merge || !reflect.DeepEqual(got.Packages, want) {
t.Fatalf("packages of %s = %+v (%v), want its digest %+v from novox/mesh-sdk at ts; asked %v", npmName, got, ok, want, asked)
@@ -116,6 +116,13 @@
"default": "30s",
"runtime": true
},
"npm-test-files": {
"type": "string",
"description": "the patterns of the files of an npm package that leave its digest, comma-separated: a segment as path.Match reads it, ** any number of directories; node's own test patterns and everything under a test directory (design 47 H2 H-A-573, H-A-598).",
"trusted": false,
"default": "**/*.test.*,**/*-test.*,**/*_test.*,**/test.*,**/test/**",
"runtime": true
},
"reserved-keep": {
"type": "string",
"description": "how long the indexer keeps a pipeline's reservation of a package's version in its state heard (reserved.<revision>.<module>), a Go duration (design 47 H2 §805, H-A-424 (2)).",