Skip to content

Commit 26cd23e

Browse files
committed
Better unit tests for eula.go which also validates the condition for window size check
1 parent 33f0d07 commit 26cd23e

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed

cmd/ui/eula.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,12 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
106106
licenseWindow.LayoutManager = func(g *gocui.Gui) error {
107107
terminalWidth, terminalHeight := g.Size()
108108

109-
marginSize := 1
110-
promptWindowHeight := 3
111-
112-
// License window dimensions
113-
licenseWindowBeginX := marginSize
114-
licenseWindowBeginY := marginSize
115-
licenseWindowEndX := terminalWidth - marginSize
116-
licenseWindowEndY := terminalHeight - marginSize - promptWindowHeight
117-
if licenseWindowBeginX >= licenseWindowEndX || licenseWindowBeginY >= licenseWindowEndY {
118-
return fmt.Errorf("increase size of license window, currently (%d, %d, %d, %d), cannot obtain user response", licenseWindowBeginX, licenseWindowBeginY, licenseWindowEndX, licenseWindowEndY)
109+
// Compute rectangles and validate them
110+
lbx, lby, lex, ley, pbx, pby, pex, pey := computeLicenseAndPromptRects(terminalWidth, terminalHeight)
111+
if err := validateLicenseRect(lbx, lby, lex, ley); err != nil {
112+
return err
119113
}
120-
if v, err := g.SetView("license", licenseWindowBeginX, licenseWindowBeginY, licenseWindowEndX, licenseWindowEndY); err != nil {
114+
if v, err := g.SetView("license", lbx, lby, lex, ley); err != nil {
121115
if err != gocui.ErrUnknownView {
122116
log.Error("Cannot modify license window: ", err)
123117
return err
@@ -128,14 +122,10 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
128122
}
129123

130124
// Prompt window dimensions
131-
promptWindowBeginX := licenseWindowBeginX
132-
promptWindowBeginY := licenseWindowEndY + marginSize
133-
promptWindowEndX := licenseWindowEndX
134-
promptWindowEndY := terminalHeight - marginSize
135-
if promptWindowBeginX >= promptWindowEndX || promptWindowBeginY >= promptWindowEndY {
136-
return fmt.Errorf("increase size of prompt window, currently (%d, %d, %d, %d)", promptWindowBeginX, promptWindowBeginY, promptWindowEndX, promptWindowEndY)
125+
if err := validatePromptRect(pbx, pby, pex, pey); err != nil {
126+
return err
137127
}
138-
if v, err := g.SetView("prompt", promptWindowBeginX, promptWindowBeginY, promptWindowEndX, promptWindowEndY); err != nil {
128+
if v, err := g.SetView("prompt", pbx, pby, pex, pey); err != nil {
139129
if err != gocui.ErrUnknownView {
140130
log.Error("Cannot modify prompt window: ", err)
141131
return err
@@ -201,6 +191,42 @@ func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *License
201191
return licenseWindow
202192
}
203193

194+
// Helper to compute rectangles for license and prompt windows based on terminal size.
195+
// Uses the same constants as LayoutManager (marginSize=1, promptWindowHeight=3).
196+
func computeLicenseAndPromptRects(terminalWidth, terminalHeight int) (lbx, lby, lex, ley, pbx, pby, pex, pey int) {
197+
marginSize := 1
198+
promptWindowHeight := 3
199+
200+
// License window dimensions
201+
lbx = marginSize
202+
lby = marginSize
203+
lex = terminalWidth - marginSize
204+
ley = terminalHeight - marginSize - promptWindowHeight
205+
206+
// Prompt window dimensions
207+
pbx = lbx
208+
pby = ley + marginSize
209+
pex = lex
210+
pey = terminalHeight - marginSize
211+
return
212+
}
213+
214+
// validateLicenseRect mirrors the guard used before creating the license view.
215+
func validateLicenseRect(bx, by, ex, ey int) error {
216+
if bx >= ex || by >= ey {
217+
return fmt.Errorf("increase size of license window, currently (%d, %d, %d, %d), cannot obtain user response", bx, by, ex, ey)
218+
}
219+
return nil
220+
}
221+
222+
// validatePromptRect mirrors the guard used before creating the prompt view.
223+
func validatePromptRect(bx, by, ex, ey int) error {
224+
if bx >= ex || by >= ey {
225+
return fmt.Errorf("increase size of prompt window, currently (%d, %d, %d, %d)", bx, by, ex, ey)
226+
}
227+
return nil
228+
}
229+
204230
func (l *LicenseWindowType) Setup() error {
205231
log.Debug("Setting up UI to display license")
206232
g, err := gocui.NewGui(gocui.OutputNormal)

cmd/ui/eula_export.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
/* Copyright Contributors to the cpackget project. */
3+
4+
package ui
5+
6+
// NOTE: These are small exported wrappers intended for tests.
7+
// They expose internal layout computations without changing runtime behavior.
8+
9+
// ComputeLicenseAndPromptRectsForTest exposes computeLicenseAndPromptRects for tests.
10+
func ComputeLicenseAndPromptRectsForTest(terminalWidth, terminalHeight int) (int, int, int, int, int, int, int, int) {
11+
return computeLicenseAndPromptRects(terminalWidth, terminalHeight)
12+
}
13+
14+
// ValidateLicenseRectForTest exposes validateLicenseRect for tests.
15+
func ValidateLicenseRectForTest(bx, by, ex, ey int) error { return validateLicenseRect(bx, by, ex, ey) }
16+
17+
// ValidatePromptRectForTest exposes validatePromptRect for tests.
18+
func ValidatePromptRectForTest(bx, by, ex, ey int) error { return validatePromptRect(bx, by, ex, ey) }

cmd/ui/eula_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,29 @@ func TestNewLicenseWindow_AgreeDisagreeExtractHandlers(t *testing.T) {
148148
assert.Equal(gocui.ErrQuit, err)
149149
assert.True(ui.Extract, "Extract handler should set Extract to true")
150150
}
151+
152+
func TestLayoutRectValidation(t *testing.T) {
153+
assert := assert.New(t)
154+
155+
t.Run("license rect invalid when terminal too small", func(t *testing.T) {
156+
lbx, lby, lex, ley, _, _, _, _ := ui.ComputeLicenseAndPromptRectsForTest(1, 1)
157+
err := ui.ValidateLicenseRectForTest(lbx, lby, lex, ley)
158+
assert.Error(err)
159+
assert.Contains(err.Error(), "increase size of license window")
160+
})
161+
162+
t.Run("prompt rect invalid when terminal too small", func(t *testing.T) {
163+
// Choose width=2 so pbx==pex and triggers invalid prompt rect
164+
_, _, _, _, pbx, pby, pex, pey := ui.ComputeLicenseAndPromptRectsForTest(2, 6)
165+
err := ui.ValidatePromptRectForTest(pbx, pby, pex, pey)
166+
if assert.Error(err) {
167+
assert.Contains(err.Error(), "increase size of prompt window")
168+
}
169+
})
170+
171+
t.Run("rects valid on reasonable terminal size", func(t *testing.T) {
172+
lbx, lby, lex, ley, pbx, pby, pex, pey := ui.ComputeLicenseAndPromptRectsForTest(80, 24)
173+
assert.NoError(ui.ValidateLicenseRectForTest(lbx, lby, lex, ley))
174+
assert.NoError(ui.ValidatePromptRectForTest(pbx, pby, pex, pey))
175+
})
176+
}

0 commit comments

Comments
 (0)