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{
|
|
|
|
{"Go files", []string{"*.go"}},
|
|
|
|
{"Web files", []string{"*.html", "*.js", "*.css"}},
|
|
|
|
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
|
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 ExampleSelectFileMutiple() {
|
|
|
|
zenity.SelectFileMutiple(
|
|
|
|
zenity.Filename(defaultPath),
|
|
|
|
zenity.FileFilters{
|
|
|
|
{"Go files", []string{"*.go"}},
|
|
|
|
{"Web files", []string{"*.html", "*.js", "*.css"}},
|
|
|
|
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
|
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(
|
2020-01-11 22:18:22 -05:00
|
|
|
zenity.ConfirmOverwrite(),
|
2020-01-10 07:00:38 -05:00
|
|
|
zenity.Filename(defaultName),
|
|
|
|
zenity.FileFilters{
|
|
|
|
{"Go files", []string{"*.go"}},
|
|
|
|
{"Web files", []string{"*.html", "*.js", "*.css"}},
|
|
|
|
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
|
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),
|
2020-01-11 22:18:22 -05:00
|
|
|
zenity.Directory())
|
2020-01-09 20:46:53 -05:00
|
|
|
}
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
func ExampleSelectFileMutiple_directory() {
|
|
|
|
zenity.SelectFileMutiple(
|
|
|
|
zenity.Filename(defaultPath),
|
2020-01-11 22:18:22 -05:00
|
|
|
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
|
|
|
}},
|
|
|
|
{"Mutiple", func(o ...zenity.Option) (string, error) {
|
2020-01-29 06:09:06 -05:00
|
|
|
_, err := zenity.SelectFileMutiple(append(o, zenity.Directory())...)
|
|
|
|
return "", err
|
2021-07-12 20:43:52 -04:00
|
|
|
}},
|
|
|
|
{"MutipleDirectory", func(o ...zenity.Option) (string, error) {
|
2020-01-29 06:09:06 -05:00
|
|
|
_, err := zenity.SelectFileMutiple(o...)
|
|
|
|
return "", err
|
2021-07-12 20:43:52 -04:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectFile_timeout(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
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 {
|
|
|
|
t.Errorf("SelectFile() = %q, %v; want [path], nil", str, err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
t.Errorf("SelectFile() = %q, %v; want [path], nil", str, err)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-12 20:43:52 -04:00
|
|
|
func TestSelectFileMutiple_script(t *testing.T) {
|
|
|
|
t.Run("Cancel", func(t *testing.T) {
|
|
|
|
zenity.Info(fmt.Sprintf("In the file selection dialog, cancel."))
|
|
|
|
lst, err := zenity.SelectFileMutiple()
|
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 {
|
|
|
|
t.Errorf("SelectFileMutiple() = %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."))
|
|
|
|
lst, err := zenity.SelectFileMutiple()
|
|
|
|
if skip, err := skip(err); skip {
|
|
|
|
t.Skip("skipping:", err)
|
|
|
|
}
|
|
|
|
if lst == nil || err != nil {
|
|
|
|
t.Errorf("SelectFileMutiple() = %v, %v; want [path, path], nil", lst, err)
|
|
|
|
}
|
|
|
|
for _, str := range lst {
|
|
|
|
if _, serr := os.Stat(str); serr != nil {
|
|
|
|
t.Errorf("SelectFileMutiple() = %q, %v; %v", lst, err, serr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Directories", func(t *testing.T) {
|
|
|
|
zenity.Info(fmt.Sprintf("In the file selection dialog, pick two directories."))
|
|
|
|
lst, err := zenity.SelectFileMutiple(zenity.Directory())
|
|
|
|
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 {
|
|
|
|
t.Errorf("SelectFileMutiple() = %v, %v; want [path, path], nil", lst, err)
|
|
|
|
}
|
|
|
|
for _, str := range lst {
|
|
|
|
if s, serr := os.Stat(str); serr != nil {
|
|
|
|
t.Errorf("SelectFileMutiple() = %q, %v; %v", str, err, serr)
|
|
|
|
} else if !s.IsDir() {
|
|
|
|
t.Errorf("SelectFileMutiple() = %q, %v; not a directory", str, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-01-29 06:09:06 -05:00
|
|
|
}
|
2021-07-28 09:49:37 -04:00
|
|
|
|
|
|
|
func TestSelectFileSave_script(t *testing.T) {
|
|
|
|
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"),
|
|
|
|
zenity.FileFilter{"Go files", []string{"*.go"}},
|
|
|
|
)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|