Timeouts, cancellation (tests).

This commit is contained in:
Nuno Cruces 2020-01-29 11:09:06 +00:00
parent 1fdf2f7d73
commit 7c4199bdd1
6 changed files with 144 additions and 5 deletions

View file

@ -1,7 +1,12 @@
package zenity_test package zenity_test
import ( import (
"context"
"errors"
"image/color" "image/color"
"os"
"testing"
"time"
"github.com/ncruces/zenity" "github.com/ncruces/zenity"
) )
@ -18,3 +23,24 @@ func ExampleSelectColor_palette() {
zenity.Color(color.NRGBA{R: 0x66, G: 0x33, B: 0x99, A: 0xff})) zenity.Color(color.NRGBA{R: 0x66, G: 0x33, B: 0x99, A: 0xff}))
// Output: // 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)
}
}

View file

@ -1,6 +1,14 @@
package zenity_test package zenity_test
import "github.com/ncruces/zenity" import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/ncruces/zenity"
)
const defaultPath = "" const defaultPath = ""
const defaultName = "" const defaultName = ""
@ -52,3 +60,44 @@ func ExampleSelectFileMutiple_directory() {
zenity.Directory()) zenity.Directory())
// Output: // 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)
}
}
}

View file

@ -33,7 +33,11 @@ func Run(ctx context.Context, script string, data interface{}) ([]byte, error) {
if ctx != nil { if ctx != nil {
cmd := exec.CommandContext(ctx, "osascript", "-l", lang) cmd := exec.CommandContext(ctx, "osascript", "-l", lang)
cmd.Stdin = strings.NewReader(script) 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 := exec.Command("osascript", "-l", lang)
cmd.Stdin = strings.NewReader(script) cmd.Stdin = strings.NewReader(script)

View file

@ -31,7 +31,11 @@ func Run(ctx context.Context, args []string) ([]byte, error) {
} }
if ctx != nil { 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() return exec.Command(tool, args...).Output()
} }

View file

@ -1,6 +1,14 @@
package zenity_test package zenity_test
import "github.com/ncruces/zenity" import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/ncruces/zenity"
)
func ExampleError() { func ExampleError() {
zenity.Error("An error has occurred.", zenity.Error("An error has occurred.",
@ -29,3 +37,35 @@ func ExampleQuestion() {
zenity.Icon(zenity.QuestionIcon)) zenity.Icon(zenity.QuestionIcon))
// Output: // 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)
}
}
}

View file

@ -1,6 +1,12 @@
package zenity_test package zenity_test
import "github.com/ncruces/zenity" import (
"context"
"errors"
"testing"
"github.com/ncruces/zenity"
)
func ExampleNotify() { func ExampleNotify() {
zenity.Notify("There are system updates necessary!", zenity.Notify("There are system updates necessary!",
@ -8,3 +14,13 @@ func ExampleNotify() {
zenity.Icon(zenity.InfoIcon)) zenity.Icon(zenity.InfoIcon))
// Output: // 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)
}
}