From 7c4199bdd19cd6e5a90d2d1d655ca95cc886af00 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 29 Jan 2020 11:09:06 +0000 Subject: [PATCH] Timeouts, cancellation (tests). --- color_test.go | 26 +++++++++++++++++ file_test.go | 51 +++++++++++++++++++++++++++++++++- internal/zenutil/run_darwin.go | 6 +++- internal/zenutil/run_unix.go | 6 +++- msg_test.go | 42 +++++++++++++++++++++++++++- notify_test.go | 18 +++++++++++- 6 files changed, 144 insertions(+), 5 deletions(-) diff --git a/color_test.go b/color_test.go index 846cd9b..a09cc06 100644 --- a/color_test.go +++ b/color_test.go @@ -1,7 +1,12 @@ package zenity_test import ( + "context" + "errors" "image/color" + "os" + "testing" + "time" "github.com/ncruces/zenity" ) @@ -18,3 +23,24 @@ func ExampleSelectColor_palette() { zenity.Color(color.NRGBA{R: 0x66, G: 0x33, B: 0x99, A: 0xff})) // Output: } + +func TestSelectColorTimeout(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + + _, err := zenity.SelectColor(zenity.Context(ctx)) + if !os.IsTimeout(err) { + t.Error("did not timeout", err) + } + + cancel() +} + +func TestSelectColorCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + _, err := zenity.SelectColor(zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("not canceled", err) + } +} diff --git a/file_test.go b/file_test.go index 502ac15..6756ffc 100644 --- a/file_test.go +++ b/file_test.go @@ -1,6 +1,14 @@ package zenity_test -import "github.com/ncruces/zenity" +import ( + "context" + "errors" + "os" + "testing" + "time" + + "github.com/ncruces/zenity" +) const defaultPath = "" const defaultName = "" @@ -52,3 +60,44 @@ func ExampleSelectFileMutiple_directory() { zenity.Directory()) // Output: } + +var fileFuncs = []func(...zenity.Option) (string, error){ + zenity.SelectFile, + zenity.SelectFileSave, + func(o ...zenity.Option) (string, error) { + return zenity.SelectFile(append(o, zenity.Directory())...) + }, + func(o ...zenity.Option) (string, error) { + _, err := zenity.SelectFileMutiple(append(o, zenity.Directory())...) + return "", err + }, + func(o ...zenity.Option) (string, error) { + _, err := zenity.SelectFileMutiple(o...) + return "", err + }, +} + +func TestFileTimeout(t *testing.T) { + for _, f := range fileFuncs { + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + + _, err := f(zenity.Context(ctx)) + if !os.IsTimeout(err) { + t.Error("did not timeout", err) + } + + cancel() + } +} + +func TestFileCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + for _, f := range fileFuncs { + _, err := f(zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("not canceled", err) + } + } +} diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index 2dbd97a..7d02df4 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -33,7 +33,11 @@ func Run(ctx context.Context, script string, data interface{}) ([]byte, error) { if ctx != nil { cmd := exec.CommandContext(ctx, "osascript", "-l", lang) cmd.Stdin = strings.NewReader(script) - return cmd.Output() + out, err := cmd.Output() + if ctx.Err() != nil { + err = ctx.Err() + } + return out, err } cmd := exec.Command("osascript", "-l", lang) cmd.Stdin = strings.NewReader(script) diff --git a/internal/zenutil/run_unix.go b/internal/zenutil/run_unix.go index 5420868..43c7f92 100644 --- a/internal/zenutil/run_unix.go +++ b/internal/zenutil/run_unix.go @@ -31,7 +31,11 @@ func Run(ctx context.Context, args []string) ([]byte, error) { } if ctx != nil { - return exec.CommandContext(ctx, tool, args...).Output() + out, err := exec.CommandContext(ctx, tool, args...).Output() + if ctx.Err() != nil { + err = ctx.Err() + } + return out, err } return exec.Command(tool, args...).Output() } diff --git a/msg_test.go b/msg_test.go index d7bc350..d0c980f 100644 --- a/msg_test.go +++ b/msg_test.go @@ -1,6 +1,14 @@ package zenity_test -import "github.com/ncruces/zenity" +import ( + "context" + "errors" + "os" + "testing" + "time" + + "github.com/ncruces/zenity" +) func ExampleError() { zenity.Error("An error has occurred.", @@ -29,3 +37,35 @@ func ExampleQuestion() { zenity.Icon(zenity.QuestionIcon)) // Output: } + +var msgFuncs = []func(string, ...zenity.Option) (bool, error){ + zenity.Error, + zenity.Info, + zenity.Warning, + zenity.Question, +} + +func TestMessageTimeout(t *testing.T) { + for _, f := range msgFuncs { + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + + _, err := f("text", zenity.Context(ctx)) + if !os.IsTimeout(err) { + t.Error("did not timeout", err) + } + + cancel() + } +} + +func TestMessageCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + for _, f := range msgFuncs { + _, err := f("text", zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("not canceled", err) + } + } +} diff --git a/notify_test.go b/notify_test.go index 1c88f36..41959b3 100644 --- a/notify_test.go +++ b/notify_test.go @@ -1,6 +1,12 @@ package zenity_test -import "github.com/ncruces/zenity" +import ( + "context" + "errors" + "testing" + + "github.com/ncruces/zenity" +) func ExampleNotify() { zenity.Notify("There are system updates necessary!", @@ -8,3 +14,13 @@ func ExampleNotify() { zenity.Icon(zenity.InfoIcon)) // Output: } + +func TestNotifyCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + err := zenity.Notify("text", zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("not canceled", err) + } +}