Skip to content

Commit

Permalink
test: compile tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Mar 11, 2024
1 parent 6ee8e05 commit e80ce28
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
4 changes: 2 additions & 2 deletions inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (inv *Inventory) Compile(target data.Path) error {
classIdentifier := target.StripPrefix(data.NewPath(string(targetScope)))
targetClass, err := registry.GetClassByIdentifier(classIdentifier.String())
if err != nil {
return err
return fmt.Errorf("cannot load target class from scope '%s' with identifier '%s': %w", targetScope, classIdentifier, err)
}

// Overwrite inventory paths with target values if applicable.
Expand All @@ -123,7 +123,7 @@ func (inv *Inventory) Compile(target data.Path) error {

// The path exists within the inventory, overwrite it with the value from the target.
spew.Println("OVERWRITTEN", pathWithoutClassName)
err = inv.SetPath(pathWithoutClassName, v)
err = inv.SetPath(pathWithoutClassName, v.Raw)
if err != nil {
return fmt.Errorf("failed to overwrite path %s: %w", pathWithoutClassName, err)
}
Expand Down
71 changes: 50 additions & 21 deletions inventory_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package skipper_test

import (
"fmt"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"

. "github.com/lukasjarosch/skipper"
Expand Down Expand Up @@ -160,27 +160,56 @@ func TestInventoryAbsolutePath(t *testing.T) {
}

func TestInventory_Compile(t *testing.T) {
commonClass, err := NewClass("testdata/compile/data/common.yaml", codec.NewYamlCodec(), data.NewPath("common"))
assert.NoError(t, err)

dataRegistry := NewRegistry()
err = dataRegistry.RegisterClass(commonClass)
assert.NoError(t, err)

testTarget, err := NewClass("testdata/compile/targets/test.yaml", codec.NewYamlCodec(), data.NewPath("test"))
assert.NoError(t, err)
targetRegistry := NewRegistry()
err = targetRegistry.RegisterClass(testTarget)
assert.NoError(t, err)
dataRegistry := func() *Registry {
commonClass, err := NewClass("testdata/compile/data/common.yaml", codec.NewYamlCodec(), data.NewPath("common"))
assert.NoError(t, err)
dataRegistry := NewRegistry()
err = dataRegistry.RegisterClass(commonClass)
assert.NoError(t, err)

return dataRegistry
}

inventory, _ := NewInventory()
err = inventory.RegisterScope(DataScope, dataRegistry)
assert.NoError(t, err)
err = inventory.RegisterScope(TargetsScope, targetRegistry)
assert.NoError(t, err)
targetRegistry := func(name string) *Registry {
testTarget, err := NewClass(fmt.Sprintf("testdata/compile/targets/%s.yaml", name), codec.NewYamlCodec(), data.NewPath(name))
assert.NoError(t, err)
targetRegistry := NewRegistry()
err = targetRegistry.RegisterClass(testTarget)
assert.NoError(t, err)

err = inventory.Compile(data.NewPath("targets.test"))
assert.NoError(t, err)
return targetRegistry
}

spew.Dump(inventory.Get("data.common"))
t.Run("valid target", func(t *testing.T) {
inventory, _ := NewInventory()
err := inventory.RegisterScope(DataScope, dataRegistry())
assert.NoError(t, err)
err = inventory.RegisterScope(TargetsScope, targetRegistry("valid"))
assert.NoError(t, err)

err = inventory.Compile(data.NewPath("targets.valid"))
assert.NoError(t, err)

expected := map[string]data.Value{
"data.common.name": data.NewValue("Jane"),
"data.common.age": data.NewValue("over 9000"),
}

for p, v := range expected {
val, err := inventory.Get(p)
assert.NoError(t, err)
assert.Equal(t, v.Raw, val.Raw)
}
})

t.Run("target cannot introduce paths in the inventory", func(t *testing.T) {
inventory, _ := NewInventory()
err := inventory.RegisterScope(DataScope, dataRegistry())
assert.NoError(t, err)
err = inventory.RegisterScope(TargetsScope, targetRegistry("introduce_paths"))
assert.NoError(t, err)

err = inventory.Compile(data.NewPath("targets.introduce_paths"))
assert.ErrorIs(t, err, ErrTargetCannotIntroducePaths)
})
}
3 changes: 2 additions & 1 deletion testdata/compile/data/common.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
common:
name: test
name: John
age: 35
4 changes: 4 additions & 0 deletions testdata/compile/targets/introduce_paths.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
introduce_paths:
data:
common:
does_not_exist: "ohai"
11 changes: 0 additions & 11 deletions testdata/compile/targets/test.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions testdata/compile/targets/valid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
valid:
data:
common:
name: "Jane"
data.common.age: "over 9000"

0 comments on commit e80ce28

Please sign in to comment.