Skip to content

Commit e127f63

Browse files
committed
complete: add SetExitFunc() to override os.Exit in tests
This eliminates the need to use the unsupported bou.ke/monkey package, which fails to build on darwin/arm64.
1 parent 3f91521 commit e127f63

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

complete.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ var (
5353
exit = os.Exit
5454
)
5555

56+
// SetExitFunc sets the function used to exit the program (by default os.Exit)
57+
// and returns the previous value.
58+
func SetExitFunc(fn func(code int)) (previous func(int)) {
59+
previous = exit
60+
exit = fn
61+
return previous
62+
}
63+
5664
// Complete the command line arguments for the given command in the case that the program
5765
// was invoked with COMP_LINE and COMP_POINT environment variables. In that case it will also
5866
// `os.Exit()`. The program name should be provided for installation purposes.

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module github.com/posener/complete/v2
22

33
require (
4-
bou.ke/monkey v1.0.2
54
github.com/hashicorp/go-multierror v1.0.0
65
github.com/posener/autogen v0.0.2
76
github.com/posener/script v1.1.5

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
2-
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
31
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
42
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
53
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
64
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
75
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
86
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
9-
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
107
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
118
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
129
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

gocomplete/tests_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212
"testing"
1313

14-
"bou.ke/monkey"
1514
"github.com/posener/complete/v2"
1615
)
1716

@@ -53,8 +52,8 @@ func TestPredictions(t *testing.T) {
5352
func BenchmarkFake(b *testing.B) {}
5453

5554
func Example() {
56-
p := monkey.Patch(os.Exit, func(int) {})
57-
defer p.Unpatch()
55+
p := complete.SetExitFunc(func(int) {})
56+
defer complete.SetExitFunc(p)
5857
os.Setenv("COMP_LINE", "go ru")
5958
os.Setenv("COMP_POINT", "5")
6059
main()
@@ -76,7 +75,8 @@ func equal(s1, s2 []string) bool {
7675
}
7776

7877
func TestErrorSupression(t *testing.T) {
79-
defer monkey.Patch(os.Exit, func(int) {}).Unpatch()
78+
p := complete.SetExitFunc(func(int) {})
79+
defer complete.SetExitFunc(p)
8080

8181
// Completion API environment variable names.
8282
const envLine, envPoint = "COMP_LINE", "COMP_POINT"

0 commit comments

Comments
 (0)