diff --git a/match_test.go b/match_test.go index 2607c89..8605194 100644 --- a/match_test.go +++ b/match_test.go @@ -4,6 +4,7 @@ import "testing" func TestMatch(t *testing.T) { t.Parallel() + initTests() tests := []struct { m Matcher diff --git a/predicate_test.go b/predicate_test.go index d376d76..264e8d1 100644 --- a/predicate_test.go +++ b/predicate_test.go @@ -1,7 +1,6 @@ package complete import ( - "os" "sort" "strings" "testing" @@ -9,12 +8,7 @@ import ( func TestPredicate(t *testing.T) { t.Parallel() - - // Change to tests directory for testing completion of files and directories - err := os.Chdir("./tests") - if err != nil { - t.Fatal(err) - } + initTests() tests := []struct { name string diff --git a/run_test.go b/run_test.go index 95b48b3..147a361 100644 --- a/run_test.go +++ b/run_test.go @@ -8,19 +8,7 @@ import ( func TestCompleter_Complete(t *testing.T) { t.Parallel() - - // Set debug environment variable so logs will be printed - if testing.Verbose() { - os.Setenv(envDebug, "1") - // refresh the logger with environment variable set - Log = getLogger() - } - - // Change to tests directory for testing completion of files and directories - err := os.Chdir("./tests") - if err != nil { - t.Fatal(err) - } + initTests() c := Command{ Sub: map[string]Command{ diff --git a/tests.go b/tests.go new file mode 100644 index 0000000..38fe5f1 --- /dev/null +++ b/tests.go @@ -0,0 +1,26 @@ +package complete + +import ( + "os" + "sync" + "testing" +) + +var once = sync.Once{} + +func initTests() { + once.Do(func() { + // Set debug environment variable so logs will be printed + if testing.Verbose() { + os.Setenv(envDebug, "1") + // refresh the logger with environment variable set + Log = getLogger() + } + + // Change to tests directory for testing completion of files and directories + err := os.Chdir("./tests") + if err != nil { + panic(err) + } + }) +}