github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/helper/pathorcontents/read_test.go (about)

     1  package pathorcontents
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/mitchellh/go-homedir"
    11  )
    12  
    13  func TestRead_Path(t *testing.T) {
    14  	isPath := true
    15  	f, cleanup := testTempFile(t)
    16  	defer cleanup()
    17  
    18  	if _, err := io.WriteString(f, "foobar"); err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  	f.Close()
    22  
    23  	contents, wasPath, err := Read(f.Name())
    24  
    25  	if err != nil {
    26  		t.Fatalf("err: %s", err)
    27  	}
    28  	if wasPath != isPath {
    29  		t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
    30  	}
    31  	if contents != "foobar" {
    32  		t.Fatalf("expected contents %s, got %s", "foobar", contents)
    33  	}
    34  }
    35  
    36  func TestRead_TildePath(t *testing.T) {
    37  	isPath := true
    38  	home, err := homedir.Dir()
    39  	if err != nil {
    40  		t.Fatalf("err: %s", err)
    41  	}
    42  	f, cleanup := testTempFile(t, home)
    43  	defer cleanup()
    44  
    45  	if _, err := io.WriteString(f, "foobar"); err != nil {
    46  		t.Fatalf("err: %s", err)
    47  	}
    48  	f.Close()
    49  
    50  	r := strings.NewReplacer(home, "~")
    51  	homePath := r.Replace(f.Name())
    52  	contents, wasPath, err := Read(homePath)
    53  
    54  	if err != nil {
    55  		t.Fatalf("err: %s", err)
    56  	}
    57  	if wasPath != isPath {
    58  		t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
    59  	}
    60  	if contents != "foobar" {
    61  		t.Fatalf("expected contents %s, got %s", "foobar", contents)
    62  	}
    63  }
    64  
    65  func TestRead_PathNoPermission(t *testing.T) {
    66  	isPath := true
    67  	f, cleanup := testTempFile(t)
    68  	defer cleanup()
    69  
    70  	if _, err := io.WriteString(f, "foobar"); err != nil {
    71  		t.Fatalf("err: %s", err)
    72  	}
    73  	f.Close()
    74  
    75  	if err := os.Chmod(f.Name(), 0); err != nil {
    76  		t.Fatalf("err: %s", err)
    77  	}
    78  
    79  	contents, wasPath, err := Read(f.Name())
    80  
    81  	if err == nil {
    82  		t.Fatal("Expected error, got none!")
    83  	}
    84  	if wasPath != isPath {
    85  		t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
    86  	}
    87  	if contents != "" {
    88  		t.Fatalf("expected contents %s, got %s", "", contents)
    89  	}
    90  }
    91  
    92  func TestRead_Contents(t *testing.T) {
    93  	isPath := false
    94  	input := "hello"
    95  
    96  	contents, wasPath, err := Read(input)
    97  
    98  	if err != nil {
    99  		t.Fatalf("err: %s", err)
   100  	}
   101  	if wasPath != isPath {
   102  		t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
   103  	}
   104  	if contents != input {
   105  		t.Fatalf("expected contents %s, got %s", input, contents)
   106  	}
   107  }
   108  
   109  func TestRead_TildeContents(t *testing.T) {
   110  	isPath := false
   111  	input := "~/hello/notafile"
   112  
   113  	contents, wasPath, err := Read(input)
   114  
   115  	if err != nil {
   116  		t.Fatalf("err: %s", err)
   117  	}
   118  	if wasPath != isPath {
   119  		t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
   120  	}
   121  	if contents != input {
   122  		t.Fatalf("expected contents %s, got %s", input, contents)
   123  	}
   124  }
   125  
   126  // Returns an open tempfile based at baseDir and a function to clean it up.
   127  func testTempFile(t *testing.T, baseDir ...string) (*os.File, func()) {
   128  	base := ""
   129  	if len(baseDir) == 1 {
   130  		base = baseDir[0]
   131  	}
   132  	f, err := ioutil.TempFile(base, "tf")
   133  	if err != nil {
   134  		t.Fatalf("err: %s", err)
   135  	}
   136  
   137  	return f, func() {
   138  		os.Remove(f.Name())
   139  	}
   140  }