github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/configs/configload/loader_test.go (about)

     1  package configload
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/go-test/deep"
    11  	"github.com/hashicorp/hcl2/hcl"
    12  	"github.com/zclconf/go-cty/cty"
    13  )
    14  
    15  // tempChdir copies the contents of the given directory to a temporary
    16  // directory and changes the test process's current working directory to
    17  // point to that directory. Also returned is a function that should be
    18  // called at the end of the test (e.g. via "defer") to restore the previous
    19  // working directory.
    20  //
    21  // Tests using this helper cannot safely be run in parallel with other tests.
    22  func tempChdir(t *testing.T, sourceDir string) (string, func()) {
    23  	t.Helper()
    24  
    25  	tmpDir, err := ioutil.TempDir("", "terraform-configload")
    26  	if err != nil {
    27  		t.Fatalf("failed to create temporary directory: %s", err)
    28  		return "", nil
    29  	}
    30  
    31  	if err := copyDir(tmpDir, sourceDir); err != nil {
    32  		t.Fatalf("failed to copy fixture to temporary directory: %s", err)
    33  		return "", nil
    34  	}
    35  
    36  	oldDir, err := os.Getwd()
    37  	if err != nil {
    38  		t.Fatalf("failed to determine current working directory: %s", err)
    39  		return "", nil
    40  	}
    41  
    42  	err = os.Chdir(tmpDir)
    43  	if err != nil {
    44  		t.Fatalf("failed to switch to temp dir %s: %s", tmpDir, err)
    45  		return "", nil
    46  	}
    47  
    48  	t.Logf("tempChdir switched to %s after copying from %s", tmpDir, sourceDir)
    49  
    50  	return tmpDir, func() {
    51  		err := os.Chdir(oldDir)
    52  		if err != nil {
    53  			panic(fmt.Errorf("failed to restore previous working directory %s: %s", oldDir, err))
    54  		}
    55  
    56  		if os.Getenv("TF_CONFIGLOAD_TEST_KEEP_TMP") == "" {
    57  			os.RemoveAll(tmpDir)
    58  		}
    59  	}
    60  }
    61  
    62  // tempChdirLoader is a wrapper around tempChdir that also returns a Loader
    63  // whose modules directory is at the conventional location within the
    64  // created temporary directory.
    65  func tempChdirLoader(t *testing.T, sourceDir string) (*Loader, func()) {
    66  	t.Helper()
    67  
    68  	_, done := tempChdir(t, sourceDir)
    69  	modulesDir := filepath.Clean(".terraform/modules")
    70  
    71  	err := os.MkdirAll(modulesDir, os.ModePerm)
    72  	if err != nil {
    73  		done() // undo the chdir in tempChdir so we can safely run other tests
    74  		t.Fatalf("failed to create modules directory: %s", err)
    75  		return nil, nil
    76  	}
    77  
    78  	loader, err := NewLoader(&Config{
    79  		ModulesDir: modulesDir,
    80  	})
    81  	if err != nil {
    82  		done() // undo the chdir in tempChdir so we can safely run other tests
    83  		t.Fatalf("failed to create loader: %s", err)
    84  		return nil, nil
    85  	}
    86  
    87  	return loader, done
    88  }
    89  
    90  func assertNoDiagnostics(t *testing.T, diags hcl.Diagnostics) bool {
    91  	t.Helper()
    92  	return assertDiagnosticCount(t, diags, 0)
    93  }
    94  
    95  func assertDiagnosticCount(t *testing.T, diags hcl.Diagnostics, want int) bool {
    96  	t.Helper()
    97  	if len(diags) != 0 {
    98  		t.Errorf("wrong number of diagnostics %d; want %d", len(diags), want)
    99  		for _, diag := range diags {
   100  			t.Logf("- %s", diag)
   101  		}
   102  		return true
   103  	}
   104  	return false
   105  }
   106  
   107  func assertDiagnosticSummary(t *testing.T, diags hcl.Diagnostics, want string) bool {
   108  	t.Helper()
   109  
   110  	for _, diag := range diags {
   111  		if diag.Summary == want {
   112  			return false
   113  		}
   114  	}
   115  
   116  	t.Errorf("missing diagnostic summary %q", want)
   117  	for _, diag := range diags {
   118  		t.Logf("- %s", diag)
   119  	}
   120  	return true
   121  }
   122  
   123  func assertResultDeepEqual(t *testing.T, got, want interface{}) bool {
   124  	t.Helper()
   125  	if diff := deep.Equal(got, want); diff != nil {
   126  		for _, problem := range diff {
   127  			t.Errorf("%s", problem)
   128  		}
   129  		return true
   130  	}
   131  	return false
   132  }
   133  
   134  func assertResultCtyEqual(t *testing.T, got, want cty.Value) bool {
   135  	t.Helper()
   136  	if !got.RawEquals(want) {
   137  		t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, want)
   138  		return true
   139  	}
   140  	return false
   141  }