2014-09-14 23:46:45 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2014-09-15 05:14:37 +02:00
|
|
|
"strings"
|
2014-09-14 23:46:45 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-10-08 05:00:36 +02:00
|
|
|
func TestTreeChild(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "child"))
|
|
|
|
if err := tree.Load(storage, GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 05:02:18 +02:00
|
|
|
// Should be able to get the root child
|
|
|
|
if c := tree.Child([]string{}); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "root" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be able to get the root child
|
|
|
|
if c := tree.Child(nil); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "root" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
|
|
|
}
|
|
|
|
|
2014-10-08 05:00:36 +02:00
|
|
|
// Should be able to get the foo child
|
|
|
|
if c := tree.Child([]string{"foo"}); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be able to get the nested child
|
|
|
|
if c := tree.Child([]string{"foo", "bar"}); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeLoad(t *testing.T) {
|
2014-09-15 05:14:37 +02:00
|
|
|
storage := testStorage(t)
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-15 05:14:37 +02:00
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// This should error because we haven't gotten things yet
|
|
|
|
if err := tree.Load(storage, GetModeNone); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// This should get things
|
|
|
|
if err := tree.Load(storage, GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// This should no longer error
|
|
|
|
if err := tree.Load(storage, GetModeNone); err != nil {
|
2014-09-15 01:17:29 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-09-15 05:14:37 +02:00
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
expected := strings.TrimSpace(treeLoadStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n\n%s", actual)
|
|
|
|
}
|
2014-09-15 01:17:29 +02:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:53:29 +02:00
|
|
|
func TestTreeLoad_duplicate(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "dup"))
|
2014-09-15 18:53:29 +02:00
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should get things
|
|
|
|
if err := tree.Load(storage, GetModeGet); err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 17:11:32 +01:00
|
|
|
func TestTreeLoad_parentRef(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "basic-parent"))
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should error because we haven't gotten things yet
|
|
|
|
if err := tree.Load(storage, GetModeNone); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should get things
|
|
|
|
if err := tree.Load(storage, GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should no longer error
|
|
|
|
if err := tree.Load(storage, GetModeNone); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
2015-03-26 17:30:32 +01:00
|
|
|
expected := strings.TrimSpace(treeLoadParentStr)
|
2015-03-26 17:11:32 +01:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:56:04 +02:00
|
|
|
func TestTreeLoad_subdir(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "basic-subdir"))
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should error because we haven't gotten things yet
|
|
|
|
if err := tree.Load(storage, GetModeNone); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should get things
|
|
|
|
if err := tree.Load(storage, GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should no longer error
|
|
|
|
if err := tree.Load(storage, GetModeNone); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
expected := strings.TrimSpace(treeLoadSubdirStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeModules(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-14 23:46:45 +02:00
|
|
|
actual := tree.Modules()
|
|
|
|
|
|
|
|
expected := []*Module{
|
|
|
|
&Module{Name: "foo", Source: "./foo"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2014-09-15 05:14:37 +02:00
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeName(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-15 05:14:37 +02:00
|
|
|
actual := tree.Name()
|
|
|
|
|
2014-09-23 01:39:01 +02:00
|
|
|
if actual != RootName {
|
2014-09-15 05:14:37 +02:00
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeValidate_badChild(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-child-bad"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
|
|
|
if err := tree.Load(testStorage(t), GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-15 22:57:07 +02:00
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeValidate_badChildOutput(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-bad-output"))
|
2014-09-15 22:57:07 +02:00
|
|
|
|
|
|
|
if err := tree.Load(testStorage(t), GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 19:32:41 +02:00
|
|
|
func TestTreeValidate_badChildVar(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-bad-var"))
|
2014-09-15 19:32:41 +02:00
|
|
|
|
|
|
|
if err := tree.Load(testStorage(t), GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeValidate_badRoot(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-root-bad"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
|
|
|
if err := tree.Load(testStorage(t), GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeValidate_good(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-child-good"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
|
|
|
if err := tree.Load(testStorage(t), GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeValidate_notLoaded(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 04:40:06 +02:00
|
|
|
func TestTreeValidate_requiredChildVar(t *testing.T) {
|
|
|
|
tree := NewTree("", testConfig(t, "validate-required-var"))
|
|
|
|
|
|
|
|
if err := tree.Load(testStorage(t), GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
2014-09-16 00:49:07 +02:00
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
const treeLoadStr = `
|
2014-09-23 01:39:01 +02:00
|
|
|
root
|
2014-09-15 05:14:37 +02:00
|
|
|
foo
|
|
|
|
`
|
2014-09-26 23:56:04 +02:00
|
|
|
|
2015-03-26 17:30:32 +01:00
|
|
|
const treeLoadParentStr = `
|
|
|
|
root
|
|
|
|
a
|
|
|
|
b
|
|
|
|
`
|
2014-09-26 23:56:04 +02:00
|
|
|
const treeLoadSubdirStr = `
|
|
|
|
root
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
`
|