Skip to content

Commit

Permalink
Fix flaky test TestCommitStatusTargetURL (#39)
Browse files Browse the repository at this point in the history
The test added in [1] uses environment variables in a parallel test
which causes it to be flaky and sometimes fail as is described in [2].

To fix the flaky test, the environment variable is pulled out and read
in the caller of the function under test, so that it can be tested
without reading the global environment variable.

[1] a43d3d1
[2] https://pkg.go.dev/testing#T.Setenv
  • Loading branch information
hnnsgstfssn authored Oct 29, 2024
1 parent d34cacf commit e695d4c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
7 changes: 4 additions & 3 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,9 @@ func SetCommitStatus(ghPrClientDetails GhPrClientDetails, state string) {
tcontext := "telefonistka"
avatarURL := "https://avatars.githubusercontent.com/u/1616153?s=64"
description := "Telefonistka GitOps Bot"
targetURL := commitStatusTargetURL(time.Now())
tmplFile := os.Getenv("CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH")

targetURL := commitStatusTargetURL(time.Now(), tmplFile)

commitStatus := &github.RepoStatus{
TargetURL: &targetURL,
Expand Down Expand Up @@ -1273,10 +1275,9 @@ func GetFileContent(ghPrClientDetails GhPrClientDetails, branch string, filePath
// If the template file is not found or an error occurs during template execution,
// it returns a default URL.
// passed parameter commitTime can be used in the template as .CommitTime
func commitStatusTargetURL(commitTime time.Time) string {
func commitStatusTargetURL(commitTime time.Time, tmplFile string) string {
const targetURL string = "https://github.com/wayfair-incubator/telefonistka"

tmplFile := os.Getenv("CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH")
tmplName := filepath.Base(tmplFile)

// dynamic parameters to be used in the template
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,12 @@ func TestCommitStatusTargetURL(t *testing.T) {

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(), now.Add(-10*time.Minute).UnixMilli())
}
}

result := commitStatusTargetURL(now)
result := commitStatusTargetURL(now, tc.templateFile)
if result != expectedURL {
t.Errorf("%s: Expected URL to be %q, got %q", name, expectedURL, result)
}
Expand Down

0 comments on commit e695d4c

Please sign in to comment.