The latest release is placed whatever the range a package.json names (ADR 0573 §2); one outside that range is now said in the journal, for the SDK and every other mesh package, so a module built against a release its range excludes is visible. A range of a form not read is said as such. Builds: design 47 H H-A-267, H-A-276
236 lines
12 KiB
Go
236 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
|
)
|
|
|
|
// tgzOf is a packed npm package holding package/package.json of name@version and one more file.
|
|
func tgzOf(t *testing.T, name, version string) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
zw := gzip.NewWriter(&buf)
|
|
tw := tar.NewWriter(zw)
|
|
for _, f := range [][2]string{{"package/package.json", `{"name":"` + name + `","version":"` + version + `"}`}, {"package/dist/index.js", "export const v = '" + version + "';\n"}} {
|
|
n, body := f[0], f[1]
|
|
tw.WriteHeader(&tar.Header{Name: n, Mode: 0o644, Size: int64(len(body)), Typeflag: tar.TypeReg})
|
|
tw.Write([]byte(body))
|
|
}
|
|
tw.Close()
|
|
zw.Close()
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// fakeRegistry serves the packument and the packed files of the SDK's versions it holds; asked counts its answers.
|
|
func fakeRegistry(t *testing.T, m *Module, held map[string][]byte) *atomic.Int32 {
|
|
t.Helper()
|
|
return fakeRegistryOf(t, m, map[string]map[string][]byte{sdkPackage: held})
|
|
}
|
|
|
|
// fakeRegistryOf serves the packument and the packed files of each package's versions it holds (by name); asked counts
|
|
// its answers.
|
|
func fakeRegistryOf(t *testing.T, m *Module, held map[string]map[string][]byte) *atomic.Int32 {
|
|
t.Helper()
|
|
var asked atomic.Int32
|
|
var srv *httptest.Server
|
|
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
asked.Add(1)
|
|
if f, ok := strings.CutPrefix(r.URL.Path, "/files/"); ok {
|
|
name, v, _ := strings.Cut(strings.TrimSuffix(f, ".tgz"), "@@")
|
|
if tgz, ok := held[strings.ReplaceAll(name, "~", "/")][v]; ok {
|
|
w.Write(tgz)
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
name, ok := strings.CutPrefix(r.URL.Path, "/npm/")
|
|
name = strings.ReplaceAll(name, "%2f", "/")
|
|
if !ok || len(held[name]) == 0 {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
versions := map[string]any{}
|
|
for v, tgz := range held[name] {
|
|
versions[v] = map[string]any{"dist": map[string]any{"tarball": srv.URL + "/files/" + strings.ReplaceAll(name, "/", "~") + "@@" + v + ".tgz", "integrity": integrityOf(tgz)}}
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"versions": versions})
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
m.Toolchain.Registry = func() (Registry, error) { return Registry{URL: srv.URL + "/npm/"}, nil }
|
|
m.HTTP = srv.Client()
|
|
return &asked
|
|
}
|
|
|
|
// memberLocking is a member's directory whose lock names @novox/mesh-sdk@version with integrity (none when version is "").
|
|
func memberLocking(t *testing.T, version, integrity string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
lock := map[string]any{"lockfileVersion": 3, "packages": map[string]any{"": map[string]any{"name": "m"}}}
|
|
if version != "" {
|
|
lock["packages"].(map[string]any)["node_modules/@novox/mesh-sdk"] = map[string]any{"version": version, "integrity": integrity}
|
|
}
|
|
raw, _ := json.Marshal(lock)
|
|
write(t, filepath.Join(dir, "package-lock.json"), string(raw), 0o644)
|
|
return dir
|
|
}
|
|
|
|
func placedVersion(dir string) string {
|
|
raw, err := os.ReadFile(filepath.Join(dir, "node_modules", "@novox", "mesh-sdk", "package.json"))
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
var p packageJSON
|
|
json.Unmarshal(raw, &p)
|
|
return p.Version
|
|
}
|
|
|
|
// ADR 0573 §2 (unpinned, the lead's decision L10's lock version gone): a member is built against the latest release the
|
|
// registry holds, whatever its lock names or when it names none, checked against the lock's integrity only when the lock
|
|
// names that version; a pre-release is never the latest; a registry holding no release is a wait, `waits for:`.
|
|
func TestAMemberGetsTheLatestPublishedSDKWhateverItsLockNames(t *testing.T) {
|
|
m, _, _ := newMachine(t)
|
|
held, older, pre := tgzOf(t, sdkPackage, "0.1.15"), tgzOf(t, sdkPackage, "0.1.9"), tgzOf(t, sdkPackage, "0.1.16-feat-x.1")
|
|
fakeRegistry(t, m, map[string][]byte{"0.1.15": held, "0.1.9": older, "0.1.16-feat-x.1": pre})
|
|
for _, c := range []struct {
|
|
name, locks, integrity, fails string
|
|
}{
|
|
{name: "a lock naming the latest", locks: "0.1.15", integrity: integrityOf(held)},
|
|
{name: "a lock naming an older version (the pin)", locks: "0.1.9", integrity: integrityOf(older)},
|
|
{name: "a lock naming a version the registry lacks", locks: "0.1.16", integrity: "sha512-x"},
|
|
{name: "a lock naming none", locks: ""},
|
|
{name: "a lock naming the latest with another integrity", locks: "0.1.15", integrity: "sha512-another", fails: "sha512-another"},
|
|
} {
|
|
dir := memberLocking(t, c.locks, c.integrity)
|
|
err := m.placeSDK(context.Background(), dir, "modules/m", nil)
|
|
switch {
|
|
case c.fails == "" && (err != nil || placedVersion(dir) != "0.1.15"):
|
|
t.Errorf("%s: %v, placed %q, want the latest release 0.1.15", c.name, err, placedVersion(dir))
|
|
case c.fails != "" && (err == nil || !strings.Contains(err.Error(), c.fails)):
|
|
t.Errorf("%s: %v, want it to fail naming %s", c.name, err, c.fails)
|
|
}
|
|
}
|
|
fakeRegistry(t, m, map[string][]byte{"0.1.16-feat-x.1": pre})
|
|
if err := m.placeSDK(context.Background(), memberLocking(t, "0.1.15", ""), "modules/m", nil); err == nil || err.Error() != builds.WaitsFor+"@novox/mesh-sdk" {
|
|
t.Errorf("a registry with no release answered %v, want %s@novox/mesh-sdk", err, builds.WaitsFor)
|
|
}
|
|
}
|
|
|
|
// design 47 G A-284, A-285 (ADR 0500 §5): the group's packed SDK is placed in place of the registry's whatever the
|
|
// member's lock names, its version, its integrity or none, and the registry is not asked.
|
|
func TestTheGroupsSDKIsPlacedInPlaceOfTheRegistrys(t *testing.T) {
|
|
m, _, _ := newMachine(t)
|
|
asked := fakeRegistry(t, m, map[string][]byte{"0.1.14": tgzOf(t, sdkPackage, "0.1.14")})
|
|
tgz := tgzOf(t, sdkPackage, "0.1.15")
|
|
group := &packed{Name: sdkPackage, Version: "0.1.15", Integrity: integrityOf(tgz), Digest: digestOf(tgz), tgz: tgz}
|
|
for _, lock := range [][2]string{{"0.1.15", group.Integrity}, {"0.1.15", "sha512-other"}, {"0.1.14", integrityOf(tgzOf(t, sdkPackage, "0.1.14"))}, {"0.1.13", "sha512-x"}, {"", ""}} {
|
|
dir := memberLocking(t, lock[0], lock[1])
|
|
if err := m.placeSDK(context.Background(), dir, "modules/m", group); err != nil || placedVersion(dir) != "0.1.15" {
|
|
t.Errorf("a member locking %q %q: %v, placed %q, want the group's 0.1.15", lock[0], lock[1], err, placedVersion(dir))
|
|
}
|
|
}
|
|
if asked.Load() != 0 {
|
|
t.Errorf("the registry was asked %d times", asked.Load())
|
|
}
|
|
}
|
|
|
|
// design 47 G A-285: each of the group's packages is placed by its package.json name in a module that names it, in
|
|
// place of what the install put there; one it does not name, the module itself and the SDK (placeSDK's) are not.
|
|
func TestTheGroupsPackagesArePlacedInEveryModuleThatNamesThem(t *testing.T) {
|
|
served := map[string]*packed{}
|
|
for _, name := range []string{sdkPackage, "@novox/mesh-delivery-contracts-git", "@novox/mesh-delivery-contracts-mesh-catalog", "@novox/mesh-contracts-x", "@novox/module-m"} {
|
|
served[name] = &packed{Name: name, Version: "0.2.0", tgz: tgzOf(t, name, "0.2.0")}
|
|
}
|
|
dir := t.TempDir()
|
|
write(t, filepath.Join(dir, "package.json"), `{"name":"@novox/module-m","version":"0.1.0","dependencies":{"@novox/mesh-delivery-contracts-git":"^0.1.0","@novox/mesh-sdk":"^0.1.19","@novox/module-m":"0.1.0"},"devDependencies":{"@novox/mesh-delivery-contracts-mesh-catalog":"^0.1.0"}}`, 0o644)
|
|
write(t, filepath.Join(dir, "node_modules", "@novox", "mesh-delivery-contracts-git", "package.json"), `{"name":"@novox/mesh-delivery-contracts-git","version":"0.1.0"}`, 0o644)
|
|
if err := placeGroup(dir, served); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, want := range map[string]string{"@novox/mesh-delivery-contracts-git": "0.2.0", "@novox/mesh-delivery-contracts-mesh-catalog": "0.2.0",
|
|
"@novox/mesh-contracts-x": "", sdkPackage: "", "@novox/module-m": ""} {
|
|
p, _ := readPackageJSON(filepath.Join(dir, "node_modules", filepath.FromSlash(name)))
|
|
if p.Version != want {
|
|
t.Errorf("%s placed at %q, want %q", name, p.Version, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The fetch's `group-packages` is a list of a repository and a full commit, with a pull request's head at most and a
|
|
// directory inside the member; anything else is refused before anything is fetched.
|
|
func TestTheGroupPackagesArgumentIsRefusedWhenItIsNotOne(t *testing.T) {
|
|
full := strings.Repeat("a", 40)
|
|
for _, bad := range []any{"novox/mesh-sdk", map[string]any{"repository": "novox/mesh-sdk", "commit": full},
|
|
[]any{map[string]any{"repository": "novox/mesh-sdk", "commit": "main"}},
|
|
[]any{map[string]any{"repository": "../x", "commit": full}},
|
|
[]any{map[string]any{"repository": "novox/mesh-sdk", "commit": full, "ref": "refs/heads/main"}},
|
|
[]any{map[string]any{"repository": "novox/mesh-delivery", "commit": full, "path": "../x"}},
|
|
[]any{map[string]any{"repository": "novox/mesh-delivery", "commit": full, "path": "/abs"}},
|
|
[]any{map[string]any{"name": sdkPackage, "version": "0.1.16-feat-x.1"}}} {
|
|
if _, err := groupPackagesArg(bad); err == nil {
|
|
t.Errorf("%v was taken", bad)
|
|
}
|
|
}
|
|
gs, err := groupPackagesArg([]any{map[string]any{"repository": "novox/mesh-sdk", "commit": full, "ref": "refs/pull/9/head"},
|
|
map[string]any{"repository": "novox/mesh-delivery", "commit": full, "path": "modules/mesh-delivery-contracts/ts/seats/git/"}})
|
|
if err != nil || len(gs) != 2 || gs[0].Ref != "refs/pull/9/head" || gs[0].Path != "" || gs[1].Path != "modules/mesh-delivery-contracts/ts/seats/git" {
|
|
t.Errorf("a member's root and a member's directory: %+v %v", gs, err)
|
|
}
|
|
}
|
|
|
|
// design 47 H, H-A-276 (amending H-A-267): the latest release placed outside the range the package file names is said in
|
|
// the journal, and placed all the same; one inside it is not said; a range of a form not read is said as such.
|
|
func TestALatestReleaseOutsideItsRangeIsSaid(t *testing.T) {
|
|
for _, c := range []struct {
|
|
spec, v string
|
|
in, read bool
|
|
}{
|
|
{"^0.1.0", "0.1.9", true, true}, {"^0.1.0", "0.2.0", false, true}, {"^1.2.3", "1.9.0", true, true},
|
|
{"^1.2.3", "2.0.0", false, true}, {"^0.0.3", "0.0.4", false, true}, {"^0.x", "0.9.0", true, true},
|
|
{"~0.1.2", "0.1.9", true, true}, {"~0.1.2", "0.2.0", false, true}, {"~1", "1.5.0", true, true},
|
|
{"0.1.4", "0.1.4", true, true}, {"0.1.4", "0.1.5", false, true}, {"0.1.x", "0.1.7", true, true},
|
|
{"0.1", "0.2.0", false, true}, {"*", "9.9.9", true, true}, {"^0.1.0", "0.0.9", false, true},
|
|
{">=0.1.0", "0.2.0", false, false}, {"^0.1.0 || ^0.2.0", "0.2.0", false, false}, {"file:../x", "0.1.0", false, false},
|
|
} {
|
|
if in, read := inRange(c.spec, c.v); in != c.in || read != c.read {
|
|
t.Errorf("inRange(%q, %q) = %v, %v; want %v, %v", c.spec, c.v, in, read, c.in, c.read)
|
|
}
|
|
}
|
|
m, _, _ := newMachine(t)
|
|
const name = "@novox/mesh-delivery-contracts-git"
|
|
fakeRegistryOf(t, m, map[string]map[string][]byte{name: {"0.1.9": tgzOf(t, name, "0.1.9"), "0.2.0": tgzOf(t, name, "0.2.0")}})
|
|
for _, c := range []struct{ spec, said string }{
|
|
{"^0.1.0", `the latest published ` + name + `@0.2.0 lies outside the range "^0.1.0"`},
|
|
{"^0.2.0", ""},
|
|
{">=0.1.0", `at ">=0.1.0", a range not read`},
|
|
} {
|
|
var said []string
|
|
m.Say = func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }
|
|
dir := t.TempDir()
|
|
write(t, filepath.Join(dir, "package.json"), `{"dependencies":{"`+name+`":"`+c.spec+`"}}`, 0o644)
|
|
if err := m.placeLatest(context.Background(), dir, "modules/m", name); err != nil {
|
|
t.Fatalf("%s: %v", c.spec, err)
|
|
}
|
|
if p, _ := readPackageJSON(filepath.Join(dir, "node_modules", filepath.FromSlash(name))); p.Version != "0.2.0" {
|
|
t.Errorf("%s: placed %q, want the latest release 0.2.0", c.spec, p.Version)
|
|
}
|
|
joined := strings.Join(said, "\n")
|
|
if c.said == "" && joined != "" || c.said != "" && !strings.Contains(joined, c.said) {
|
|
t.Errorf("%s: said %q, want %q", c.spec, joined, c.said)
|
|
}
|
|
}
|
|
}
|