github.com/remind101/go-getter@v0.0.0-20180809191950-4bda8fa99001/get_file_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func TestFileGetter_impl(t *testing.T) {
    10  	var _ Getter = new(FileGetter)
    11  }
    12  
    13  func TestFileGetter(t *testing.T) {
    14  	g := new(FileGetter)
    15  	dst := tempDir(t)
    16  
    17  	// With a dir that doesn't exist
    18  	if err := g.Get(dst, testModuleURL("basic")); err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  
    22  	// Verify the destination folder is a symlink
    23  	fi, err := os.Lstat(dst)
    24  	if err != nil {
    25  		t.Fatalf("err: %s", err)
    26  	}
    27  	if fi.Mode()&os.ModeSymlink == 0 {
    28  		t.Fatal("destination is not a symlink")
    29  	}
    30  
    31  	// Verify the main file exists
    32  	mainPath := filepath.Join(dst, "main.tf")
    33  	if _, err := os.Stat(mainPath); err != nil {
    34  		t.Fatalf("err: %s", err)
    35  	}
    36  }
    37  
    38  func TestFileGetter_sourceFile(t *testing.T) {
    39  	g := new(FileGetter)
    40  	dst := tempDir(t)
    41  
    42  	// With a source URL that is a path to a file
    43  	u := testModuleURL("basic")
    44  	u.Path += "/main.tf"
    45  	if err := g.Get(dst, u); err == nil {
    46  		t.Fatal("should error")
    47  	}
    48  }
    49  
    50  func TestFileGetter_sourceNoExist(t *testing.T) {
    51  	g := new(FileGetter)
    52  	dst := tempDir(t)
    53  
    54  	// With a source URL that doesn't exist
    55  	u := testModuleURL("basic")
    56  	u.Path += "/main"
    57  	if err := g.Get(dst, u); err == nil {
    58  		t.Fatal("should error")
    59  	}
    60  }
    61  
    62  func TestFileGetter_dir(t *testing.T) {
    63  	g := new(FileGetter)
    64  	dst := tempDir(t)
    65  
    66  	if err := os.MkdirAll(dst, 0755); err != nil {
    67  		t.Fatalf("err: %s", err)
    68  	}
    69  
    70  	// With a dir that exists that isn't a symlink
    71  	if err := g.Get(dst, testModuleURL("basic")); err == nil {
    72  		t.Fatal("should error")
    73  	}
    74  }
    75  
    76  func TestFileGetter_dirSymlink(t *testing.T) {
    77  	g := new(FileGetter)
    78  	dst := tempDir(t)
    79  	dst2 := tempDir(t)
    80  
    81  	// Make parents
    82  	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  	if err := os.MkdirAll(dst2, 0755); err != nil {
    86  		t.Fatalf("err: %s", err)
    87  	}
    88  
    89  	// Make a symlink
    90  	if err := os.Symlink(dst2, dst); err != nil {
    91  		t.Fatalf("err: %s", err)
    92  	}
    93  
    94  	// With a dir that exists that isn't a symlink
    95  	if err := g.Get(dst, testModuleURL("basic")); err != nil {
    96  		t.Fatalf("err: %s", err)
    97  	}
    98  
    99  	// Verify the main file exists
   100  	mainPath := filepath.Join(dst, "main.tf")
   101  	if _, err := os.Stat(mainPath); err != nil {
   102  		t.Fatalf("err: %s", err)
   103  	}
   104  }
   105  
   106  func TestFileGetter_GetFile(t *testing.T) {
   107  	g := new(FileGetter)
   108  	dst := tempFile(t)
   109  
   110  	// With a dir that doesn't exist
   111  	if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  
   115  	// Verify the destination folder is a symlink
   116  	fi, err := os.Lstat(dst)
   117  	if err != nil {
   118  		t.Fatalf("err: %s", err)
   119  	}
   120  	if fi.Mode()&os.ModeSymlink == 0 {
   121  		t.Fatal("destination is not a symlink")
   122  	}
   123  
   124  	// Verify the main file exists
   125  	assertContents(t, dst, "Hello\n")
   126  }
   127  
   128  func TestFileGetter_GetFile_Copy(t *testing.T) {
   129  	g := new(FileGetter)
   130  	g.Copy = true
   131  
   132  	dst := tempFile(t)
   133  
   134  	// With a dir that doesn't exist
   135  	if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); err != nil {
   136  		t.Fatalf("err: %s", err)
   137  	}
   138  
   139  	// Verify the destination folder is a symlink
   140  	fi, err := os.Lstat(dst)
   141  	if err != nil {
   142  		t.Fatalf("err: %s", err)
   143  	}
   144  	if fi.Mode()&os.ModeSymlink != 0 {
   145  		t.Fatal("destination is a symlink")
   146  	}
   147  
   148  	// Verify the main file exists
   149  	assertContents(t, dst, "Hello\n")
   150  }
   151  
   152  // https://github.com/hashicorp/terraform/issues/8418
   153  func TestFileGetter_percent2F(t *testing.T) {
   154  	g := new(FileGetter)
   155  	dst := tempDir(t)
   156  
   157  	// With a dir that doesn't exist
   158  	if err := g.Get(dst, testModuleURL("basic%2Ftest")); err != nil {
   159  		t.Fatalf("err: %s", err)
   160  	}
   161  
   162  	// Verify the main file exists
   163  	mainPath := filepath.Join(dst, "main.tf")
   164  	if _, err := os.Stat(mainPath); err != nil {
   165  		t.Fatalf("err: %s", err)
   166  	}
   167  }
   168  
   169  func TestFileGetter_ClientMode_notexist(t *testing.T) {
   170  	g := new(FileGetter)
   171  
   172  	u := testURL("nonexistent")
   173  	if _, err := g.ClientMode(u); err == nil {
   174  		t.Fatal("expect source file error")
   175  	}
   176  }
   177  
   178  func TestFileGetter_ClientMode_file(t *testing.T) {
   179  	g := new(FileGetter)
   180  
   181  	// Check the client mode when pointed at a file.
   182  	mode, err := g.ClientMode(testModuleURL("basic-file/foo.txt"))
   183  	if err != nil {
   184  		t.Fatalf("err: %s", err)
   185  	}
   186  	if mode != ClientModeFile {
   187  		t.Fatal("expect ClientModeFile")
   188  	}
   189  }
   190  
   191  func TestFileGetter_ClientMode_dir(t *testing.T) {
   192  	g := new(FileGetter)
   193  
   194  	// Check the client mode when pointed at a directory.
   195  	mode, err := g.ClientMode(testModuleURL("basic"))
   196  	if err != nil {
   197  		t.Fatalf("err: %s", err)
   198  	}
   199  	if mode != ClientModeDir {
   200  		t.Fatal("expect ClientModeDir")
   201  	}
   202  }