forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_test.go
89 lines (81 loc) · 2.27 KB
/
cli_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cli_test
import (
"math/rand"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/briandowns/spinner"
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/commands"
"github.com/hasura/graphql-engine/cli/util/fake"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/viper"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func TestPrepare(t *testing.T) {
logger, _ := test.NewNullLogger()
ec := cli.NewExecutionContext()
ec.Logger = logger
ec.Spinner = spinner.New(spinner.CharSets[7], 100*time.Millisecond)
ec.Spinner.Writer = &fake.FakeWriter{}
err := ec.Prepare()
if err != nil {
t.Fatalf("prepare failed: %v", err)
}
if ec.CMDName == "" {
t.Fatalf("expected CMDName, got: %v", ec.CMDName)
}
if ec.Spinner == nil {
t.Fatal("got spinner empty")
}
if ec.Logger == nil {
t.Fatal("got empty logger")
}
if ec.GlobalConfigDir == "" {
t.Fatalf("global config dir: expected $HOME/%s, got %s", cli.GlobalConfigDirName, ec.GlobalConfigDir)
}
if ec.GlobalConfigFile == "" {
t.Fatalf("global config file: expected $HOME/%s/%s, got %s", cli.GlobalConfigDirName, cli.GlobalConfigFileName, ec.GlobalConfigFile)
}
if ec.ServerConfig == nil {
t.Fatal("got empty Config")
}
}
func TestValidate(t *testing.T) {
logger, _ := test.NewNullLogger()
ec := cli.NewExecutionContext()
ec.Logger = logger
ec.Spinner = spinner.New(spinner.CharSets[7], 100*time.Millisecond)
ec.Spinner.Writer = &fake.FakeWriter{}
ec.ExecutionDirectory = filepath.Join(os.TempDir(), "hasura-gql-tests-"+strconv.Itoa(rand.Intn(1000)))
ec.Viper = viper.New()
// validate a directory created by init
initCmd := commands.NewInitCmd(ec)
initCmd.Flags().Set("directory", ec.ExecutionDirectory)
err := initCmd.Execute()
if err != nil {
t.Fatalf("execution failed: %v", err)
}
err = ec.Prepare()
if err != nil {
t.Fatalf("prepare failed: %v", err)
}
err = ec.Validate()
if err != nil {
t.Fatalf("validate failed: %v", err)
}
// remove config.yaml and validate, should result in an error
err = os.Remove(filepath.Join(ec.ExecutionDirectory, "config.yaml"))
if err != nil {
t.Fatalf("remove failed: %v", err)
}
err = ec.Validate()
if err == nil {
t.Fatal("validate succeeded with no config.yaml")
}
os.RemoveAll(ec.ExecutionDirectory)
}