fix(kiosk): fetch a required plugin module whose replace directory is missing
A plugin developed inside an application replaces its required plugins with sibling directories (../user). Checked out on its own, that directory is missing and plugin.yaml lost the minimum (golem15.user: ""), so the manifest differed between the two layouts and TestPluginManifest could not pass in both. Such a module is now fetched at the version go.mod requires, as a consumer would see it. - listedModule.fallback marks a local replace whose directory is absent - fallback modules download separately, so one unfetchable version does not hold up the others - TestDeriveMissingReplace over a file:// proxy (offline)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package kiosk
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"context"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
@@ -250,3 +253,105 @@ func TestPluginDirs(t *testing.T) {
|
||||
t.Errorf("pluginDirs = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeriveMissingReplace: a required plugin module replaced by a local
|
||||
// directory that is not checked out (a plugin developed inside an
|
||||
// application, checked out on its own) gets the version go.mod requires,
|
||||
// fetched from the module proxy, so the manifest is the same in both
|
||||
// layouts. A version the proxy does not have leaves the minimum empty with
|
||||
// a warning. The proxy is a file:// tree; no network is used.
|
||||
func TestDeriveMissingReplace(t *testing.T) {
|
||||
requireGo(t)
|
||||
ctx := context.Background()
|
||||
root := t.TempDir()
|
||||
proxy := filepath.Join(root, "proxy")
|
||||
userMod := "module example.test/sm-user-plugin\n\ngo 1.27.0\n\nrequire " + FrameworkModule + " v0.6.0\n"
|
||||
writeProxyModule(t, proxy, "example.test/sm-user-plugin", "v0.1.2", map[string]string{
|
||||
"go.mod": userMod,
|
||||
"plugin.go": pluginHead + `func (*Plugin) ID() string { return "acme.user" }`,
|
||||
})
|
||||
modCache := filepath.Join(root, "modcache")
|
||||
t.Cleanup(func() {
|
||||
_ = filepath.WalkDir(modCache, func(path string, d os.DirEntry, err error) error {
|
||||
if err == nil {
|
||||
_ = os.Chmod(path, 0o755)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
})
|
||||
t.Setenv("GOPROXY", "file://"+filepath.ToSlash(proxy))
|
||||
t.Setenv("GOMODCACHE", modCache)
|
||||
t.Setenv("GOFLAGS", "-modcacherw")
|
||||
t.Setenv("GOSUMDB", "off")
|
||||
t.Setenv("GONOSUMDB", "")
|
||||
t.Setenv("GOPRIVATE", "")
|
||||
t.Setenv("GONOPROXY", "")
|
||||
t.Setenv("GOTOOLCHAIN", "local")
|
||||
|
||||
shopMod := func(userVersion string) string {
|
||||
return "module example.test/sm-shop-plugin\n\ngo 1.27.0\n\nrequire (\n\t" + FrameworkModule + " v0.6.0\n\texample.test/sm-user-plugin " + userVersion + "\n)\n\n" +
|
||||
"replace " + FrameworkModule + " => ../framework\n\nreplace example.test/sm-user-plugin => ../user\n"
|
||||
}
|
||||
writeTree(t, root, map[string]string{
|
||||
"framework/go.mod": "module " + FrameworkModule + "\n\ngo 1.27.0\n",
|
||||
"shop/go.mod": shopMod("v0.1.2"),
|
||||
"shop/plugin.go": pluginHead + `func (*Plugin) ID() string { return "acme.shop" }
|
||||
func (*Plugin) Requires() []string { return []string{"acme.user"} }`,
|
||||
"shop/README.md": "# Shop\n\nA shop.\n",
|
||||
})
|
||||
// ../user is the replace target and does not exist.
|
||||
m, warnings, err := Derive(ctx, filepath.Join(root, "shop"), DeriveOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Derive: %v", err)
|
||||
}
|
||||
if got := m.Requires["acme.user"]; got != "v0.1.2" || len(warnings) != 0 {
|
||||
t.Errorf("requires[acme.user] = %q, warnings %v; want v0.1.2 and none", got, warnings)
|
||||
}
|
||||
|
||||
// A required version the proxy does not have: no minimum, a warning.
|
||||
writeTree(t, root, map[string]string{"shop/go.mod": shopMod("v0.1.3")})
|
||||
m, warnings, err = Derive(ctx, filepath.Join(root, "shop"), DeriveOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Derive v0.1.3: %v", err)
|
||||
}
|
||||
if got, ok := m.Requires["acme.user"]; !ok || got != "" || len(warnings) != 1 || !strings.Contains(warnings[0], "not matched") {
|
||||
t.Errorf("v0.1.3: requires[acme.user] = %q (present %v), warnings %v", got, ok, warnings)
|
||||
}
|
||||
}
|
||||
|
||||
// writeProxyModule adds modPath@version to the file:// GOPROXY tree at
|
||||
// proxyDir: @v/list, .info, .mod (files["go.mod"]) and a .zip whose
|
||||
// entries are prefixed <module>@<version>/. Module paths are lower case,
|
||||
// so no escaping is needed.
|
||||
func writeProxyModule(t *testing.T, proxyDir, modPath, version string, files map[string]string) {
|
||||
t.Helper()
|
||||
dir := filepath.Join(proxyDir, filepath.FromSlash(modPath), "@v")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
zw := zip.NewWriter(&buf)
|
||||
names := slices.Sorted(maps.Keys(files))
|
||||
for _, name := range names {
|
||||
w, err := zw.Create(modPath + "@" + version + "/" + name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := w.Write([]byte(files[name])); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := zw.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, body := range map[string][]byte{
|
||||
"list": []byte(version + "\n"),
|
||||
version + ".info": []byte(`{"Version":"` + version + `","Time":"2026-10-01T00:00:00Z"}`),
|
||||
version + ".mod": []byte(files["go.mod"]),
|
||||
version + ".zip": buf.Bytes(),
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), body, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user