-
Notifications
You must be signed in to change notification settings - Fork 1
feat(update): self-update notifications and auto-update #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2db043b
feat(update): self-update notifications and auto-update
neurolabs 1a9dae3
Merge branch 'main' into opencode/release-handling
neurolabs 0d65957
refactor(upgrade): address review feedback on self-upgrade
neurolabs efa366d
test(upgrade): cover self-upgrade code paths
neurolabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| sandboxmsb "github.com/inoio/opencode-sandbox/internal/sandbox/msb" | ||
| "github.com/inoio/opencode-sandbox/internal/termio" | ||
| "github.com/inoio/opencode-sandbox/internal/upgrade" | ||
| launcherconfig "github.com/inoio/opencode-sandbox/internal/viperconfig" | ||
| ) | ||
|
|
||
| func TestUpgrade(t *testing.T) { | ||
| origVersion := version | ||
| origLatest := upgrade.LatestVersion | ||
| origUpdate := upgrade.Update | ||
| t.Cleanup(func() { | ||
| version = origVersion | ||
| upgrade.LatestVersion = origLatest | ||
| upgrade.Update = origUpdate | ||
| }) | ||
|
|
||
| t.Run("dev build is rejected", func(t *testing.T) { | ||
| version = devVersion | ||
| updateCmd, _ := setupUpgradeTestFixtures(t) | ||
| err := updateCmd.RunE(updateCmd, nil) | ||
| if err == nil { | ||
| t.Fatal("expected error for dev build") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("up to date", func(t *testing.T) { | ||
| version = "1.0.0" | ||
| upgrade.LatestVersion = func(context.Context) (string, error) { return "1.0.0", nil } | ||
| upgrade.Update = func(context.Context, string) error { t.Fatal("Update should not be called"); return nil } | ||
|
|
||
| updateCmd, testUI := setupUpgradeTestFixtures(t) | ||
| if err := updateCmd.RunE(updateCmd, nil); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !strings.Contains(strings.Join(testUI.InfoCalls, " "), "up to date") { | ||
| t.Fatalf("expected 'up to date' info, got %v", testUI.InfoCalls) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("update available", func(t *testing.T) { | ||
| version = "1.0.0" | ||
| upgrade.LatestVersion = func(context.Context) (string, error) { return "2.0.0", nil } | ||
| var installed string | ||
| upgrade.Update = func(_ context.Context, latest string) error { | ||
| installed = latest | ||
| return nil | ||
| } | ||
|
|
||
| updateCmd, testUI := setupUpgradeTestFixtures(t) | ||
| if err := updateCmd.RunE(updateCmd, nil); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if installed != "2.0.0" { | ||
| t.Fatalf("installed version = %q, want 2.0.0", installed) | ||
| } | ||
| if !strings.Contains(strings.Join(testUI.InfoCalls, " "), "upgraded") { | ||
| t.Fatalf("expected 'upgraded' info, got %v", testUI.InfoCalls) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("latest lookup failure", func(t *testing.T) { | ||
| version = "1.0.0" | ||
| upgrade.LatestVersion = func(context.Context) (string, error) { return "", errors.New("boom") } | ||
| upgrade.Update = func(context.Context, string) error { t.Fatal("Update should not be called"); return nil } | ||
|
|
||
| updateCmd, _ := setupUpgradeTestFixtures(t) | ||
| if err := updateCmd.RunE(updateCmd, nil); err == nil { | ||
| t.Fatal("expected error when latest lookup fails") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func setupUpgradeTestFixtures(t *testing.T) (*cobra.Command, *termio.Mock) { | ||
| t.Helper() | ||
| testUI := termio.NewTestMock(t) | ||
| root := buildRootCmd(&testUI) | ||
| upgradeCmd, _, _ := root.Find([]string{"upgrade"}) | ||
| return upgradeCmd, &testUI | ||
| } | ||
|
|
||
| func TestCheckForUpgrade(t *testing.T) { | ||
| origVersion := version | ||
| origCheck := upgradeCheck | ||
| t.Cleanup(func() { | ||
| version = origVersion | ||
| upgradeCheck = origCheck | ||
| }) | ||
|
|
||
| t.Run("nil resolver no-ops", func(t *testing.T) { | ||
| exit, err := checkForUpgrade(context.Background(), nil, &termio.Mock{}) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if exit { | ||
| t.Fatal("expected exit=false for a nil resolver") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("dev version no-ops", func(t *testing.T) { | ||
| version = devVersion | ||
| r := launcherconfig.NewResolverWithConfig(launcherconfig.Config{}) | ||
| exit, err := checkForUpgrade(context.Background(), r, &termio.Mock{}) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if exit { | ||
| t.Fatal("expected exit=false for a dev version") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("propagates check error", func(t *testing.T) { | ||
| upgradeCheck = func(context.Context, upgrade.Options) (upgrade.Result, error) { | ||
| return upgrade.Result{}, errors.New("boom") | ||
| } | ||
| r := launcherconfig.NewResolverWithConfig(launcherconfig.Config{}) | ||
| if _, err := checkForUpgrade(context.Background(), r, &termio.Mock{}); err == nil { | ||
| t.Fatal("expected the upgrade-check error to propagate") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("reports exit", func(t *testing.T) { | ||
| upgradeCheck = func(context.Context, upgrade.Options) (upgrade.Result, error) { | ||
| return upgrade.Result{Exit: true}, nil | ||
| } | ||
| r := launcherconfig.NewResolverWithConfig(launcherconfig.Config{}) | ||
| exit, err := checkForUpgrade(context.Background(), r, &termio.Mock{}) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !exit { | ||
| t.Fatal("expected exit=true when the upgrade-and-exit mode ran") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestRunShellUpgradeExit(t *testing.T) { | ||
| for _, cmdName := range []string{"run", "shell"} { | ||
| t.Run(cmdName, func(t *testing.T) { | ||
| initTestRepo(t) | ||
| origCheck := upgradeCheck | ||
| t.Cleanup(func() { upgradeCheck = origCheck }) | ||
| upgradeCheck = func(context.Context, upgrade.Options) (upgrade.Result, error) { | ||
| return upgrade.Result{Exit: true}, nil | ||
| } | ||
|
|
||
| mock := &sandboxmsb.MockMsbClient{} | ||
| root, _ := setupRunMocks(t, mock, &sandboxmsb.MockSandbox{}, cmdName) | ||
|
|
||
| // The upgrade-and-exit path must terminate before starting a session, | ||
| // so the command succeeds without ever creating a sandbox. | ||
| if err := root.Execute(); err != nil { | ||
| t.Fatalf("expected clean exit after upgrade-and-exit, got: %v", err) | ||
| } | ||
| if len(mock.CreatedSandboxCalls) != 0 { | ||
| t.Errorf("expected no sandbox creation, got %d calls", len(mock.CreatedSandboxCalls)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRunShellUpgradeError(t *testing.T) { | ||
| for _, cmdName := range []string{"run", "shell"} { | ||
| t.Run(cmdName, func(t *testing.T) { | ||
| initTestRepo(t) | ||
| origCheck := upgradeCheck | ||
| t.Cleanup(func() { upgradeCheck = origCheck }) | ||
| upgradeCheck = func(context.Context, upgrade.Options) (upgrade.Result, error) { | ||
| return upgrade.Result{}, errors.New("upgrade failed") | ||
| } | ||
|
|
||
| mock := &sandboxmsb.MockMsbClient{} | ||
| root, _ := setupRunMocks(t, mock, &sandboxmsb.MockSandbox{}, cmdName) | ||
|
|
||
| err := root.Execute() | ||
| if err == nil { | ||
| t.Fatal("expected the upgrade-check error to abort the command") | ||
| } | ||
| if !strings.Contains(err.Error(), "upgrade failed") { | ||
| t.Errorf("expected error containing 'upgrade failed', got: %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update vs. upgrade naming clash. upgrade is better IMHO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed update->upgrade across the package, config keys (upgrade.mode/upgrade.interval), env vars, state file, and docs for consistent naming.