A step of one TypeScript package of a module with several (mesh-contracts' root: ts-meshbroker beside ts-meshrecovery, pipeline main.3 of 2026-11-20) fetches a workspace narrowed to that package's path. The fetch and the lint and test levels read every package artifact of the manifest, and one whose path the narrowing left out fell back to the module's directory, which holds no lock: every TypeScript step of mesh-contracts ended "could not run: the package in . has no package-lock.json". Such a package is now left out of the fetch's install and of the levels' targets; an unnarrowed tree is read as before. Builds: design 47 H H-A-136 (a step's workspace holds the artifact's paths; nothing else is fetched), ADR 0499 clause 4
145 lines
5.3 KiB
Go
145 lines
5.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A folder module (mesh-contracts' root, a domain folder) declares several package artifacts, each at its own path
|
|
// with its own package.json and package-lock.json and none at the module's directory (ADR 0634 section 8).
|
|
const folderManifest = `{"module":"c","build":{"artifacts":[` +
|
|
`{"name":"ts-a","kind":"package","language":"typescript","paths":["ts/seats/a"]},` +
|
|
`{"name":"ts-b","kind":"package","language":"typescript","paths":["ts/seats/b/"]}]}}`
|
|
|
|
func folderFiles(prefix string) map[string]string {
|
|
files := map[string]string{prefix + "module.json": folderManifest}
|
|
for _, s := range []string{"a", "b"} {
|
|
files[prefix+"ts/seats/"+s+"/package.json"] = `{"name":"@x/` + s + `","version":"0.1.0","scripts":{"build":"tsc","test":"node --test"}}`
|
|
files[prefix+"ts/seats/"+s+"/package-lock.json"] = `{"lockfileVersion":3}`
|
|
files[prefix+"ts/seats/"+s+"/index.ts"] = "export const v = 1;\n"
|
|
}
|
|
return files
|
|
}
|
|
|
|
// The fetch installs each package artifact at its path from the lock there, and finds no lock missing at the module.
|
|
func TestAFolderModulesPackagesAreInstalledEachAtItsPath(t *testing.T) {
|
|
m, f := fetchMachine(t, folderFiles("modules/c/"), nil)
|
|
out, err := m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := out.(FetchResult)
|
|
if len(r.Modules) != 2 || r.Modules[0] != "modules/c/ts/seats/a" || r.Modules[1] != "modules/c/ts/seats/b" || len(r.Installed) != 2 {
|
|
t.Errorf("result = %+v, want both package directories found and installed", r)
|
|
}
|
|
var prefixes []string
|
|
for _, c := range npmCalls(f) {
|
|
for i, a := range c.args {
|
|
if a == "--prefix" {
|
|
prefixes = append(prefixes, strings.TrimPrefix(c.args[i+1], filepath.Join(m.Work, r.Workspace, "tree")))
|
|
}
|
|
}
|
|
}
|
|
if len(prefixes) != 2 || prefixes[0] != "/modules/c/ts/seats/a" || prefixes[1] != "/modules/c/ts/seats/b" {
|
|
t.Errorf("npm ci ran with --prefix %v, want each package's own directory", prefixes)
|
|
}
|
|
}
|
|
|
|
// The build runs the package's script and pack in its own path, and the test level runs in each path.
|
|
func TestAFolderModulesPackageIsBuiltAndTestedInItsPath(t *testing.T) {
|
|
m, f, work := newMachine(t)
|
|
workspaceWith(t, work, folderFiles(""))
|
|
f.effect = func(c call) {
|
|
if c.name == "npm" && len(c.args) > 0 && c.args[0] == "pack" {
|
|
for i, a := range c.args {
|
|
if a == "--pack-destination" {
|
|
write(t, filepath.Join(c.args[i+1], "x.tgz"), "packed", 0o644)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if _, err := m.build(context.Background(), map[string]any{"workspace": "w1", "module": ".", "artifact": "ts-b"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := filepath.Join(work, "w1", "tree", "ts", "seats", "b")
|
|
built := 0
|
|
for _, c := range f.calls {
|
|
if c.name == "npm" {
|
|
built++
|
|
if c.dir != want {
|
|
t.Errorf("%s ran in %s, want %s", joined(c), c.dir, want)
|
|
}
|
|
}
|
|
}
|
|
if built != 2 {
|
|
t.Errorf("%d npm commands, want the build and the pack", built)
|
|
}
|
|
f.calls = nil
|
|
res, err := m.test(context.Background(), testArgs("unit"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tests := map[string]bool{}
|
|
for _, c := range f.calls {
|
|
if c.name == "npm" {
|
|
tests[c.dir] = true
|
|
}
|
|
}
|
|
if r := res.(*TestResult); !r.Passed || len(tests) != 2 || !tests[want] || !tests[filepath.Join(work, "w1", "tree", "ts", "seats", "a")] {
|
|
t.Errorf("unit level = %+v, ran tests in %v, want both package paths", r, tests)
|
|
}
|
|
}
|
|
|
|
// A step of one package of a root module with several (mesh-contracts' ts-meshbroker beside ts-meshrecovery, pipeline
|
|
// main.3 of 2026-11-20) fetches a workspace narrowed to that package's path (ADR 0499 clause 4; design 47 H, H-A-136:
|
|
// nothing else is fetched). The other packages' paths are not in it: they are no module of this build, never the
|
|
// module's directory, which holds no lock ("the package in . has no package-lock.json").
|
|
func TestANarrowedFetchOfOneFolderPackageLeavesTheOthersOut(t *testing.T) {
|
|
m, f := fetchMachine(t, folderFiles(""), nil)
|
|
args := map[string]any{"repository": "novox/mesh-contracts", "commit": commitSHA, "dir": ".", "paths": []any{"ts/seats/a"}}
|
|
out, err := m.fetch(context.Background(), args)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := out.(FetchResult)
|
|
if strings.Join(r.Modules, ",") != "ts/seats/a" || strings.Join(r.Installed, ",") != "ts/seats/a" {
|
|
t.Errorf("modules %v, installed %v; want only the named package ts/seats/a", r.Modules, r.Installed)
|
|
}
|
|
if n := len(npmCalls(f)); n != 1 {
|
|
t.Errorf("%d npm commands, want the one install of the named package", n)
|
|
}
|
|
}
|
|
|
|
// The lint and test levels of that narrowed workspace run in the named package's path alone: a package whose path was
|
|
// left out is no target, and the module's directory is not tested whole in its place (H-A-136).
|
|
func TestANarrowedWorkspaceTestsOnlyItsPackage(t *testing.T) {
|
|
work := t.TempDir()
|
|
files := folderFiles("")
|
|
for p := range files {
|
|
if strings.HasPrefix(p, "ts/seats/b/") {
|
|
delete(files, p)
|
|
}
|
|
}
|
|
for p, body := range files {
|
|
write(t, filepath.Join(work, filepath.FromSlash(p)), body, 0o644)
|
|
}
|
|
for _, tc := range []struct {
|
|
narrowed bool
|
|
want string
|
|
}{{true, "ts/seats/a"}, {false, "."}} {
|
|
got, err := testTargets(work, ".", tc.narrowed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var where []string
|
|
for _, g := range got {
|
|
where = append(where, g.where)
|
|
}
|
|
if strings.Join(where, ",") != tc.want {
|
|
t.Errorf("narrowed %v: targets %v, want %s", tc.narrowed, where, tc.want)
|
|
}
|
|
}
|
|
}
|