github.com/hashicorp/go-getter/v2@v2.2.2/detect_test.go (about) 1 package getter 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestDetect(t *testing.T) { 9 gitGetter := &GitGetter{ 10 Detectors: []Detector{ 11 new(GitDetector), 12 new(BitBucketDetector), 13 new(GitHubDetector), 14 }, 15 } 16 cases := []struct { 17 Input string 18 Pwd string 19 Output string 20 Err bool 21 getter Getter 22 }{ 23 {"./foo", "/foo", "/foo/foo", false, new(FileGetter)}, 24 {"git::./foo", "/foo", "/foo/foo", false, gitGetter}, 25 { 26 "git::github.com/hashicorp/foo", 27 "", 28 "https://github.com/hashicorp/foo.git", 29 false, 30 gitGetter, 31 }, 32 { 33 "./foo", 34 "/foo", 35 "/foo/foo", 36 false, 37 new(FileGetter), 38 }, 39 { 40 "git::https://github.com/hashicorp/consul.git", 41 "", 42 "https://github.com/hashicorp/consul.git", 43 false, 44 gitGetter, 45 }, 46 { 47 "git::https://person@someothergit.com/foo/bar", 48 "", 49 "https://person@someothergit.com/foo/bar", 50 false, 51 gitGetter, 52 }, 53 { 54 "git::https://person@someothergit.com/foo/bar", 55 "/bar", 56 "https://person@someothergit.com/foo/bar", 57 false, 58 gitGetter, 59 }, 60 61 // https://github.com/hashicorp/go-getter/pull/124 62 { 63 "git::ssh://git@my.custom.git/dir1/dir2", 64 "", 65 "ssh://git@my.custom.git/dir1/dir2", 66 false, 67 gitGetter, 68 }, 69 { 70 "git::git@my.custom.git:dir1/dir2", 71 "/foo", 72 "ssh://git@my.custom.git/dir1/dir2", 73 false, 74 gitGetter, 75 }, 76 { 77 "git::git@my.custom.git:dir1/dir2", 78 "", 79 "ssh://git@my.custom.git/dir1/dir2", 80 false, 81 gitGetter, 82 }, 83 } 84 85 for i, tc := range cases { 86 t.Run(fmt.Sprintf("%d %s", i, tc.Input), func(t *testing.T) { 87 req := &Request{ 88 Src: tc.Input, 89 Pwd: tc.Pwd, 90 } 91 ok, err := Detect(req, tc.getter) 92 if err != nil != tc.Err { 93 t.Fatalf("%d: bad err: %s", i, err) 94 } 95 96 if !tc.Err && !ok { 97 t.Fatalf("%d: should be ok", i) 98 } 99 100 if req.Src != tc.Output { 101 t.Fatalf("%d: bad output: %s\nexpected: %s", i, req.Src, tc.Output) 102 } 103 }) 104 } 105 }