summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/config/module/testing.go
blob: fc9e7331afdb2a93149b24c0df87b6cd4256b5d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package module

import (
	"io/ioutil"
	"os"
	"testing"

	"github.com/hashicorp/go-getter"
)

// TestTree loads a module at the given path and returns the tree as well
// as a function that should be deferred to clean up resources.
func TestTree(t *testing.T, path string) (*Tree, func()) {
	// Create a temporary directory for module storage
	dir, err := ioutil.TempDir("", "tf")
	if err != nil {
		t.Fatalf("err: %s", err)
		return nil, nil
	}

	// Load the module
	mod, err := NewTreeModule("", path)
	if err != nil {
		t.Fatalf("err: %s", err)
		return nil, nil
	}

	// Get the child modules
	s := &getter.FolderStorage{StorageDir: dir}
	if err := mod.Load(s, GetModeGet); err != nil {
		t.Fatalf("err: %s", err)
		return nil, nil
	}

	return mod, func() {
		os.RemoveAll(dir)
	}
}