github.com/weiwenhao/getter@v1.30.1/detect_git_test.go (about) 1 package getter 2 3 import ( 4 "testing" 5 ) 6 7 func TestGitDetector(t *testing.T) { 8 cases := []struct { 9 Input string 10 Output string 11 }{ 12 { 13 "git@github.com:hashicorp/foo.git", 14 "git::ssh://git@github.com/hashicorp/foo.git", 15 }, 16 { 17 "git@github.com:org/project.git?ref=test-branch", 18 "git::ssh://git@github.com/org/project.git?ref=test-branch", 19 }, 20 { 21 "git@github.com:hashicorp/foo.git//bar", 22 "git::ssh://git@github.com/hashicorp/foo.git//bar", 23 }, 24 { 25 "git@github.com:hashicorp/foo.git?foo=bar", 26 "git::ssh://git@github.com/hashicorp/foo.git?foo=bar", 27 }, 28 { 29 "git@github.xyz.com:org/project.git", 30 "git::ssh://git@github.xyz.com/org/project.git", 31 }, 32 { 33 "git@github.xyz.com:org/project.git?ref=test-branch", 34 "git::ssh://git@github.xyz.com/org/project.git?ref=test-branch", 35 }, 36 { 37 "git@github.xyz.com:org/project.git//module/a", 38 "git::ssh://git@github.xyz.com/org/project.git//module/a", 39 }, 40 { 41 "git@github.xyz.com:org/project.git//module/a?ref=test-branch", 42 "git::ssh://git@github.xyz.com/org/project.git//module/a?ref=test-branch", 43 }, 44 { 45 // Already in the canonical form, so no rewriting required 46 // When the ssh: protocol is used explicitly, we recognize it as 47 // URL form rather than SCP-like form, so the part after the colon 48 // is a port number, not part of the path. 49 "git::ssh://git@git.example.com:2222/hashicorp/foo.git", 50 "git::ssh://git@git.example.com:2222/hashicorp/foo.git", 51 }, 52 } 53 54 pwd := "/pwd" 55 f := new(GitDetector) 56 ds := []Detector{f} 57 for _, tc := range cases { 58 t.Run(tc.Input, func(t *testing.T) { 59 output, err := Detect(tc.Input, pwd, ds) 60 if err != nil { 61 t.Fatalf("unexpected error: %s", err) 62 } 63 64 if output != tc.Output { 65 t.Errorf("wrong result\ninput: %s\ngot: %s\nwant: %s", tc.Input, output, tc.Output) 66 } 67 }) 68 } 69 }