Skip to content

Commit

Permalink
Move more integration tests to RobotView
Browse files Browse the repository at this point in the history
Port more of the integration tests that currently rely on with-term
to the new RobotView mechanism.

The couple remaining with-term tests are harder to port to RobotView
as they rely on more advanced functionality.

[skip changelog]: no user facing changes
  • Loading branch information
abhinav committed Nov 29, 2024
1 parent 1731f3e commit da9d57c
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 236 deletions.
6 changes: 6 additions & 0 deletions internal/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ func (i *Input) WithValue(value *string) *Input {
}

// UnmarshalValue reads a string value for the input field.
// Optionally, the input may be a boolean true to accept
// the input as is.
func (i *Input) UnmarshalValue(unmarshal func(any) error) error {
if ok := new(bool); unmarshal(ok) == nil && *ok {
return nil
}

return unmarshal(i.value)
}

Expand Down
10 changes: 10 additions & 0 deletions internal/ui/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,17 @@ func (s *Select[T]) Value() T {

// UnmarshalValue unmarshals the value of the select field
// using the provided unmarshal function.
//
// It accepts one of the following types:
//
// - bool: if the value is true, accept the field
// - string: pick the option with a matching label
func (s *Select[T]) UnmarshalValue(unmarshal func(any) error) error {
if ok := new(bool); unmarshal(ok) == nil && *ok {
// Leave the field as is.
return nil
}

var selectLabel string
if err := unmarshal(&selectLabel); err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions internal/ui/widget/branch_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,22 @@ func (b *BranchTreeSelect) WithValue(value *string) *BranchTreeSelect {

// UnmarshalValue unmarshals the value of the field
// using the given unmarshal function.
//
// It accepts one of the following types:
//
// - bool: if true, the current selection is accepted
// - string: the name of the selected branch (must be a known branch)
func (b *BranchTreeSelect) UnmarshalValue(unmarshal func(interface{}) error) error {
if ok := new(bool); unmarshal(ok) == nil && *ok {
if b.focused >= 0 && b.focused < len(b.selectable) {
*b.value = b.all[b.selectable[b.focused]].Branch
b.accepted = true
return nil
}

return fmt.Errorf("no branch selected")
}

var got string
if err := unmarshal(&got); err != nil {
return err
Expand Down
99 changes: 39 additions & 60 deletions testdata/script/branch_checkout_prompt.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# branch checkout with lots of branches
# and no argument prompts for branch name.

[!unix] skip # pending github.com/creack/pty/pull/155

as 'Test <[email protected]>'
at '2024-03-30T14:59:32Z'

Expand Down Expand Up @@ -31,28 +29,30 @@ git commit -m 'Add qux'
git checkout -b quux main
git merge -m 'Merge many' foo bar baz qux

env ROBOT_INPUT=$WORK/robot.golden ROBOT_OUTPUT=$WORK/robot.actual

# showing only tracked branches
gs trunk
with-term -rows 8 -cols 50 $WORK/input/tracked.txt -- gs branch checkout
cmp stdout $WORK/golden/prompt-tracked.txt
gs branch checkout
git branch --show-current
stdout 'bar'

# showing all branches
gs trunk
with-term -rows 8 -cols 50 $WORK/input/all.txt -- gs branch checkout -u
cmp stdout $WORK/golden/prompt-all.txt
gs branch checkout -u
git branch --show-current
stdout 'baz'

# showing all branches via config
gs branch untrack baz
gs trunk
with-term -rows 8 -cols 50 $WORK/input/all.txt -- gs branch checkout -u
cmp stdout $WORK/golden/prompt-all.txt
git config spice.branchCheckout.showUntracked true
gs branch checkout
git branch --show-current
stdout 'baz'

cmp $WORK/robot.actual $WORK/robot.golden

-- repo/foo.txt --
whatever

Expand All @@ -65,55 +65,34 @@ whatever
-- repo/qux.txt --
whatever

-- input/tracked.txt --
await Select a branch
snapshot init
feed ba
await
snapshot after
feed \r

-- input/all.txt --
await Select a branch
snapshot init
feed ba
await
snapshot after-ba
feed \x1b[B
await
snapshot select
feed z\r
await Do you want to track
snapshot track
feed \r

-- golden/prompt-tracked.txt --
### init ###
Select a branch to checkout:
┏━□ bar
┣━□ foo
main ◀
### after ###
Select a branch to checkout:
bar ◀
-- golden/prompt-all.txt --
### init ###
Select a branch to checkout:
baz
┏━□ bar
┣━□ foo
main ◀
quux
qux
### after-ba ###
Select a branch to checkout:
baz
bar ◀
### select ###
Select a branch to checkout:
baz ◀
bar
### track ###
Select a branch to checkout: baz
WRN baz: branch not tracked
Do you want to track this branch now?: [Y/n]
-- robot.golden --
===
> Select a branch to checkout:
> ┏━□ bar
> ┣━□ foo
> main ◀
"bar"
===
> Select a branch to checkout:
> baz
> ┏━□ bar
> ┣━□ foo
> main ◀
> quux
> qux
"baz"
===
> Do you want to track this branch now?: [Y/n]
true
===
> Select a branch to checkout:
> baz
> ┏━□ bar
> ┣━□ foo
> main ◀
> quux
> qux
"baz"
===
> Do you want to track this branch now?: [Y/n]
true
32 changes: 14 additions & 18 deletions testdata/script/branch_delete_prompt_untracked.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#
# https://github.com/abhinav/git-spice/issues/258

[!unix] skip # pending github.com/creack/pty/pull/155

as 'Test <[email protected]>'
at '2024-07-20T22:53:32Z'

Expand All @@ -20,22 +18,20 @@ git branch c
git branch d

git checkout c
with-term -final exit $WORK/input.txt -- gs branch delete
cmp stdout $WORK/golden.txt
env ROBOT_INPUT=$WORK/robot.golden ROBOT_OUTPUT=$WORK/robot.actual
gs branch delete
cmp $WORK/robot.actual $WORK/robot.golden
cmp stderr $WORK/stderr.golden

-- input.txt --
await Select a branch
snapshot prompt
feed \r
-- golden.txt --
### prompt ###
Select a branch to delete:
a
b
c ◀
d
main
### exit ###
Select a branch to delete: c
-- robot.golden --
===
> Select a branch to delete:
> a
> b
> c ◀
> d
> main
true
-- stderr.golden --
INF branch is not tracked: deleting anyway branch=c
INF c: deleted (was 2848185)
46 changes: 16 additions & 30 deletions testdata/script/branch_split_reassign_submitted.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# branch split supports reassigning submitted CRs
# to a different branch.

[!unix] skip # pending https://github.com/creack/pty/pull/155

as 'Test <[email protected]>'
at '2024-10-26T17:30:31Z'

Expand Down Expand Up @@ -34,8 +32,10 @@ cmp stderr $WORK/golden/ll-before.txt
gs bs --fill

# splitting the branch prompts for reassignment
with-term -final exit $WORK/input/prompt.txt -- gs branch split --at HEAD~2:feat1 --at HEAD^:feat2
cmp stdout $WORK/golden/prompt.txt
env ROBOT_INPUT=$WORK/robot.golden ROBOT_OUTPUT=$WORK/robot.actual
gs branch split --at HEAD~2:feat1 --at HEAD^:feat2
cmp $WORK/robot.actual $WORK/robot.golden
cmp stderr $WORK/stderr.golden

gs ll -a
cmp stderr $WORK/golden/ll-split.txt
Expand Down Expand Up @@ -71,32 +71,18 @@ new feat 1
┃ f9c1263 feat2 (now)
┃ 1578492 feat1 (now)
main
-- input/prompt.txt --
await Assign CR #1 to branch
snapshot init
feed feat1
await
snapshot search
feed \r
-- golden/prompt.txt --
### init ###
Assign CR #1 to branch:

feat1
feat2
▶ features

Branch being split has an open CR assigned to it.
Select which branch should take over the CR.
### search ###
Assign CR #1 to branch:

▶ feat1

Branch being split has an open CR assigned to it.
Select which branch should take over the CR.
### exit ###
Assign CR #1 to branch: feat1
-- robot.golden --
===
> Assign CR #1 to branch:
>
> feat1
> feat2
> ▶ features
>
> Branch being split has an open CR assigned to it.
> Select which branch should take over the CR.
"feat1"
-- stderr.golden --
INF features: Upstream branch 'features' transferred to 'feat1'
WRN features: If you push this branch with 'git push' instead of 'gs branch submit',
WRN features: remember to use a different upstream branch name with the command:
Expand Down
39 changes: 16 additions & 23 deletions testdata/script/branch_submit_long_body.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# branch submit should not truncate a long body.
# https://github.com/abhinav/git-spice/issues/90

[!unix] skip # pending github.com/creack/pty/pull/155

as 'Test <[email protected]>'
at '2024-05-23T20:06:32Z'

Expand Down Expand Up @@ -30,8 +28,9 @@ git log -n1 --format=%B
cmp stdout $WORK/input/commit-msg

# submit the branch
with-term $WORK/input/prompt.txt -- gs branch submit
cmp stdout $WORK/golden/prompt.txt
env ROBOT_INPUT=$WORK/robot.golden ROBOT_OUTPUT=$WORK/robot.actual
gs branch submit
cmp $WORK/robot.actual $WORK/robot.golden

shamhub dump changes
stdout 'End of message.'
Expand All @@ -58,22 +57,16 @@ no sea takimata sanctus est Lorem ipsum dolor sit amet.

End of message.

-- input/prompt.txt --
await Title:
snapshot initial
feed \t
await Body:
feed \t
await Draft:
snapshot last
feed \r

-- golden/prompt.txt --
### initial ###
Title: Add feature
Short summary of the change
### last ###
Title: Add feature
Body: Press [e] to open mockedit or [enter/tab] to skip
Draft: [y/N]
Mark the change as a draft?
-- robot.golden --
===
> Title: Add feature
> Short summary of the change
"Add feature"
===
> Body: Press [e] to open mockedit or [enter/tab] to skip
> Open your editor to write a detailed description of the change
{}
===
> Draft: [y/N]
> Mark the change as a draft?
false
Loading

0 comments on commit da9d57c

Please sign in to comment.