From 584077dfc7a5d1fc6888864cfe44dbc613c3b5bc Mon Sep 17 00:00:00 2001 From: Patrick Creighton Date: Thu, 29 Oct 2026 22:51:08 +0000 Subject: [PATCH] p Reselect assets only when --filter changes cmd.Flags().Changed reports that --filter was passed, not that it differs from the saved filter. Repeating the same install command therefore selected assets again and reinstalled the package at the installed version, replacing the remembered selection. Compare --filter with the saved filter instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- AGENTS.md | 3 ++ README.md | 12 +++--- cmd/install.go | 10 +++-- cmd/install_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 8 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0f53ed1..f60f6ee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -147,6 +147,9 @@ that ordering. `--binary` are strict, repeatable selectors. Changing them works at the same release version and replaces the affected selection. Unselected old outputs are listed in the plan and removed only after the new installs succeed. + `--filter` counts as a change only when it differs from the saved filter, + because scripts repeat the same install command and expect nothing to happen + when the package is current. - **`--yes` installs all archive binaries on initial selection.** Asset selection still defaults to the first compatible candidate, with RPMs ranked first. Remembered archive subsets stay unchanged on updates, including with `--yes`. diff --git a/README.md b/README.md index 30b8f2f..48cc776 100644 --- a/README.md +++ b/README.md @@ -116,11 +116,13 @@ archive binaries are not added to remembered selections automatically. Passing `--asset` replaces the saved asset selection; passing `--binary` replaces the binary selection in the assets it applies to. Changing `--filter` -starts asset selection again. These flags work even when the release version is -already installed. The installation plan lists all outputs and any previously -tracked components that will be removed when the new selection succeeds. grm -asks for confirmation only when the plan removes components, so an install -with one suitable asset and one binary runs without prompts. +starts asset selection again; passing the saved filter again keeps the +selection, so a repeated install command does not reinstall the package. These +flags work even when the release version is already installed. The installation +plan lists all outputs and any previously tracked components that will be +removed when the new selection succeeds. grm asks for confirmation only when the +plan removes components, so an install with one suitable asset and one binary +runs without prompts. All components share a repository's release and lock. `update` updates them together; `remove` removes every tracked binary and system package. Downloads diff --git a/cmd/install.go b/cmd/install.go index 2e47f7b..88e10ad 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -7,6 +7,7 @@ import ( "os/exec" "path" "runtime" + "slices" "sort" "strings" @@ -81,7 +82,10 @@ Examples: previous.Components = append([]InstalledComponent(nil), previous.Components...) pkg = &previous } - if cmd.Flags().Changed("filter") { + // Changed only reports that --filter was passed. Repeating the saved + // filter keeps the remembered selection, so the command stays idempotent. + filterChanged := cmd.Flags().Changed("filter") && !slices.Equal(installFilter, pkg.Filter) + if filterChanged { pkg.Filter = installFilter pkg.Selections = nil } @@ -96,7 +100,7 @@ Examples: if err != nil { return err } - options := installOptions{assets: installAssets, binaries: binaries, rename: installRename, lock: installLock, reselect: cmd.Flags().Changed("filter")} + options := installOptions{assets: installAssets, binaries: binaries, rename: installRename, lock: installLock, reselect: filterChanged} // Check that package is not locked for _, installedItem := range config.Packages { @@ -115,7 +119,7 @@ Examples: } msgOK("Found release %s", boldCyan(release.GetTagName())) - if !installRefresh && !installLock && requested.VersionFilter == "" && !cmd.Flags().Changed("asset") && !cmd.Flags().Changed("binary") && !cmd.Flags().Changed("rename") && !cmd.Flags().Changed("filter") { + if !installRefresh && !installLock && requested.VersionFilter == "" && !cmd.Flags().Changed("asset") && !cmd.Flags().Changed("binary") && !cmd.Flags().Changed("rename") && !filterChanged { // Check if package of selected release has already been installed for _, installedItem := range config.Packages { if installedItem.GetFullName() == pkg.GetFullName() { diff --git a/cmd/install_test.go b/cmd/install_test.go index 408ed6e..5cf9b7d 100644 --- a/cmd/install_test.go +++ b/cmd/install_test.go @@ -1,7 +1,15 @@ package cmd import ( + "bytes" + "fmt" + "io" + "net/http" + "os" "os/exec" + "path/filepath" + "slices" + "strings" "testing" "github.com/google/go-github/v93/github" @@ -662,3 +670,85 @@ func stringInSlice(a string, list []string) bool { } return false } + +func withInstallFilter(t *testing.T, value string) { + t.Helper() + t.Cleanup(func() { + installFilter = nil + installCmd.Flags().Lookup("filter").Changed = false + }) + if err := installCmd.Flags().Set("filter", value); err != nil { + t.Fatal(err) + } +} + +func TestInstallReselectsOnlyWhenFilterChanges(t *testing.T) { + cases := []struct { + name, filter, latest string + wantDownloads int + wantBinaries []string + }{ + {name: "same filter at installed version", filter: "linux", latest: "v1.0.0", wantBinaries: []string{"tool"}}, + {name: "same filter keeps remembered binaries", filter: "linux", latest: "v2.0.0", wantDownloads: 1, wantBinaries: []string{"tool"}}, + {name: "changed filter selects again", filter: "zip", latest: "v1.0.0", wantDownloads: 1, wantBinaries: []string{"helper", "tool"}}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + withYes(t, true) + payload, err := os.ReadFile(releaseArchive(t, map[string][]byte{ + "tools-" + c.latest + "/helper": fixtureBinary, + "tools-" + c.latest + "/tool": fixtureBinary, + })) + if err != nil { + t.Fatal(err) + } + release := fmt.Sprintf(`{"tag_name":%q,"assets":[{"id":1,"name":"tools_%s_linux_amd64.zip"}]}`, c.latest, c.latest) + downloads := 0 + withGitHubTransport(t, func(request *http.Request) (*http.Response, error) { + if strings.HasSuffix(request.URL.Path, "/releases/assets/1") { + downloads++ + return &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"application/octet-stream"}}, Body: io.NopCloser(bytes.NewReader(payload)), Request: request}, nil + } + return httpResponse(request, http.StatusOK, []byte(release)), nil + }) + fakes := t.TempDir() + fakeProgram(t, fakes, "sudo", `exec "$@"`) + for _, program := range []string{"cp", "chmod", "mv", "rm"} { + fakeProgram(t, fakes, program, "exit 0") + } + t.Setenv("PATH", fakes) + // The installed binary lives in a temporary directory so VerifyVersion can hash it. + installed := filepath.Join(t.TempDir(), "tool") + if err := os.WriteFile(installed, fixtureBinary, 0755); err != nil { + t.Fatal(err) + } + config, err := ReadConfig(ConfigFile) + if err != nil { + t.Fatal(err) + } + if err := config.PutPackage(&Package{ + Owner: "owner", Repo: "tools", Version: "v1.0.0", Filter: []string{"linux"}, + Selections: []AssetSelection{{Pattern: "tools_*_linux_amd64.zip", Binaries: []BinarySelection{{Path: "tools-*/tool"}}}}, + Components: []InstalledComponent{{Asset: "tools_v1.0.0_linux_amd64.zip", Source: "tools-v1.0.0/tool", Version: "v1.0.0", Filename: installed}}, + }); err != nil { + t.Fatal(err) + } + withInstallFilter(t, c.filter) + if err := installCmd.RunE(installCmd, []string{"owner/tools"}); err != nil { + t.Fatal(err) + } + config, err = ReadConfig(ConfigFile) + if err != nil { + t.Fatal(err) + } + var binaries []string + for _, component := range config.Packages["owner/tools"].Components { + binaries = append(binaries, filepath.Base(component.Filename)) + } + slices.Sort(binaries) + if downloads != c.wantDownloads || !slices.Equal(binaries, c.wantBinaries) { + t.Errorf("downloaded %d assets and installed %v, want %d and %v", downloads, binaries, c.wantDownloads, c.wantBinaries) + } + }) + } +}