From 524f77df65504efd1e6384f92446af481c7ff252 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 25 Mar 2021 14:35:55 -0700 Subject: [PATCH] tests: fix unit tests Signed-off-by: Andrea Luzzardi --- dagger/compiler/compiler_test.go | 45 +++++++------------------ dagger/dagger_test.go | 56 -------------------------------- dagger/input_test.go | 16 +++------ dagger/route.go | 13 ++++---- go.mod | 1 + 5 files changed, 24 insertions(+), 107 deletions(-) delete mode 100644 dagger/dagger_test.go diff --git a/dagger/compiler/compiler_test.go b/dagger/compiler/compiler_test.go index 37c298ee..c4ff093f 100644 --- a/dagger/compiler/compiler_test.go +++ b/dagger/compiler/compiler_test.go @@ -2,55 +2,34 @@ package compiler import ( "testing" + + "github.com/stretchr/testify/require" ) // Test that a non-existing field is detected correctly func TestFieldNotExist(t *testing.T) { c := &Compiler{} root, err := c.Compile("test.cue", `foo: "bar"`) - if err != nil { - t.Fatal(err) - } - if v := root.Get("foo"); !v.Exists() { - // value should exist - t.Fatal(v) - } - if v := root.Get("bar"); v.Exists() { - // value should NOT exist - t.Fatal(v) - } + require.NoError(t, err) + require.True(t, root.Get("foo").Exists()) + require.False(t, root.Get("bar").Exists()) } // Test that a non-existing definition is detected correctly func TestDefNotExist(t *testing.T) { c := &Compiler{} root, err := c.Compile("test.cue", `foo: #bla: "bar"`) - if err != nil { - t.Fatal(err) - } - if v := root.Get("foo.#bla"); !v.Exists() { - // value should exist - t.Fatal(v) - } - if v := root.Get("foo.#nope"); v.Exists() { - // value should NOT exist - t.Fatal(v) - } + require.NoError(t, err) + require.True(t, root.Get("foo.#bla").Exists()) + require.False(t, root.Get("foo.#nope").Exists()) } func TestJSON(t *testing.T) { c := &Compiler{} v, err := c.Compile("", `foo: hello: "world"`) - if err != nil { - t.Fatal(err) - } - b1 := v.JSON() - if string(b1) != `{"foo":{"hello":"world"}}` { - t.Fatal(b1) - } + require.NoError(t, err) + require.Equal(t, `{"foo":{"hello":"world"}}`, string(v.JSON())) + // Reproduce a bug where Value.Get().JSON() ignores Get() - b2 := v.Get("foo").JSON() - if string(b2) != `{"hello":"world"}` { - t.Fatal(b2) - } + require.Equal(t, `{"hello":"world"}`, string(v.Get("foo").JSON())) } diff --git a/dagger/dagger_test.go b/dagger/dagger_test.go deleted file mode 100644 index 43dd99c6..00000000 --- a/dagger/dagger_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package dagger - -import ( - "testing" - - "dagger.io/go/dagger/compiler" -) - -func TestLocalDirs(t *testing.T) { - env := mkEnv(t, - `#compute: [ - { - do: "local" - dir: "bar" - } - ]`, - `dir: #compute: [ - { - do: "local" - dir: "foo" - } - ]`, - ) - dirs := env.LocalDirs() - if len(dirs) != 2 { - t.Fatal(dirs) - } - if _, ok := dirs["foo"]; !ok { - t.Fatal(dirs) - } - if _, ok := dirs["bar"]; !ok { - t.Fatal(dirs) - } -} - -func mkEnv(t *testing.T, updater, input string) *Route { - env, err := NewRoute() - if err != nil { - t.Fatal(err) - } - u, err := compiler.Compile("updater.cue", updater) - if err != nil { - t.Fatal(err) - } - if err := env.SetUpdater(u); err != nil { - t.Fatal(err) - } - i, err := compiler.Compile("input.cue", input) - if err != nil { - t.Fatal(err) - } - if err := env.SetInput(i); err != nil { - t.Fatal(err) - } - return env -} diff --git a/dagger/input_test.go b/dagger/input_test.go index bd71070d..8702f8c8 100644 --- a/dagger/input_test.go +++ b/dagger/input_test.go @@ -2,24 +2,18 @@ package dagger import ( "testing" + + "github.com/stretchr/testify/require" ) func TestEnvInputFlag(t *testing.T) { - env, err := NewRoute() - if err != nil { - t.Fatal(err) - } + st := &RouteState{} + require.NoError(t, st.AddInput("www.source", DirInput(".", []string{}))) - input, err := NewInputValue(`{}`) + env, err := NewRoute(st) if err != nil { t.Fatal(err) } - if err := input.DirFlag().Set("www.source=."); err != nil { - t.Fatal(err) - } - if err := env.SetInput(input.Value()); err != nil { - t.Fatal(err) - } localdirs := env.LocalDirs() if len(localdirs) != 1 { diff --git a/dagger/route.go b/dagger/route.go index 1634ad5b..52678c1a 100644 --- a/dagger/route.go +++ b/dagger/route.go @@ -60,9 +60,6 @@ type Route struct { // FIXME: embed update script in base as '#update' ? // FIXME: simplify Env by making it single layer? Each layer is one r. - // How to update the base configuration - updater *compiler.Value - // Layer 1: layout configuration layout *compiler.Value @@ -200,11 +197,13 @@ func (r *Route) LocalDirs() map[string]string { } // 2. Scan the layout - layout, err := r.st.LayoutSource.Compile() - if err != nil { - panic(err) + if r.st.LayoutSource != nil { + layout, err := r.st.LayoutSource.Compile() + if err != nil { + panic(err) + } + localdirs(layout) } - localdirs(layout) return dirs } diff --git a/go.mod b/go.mod index c5d39008..219e6ed8 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/spf13/cobra v1.1.3 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.7.1 + github.com/stretchr/testify v1.5.1 // indirect github.com/tonistiigi/fsutil v0.0.0-20201103201449-0834f99b7b85 github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea github.com/uber/jaeger-client-go v2.25.0+incompatible