zenity/file_test.go

245 lines
6.8 KiB
Go
Raw Normal View History

2020-01-10 07:00:38 -05:00
package zenity_test
2019-12-11 05:29:31 -05:00
2020-01-29 06:09:06 -05:00
import (
"context"
"errors"
2021-07-12 20:43:52 -04:00
"fmt"
2020-01-29 06:09:06 -05:00
"os"
2021-07-28 09:49:37 -04:00
"path/filepath"
2020-01-29 06:09:06 -05:00
"testing"
"time"
"github.com/ncruces/zenity"
2021-04-30 14:19:14 -04:00
"go.uber.org/goleak"
2020-01-29 06:09:06 -05:00
)
2019-12-11 05:29:31 -05:00
2021-04-13 08:28:26 -04:00
const defaultPath = ``
const defaultName = ``
2020-01-10 07:00:38 -05:00
func ExampleSelectFile() {
zenity.SelectFile(
zenity.Filename(defaultPath),
zenity.FileFilters{
2022-12-14 22:05:44 -05:00
{"Go files", []string{"*.go"}, true},
{"Web files", []string{"*.html", "*.js", "*.css"}, true},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true},
2020-01-24 07:52:45 -05:00
})
2019-12-11 05:29:31 -05:00
}
2022-05-19 09:09:21 -04:00
func ExampleSelectFileMultiple() {
zenity.SelectFileMultiple(
2020-01-10 07:00:38 -05:00
zenity.Filename(defaultPath),
zenity.FileFilters{
2022-12-14 22:05:44 -05:00
{"Go files", []string{"*.go"}, true},
{"Web files", []string{"*.html", "*.js", "*.css"}, true},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true},
2020-01-24 07:52:45 -05:00
})
2019-12-11 05:29:31 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleSelectFileSave() {
zenity.SelectFileSave(
zenity.ConfirmOverwrite(),
2020-01-10 07:00:38 -05:00
zenity.Filename(defaultName),
zenity.FileFilters{
2022-12-14 22:05:44 -05:00
{"Go files", []string{"*.go"}, true},
{"Web files", []string{"*.html", "*.js", "*.css"}, true},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true},
2020-01-24 07:52:45 -05:00
})
2019-12-11 05:29:31 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleSelectFile_directory() {
zenity.SelectFile(
zenity.Filename(defaultPath),
zenity.Directory())
2020-01-09 20:46:53 -05:00
}
2022-05-19 09:09:21 -04:00
func ExampleSelectFileMultiple_directory() {
zenity.SelectFileMultiple(
2020-01-10 07:00:38 -05:00
zenity.Filename(defaultPath),
zenity.Directory())
2019-12-11 05:29:31 -05:00
}
2020-01-29 06:09:06 -05:00
2021-07-12 20:43:52 -04:00
var fileFuncs = []struct {
name string
fn func(...zenity.Option) (string, error)
}{
{"Open", zenity.SelectFile},
{"Save", zenity.SelectFileSave},
{"Directory", func(o ...zenity.Option) (string, error) {
2020-01-29 06:09:06 -05:00
return zenity.SelectFile(append(o, zenity.Directory())...)
2021-07-12 20:43:52 -04:00
}},
2022-05-19 09:09:21 -04:00
{"Multiple", func(o ...zenity.Option) (string, error) {
_, err := zenity.SelectFileMultiple(append(o, zenity.Directory())...)
2020-01-29 06:09:06 -05:00
return "", err
2021-07-12 20:43:52 -04:00
}},
2022-05-19 09:09:21 -04:00
{"MultipleDirectory", func(o ...zenity.Option) (string, error) {
_, err := zenity.SelectFileMultiple(o...)
2020-01-29 06:09:06 -05:00
return "", err
2021-07-12 20:43:52 -04:00
}},
}
func TestSelectFile_timeout(t *testing.T) {
2022-05-18 09:48:09 -04:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2021-07-12 20:43:52 -04:00
for _, tt := range fileFuncs {
t.Run(tt.name, func(t *testing.T) {
defer goleak.VerifyNone(t)
2021-07-29 11:20:50 -04:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
2021-07-12 20:43:52 -04:00
defer cancel()
_, err := tt.fn(zenity.Context(ctx))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !os.IsTimeout(err) {
t.Error("did not timeout:", err)
}
})
}
2020-01-29 06:09:06 -05:00
}
2021-07-12 20:43:52 -04:00
func TestSelectFile_cancel(t *testing.T) {
for _, tt := range fileFuncs {
t.Run(tt.name, func(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
cancel()
2020-01-29 06:09:06 -05:00
2021-07-12 20:43:52 -04:00
_, err := tt.fn(zenity.Context(ctx))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !errors.Is(err, context.Canceled) {
t.Error("was not canceled:", err)
}
})
}
}
func TestSelectFile_script(t *testing.T) {
2022-05-18 09:48:09 -04:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2021-07-12 20:43:52 -04:00
t.Run("Cancel", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file selection dialog, cancel."))
str, err := zenity.SelectFile()
2021-07-06 20:01:22 -04:00
if skip, err := skip(err); skip {
2021-06-18 10:04:09 -04:00
t.Skip("skipping:", err)
}
2021-07-12 20:43:52 -04:00
if str != "" || err != zenity.ErrCanceled {
t.Errorf("SelectFile() = %q, %v; want %q, %v", str, err, "", zenity.ErrCanceled)
2020-01-29 06:09:06 -05:00
}
2021-07-12 20:43:52 -04:00
})
t.Run("File", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file selection dialog, pick any file."))
str, err := zenity.SelectFile()
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if str == "" || err != nil {
2022-06-22 10:14:52 -04:00
t.Fatalf("SelectFile() = %q, %v; want [path], nil", str, err)
2021-07-12 20:43:52 -04:00
}
if _, serr := os.Stat(str); serr != nil {
t.Errorf("SelectFile() = %q, %v; %v", str, err, serr)
}
})
t.Run("Directory", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file selection dialog, pick any directory."))
str, err := zenity.SelectFile(zenity.Directory())
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if str == "" || err != nil {
2022-06-22 10:14:52 -04:00
t.Fatalf("SelectFile() = %q, %v; want [path], nil", str, err)
2021-07-12 20:43:52 -04:00
}
if s, serr := os.Stat(str); serr != nil {
t.Errorf("SelectFile() = %q, %v; %v", str, err, serr)
} else if !s.IsDir() {
t.Errorf("SelectFile() = %q, %v; not a directory", str, err)
}
})
2020-01-29 06:09:06 -05:00
}
2022-05-19 09:09:21 -04:00
func TestSelectFileMultiple_script(t *testing.T) {
2022-05-18 09:48:09 -04:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2021-07-12 20:43:52 -04:00
t.Run("Cancel", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file selection dialog, cancel."))
2022-05-19 09:09:21 -04:00
lst, err := zenity.SelectFileMultiple()
2021-07-06 20:01:22 -04:00
if skip, err := skip(err); skip {
2021-06-18 10:04:09 -04:00
t.Skip("skipping:", err)
}
2021-07-12 20:43:52 -04:00
if lst != nil || err != zenity.ErrCanceled {
2022-05-19 09:09:21 -04:00
t.Errorf("SelectFileMultiple() = %v, %v; want nil, %v", lst, err, zenity.ErrCanceled)
2020-01-29 06:09:06 -05:00
}
2021-07-12 20:43:52 -04:00
})
t.Run("Files", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file selection dialog, pick two files."))
2022-05-19 09:09:21 -04:00
lst, err := zenity.SelectFileMultiple()
2021-07-12 20:43:52 -04:00
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if lst == nil || err != nil {
2022-06-22 10:14:52 -04:00
t.Fatalf("SelectFileMultiple() = %v, %v; want [path, path], nil", lst, err)
2021-07-12 20:43:52 -04:00
}
for _, str := range lst {
if _, serr := os.Stat(str); serr != nil {
2022-05-19 09:09:21 -04:00
t.Errorf("SelectFileMultiple() = %q, %v; %v", lst, err, serr)
2021-07-12 20:43:52 -04:00
}
}
})
t.Run("Directories", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file selection dialog, pick two directories."))
2022-05-19 09:09:21 -04:00
lst, err := zenity.SelectFileMultiple(zenity.Directory())
2021-07-12 20:43:52 -04:00
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
2022-05-03 09:20:22 -04:00
if errors.Is(err, zenity.ErrUnsupported) {
2021-07-12 20:43:52 -04:00
t.Skip("was not unsupported:", err)
}
if lst == nil || err != nil {
2022-06-22 10:14:52 -04:00
t.Fatalf("SelectFileMultiple() = %v, %v; want [path, path], nil", lst, err)
2021-07-12 20:43:52 -04:00
}
for _, str := range lst {
if s, serr := os.Stat(str); serr != nil {
2022-05-19 09:09:21 -04:00
t.Errorf("SelectFileMultiple() = %q, %v; %v", str, err, serr)
2021-07-12 20:43:52 -04:00
} else if !s.IsDir() {
2022-05-19 09:09:21 -04:00
t.Errorf("SelectFileMultiple() = %q, %v; not a directory", str, err)
2021-07-12 20:43:52 -04:00
}
}
})
2020-01-29 06:09:06 -05:00
}
2021-07-28 09:49:37 -04:00
func TestSelectFileSave_script(t *testing.T) {
2022-05-18 09:48:09 -04:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2021-07-28 09:49:37 -04:00
t.Run("Cancel", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file save dialog, cancel."))
str, err := zenity.SelectFileSave()
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if str != "" || err != zenity.ErrCanceled {
t.Errorf("SelectFileSave() = %q, %v; want %q, %v", str, err, "", zenity.ErrCanceled)
}
})
t.Run("Name", func(t *testing.T) {
zenity.Info(fmt.Sprintf("In the file save dialog, press OK."))
str, err := zenity.SelectFileSave(
zenity.ConfirmOverwrite(),
zenity.Filename("Χρτο.go"),
2022-12-14 22:05:44 -05:00
zenity.FileFilter{"Go files", []string{"*.go"}, true},
2021-07-28 09:49:37 -04:00
)
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if _, name := filepath.Split(str); name != "Χρτο.go" || err != nil {
t.Errorf("SelectFileSave() = %q, %v; want %q, nil", str, err, "Χρτο.go")
}
})
}