From fc72bdfcd02d9b26ee87de2e0f1304cae998c4b7 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Mon, 5 Mar 2018 23:41:54 +0000 Subject: [PATCH] Update vcs_test.go for new vcs method --- vcs.go | 6 ++--- vcs_test.go | 67 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/vcs.go b/vcs.go index fa95578e..dc5d9204 100644 --- a/vcs.go +++ b/vcs.go @@ -38,14 +38,14 @@ func parseSource(source string) (url string, branch string, protocols []string) if !(strings.Contains(source, "git://") || strings.Contains(source, ".git") || strings.Contains(source, "git+https://")) { - return + return "", "", nil } split := strings.Split(source, "::") source = split[len(split)-1] split = strings.SplitN(source, "://", 2) if len(split) != 2 { - return + return "", "", nil } protocols = strings.Split(split[0], "+") @@ -55,7 +55,7 @@ func parseSource(source string) (url string, branch string, protocols []string) if secondSplit[0] != "branch" { //source has #commit= or #tag= which makes them not vcs //packages because they reference a specific point - return + return "", "", nil } if len(secondSplit) == 2 { diff --git a/vcs_test.go b/vcs_test.go index f6e96dd9..afe401be 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -4,29 +4,64 @@ import ( "testing" ) +func isEqual(a, b []string) bool { + + if a == nil && b == nil { + return true + } + + if a == nil || b == nil { + return false + } + + if len(a) != len(b) { + return false + } + + for i := range a { + if a[i] != b[i] { + return false + } + } + + return true +} + func TestParsing(t *testing.T) { type source struct { - sourceurl string - owner string - repo string + URL string + Branch string + Protocols []string } - neovim := source{sourceurl: "git+https://github.com/neovim/neovim.git"} - neovim.owner, neovim.repo = parseSource(neovim.sourceurl) - - if neovim.owner != "neovim" || neovim.repo != "neovim" { - t.Fatalf("Expected to find neovim/neovim, found %+v/%+v", neovim.owner, neovim.repo) + urls := []string{ + "git+https://github.com/neovim/neovim.git", + "git://github.com/jguer/yay.git#branch=master", + "git://github.com/davidgiven/ack", + "git://github.com/jguer/yay.git#tag=v3.440", + "git://github.com/jguer/yay.git#commit=e5470c88c6e2f9e0f97deb4728659ffa70ef5d0c", + "a+b+c+d+e+f://github.com/jguer/yay.git#branch=foo", } - yay := source{sourceurl: "git://github.com/jguer/yay.git#branch=master"} - yay.owner, yay.repo = parseSource(yay.sourceurl) - if yay.owner != "jguer" || yay.repo != "yay" { - t.Fatalf("Expected to find jguer/yay, found %+v/%+v", yay.owner, yay.repo) + sources := []source{ + {"github.com/neovim/neovim.git", "HEAD", []string{"git", "https"}}, + {"github.com/jguer/yay.git", "master", []string{"git"}}, + {"github.com/davidgiven/ack", "HEAD", []string{"git"}}, + {"", "", nil}, + {"", "", nil}, + {"github.com/jguer/yay.git", "foo", []string{"a", "b", "c", "d", "e", "f"}}, } - ack := source{sourceurl: "git://github.com/davidgiven/ack"} - ack.owner, ack.repo = parseSource(ack.sourceurl) - if ack.owner != "davidgiven" || ack.repo != "ack" { - t.Fatalf("Expected to find davidgiven/ack, found %+v/%+v", ack.owner, ack.repo) + for n, url := range urls { + url, branch, protocols := parseSource(url) + compare := sources[n] + + if url != compare.URL || + branch != compare.Branch || + !isEqual(protocols, compare.Protocols) { + + t.Fatalf("Expected url=%+v branch=%+v protocols=%+v\ngot url=%+v branch=%+v protocols=%+v", url, branch, protocols, compare.URL, compare.Branch, compare.Protocols) + } } + }