| 1 | package cage |
| 2 | |
| 3 | import ( |
| 4 | "archive/tar" |
| 5 | "bytes" |
| 6 | "compress/gzip" |
| 7 | "crypto/sha256" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | func miseArchive(t *testing.T, member, content string) ([]byte, string) { |
| 17 | t.Helper() |
| 18 | var buffer bytes.Buffer |
| 19 | z := gzip.NewWriter(&buffer) |
| 20 | w := tar.NewWriter(z) |
| 21 | if err := w.WriteHeader(&tar.Header{Name: member, Mode: 0755, Size: int64(len(content))}); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if _, err := w.Write([]byte(content)); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := w.Close(); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := z.Close(); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | data := buffer.Bytes() |
| 34 | return data, fmt.Sprintf("%x", sha256.Sum256(data)) |
| 35 | } |
| 36 | |
| 37 | func TestMiseArchivesHaveIndependentPinsResumeAndFallback(t *testing.T) { |
| 38 | for _, tc := range []struct{ arch, mode, sources string }{ |
| 39 | {"amd64", "fresh", "release:0"}, {"arm64", "fresh", "release:0"}, |
| 40 | {"amd64", "official-failure", "release:0 npm:0"}, |
| 41 | {"amd64", "bad-mirror", "release:0 npm:0 release:0"}, |
| 42 | {"arm64", "cached-release", ""}, {"amd64", "partial-release", "release:7"}, {"arm64", "partial-npm", "release:0 npm:7"}, |
| 43 | {"amd64", "both-corrupt", "release:0 npm:0 release:0"}, |
| 44 | } { |
| 45 | t.Run(tc.arch+"/"+tc.mode, func(t *testing.T) { |
| 46 | dir := t.TempDir() |
| 47 | binary := "#!/bin/sh\necho 2026.10.25\n# " + tc.arch + "\n" |
| 48 | npm, nh := miseArchive(t, "package/bin/mise", binary) |
| 49 | release, rh := miseArchive(t, "mise/bin/mise", binary) |
| 50 | cache := filepath.Join(dir, ".cache", "cage", "downloads", rh) |
| 51 | write := func(path string, data []byte, mode os.FileMode) { |
| 52 | t.Helper() |
| 53 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := os.WriteFile(path, data, mode); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | } |
| 60 | write(filepath.Join(dir, "npm-input"), npm, 0600) |
| 61 | write(filepath.Join(dir, "release-input"), release, 0600) |
| 62 | switch tc.mode { |
| 63 | case "cached-release": |
| 64 | write(filepath.Join(cache, "mise.tar.gz"), release, 0600) |
| 65 | case "partial-release": |
| 66 | write(filepath.Join(cache, "mise.tar.gz.part"), release[:7], 0600) |
| 67 | case "partial-npm": |
| 68 | write(filepath.Join(cache, nh, "mise.tar.gz.part"), npm[:7], 0600) |
| 69 | } |
| 70 | if _, err := exec.LookPath("shasum"); err == nil { |
| 71 | write(filepath.Join(dir, "sha256sum"), []byte("#!/bin/sh\nexec shasum -a 256 \"$@\"\n"), 0755) |
| 72 | } else if _, err = exec.LookPath("sha256sum"); err != nil { |
| 73 | t.Skip("SHA-256 command unavailable") |
| 74 | } |
| 75 | for name, cmd := range map[string]string{ |
| 76 | "flock": "exit 0", |
| 77 | "dpkg": "if [ \"$1\" = --print-architecture ]; then echo \"$TEST_ARCH\"; else test \"$2\" = \"$4\"; fi", |
| 78 | "curl": `set -eu |
| 79 | while [ "$#" -gt 0 ]; do |
| 80 | case "$1" in |
| 81 | "https://cdn.npmmirror.com/packages/%40jdxcode/mise-linux-$NPM_ARCH/2026.10.25/mise-linux-$NPM_ARCH-2026.10.25.tgz") src=npm ;; |
| 82 | "https://github.com/jdx/mise/releases/download/v2026.10.25/mise-v2026.10.25-linux-$NPM_ARCH.tar.gz") src=release ;; |
| 83 | -o) shift; out=$1 ;; |
| 84 | esac |
| 85 | shift |
| 86 | done |
| 87 | n=0 |
| 88 | if [ -f "$out" ]; then n=$(wc -c <"$out" | tr -d ' '); fi |
| 89 | printf '%s:%s\n' "$src" "$n" >> "$HOME/sources" |
| 90 | if [ "$src" = release ]; then |
| 91 | case "$TEST_MODE" in official-failure|partial-npm) printf 503; exit 22;; esac |
| 92 | if [ "$TEST_MODE" = bad-mirror ] && [ "$(wc -l < "$HOME/sources" | tr -d ' ')" = 1 ]; then printf 503; exit 22; fi |
| 93 | fi |
| 94 | if [ "$TEST_MODE" = both-corrupt ] || { [ "$src" = npm ] && [ "$TEST_MODE" = bad-mirror ]; }; then |
| 95 | printf wrong-archive >"$out" |
| 96 | else |
| 97 | dd if="$HOME/$src-input" bs=1 skip="$n" 2>/dev/null >>"$out" |
| 98 | fi |
| 99 | printf 200`, |
| 100 | } { |
| 101 | write(filepath.Join(dir, name), []byte("#!/bin/sh\n"+cmd+"\n"), 0755) |
| 102 | } |
| 103 | npmArch := tc.arch |
| 104 | if npmArch == "amd64" { |
| 105 | npmArch = "x64" |
| 106 | } |
| 107 | script := ToolInstallPrelude() + fmt.Sprintf("\nCAGE_MISE_%s_SHA256=%s\nCAGE_MISE_NPM_%s_SHA256=%s\n", strings.ToUpper(tc.arch), rh, strings.ToUpper(tc.arch), nh) + "cage_install_tool mise" |
| 108 | run := func() ([]byte, error) { |
| 109 | cmd := exec.CommandContext(t.Context(), "bash", "-c", script) |
| 110 | cmd.Env = append(os.Environ(), "HOME="+dir, "PATH="+dir+":"+os.Getenv("PATH"), "NPM_ARCH="+npmArch, "TEST_ARCH="+tc.arch, "TEST_MODE="+tc.mode, "CAGE_SETUP_PROGRESS=1") |
| 111 | return cmd.CombinedOutput() |
| 112 | } |
| 113 | out, err := run() |
| 114 | got, fileErr := os.ReadFile(filepath.Join(dir, ".local", "bin", "mise")) |
| 115 | if tc.mode == "both-corrupt" { |
| 116 | if err == nil || !os.IsNotExist(fileErr) { |
| 117 | t.Fatalf("corrupted tool installed: %s", out) |
| 118 | } |
| 119 | } else { |
| 120 | if err != nil || fileErr != nil || string(got) != binary { |
| 121 | t.Fatalf("mise installation: %v %v %s", err, fileErr, out) |
| 122 | } |
| 123 | if tc.sources != "" && !strings.Contains(string(out), "CAGE_TOOL_BYTES mise.tar.gz ") { |
| 124 | t.Fatalf("missing byte progress: %s", out) |
| 125 | } |
| 126 | if out, err = run(); err != nil || !strings.Contains(string(out), "already meets") { |
| 127 | t.Fatalf("installed version was not reused: %v %s", err, out) |
| 128 | } |
| 129 | } |
| 130 | sources, _ := os.ReadFile(filepath.Join(dir, "sources")) |
| 131 | if got := strings.Join(strings.Fields(string(sources)), " "); got != tc.sources { |
| 132 | t.Fatalf("sources %q want %q", got, tc.sources) |
| 133 | } |
| 134 | }) |
| 135 | } |
| 136 | } |