A mesh package's tarball is unpacked straight into node_modules, so its own dependencies were never there: the forge module's bundle failed with esbuild "Could not resolve blakejs" from the SDK's seal code. After every mesh package is placed, the fetch installs the dependencies they declare (not their devDependencies, not a mesh package, not one already present) through the same npm path as the module's own, before the bundle. Builds: design 47 H H-A-491 (addendum)
441 lines
19 KiB
Go
441 lines
19 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
dforgekey "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/forgekey"
|
|
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
|
)
|
|
|
|
const commitSHA = "0123456789abcdef0123456789abcdef01234567"
|
|
|
|
var fetchArgs = map[string]any{"repository": "novox/mesh-catalog", "commit": commitSHA}
|
|
|
|
// fetchMachine is a machine whose git checks out the given files into the tree it is asked to fetch into (the fake
|
|
// answers the commands of FetchCommit and `git ls-files`), and whose npm ci writes the packages the test names.
|
|
func fetchMachine(t *testing.T, files map[string]string, installs map[string]string) (*Module, *fakeMachine) {
|
|
t.Helper()
|
|
m, f, _ := newMachine(t)
|
|
f.answer["git rev-parse"] = forgekey.Ran{Stdout: commitSHA + "\n"}
|
|
var manifests []string
|
|
for p := range files {
|
|
if strings.HasSuffix(p, "module.json") {
|
|
manifests = append(manifests, p)
|
|
}
|
|
}
|
|
f.answer["git ls-files"] = forgekey.Ran{Stdout: strings.Join(manifests, "\n") + "\n"}
|
|
f.effect = func(c call) {
|
|
switch {
|
|
case c.name == "git" && slices_contains(c.args, "checkout"):
|
|
for i, a := range c.args {
|
|
if a == "-C" {
|
|
for p, body := range files {
|
|
write(t, filepath.Join(c.args[i+1], filepath.FromSlash(p)), body, 0o644)
|
|
}
|
|
}
|
|
}
|
|
case c.name == "npm":
|
|
for i, a := range c.args {
|
|
if a == "--prefix" {
|
|
for p, body := range installs {
|
|
write(t, filepath.Join(c.args[i+1], "node_modules", filepath.FromSlash(p)), body, 0o644)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return m, f
|
|
}
|
|
|
|
func slices_contains(l []string, s string) bool {
|
|
for _, x := range l {
|
|
if x == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func typeScriptModule(pkg string) map[string]string {
|
|
return map[string]string{
|
|
"modules/m/module.json": `{"module":"m","build":{"artifacts":[{"name":"tools","kind":"bundle","language":"typescript","entrypoints":["tools/index.js"]}]}}`,
|
|
"modules/m/package.json": pkg,
|
|
}
|
|
}
|
|
|
|
func npmCalls(f *fakeMachine) []call {
|
|
var out []call
|
|
for _, c := range f.calls {
|
|
if c.name == "npm" {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// 1489/AC1: a module whose package.json names only @novox/mesh-sdk and @types/node (and typescript) installs nothing
|
|
// and the step passes.
|
|
func TestAModuleNamingOnlyTheSdkAndTypesInstallsNothing(t *testing.T) {
|
|
m, f := fetchMachine(t, typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6"},"devDependencies":{"@types/node":"^22","typescript":"^5"}}`), nil)
|
|
out, err := m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := out.(FetchResult)
|
|
if len(npmCalls(f)) != 0 || len(r.Installed) != 0 || len(r.Modules) != 1 || r.Modules[0] != "modules/m" || r.Commit != commitSHA {
|
|
t.Errorf("result = %+v, npm calls %d, want the module found and nothing installed", r, len(npmCalls(f)))
|
|
}
|
|
if _, err := os.Stat(filepath.Join(m.Work, r.Workspace, "tree", "modules", "m", "module.json")); err != nil {
|
|
t.Errorf("the commit is not checked out in the workspace: %v", err)
|
|
}
|
|
}
|
|
|
|
// 47/G A-72: a module naming another @novox/mesh-* package beside the SDK has no package beyond the SDK, so it installs
|
|
// nothing and is not refused for want of a lock. 47/H H-A-267 (ADR 0573 §2): a fetch with no group and no members' builds
|
|
// places that package at the latest release the registry holds, as it places the SDK, whatever the module's lock names.
|
|
func TestAMeshPackageIsNotBeyondTheSdk(t *testing.T) {
|
|
const contracts = "@novox/mesh-delivery-contracts-git"
|
|
files := typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6","` + contracts + `":"^0.1.2"}}`)
|
|
files["modules/m/package-lock.json"] = `{"lockfileVersion":3,"packages":{"node_modules/` + contracts + `":{"version":"0.1.2","integrity":"sha512-old"}}}`
|
|
m, f := fetchMachine(t, files, nil)
|
|
fakeRegistryOf(t, m, map[string]map[string][]byte{
|
|
sdkPackage: {"0.1.15": tgzOf(t, sdkPackage, "0.1.15")},
|
|
contracts: {"0.1.2": tgzOf(t, contracts, "0.1.2"), "0.1.4": tgzOf(t, contracts, "0.1.4"), "0.1.5-feat.1": tgzOf(t, contracts, "0.1.5-feat.1")},
|
|
})
|
|
out, err := m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := out.(FetchResult)
|
|
if len(npmCalls(f)) != 0 || len(r.Installed) != 0 || len(r.Refused) != 0 {
|
|
t.Errorf("result = %+v, npm calls %d, want nothing installed and nothing refused", r, len(npmCalls(f)))
|
|
}
|
|
dir := filepath.Join(m.Work, r.Workspace, "tree", "modules", "m")
|
|
got, err := readPackageJSON(filepath.Join(dir, "node_modules", filepath.FromSlash(contracts)))
|
|
if err != nil || got.Version != "0.1.4" {
|
|
t.Errorf("%s placed %q (%v), want the latest release 0.1.4", contracts, got.Version, err)
|
|
}
|
|
if v := placedSDK(dir); v != "0.1.15" || r.SDK["modules/m"] != "0.1.15" {
|
|
t.Errorf("the SDK placed %q, answered %q, want the latest release 0.1.15", v, r.SDK["modules/m"])
|
|
}
|
|
|
|
// A member's build of a mesh package takes precedence: it is laid, and the registry, holding none of it, is not
|
|
// waited on.
|
|
const member = "@novox/mesh-tools"
|
|
m, _ = fetchMachine(t, typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6","`+member+`":"^0.1.6"}}`), nil)
|
|
tgz := tgzOf(t, member, "0.0.0-member")
|
|
digest := digestOf(tgz)
|
|
store := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v2/tools/pkg/blobs/"+digest {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write(tgz)
|
|
}))
|
|
defer store.Close()
|
|
m.ArtifactStoreFile = filepath.Join(t.TempDir(), "artifact-store.json")
|
|
write(t, m.ArtifactStoreFile, `{"at":"`+strings.TrimPrefix(store.URL, "http://")+`"}`, 0o600)
|
|
args := map[string]any{"repository": fetchArgs["repository"], "commit": fetchArgs["commit"],
|
|
"builds": []any{map[string]any{"package": member, "module": "tools", "artifact": "pkg", "digest": digest}}}
|
|
out, err = m.fetch(context.Background(), args)
|
|
if err != nil {
|
|
t.Fatalf("a fetch with a member's build of %s: %v", member, err)
|
|
}
|
|
r = out.(FetchResult)
|
|
got, err = readPackageJSON(filepath.Join(m.Work, r.Workspace, "tree", "modules", "m", "node_modules", filepath.FromSlash(member)))
|
|
if err != nil || got.Version != "0.0.0-member" {
|
|
t.Errorf("%s placed %q (%v), want the member's build", member, got.Version, err)
|
|
}
|
|
|
|
// Design 47 H, H-A-491 addendum: the dependencies a placed mesh package declares are installed beside it, as a
|
|
// registry install would (the SDK's seal code imports blakejs); its devDependencies, a mesh package and one the
|
|
// module's node_modules holds already are not asked.
|
|
files = typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6"}}`)
|
|
files["modules/m/node_modules/tweetnacl/package.json"] = `{"name":"tweetnacl","version":"1.0.3"}`
|
|
m, f = fetchMachine(t, files, map[string]string{"blakejs/package.json": `{"name":"blakejs","version":"1.2.1"}`})
|
|
var asked []string
|
|
effect := f.effect
|
|
f.effect = func(c call) {
|
|
if c.name == "npm" {
|
|
for i, a := range c.args {
|
|
if a == "--prefix" {
|
|
raw, _ := os.ReadFile(filepath.Join(c.args[i+1], "package.json"))
|
|
asked = append(asked, strings.Join(c.args[:1], " ")+" "+string(raw))
|
|
}
|
|
}
|
|
}
|
|
effect(c)
|
|
}
|
|
fakeRegistryOf(t, m, map[string]map[string][]byte{sdkPackage: {"0.1.15": tgzDeclaring(t, sdkPackage, "0.1.15",
|
|
`{"blakejs":"1.2.1","tweetnacl":"1.0.3","@novox/mesh-other":"^0.1.0"}`, `{"typescript":"^5","left-out":"1"}`)}})
|
|
out, err = m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatalf("a fetch placing an SDK that declares dependencies: %v", err)
|
|
}
|
|
r = out.(FetchResult)
|
|
if want := `install {"dependencies":{"blakejs":"1.2.1"},"name":"placed-dependencies","private":true}`; len(asked) != 1 || asked[0] != want {
|
|
t.Errorf("npm was asked %q, want once %q", asked, want)
|
|
}
|
|
nm := filepath.Join(m.Work, r.Workspace, "tree", "modules", "m", "node_modules")
|
|
if _, err := os.Stat(filepath.Join(nm, "blakejs", "package.json")); err != nil {
|
|
t.Errorf("the SDK's dependency blakejs is not installed beside it: %v", err)
|
|
}
|
|
if v := placedSDK(filepath.Join(m.Work, r.Workspace, "tree", "modules", "m")); v != "0.1.15" {
|
|
t.Errorf("the SDK placed %q after the install, want 0.1.15", v)
|
|
}
|
|
}
|
|
|
|
// tgzDeclaring is a packed npm package of name@version whose package.json declares the dependencies and
|
|
// devDependencies given (JSON objects).
|
|
func tgzDeclaring(t *testing.T, name, version, deps, devDeps string) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
zw := gzip.NewWriter(&buf)
|
|
tw := tar.NewWriter(zw)
|
|
body := `{"name":"` + name + `","version":"` + version + `","dependencies":` + deps + `,"devDependencies":` + devDeps + `}`
|
|
tw.WriteHeader(&tar.Header{Name: "package/package.json", Mode: 0o644, Size: int64(len(body)), Typeflag: tar.TypeReg})
|
|
tw.Write([]byte(body))
|
|
tw.Close()
|
|
zw.Close()
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// 1489/AC2: a module naming lodash with a lock gets npm ci --omit=dev --ignore-scripts (fake runner), its install put
|
|
// at its node_modules without the install's SDK or any copy of it; without a lock the step fails naming the directory.
|
|
func TestAModuleNamingLodashWithALockIsInstalledWithoutTheSdkAndWithoutALockFails(t *testing.T) {
|
|
files := typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6","lodash":"^4"},"devDependencies":{"x":"1"},"scripts":{"test":"y"}}`)
|
|
files["modules/m/package-lock.json"] = `{"lockfileVersion":3}`
|
|
m, f := fetchMachine(t, files, map[string]string{"lodash/index.js": "l", "@novox/mesh-sdk/index.js": "sdk", "a/node_modules/@novox/mesh-sdk/i.js": "nested"})
|
|
out, err := m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := out.(FetchResult)
|
|
calls := npmCalls(f)
|
|
if len(calls) != 1 {
|
|
t.Fatalf("npm ran %d times, want once", len(calls))
|
|
}
|
|
line := strings.Join(calls[0].args, " ")
|
|
if !strings.HasPrefix(line, "ci --omit=dev --omit=peer --ignore-scripts --no-bin-links") {
|
|
t.Errorf("ran npm %s", line)
|
|
}
|
|
nm := filepath.Join(m.Work, r.Workspace, "tree", "modules", "m", "node_modules")
|
|
if _, err := os.Stat(filepath.Join(nm, "lodash", "index.js")); err != nil {
|
|
t.Errorf("lodash is not at the module's node_modules: %v", err)
|
|
}
|
|
// The install's copies are gone; the one at the top is the latest release placed after it (placeSDK).
|
|
for _, gone := range []string{"@novox/mesh-sdk/index.js", "a/node_modules/@novox/mesh-sdk"} {
|
|
if _, err := os.Stat(filepath.Join(nm, gone)); err == nil {
|
|
t.Errorf("a copy of the SDK is left at %s", gone)
|
|
}
|
|
}
|
|
if got := strings.Join(r.Installed, ","); got != "modules/m" {
|
|
t.Errorf("installed = %q", got)
|
|
}
|
|
// the module's own package.json is untouched: the install worked on a scratch copy.
|
|
if raw, _ := os.ReadFile(filepath.Join(m.Work, r.Workspace, "tree", "modules", "m", "package.json")); !strings.Contains(string(raw), "scripts") {
|
|
t.Errorf("the module's package.json was rewritten: %s", raw)
|
|
}
|
|
var scratch []string
|
|
for _, e := range mustReadDir(t, filepath.Join(m.Work, r.Workspace)) {
|
|
scratch = append(scratch, e)
|
|
}
|
|
if strings.Contains(strings.Join(scratch, ","), "npm-") {
|
|
t.Errorf("the scratch install is left in the workspace: %v", scratch)
|
|
}
|
|
|
|
}
|
|
|
|
// 1489 (the 1459 settlement): a module naming packages with no package-lock.json does not fail the fetch: it passes,
|
|
// installs nothing for that module, and its result lists the refused bundle with its reason; a build of that bundle
|
|
// fails naming it.
|
|
func TestAModuleWithoutALockIsRefusedInTheResultNotTheFetch(t *testing.T) {
|
|
files := typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6","lodash":"^4"}}`)
|
|
files["modules/ok/module.json"] = `{"module":"ok","build":{"artifacts":[{"name":"tools","kind":"bundle","language":"typescript","entrypoints":["tools/index.js"]}]}}`
|
|
files["modules/ok/package.json"] = `{"dependencies":{"@novox/mesh-sdk":"^0.1.6"}}`
|
|
m, f := fetchMachine(t, files, nil)
|
|
out, err := m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatalf("a module without a lock failed the fetch: %v", err)
|
|
}
|
|
r := out.(FetchResult)
|
|
if len(npmCalls(f)) != 0 || len(r.Installed) != 0 || len(r.Modules) != 2 {
|
|
t.Errorf("result = %+v, npm calls %d", r, len(npmCalls(f)))
|
|
}
|
|
if len(r.Refused) != 1 || r.Refused[0].Module != "modules/m" ||
|
|
!strings.Contains(r.Refused[0].Reason, "names packages beyond the SDK and has no package-lock.json") || !strings.Contains(r.Refused[0].Reason, "lodash") {
|
|
t.Fatalf("refused = %+v, want modules/m with its reason", r.Refused)
|
|
}
|
|
// the build of that bundle fails naming it, without running a compiler.
|
|
f.calls = nil
|
|
_, err = m.build(context.Background(), map[string]any{"workspace": r.Workspace, "module": "modules/m", "artifact": "tools"})
|
|
if err == nil || !strings.Contains(err.Error(), "modules/m") || !strings.Contains(err.Error(), "no package-lock.json") || len(f.calls) != 0 {
|
|
t.Errorf("build = %v after %d commands, want the refusal naming modules/m and nothing run", err, len(f.calls))
|
|
}
|
|
}
|
|
|
|
// A dependency on a directory by a relative path (file: or link:, as a go.mod replace is in Go) would resolve to nothing in
|
|
// the scratch copy `npm ci` runs in, so the module is refused naming the package and its path, as one without a lock is,
|
|
// never installed wrongly (the kind of the 2026-11-02 13:53 route-proxy failure in mesh-build-go).
|
|
func TestAModuleDependingOnARelativeDirectoryIsRefusedNamingIt(t *testing.T) {
|
|
files := typeScriptModule(`{"dependencies":{"@novox/mesh-sdk":"^0.1.6","sibling":"file:../sibling"}}`)
|
|
files["modules/m/package-lock.json"] = `{"lockfileVersion":3}`
|
|
m, f := fetchMachine(t, files, nil)
|
|
out, err := m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatalf("the fetch failed: %v", err)
|
|
}
|
|
r := out.(FetchResult)
|
|
if len(npmCalls(f)) != 0 || len(r.Refused) != 1 || !strings.Contains(r.Refused[0].Reason, "sibling") || !strings.Contains(r.Refused[0].Reason, "file:../sibling") {
|
|
t.Errorf("refused = %+v after %d npm calls, want the dependency named and nothing installed", r.Refused, len(npmCalls(f)))
|
|
}
|
|
}
|
|
|
|
// 1489 (review 4): a bundle depending on another @novox package is installed, the mesh's registry given to npm ci
|
|
// through --userconfig, as the toolchain's install gives it.
|
|
func TestAnotherNovoxPackageIsInstalledFromTheRegistryThroughAUserconfig(t *testing.T) {
|
|
files := typeScriptModule(`{"dependencies":{"@novox/other":"^1","lodash":"^4"}}`)
|
|
files["modules/m/package-lock.json"] = `{}`
|
|
m, f := fetchMachine(t, files, map[string]string{"@novox/other/index.js": "o", "lodash/index.js": "l"})
|
|
reg, _ := m.Toolchain.Registry()
|
|
var npmrc string
|
|
inner := f.effect
|
|
f.effect = func(c call) {
|
|
inner(c)
|
|
for i, a := range c.args {
|
|
if c.name == "npm" && a == "--userconfig" {
|
|
raw, _ := os.ReadFile(c.args[i+1])
|
|
npmrc = string(raw)
|
|
}
|
|
}
|
|
}
|
|
out, err := m.fetch(context.Background(), fetchArgs)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Reads are anonymous (ADR 0346, CAT-BN3): the config names the scope's registry and no credential.
|
|
if !strings.Contains(npmrc, "@novox:registry="+reg.URL+"\n") || strings.Contains(npmrc, "_auth") {
|
|
t.Errorf("npm ci was given the config %q", npmrc)
|
|
}
|
|
r := out.(FetchResult)
|
|
if _, err := os.Stat(filepath.Join(m.Work, r.Workspace, "tree", "modules", "m", "node_modules", "@novox", "other", "index.js")); err != nil {
|
|
t.Errorf("@novox/other was not installed: %v", err)
|
|
}
|
|
for _, e := range mustReadDir(t, filepath.Join(m.Work, r.Workspace)) {
|
|
if strings.HasPrefix(e, "npm-") {
|
|
t.Errorf("the scratch install is left: %s", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
func mustReadDir(t *testing.T, dir string) []string {
|
|
t.Helper()
|
|
es, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out []string
|
|
for _, e := range es {
|
|
out = append(out, e.Name())
|
|
}
|
|
return out
|
|
}
|
|
|
|
// 1489/AC3: given a process environment holding a token, no command of the step sees it. The commands the step makes
|
|
// are given PATH, HOME and the locale and their own words; and the real clean runner, run on `env` here, prints only
|
|
// those.
|
|
func TestNoCommandOfTheStepSeesATokenOfTheProcess(t *testing.T) {
|
|
t.Setenv("MESH_BUILD_CPU_SHARE", "50")
|
|
t.Setenv("MESH_BUS_CREDENTIAL", "hunter2")
|
|
t.Setenv("NPM_TOKEN", "hunter3")
|
|
files := typeScriptModule(`{"dependencies":{"lodash":"^4"}}`)
|
|
files["modules/m/package-lock.json"] = `{}`
|
|
m, f := fetchMachine(t, files, map[string]string{"lodash/index.js": "l"})
|
|
if _, err := m.fetch(context.Background(), fetchArgs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(f.calls) < 6 {
|
|
t.Fatalf("only %d commands ran", len(f.calls))
|
|
}
|
|
for _, c := range f.calls {
|
|
for _, e := range c.env {
|
|
if strings.Contains(e, "hunter") || strings.HasPrefix(e, "MESH_") || strings.HasPrefix(e, "NPM_TOKEN") {
|
|
t.Errorf("%s %v was given %s", c.name, c.args[:min(len(c.args), 3)], e)
|
|
}
|
|
}
|
|
if len(c.env) < 3 || !strings.HasPrefix(c.env[0], "PATH=") || !strings.HasPrefix(c.env[1], "HOME=") || c.env[2] != "LC_ALL=C" {
|
|
t.Errorf("%s was given %v, want PATH, HOME and the locale first", c.name, c.env)
|
|
}
|
|
// And its threads, its part of the node's share the composer gave (ADR 0553).
|
|
if !slices.ContainsFunc(c.env, func(e string) bool { return strings.HasPrefix(e, "GOMAXPROCS=") }) ||
|
|
!slices.ContainsFunc(c.env, func(e string) bool { return strings.HasPrefix(e, "UV_THREADPOOL_SIZE=") }) {
|
|
t.Errorf("%s was given %v, without its threads", c.name, c.env)
|
|
}
|
|
}
|
|
// The runner main wires: what it hands a command is exactly what the step gave.
|
|
ran := dforgekey.ExecRunnerClean(10*time.Second)(context.Background(), append(m.baseEnv(), "ONLY=this"), "env")
|
|
if ran.Status != 0 || strings.Contains(ran.Stdout, "hunter") || !strings.Contains(ran.Stdout, "ONLY=this") {
|
|
t.Errorf("the clean runner printed %q", ran.Stdout)
|
|
}
|
|
}
|
|
|
|
func TestFetchRefusesWhatIsNotARepositoryBeforeAnythingRuns(t *testing.T) {
|
|
m, f := fetchMachine(t, nil, nil)
|
|
for _, repo := range []string{"", "x", "../etc/passwd", "a/b/c", "a/..", "o/r;rm"} {
|
|
if _, err := m.fetch(context.Background(), map[string]any{"repository": repo, "commit": commitSHA}); err == nil {
|
|
t.Errorf("%q was accepted", repo)
|
|
}
|
|
}
|
|
if len(f.calls) != 0 {
|
|
t.Errorf("%d commands ran", len(f.calls))
|
|
}
|
|
}
|
|
|
|
func TestFetchAsksGitForTheSshRemote(t *testing.T) {
|
|
m, f := fetchMachine(t, typeScriptModule(`{}`), nil)
|
|
if _, err := m.fetch(context.Background(), fetchArgs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var all []string
|
|
for _, c := range f.calls {
|
|
all = append(all, c.name+" "+strings.Join(c.args, " "))
|
|
}
|
|
lines := strings.Join(all, "\n")
|
|
for _, want := range []string{"remote add origin ssh://mesh-forge/novox/mesh-catalog.git", "fetch --no-tags --depth 1 origin " + commitSHA, "checkout -q --detach " + commitSHA} {
|
|
if !strings.Contains(lines, want) {
|
|
t.Errorf("git was not asked %q:\n%s", want, lines)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWorkspacesOlderThanADayAreRemoved(t *testing.T) {
|
|
m, _, work := newMachine(t)
|
|
old, fresh := filepath.Join(work, "aaaa"), filepath.Join(work, "bbbb")
|
|
for _, d := range []string{old, fresh, filepath.Join(work, "Not-A-Workspace")} {
|
|
os.MkdirAll(d, 0o755)
|
|
}
|
|
os.Chtimes(old, time.Now().Add(-25*time.Hour), time.Now().Add(-25*time.Hour))
|
|
os.Chtimes(filepath.Join(work, "Not-A-Workspace"), time.Now().Add(-48*time.Hour), time.Now().Add(-48*time.Hour))
|
|
m.CleanWorkspaces(time.Now())
|
|
if _, err := os.Stat(old); err == nil {
|
|
t.Errorf("the old workspace is kept")
|
|
}
|
|
if _, err := os.Stat(fresh); err != nil {
|
|
t.Errorf("the fresh workspace was removed")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(work, "Not-A-Workspace")); err != nil {
|
|
t.Errorf("a directory that is no workspace was removed")
|
|
}
|
|
}
|