2018-05-03 20:28:12 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-12-19 14:29:29 -06:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
2018-05-03 20:28:12 -05:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEvaluatorGetTerraformAttr(t *testing.T) {
|
|
|
|
evaluator := &Evaluator{
|
|
|
|
Meta: &ContextMeta{
|
|
|
|
Env: "foo",
|
|
|
|
},
|
|
|
|
}
|
2018-05-04 21:24:06 -05:00
|
|
|
data := &evaluationStateData{
|
|
|
|
Evaluator: evaluator,
|
|
|
|
}
|
|
|
|
scope := evaluator.Scope(data, nil)
|
2018-05-03 20:28:12 -05:00
|
|
|
|
|
|
|
t.Run("workspace", func(t *testing.T) {
|
|
|
|
want := cty.StringVal("foo")
|
|
|
|
got, diags := scope.Data.GetTerraformAttr(addrs.TerraformAttr{
|
|
|
|
Name: "workspace",
|
|
|
|
}, tfdiags.SourceRange{})
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("unexpected diagnostics %s", spew.Sdump(diags))
|
|
|
|
}
|
|
|
|
if !got.RawEquals(want) {
|
|
|
|
t.Errorf("wrong result %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-12-19 14:29:29 -06:00
|
|
|
|
|
|
|
func TestEvaluatorGetPathAttr(t *testing.T) {
|
|
|
|
evaluator := &Evaluator{
|
|
|
|
Meta: &ContextMeta{
|
|
|
|
Env: "foo",
|
|
|
|
},
|
|
|
|
Config: &configs.Config{
|
|
|
|
Module: &configs.Module{
|
|
|
|
SourceDir: "bar/baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
data := &evaluationStateData{
|
|
|
|
Evaluator: evaluator,
|
|
|
|
}
|
|
|
|
scope := evaluator.Scope(data, nil)
|
|
|
|
|
|
|
|
t.Run("module", func(t *testing.T) {
|
|
|
|
want := cty.StringVal("bar/baz")
|
|
|
|
got, diags := scope.Data.GetPathAttr(addrs.PathAttr{
|
|
|
|
Name: "module",
|
|
|
|
}, tfdiags.SourceRange{})
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("unexpected diagnostics %s", spew.Sdump(diags))
|
|
|
|
}
|
|
|
|
if !got.RawEquals(want) {
|
|
|
|
t.Errorf("wrong result %#v; want %#v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("root", func(t *testing.T) {
|
|
|
|
want := cty.StringVal("bar/baz")
|
|
|
|
got, diags := scope.Data.GetPathAttr(addrs.PathAttr{
|
|
|
|
Name: "root",
|
|
|
|
}, tfdiags.SourceRange{})
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("unexpected diagnostics %s", spew.Sdump(diags))
|
|
|
|
}
|
|
|
|
if !got.RawEquals(want) {
|
|
|
|
t.Errorf("wrong result %#v; want %#v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|