github.com/weiwenhao/getter@v1.30.1/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 := tempTestFile(t) 109 defer os.RemoveAll(filepath.Dir(dst)) 110 111 // With a dir that doesn't exist 112 if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); err != nil { 113 t.Fatalf("err: %s", err) 114 } 115 116 // Verify the destination folder is a symlink 117 fi, err := os.Lstat(dst) 118 if err != nil { 119 t.Fatalf("err: %s", err) 120 } 121 if fi.Mode()&os.ModeSymlink == 0 { 122 t.Fatal("destination is not a symlink") 123 } 124 125 // Verify the main file exists 126 assertContents(t, dst, "Hello\n") 127 } 128 129 func TestFileGetter_GetFile_Copy(t *testing.T) { 130 g := new(FileGetter) 131 g.Copy = true 132 133 dst := tempTestFile(t) 134 defer os.RemoveAll(filepath.Dir(dst)) 135 136 // With a dir that doesn't exist 137 if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); err != nil { 138 t.Fatalf("err: %s", err) 139 } 140 141 // Verify the destination folder is a symlink 142 fi, err := os.Lstat(dst) 143 if err != nil { 144 t.Fatalf("err: %s", err) 145 } 146 if fi.Mode()&os.ModeSymlink != 0 { 147 t.Fatal("destination is a symlink") 148 } 149 150 // Verify the main file exists 151 assertContents(t, dst, "Hello\n") 152 } 153 154 // https://github.com/hashicorp/terraform/issues/8418 155 func TestFileGetter_percent2F(t *testing.T) { 156 g := new(FileGetter) 157 dst := tempDir(t) 158 159 // With a dir that doesn't exist 160 if err := g.Get(dst, testModuleURL("basic%2Ftest")); err != nil { 161 t.Fatalf("err: %s", err) 162 } 163 164 // Verify the main file exists 165 mainPath := filepath.Join(dst, "main.tf") 166 if _, err := os.Stat(mainPath); err != nil { 167 t.Fatalf("err: %s", err) 168 } 169 } 170 171 func TestFileGetter_ClientMode_notexist(t *testing.T) { 172 g := new(FileGetter) 173 174 u := testURL("nonexistent") 175 if _, err := g.ClientMode(u); err == nil { 176 t.Fatal("expect source file error") 177 } 178 } 179 180 func TestFileGetter_ClientMode_file(t *testing.T) { 181 g := new(FileGetter) 182 183 // Check the client mode when pointed at a file. 184 mode, err := g.ClientMode(testModuleURL("basic-file/foo.txt")) 185 if err != nil { 186 t.Fatalf("err: %s", err) 187 } 188 if mode != ClientModeFile { 189 t.Fatal("expect ClientModeFile") 190 } 191 } 192 193 func TestFileGetter_ClientMode_dir(t *testing.T) { 194 g := new(FileGetter) 195 196 // Check the client mode when pointed at a directory. 197 mode, err := g.ClientMode(testModuleURL("basic")) 198 if err != nil { 199 t.Fatalf("err: %s", err) 200 } 201 if mode != ClientModeDir { 202 t.Fatal("expect ClientModeDir") 203 } 204 }