Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions internal/installer/applycontext_abort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import (
)

// recordReporter captures Header calls so a test can assert which install
// phases ApplyContext actually entered.
type recordReporter struct{ headers []string }

func (r *recordReporter) Header(msg string) { r.headers = append(r.headers, msg) }
func (r *recordReporter) Info(string) {}
func (r *recordReporter) Success(string) {}
func (r *recordReporter) Warn(string) {}
func (r *recordReporter) Error(string) {}
func (r *recordReporter) Muted(string) {}
// phases ApplyContext actually entered, and every other message in lines so a
// test can assert what the user was told.
type recordReporter struct {
headers []string
lines []string
}

func (r *recordReporter) Header(msg string) { r.headers = append(r.headers, msg) }
func (r *recordReporter) Info(msg string) { r.lines = append(r.lines, msg) }
func (r *recordReporter) Success(msg string) { r.lines = append(r.lines, msg) }
func (r *recordReporter) Warn(msg string) { r.lines = append(r.lines, msg) }
func (r *recordReporter) Error(msg string) { r.lines = append(r.lines, msg) }
func (r *recordReporter) Muted(msg string) { r.lines = append(r.lines, msg) }

// A cancelled context must stop ApplyContext before the config steps run, so a
// ctrl+c abort doesn't keep symlinking dotfiles / rewriting macOS defaults after
Expand Down
33 changes: 33 additions & 0 deletions internal/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func abortWith(softErrs []error, cause error) error {
}

func showCompletionFromPlan(plan InstallPlan, r Reporter, errCount int) {
if plan.DryRun {
showDryRunCompletionFromPlan(plan, r, errCount)
return
}
ui.Println()
if errCount > 0 {
r.Header("Installation finished with errors")
Expand Down Expand Up @@ -249,6 +253,35 @@ func showCompletionFromPlan(plan InstallPlan, r Reporter, errCount int) {
ui.Println()
}

// showDryRunCompletionFromPlan is the summary for a --dry-run. Nothing was
// changed, so it must not claim anything was installed or configured: it says
// what a real run would install and how to start one. Git has no line here —
// whether it would be configured depends on the existing identity, and the Git
// step above has already said which way it goes.
func showDryRunCompletionFromPlan(plan InstallPlan, r Reporter, errCount int) {
ui.Println()
if errCount > 0 {
r.Header("Dry run finished with errors — no changes were made")
ui.Println()
r.Warn(fmt.Sprintf("%d step(s) had errors — check the output above for details.", errCount))
} else {
r.Header("Dry run complete — no changes were made")
}
ui.Println()

r.Info("Would install:")
r.Info(fmt.Sprintf(" - %d CLI packages", len(plan.Formulae)))
r.Info(fmt.Sprintf(" - %d GUI applications", len(plan.Casks)))
if len(plan.Npm) > 0 {
r.Info(fmt.Sprintf(" - %d npm global packages", len(plan.Npm)))
}
ui.Println()

r.Info("Next steps:")
r.Info(" - Run the same command without --dry-run to apply these changes")
ui.Println()
}

func showScreenRecordingReminderFromPlan(plan InstallPlan) {
if plan.DryRun || plan.Silent {
return
Expand Down
86 changes: 86 additions & 0 deletions internal/installer/installer_extra_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package installer

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -133,6 +134,91 @@ func TestShowCompletionFromPlan_WithNpm(t *testing.T) {
})
}

// A real run's summary is pinned line for line, so making the dry-run summary
// honest can't quietly reword it.
func TestShowCompletionFromPlan_RealRunOutput(t *testing.T) {
tests := []struct {
name string
errCount int
wantHeaders []string
wantStatus string
}{
{"clean", 0, []string{"Installation Complete!"}, "OpenBoot has successfully configured your Mac."},
{"with errors", 2, []string{"Installation finished with errors"}, "2 step(s) had errors — check the output above for details."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("HOME", t.TempDir())
plan := InstallPlan{
Silent: true, // keep the screen-recording reminder out of the test
Formulae: []string{"git", "curl"},
Casks: []string{"firefox"},
Npm: []string{"typescript"},
}
rr := &recordReporter{}
showCompletionFromPlan(plan, rr, tt.errCount)

assert.Equal(t, tt.wantHeaders, rr.headers)
assert.Equal(t, []string{
tt.wantStatus,
"What was installed:",
" - Git configured with your identity",
" - 2 CLI packages",
" - 1 GUI applications",
" - 1 npm global packages",
"Next steps:",
" - Restart your terminal to apply changes",
" - Run 'brew doctor' to verify Homebrew health",
}, rr.lines)
})
}
}

// A --dry-run changes nothing, so its summary must not say it did — no
// "Installation Complete!", no "What was installed:", no "restart your
// terminal to apply changes". It reports what a real run would install.
func TestShowCompletionFromPlan_DryRunDoesNotClaimChanges(t *testing.T) {
tests := []struct {
name string
errCount int
wantHeader string
wantWarn string
}{
{"clean", 0, "Dry run complete — no changes were made", ""},
{"with errors", 2, "Dry run finished with errors — no changes were made", "2 step(s) had errors — check the output above for details."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("HOME", t.TempDir())
plan := InstallPlan{
DryRun: true,
Formulae: []string{"git", "curl"},
Casks: []string{"firefox"},
Npm: []string{"typescript"},
}
rr := &recordReporter{}
showCompletionFromPlan(plan, rr, tt.errCount)

assert.Equal(t, []string{tt.wantHeader}, rr.headers)
out := strings.Join(rr.lines, "\n")
assert.Contains(t, out, "Would install:\n - 2 CLI packages\n - 1 GUI applications\n - 1 npm global packages")
assert.Contains(t, out, "Run the same command without --dry-run to apply these changes")
if tt.wantWarn != "" {
assert.Contains(t, out, tt.wantWarn)
}
for _, claim := range []string{
"What was installed:",
"successfully configured",
"Git configured",
"Restart your terminal",
"brew doctor",
} {
assert.NotContains(t, out, claim)
}
})
}
}

// ---------------------------------------------------------------------------
// Apply (happy path with NopReporter through DryRun)
// ---------------------------------------------------------------------------
Expand Down
Loading