2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2015-02-02 05:04:02 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-02-08 15:01:49 -06:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/dag"
|
2015-02-02 05:04:02 -06:00
|
|
|
)
|
|
|
|
|
2015-02-08 15:01:49 -06:00
|
|
|
func TestBasicGraphBuilder_impl(t *testing.T) {
|
|
|
|
var _ GraphBuilder = new(BasicGraphBuilder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicGraphBuilder(t *testing.T) {
|
|
|
|
b := &BasicGraphBuilder{
|
|
|
|
Steps: []GraphTransformer{
|
|
|
|
&testBasicGraphBuilderTransform{1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g, err := b.Build(addrs.RootModuleInstance)
|
2015-02-08 15:01:49 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
if g.Path.String() != addrs.RootModuleInstance.String() {
|
|
|
|
t.Fatalf("wrong module path %q", g.Path)
|
2015-02-08 15:01:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testBasicGraphBuilderStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicGraphBuilder_validate(t *testing.T) {
|
|
|
|
b := &BasicGraphBuilder{
|
|
|
|
Steps: []GraphTransformer{
|
|
|
|
&testBasicGraphBuilderTransform{1},
|
|
|
|
&testBasicGraphBuilderTransform{2},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
_, err := b.Build(addrs.RootModuleInstance)
|
2015-02-08 15:01:49 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testBasicGraphBuilderTransform struct {
|
|
|
|
V dag.Vertex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testBasicGraphBuilderTransform) Transform(g *Graph) error {
|
|
|
|
g.Add(t.V)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const testBasicGraphBuilderStr = `
|
|
|
|
1
|
|
|
|
`
|