[test] chdir and set logger only once

This commit is contained in:
Eyal Posener 2017-05-06 23:06:58 +03:00
parent 8d6ebaa183
commit f740f61fbb
4 changed files with 29 additions and 20 deletions

View File

@ -4,6 +4,7 @@ import "testing"
func TestMatch(t *testing.T) {
t.Parallel()
initTests()
tests := []struct {
m Matcher

View File

@ -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

View File

@ -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{

26
tests.go Normal file
View File

@ -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)
}
})
}