github.com/remind101/go-getter@v0.0.0-20180809191950-4bda8fa99001/source_test.go (about) 1 package getter 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func TestSourceDirSubdir(t *testing.T) { 11 cases := []struct { 12 Input string 13 Dir, Sub string 14 }{ 15 { 16 "hashicorp.com", 17 "hashicorp.com", "", 18 }, 19 { 20 "hashicorp.com//foo", 21 "hashicorp.com", "foo", 22 }, 23 { 24 "hashicorp.com//foo?bar=baz", 25 "hashicorp.com?bar=baz", "foo", 26 }, 27 { 28 "https://hashicorp.com/path//*?archive=foo", 29 "https://hashicorp.com/path?archive=foo", "*", 30 }, 31 { 32 "file://foo//bar", 33 "file://foo", "bar", 34 }, 35 } 36 37 for i, tc := range cases { 38 adir, asub := SourceDirSubdir(tc.Input) 39 if adir != tc.Dir { 40 t.Fatalf("%d: bad dir: %#v", i, adir) 41 } 42 if asub != tc.Sub { 43 t.Fatalf("%d: bad sub: %#v", i, asub) 44 } 45 } 46 } 47 48 func TestSourceSubdirGlob(t *testing.T) { 49 td, err := ioutil.TempDir("", "subdir-glob") 50 if err != nil { 51 t.Fatal(err) 52 } 53 defer os.RemoveAll(td) 54 55 if err := os.Mkdir(filepath.Join(td, "subdir"), 0755); err != nil { 56 t.Fatal(err) 57 } 58 59 if err := os.Mkdir(filepath.Join(td, "subdir/one"), 0755); err != nil { 60 t.Fatal(err) 61 } 62 63 if err := os.Mkdir(filepath.Join(td, "subdir/two"), 0755); err != nil { 64 t.Fatal(err) 65 } 66 67 subdir := filepath.Join(td, "subdir") 68 69 // match the exact directory 70 res, err := SubdirGlob(td, "subdir") 71 if err != nil { 72 t.Fatal(err) 73 } 74 if res != subdir { 75 t.Fatalf(`expected "subdir", got: %q`, subdir) 76 } 77 78 // single match from a wildcard 79 res, err = SubdirGlob(td, "*") 80 if err != nil { 81 t.Fatal(err) 82 } 83 if res != subdir { 84 t.Fatalf(`expected "subdir", got: %q`, subdir) 85 } 86 87 // multiple matches 88 res, err = SubdirGlob(td, "subdir/*") 89 if err == nil { 90 t.Fatalf("expected multiple matches, got %q", res) 91 } 92 93 // non-existent 94 res, err = SubdirGlob(td, "foo") 95 if err == nil { 96 t.Fatalf("expected no matches, got %q", res) 97 } 98 }