From 89e6edd69f5df4d6d8301d81a818aac1ca6a5cc6 Mon Sep 17 00:00:00 2001 From: Yazdan Mohammadi Date: Mon, 21 Oct 2024 14:59:58 +0200 Subject: [PATCH] Add tests --- internal/pkg/githubapi/github_test.go | 51 +++++++++++++++++++ ...stom_commit_status_invalid_template.gotmpl | 1 + ...custom_commit_status_valid_template.gotmpl | 1 + 3 files changed, 53 insertions(+) create mode 100644 internal/pkg/githubapi/testdata/custom_commit_status_invalid_template.gotmpl create mode 100644 internal/pkg/githubapi/testdata/custom_commit_status_valid_template.gotmpl diff --git a/internal/pkg/githubapi/github_test.go b/internal/pkg/githubapi/github_test.go index 3814e2b8..e33cfd0b 100644 --- a/internal/pkg/githubapi/github_test.go +++ b/internal/pkg/githubapi/github_test.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -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) + } + }) + } +} diff --git a/internal/pkg/githubapi/testdata/custom_commit_status_invalid_template.gotmpl b/internal/pkg/githubapi/testdata/custom_commit_status_invalid_template.gotmpl new file mode 100644 index 00000000..32e4e312 --- /dev/null +++ b/internal/pkg/githubapi/testdata/custom_commit_status_invalid_template.gotmpl @@ -0,0 +1 @@ +https://custom-url.com?time={{.InvalidField}} diff --git a/internal/pkg/githubapi/testdata/custom_commit_status_valid_template.gotmpl b/internal/pkg/githubapi/testdata/custom_commit_status_valid_template.gotmpl new file mode 100644 index 00000000..f0d4c9a0 --- /dev/null +++ b/internal/pkg/githubapi/testdata/custom_commit_status_valid_template.gotmpl @@ -0,0 +1 @@ +https://custom-url.com?time={{.CommitTime.UnixMilli}}