Commit 41fe3a6

mo khan <mo@mokhan.ca>
2024-05-18 18:13:45
test: start to add unit tests
1 parent cf1abd6
cmd/stanuki/main.go
@@ -9,16 +9,9 @@ import (
 	"github.com/xlgmokha/x/pkg/env"
 	"github.com/xlgmokha/x/pkg/serde"
 	"github.com/xlgmokha/x/pkg/x"
+	"gitlab.com/mokhax/stanuki/pkg/gitlab"
 )
 
-type Issue struct {
-	ID          int    `json:"id"`
-	IID         int    `json:"iid"`
-	ProjectID   int    `json:"project_id"`
-	Title       string `json:"title"`
-	Description string `json:"description"`
-}
-
 func main() {
 	token := env.Fetch("GITLAB_TOKEN", "")
 	url := "https://gitlab.com/api/v4/groups/9970/issues"
@@ -29,7 +22,7 @@ func main() {
 	if env.Fetch("DUMP", "") != "" {
 		fmt.Println(string(x.Must(io.ReadAll(response.Body))))
 	} else {
-		issues := x.Must(serde.From[[]Issue](response.Body, serde.JSON))
+		issues := x.Must(serde.From[[]gitlab.Issue](response.Body, serde.JSON))
 		for _, issue := range issues {
 			fmt.Printf("%v: %v\n", issue.ID, issue.Title)
 		}
pkg/gitlab/issue.go
@@ -0,0 +1,9 @@
+package gitlab
+
+type Issue struct {
+	ID          int    `json:"id"`
+	IID         int    `json:"iid"`
+	ProjectID   int    `json:"project_id"`
+	Title       string `json:"title"`
+	Description string `json:"description"`
+}
pkg/gitlab/issue_test.go
@@ -0,0 +1,15 @@
+package gitlab
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestIssue(t *testing.T) {
+	t.Run("ParseIssues", func(t *testing.T) {
+		t.Run("parses the array of issues from IO", func(t *testing.T) {
+			assert.Equal(t, false, true)
+		})
+	})
+}
.gitlab-ci.yml
@@ -2,6 +2,7 @@ image: golang:alpine
 
 stages:
   - build
+  - test
 
 go_build:
   stage: build
@@ -13,3 +14,10 @@ go_build:
     - apk add --no-cache git
   script:
     - go build -o stanuki cmd/stanuki/main.go
+
+go_test:
+  stage: test
+  before_script:
+    - apk add --no-cache git
+  script:
+    - go test ./...
go.mod
@@ -2,6 +2,14 @@ module gitlab.com/mokhax/stanuki
 
 go 1.22
 
-require github.com/xlgmokha/x v0.0.0-20221023040112-0610463739d1
+require (
+	github.com/stretchr/testify v1.8.0
+	github.com/xlgmokha/x v0.0.0-20221023040112-0610463739d1
+)
 
-require github.com/google/jsonapi v1.0.0 // indirect
+require (
+	github.com/davecgh/go-spew v1.1.1 // indirect
+	github.com/google/jsonapi v1.0.0 // indirect
+	github.com/pmezard/go-difflib v1.0.0 // indirect
+	gopkg.in/yaml.v3 v3.0.1 // indirect
+)
go.sum
@@ -1,12 +1,19 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/google/jsonapi v1.0.0 h1:qIGgO5Smu3yJmSs+QlvhQnrscdZfFhiV6S8ryJAglqU=
 github.com/google/jsonapi v1.0.0/go.mod h1:YYHiRPJT8ARXGER8In9VuLv4qvLfDmA9ULQqptbLE4s=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
 github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
 github.com/xlgmokha/x v0.0.0-20221023040112-0610463739d1 h1:MIqcDqEAcr16QUq/kFJC3xbYoaAjjsxYEJLGtol2U58=
 github.com/xlgmokha/x v0.0.0-20221023040112-0610463739d1/go.mod h1:Hf/0a9FveTBuVKvZ+Emmw6BlJyLGlhgjQwa4dFXp48s=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=