[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) { func TestMatch(t *testing.T) {
t.Parallel() t.Parallel()
initTests()
tests := []struct { tests := []struct {
m Matcher m Matcher

View File

@ -1,7 +1,6 @@
package complete package complete
import ( import (
"os"
"sort" "sort"
"strings" "strings"
"testing" "testing"
@ -9,12 +8,7 @@ import (
func TestPredicate(t *testing.T) { func TestPredicate(t *testing.T) {
t.Parallel() t.Parallel()
initTests()
// Change to tests directory for testing completion of files and directories
err := os.Chdir("./tests")
if err != nil {
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string

View File

@ -8,19 +8,7 @@ import (
func TestCompleter_Complete(t *testing.T) { func TestCompleter_Complete(t *testing.T) {
t.Parallel() t.Parallel()
initTests()
// 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)
}
c := Command{ c := Command{
Sub: map[string]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)
}
})
}