Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yzdann committed Oct 21, 2024
1 parent 94960ef commit 89e6edd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -289,3 +290,53 @@ func TestGhPrClientDetailsGetBlameURLPrefix(t *testing.T) {
assert.Equal(t, tc.ExpectURL, blameURLPrefix)
}
}

func TestCommitStatusTargetURL(t *testing.T) {
t.Parallel()

tests := map[string]struct {
expectedURL string
templateFile string
validTemplate bool
}{
"Default URL when no env var is set": {
expectedURL: "https://github.com/wayfair-incubator/telefonistka",
templateFile: "",
validTemplate: false,
},
"Custom URL from template": {
expectedURL: "https://custom-url.com?time=%d",
templateFile: "./testdata/custom_commit_status_valid_template.gotmpl",
validTemplate: true,
},
"Invalid template": {
expectedURL: "https://github.com/wayfair-incubator/telefonistka",
templateFile: "./testdata/custom_commit_status_invalid_template.gotmpl",
validTemplate: false,
},
}

for name, tc := range tests {
tc := tc
name := name
t.Run(name, func(t *testing.T) {
t.Parallel()
now := time.Now()

expectedURL := tc.expectedURL
if tc.templateFile != "" {
os.Setenv("CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH", tc.templateFile)
defer os.Unsetenv("CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH")

if tc.validTemplate {
expectedURL = fmt.Sprintf(expectedURL, now.UnixMilli())
}
}

result := commitStatusTargetURL(now)
if result != expectedURL {
t.Errorf("%s: Expected URL to be %q, got %q", name, expectedURL, result)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://custom-url.com?time={{.InvalidField}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://custom-url.com?time={{.CommitTime.UnixMilli}}

0 comments on commit 89e6edd

Please sign in to comment.