github.com/medzin/terraform@v0.11.11/config/module/testing.go (about)

     1  package module
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  // TestTree loads a module at the given path and returns the tree as well
    10  // as a function that should be deferred to clean up resources.
    11  func TestTree(t *testing.T, path string) (*Tree, func()) {
    12  	// Create a temporary directory for module storage
    13  	dir, err := ioutil.TempDir("", "tf")
    14  	if err != nil {
    15  		t.Fatalf("err: %s", err)
    16  		return nil, nil
    17  	}
    18  
    19  	// Load the module
    20  	mod, err := NewTreeModule("", path)
    21  	if err != nil {
    22  		t.Fatalf("err: %s", err)
    23  		return nil, nil
    24  	}
    25  
    26  	// Get the child modules
    27  	s := &Storage{StorageDir: dir, Mode: GetModeGet}
    28  	if err := mod.Load(s); err != nil {
    29  		t.Fatalf("err: %s", err)
    30  		return nil, nil
    31  	}
    32  
    33  	return mod, func() {
    34  		os.RemoveAll(dir)
    35  	}
    36  }