2018-06-27 15:04:09 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-07-06 08:06:27 +03:00
|
|
|
"github.com/briandowns/spinner"
|
2018-06-27 15:04:09 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli"
|
2018-07-06 08:06:27 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/util/fake"
|
2018-06-29 13:02:33 +03:00
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
2018-06-27 15:04:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitCmd(t *testing.T) {
|
2018-06-29 13:02:33 +03:00
|
|
|
logger, _ := test.NewNullLogger()
|
2019-01-28 16:55:28 +03:00
|
|
|
ec := cli.NewExecutionContext()
|
|
|
|
ec.Logger = logger
|
|
|
|
ec.Spinner = spinner.New(spinner.CharSets[7], 100*time.Millisecond)
|
2018-06-27 15:04:09 +03:00
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
opts *initOptions
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{"only-init-dir", &initOptions{
|
2019-02-14 12:37:47 +03:00
|
|
|
EC: ec,
|
|
|
|
Endpoint: "",
|
|
|
|
AdminSecret: "",
|
|
|
|
InitDir: filepath.Join(os.TempDir(), "hasura-cli-test-"+strconv.Itoa(rand.Intn(1000))),
|
2018-06-27 15:04:09 +03:00
|
|
|
}, nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2018-07-06 08:06:27 +03:00
|
|
|
tc.opts.EC.Spinner.Writer = &fake.FakeWriter{}
|
2018-06-27 15:04:09 +03:00
|
|
|
err := tc.opts.EC.Prepare()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: prep failed: %v", tc.name, err)
|
|
|
|
}
|
2018-06-28 11:44:01 +03:00
|
|
|
err = tc.opts.run()
|
2018-06-27 15:04:09 +03:00
|
|
|
if err != tc.err {
|
|
|
|
t.Fatalf("%s: expected %v, got %v", tc.name, tc.err, err)
|
|
|
|
} else {
|
|
|
|
// TODO: (shahidhk) need to verify the contents of the spec generated
|
|
|
|
os.RemoveAll(tc.opts.InitDir)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|