From ba1f15ed0e6ee2cacc390b8cd96bcadde4442b98 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 29 Jan 2020 14:15:21 +0000 Subject: [PATCH] Timeouts, cancellation (tests). --- color_test.go | 4 ++-- color_windows.go | 4 ++-- file_test.go | 4 ++-- file_windows.go | 12 ++++++------ go.mod | 7 ++++++- go.sum | 39 ++++++++++++++++++++++++++++++++++++++- msg_test.go | 4 ++-- msg_windows.go | 8 ++++---- notify_test.go | 2 +- notify_windows.go | 4 ++-- util_windows.go | 6 +++--- zenity.go | 1 + zenity_test.go | 11 +++++++++++ 13 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 zenity_test.go diff --git a/color_test.go b/color_test.go index a09cc06..1a19091 100644 --- a/color_test.go +++ b/color_test.go @@ -29,7 +29,7 @@ func TestSelectColorTimeout(t *testing.T) { _, err := zenity.SelectColor(zenity.Context(ctx)) if !os.IsTimeout(err) { - t.Error("did not timeout", err) + t.Error("did not timeout:", err) } cancel() @@ -41,6 +41,6 @@ func TestSelectColorCancel(t *testing.T) { _, err := zenity.SelectColor(zenity.Context(ctx)) if !errors.Is(err, context.Canceled) { - t.Error("not canceled", err) + t.Error("was not canceled:", err) } } diff --git a/color_windows.go b/color_windows.go index df5aeec..64b840a 100644 --- a/color_windows.go +++ b/color_windows.go @@ -54,8 +54,8 @@ func selectColor(options []Option) (color.Color, error) { defer unhookWindowsHookEx.Call(hook) } - n, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args))) - if n == 0 { + s, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args))) + if s == 0 { return nil, commDlgError() } diff --git a/file_test.go b/file_test.go index 6756ffc..5ee972f 100644 --- a/file_test.go +++ b/file_test.go @@ -83,7 +83,7 @@ func TestFileTimeout(t *testing.T) { _, err := f(zenity.Context(ctx)) if !os.IsTimeout(err) { - t.Error("did not timeout", err) + t.Error("did not timeout:", err) } cancel() @@ -97,7 +97,7 @@ func TestFileCancel(t *testing.T) { for _, f := range fileFuncs { _, err := f(zenity.Context(ctx)) if !errors.Is(err, context.Canceled) { - t.Error("not canceled", err) + t.Error("was not canceled:", err) } } } diff --git a/file_windows.go b/file_windows.go index 0660ee8..2455e3e 100644 --- a/file_windows.go +++ b/file_windows.go @@ -46,8 +46,8 @@ func selectFile(options []Option) (string, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - n, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) - if n == 0 { + s, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) + if s == 0 { return "", commDlgError() } return syscall.UTF16ToString(res[:]), nil @@ -82,8 +82,8 @@ func selectFileMutiple(options []Option) ([]string, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - n, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) - if n == 0 { + s, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) + if s == 0 { return nil, commDlgError() } @@ -149,8 +149,8 @@ func selectFileSave(options []Option) (string, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - n, _, _ := getSaveFileName.Call(uintptr(unsafe.Pointer(&args))) - if n == 0 { + s, _, _ := getSaveFileName.Call(uintptr(unsafe.Pointer(&args))) + if s == 0 { return "", commDlgError() } return syscall.UTF16ToString(res[:]), nil diff --git a/go.mod b/go.mod index 0d81813..497e5c6 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module github.com/ncruces/zenity go 1.13 -require golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 +require ( + go.uber.org/goleak v1.0.0 // test + golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 + golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect + golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf // indirect +) diff --git a/go.sum b/go.sum index 90284fd..347f39e 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,41 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo= +go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 h1:2fktqPPvDiVEEVT/vSTeoUPXfmRxRaGy6GU8jypvEn0= golang.org/x/image v0.0.0-20191214001246-9130b4cfad52/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE= +golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf h1:mFgR10kFfr83r2+nXf0GZC2FKrFhMSs9NdJ0YdEaGiY= +golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/msg_test.go b/msg_test.go index d0c980f..984669f 100644 --- a/msg_test.go +++ b/msg_test.go @@ -51,7 +51,7 @@ func TestMessageTimeout(t *testing.T) { _, err := f("text", zenity.Context(ctx)) if !os.IsTimeout(err) { - t.Error("did not timeout", err) + t.Error("did not timeout:", err) } cancel() @@ -65,7 +65,7 @@ func TestMessageCancel(t *testing.T) { for _, f := range msgFuncs { _, err := f("text", zenity.Context(ctx)) if !errors.Is(err, context.Canceled) { - t.Error("not canceled", err) + t.Error("was not canceled:", err) } } } diff --git a/msg_windows.go b/msg_windows.go index 1f148e0..e3144d5 100644 --- a/msg_windows.go +++ b/msg_windows.go @@ -52,17 +52,17 @@ func message(kind messageKind, text string, options []Option) (bool, error) { defer unhookWindowsHookEx.Call(hook) } - n, _, err := messageBox.Call(0, + s, _, err := messageBox.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title))), flags) - if n == 0 { + if s == 0 { return false, err } - if n == 7 || n == 2 && kind != questionKind { // IDNO + if s == 7 || s == 2 && kind != questionKind { // IDNO return false, ErrExtraButton } - if n == 1 || n == 6 { // IDOK, IDYES + if s == 1 || s == 6 { // IDOK, IDYES return true, nil } return false, nil diff --git a/notify_test.go b/notify_test.go index 41959b3..c002d12 100644 --- a/notify_test.go +++ b/notify_test.go @@ -21,6 +21,6 @@ func TestNotifyCancel(t *testing.T) { err := zenity.Notify("text", zenity.Context(ctx)) if !errors.Is(err, context.Canceled) { - t.Error("not canceled", err) + t.Error("was not canceled:", err) } } diff --git a/notify_windows.go b/notify_windows.go index 4cd2a81..8ab0b25 100644 --- a/notify_windows.go +++ b/notify_windows.go @@ -37,8 +37,8 @@ func notify(text string, options []Option) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - n, _, err := shellNotifyIcon.Call(0 /* NIM_ADD */, uintptr(unsafe.Pointer(&args))) - if n == 0 { + s, _, err := shellNotifyIcon.Call(0 /* NIM_ADD */, uintptr(unsafe.Pointer(&args))) + if s == 0 { if errno, ok := err.(syscall.Errno); ok && errno == 0 { _, err = Info(text, Title(opts.title), Icon(opts.icon)) } diff --git a/util_windows.go b/util_windows.go index 0a211b8..2e2873a 100644 --- a/util_windows.go +++ b/util_windows.go @@ -33,11 +33,11 @@ var ( ) func commDlgError() error { - n, _, _ := commDlgExtendedError.Call() - if n == 0 { + s, _, _ := commDlgExtendedError.Call() + if s == 0 { return nil } else { - return fmt.Errorf("Common Dialog error: %x", n) + return fmt.Errorf("Common Dialog error: %x", s) } } diff --git a/zenity.go b/zenity.go index 684a023..4f04e0d 100644 --- a/zenity.go +++ b/zenity.go @@ -86,6 +86,7 @@ func Icon(icon DialogIcon) Option { return funcOption(func(o *options) { o.icon = icon }) } +// Context returns an Option to set a Context that can dismiss the dialog. func Context(ctx context.Context) Option { return funcOption(func(o *options) { o.ctx = ctx }) } diff --git a/zenity_test.go b/zenity_test.go new file mode 100644 index 0000000..f5df56c --- /dev/null +++ b/zenity_test.go @@ -0,0 +1,11 @@ +package zenity_test + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +}