Repository: preslavmihaylov/todocheck
Pull request #125 (2021-01-03 20:51:36)

add unit tests for github, gitlab, pivotaltracker


Patch:
diff --git a/Makefile b/Makefile
index 9a23e8c..368cc33 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ build:
 	go build -ldflags "-X main.version=$(version)"
 
 test: build
-	go test -v -count=1 ./testing
+	go test -v -count=1 ./...
 
 release:
 	@echo "Generating binaries for version $(version)..."
diff --git a/issuetracker/github/github.go b/issuetracker/github/github.go
index 2c8705a..3fd5e1f 100644
--- a/issuetracker/github/github.go
+++ b/issuetracker/github/github.go
@@ -34,7 +34,7 @@ func (it *IssueTracker) taskURLFrom(taskID string) string {
 
 // IssueAPIOrigin returns the URL for github's issue-fetching API
 func (it *IssueTracker) issueAPIOrigin() string {
-	tokens := common.RemoveEmptyTokens(strings.Split(it.Origin, "/"))
+	tokens := common.RemoveEmptyTokens(strings.Split(strings.ToLower(it.Origin), "/"))
 	if !strings.HasPrefix(tokens[0], "http") {
 		tokens = append([]string{"https:"}, tokens...)
 	}
diff --git a/issuetracker/gitlab/gitlab.go b/issuetracker/gitlab/gitlab.go
index a4a0012..b6c55e6 100644
--- a/issuetracker/gitlab/gitlab.go
+++ b/issuetracker/gitlab/gitlab.go
@@ -35,7 +35,7 @@ func (it *IssueTracker) taskURLFrom(taskID string) string {
 
 // IssueAPIOrigin returns the URL for github's issue-fetching API
 func (it *IssueTracker) issueAPIOrigin() string {
-	tokens := common.RemoveEmptyTokens(strings.Split(it.Origin, "/"))
+	tokens := common.RemoveEmptyTokens(strings.Split(strings.ToLower(it.Origin), "/"))
 	if !strings.HasPrefix(tokens[0], "http:") && !strings.HasPrefix(tokens[0], "https:") {
 		tokens = append([]string{"https:"}, tokens...)
 	}
diff --git a/issuetracker/pivotaltracker/pivotaltracker.go b/issuetracker/pivotaltracker/pivotaltracker.go
index 8ef115b..7541939 100644
--- a/issuetracker/pivotaltracker/pivotaltracker.go
+++ b/issuetracker/pivotaltracker/pivotaltracker.go
@@ -34,7 +34,7 @@ func (it *IssueTracker) taskURLFrom(taskID string) string {
 
 // IssueAPIOrigin returns the URL for pivotaltracker's issue-fetching API
 func (it *IssueTracker) issueAPIOrigin() string {
-	tokens := common.RemoveEmptyTokens(strings.Split(it.Origin, "/"))
+	tokens := common.RemoveEmptyTokens(strings.Split(strings.ToLower(it.Origin), "/"))
 	if tokens[0] == "pivotaltracker.com" {
 		tokens = append([]string{"https:"}, tokens...)
 	}


Test patch:
diff --git a/issuetracker/github/github_test.go b/issuetracker/github/github_test.go
new file mode 100644
index 0000000..4f6dae2
--- /dev/null
+++ b/issuetracker/github/github_test.go
@@ -0,0 +1,37 @@
+package github
+
+import (
+	"fmt"
+	"testing"
+)
+
+func Test_IssueTracker_IssueURLFor(t *testing.T) {
+
+	var tests = []struct {
+		input  string
+		taskID string
+		want   string
+	}{
+		{"https://github.com/preslavmihaylov1/todocheck", "1", "https://api.github.com/repos/preslavmihaylov1/todocheck/issues/1"},
+		{"github.com/preslavmihaylov/todocheck/", "8", "https://api.github.com/repos/preslavmihaylov/todocheck/issues/8"},
+		{"github.com/uSER-1989/todocheck/", "8", "https://api.github.com/repos/user-1989/todocheck/issues/8"},
+		{"github.com/user2020/hyphen-1/", "8", "https://api.github.com/repos/user2020/hyphen-1/issues/8"},
+		{"github.com/user2020/hyphen-1_underscore/", "8", "https://api.github.com/repos/user2020/hyphen-1_underscore/issues/8"},
+		{"GITHUB.com/u1u/hyphen-1_underscore_x-2/", "8", "https://api.github.com/repos/u1u/hyphen-1_underscore_x-2/issues/8"},
+	}
+
+	for _, tt := range tests {
+
+		var it IssueTracker
+
+		testname := fmt.Sprintf("%q", tt.input)
+		t.Run(testname, func(t *testing.T) {
+			it.Origin = tt.input
+			res := it.IssueURLFor(tt.taskID)
+			if res != tt.want {
+				t.Errorf("got %s, want %s", res, tt.want)
+			}
+		})
+	}
+
+}
diff --git a/issuetracker/gitlab/gitlab_test.go b/issuetracker/gitlab/gitlab_test.go
new file mode 100644
index 0000000..f54a7a2
--- /dev/null
+++ b/issuetracker/gitlab/gitlab_test.go
@@ -0,0 +1,41 @@
+package gitlab
+
+import (
+	"fmt"
+	"testing"
+)
+
+func Test_IssueTracker_IssueURLFor(t *testing.T) {
+
+	var tests = []struct {
+		input  string
+		taskID string
+		want   string
+	}{
+		{"gitlab.com/PRESLAVmihaylov/project", "1", "https://gitlab.com/api/v4/projects/preslavmihaylov%2Fproject/issues/1"},
+		{"gitlab.com/user-5/project8-9_1", "1111", "https://gitlab.com/api/v4/projects/user-5%2Fproject8-9_1/issues/1111"},
+		{"https://gitLAB.com/user-1/project", "2020", "https://gitlab.com/api/v4/projects/user-1%2Fproject/issues/2020"},
+		{"https://gitlab.com/u/project9-1_2-3-4", "2020", "https://gitlab.com/api/v4/projects/u%2Fproject9-1_2-3-4/issues/2020"},
+		{"gitlab.myorg.com/PRESLAVmihaylov/project", "1", "https://gitlab.myorg.com/api/v4/projects/preslavmihaylov%2Fproject/issues/1"},
+		{"gitlab.myorg.com/user-5/project8-9_1", "1111", "https://gitlab.myorg.com/api/v4/projects/user-5%2Fproject8-9_1/issues/1111"},
+		{"https://gitLAB.myorg.com/user-1/project", "2020", "https://gitlab.myorg.com/api/v4/projects/user-1%2Fproject/issues/2020"},
+		{"https://gitlab.myORG.com/u/project9-1_2-3-4", "2020", "https://gitlab.myorg.com/api/v4/projects/u%2Fproject9-1_2-3-4/issues/2020"},
+		{"myorg.com/PRESLAVmihaylov/project", "20201225", "https://myorg.com/api/v4/projects/preslavmihaylov%2Fproject/issues/20201225"},
+		{"myorg.co.uk/PreslavMihaylov/project", "20201226", "https://myorg.co.uk/api/v4/projects/preslavmihaylov%2Fproject/issues/20201226"},
+	}
+
+	for _, tt := range tests {
+
+		var it IssueTracker
+
+		testname := fmt.Sprintf("%q", tt.input)
+		t.Run(testname, func(t *testing.T) {
+			it.Origin = tt.input
+			res := it.IssueURLFor(tt.taskID)
+			if res != tt.want {
+				t.Errorf("got %s, want %s", res, tt.want)
+			}
+		})
+	}
+
+}
diff --git a/issuetracker/pivotaltracker/pivotaltracker_test.go b/issuetracker/pivotaltracker/pivotaltracker_test.go
new file mode 100644
index 0000000..24d572c
--- /dev/null
+++ b/issuetracker/pivotaltracker/pivotaltracker_test.go
@@ -0,0 +1,35 @@
+package pivotaltracker
+
+import (
+	"fmt"
+	"testing"
+)
+
+func Test_IssueTracker_IssueURLFor(t *testing.T) {
+	var tests = []struct {
+		input  string
+		taskID string
+		want   string
+	}{
+		{"https://pivotaltracker.com/n/projects/1", "2020", "https://www.pivotaltracker.com/services/v5/projects/1/stories/2020"},
+		{"pivotaltracker.com/n/projects/1-2_3", "2020", "https://www.pivotaltracker.com/services/v5/projects/1-2_3/stories/2020"},
+		{"pIVOTALtracker.com/projects/1-2_3", "2020", "https://www.pivotaltracker.com/services/v5/projects/1-2_3/stories/2020"},
+		{"httPS://pivotaltracker.com/projects/1-2_3", "2020", "https://www.pivotaltracker.com/services/v5/projects/1-2_3/stories/2020"},
+		{"https://www.pivotaltracker.com/n/projects/2459511", "21", "https://www.pivotaltracker.com/services/v5/projects/2459511/stories/21"},
+	}
+
+	for _, tt := range tests {
+
+		var it IssueTracker
+
+		testname := fmt.Sprintf("%q", tt.input)
+		t.Run(testname, func(t *testing.T) {
+			it.Origin = tt.input
+			res := it.IssueURLFor(tt.taskID)
+			if res != tt.want {
+				t.Errorf("got %s, want %s", res, tt.want)
+			}
+		})
+	}
+
+}