tests: fix unit tests

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-25 14:35:55 -07:00 committed by Solomon Hykes
parent 7ad541feb1
commit 524f77df65
5 changed files with 24 additions and 107 deletions

View File

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

View File

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

View File

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

View File

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

1
go.mod
View File

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