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) } }