Skip to content

Commit

Permalink
Added basic tests for comment formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
melinath committed Mar 11, 2024
1 parent 3c361a6 commit 7bb79f1
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .ci/magician/cmd/DIFF_COMMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Your PR hasn't generated any diffs, but I'll let you know if a future commit doe
Your PR generated some diffs in downstreams - here they are.

{{range .Diffs -}}
{{.Repo.Title}}: [Diff](https://github.com/modular-magician/{{.Repo.Name}}/compare/auto-pr-{{$.PrNumber}}-old..auto-pr-{{$.PrNumber}}) ({{.DiffStats}})
{{.Title}}: [Diff](https://github.com/modular-magician/{{.Repo}}/compare/auto-pr-{{$.PrNumber}}-old..auto-pr-{{$.PrNumber}}) ({{.DiffStats}})
{{end -}}
{{end -}}

Expand All @@ -17,6 +17,9 @@ Your PR generated some diffs in downstreams - here they are.

The following breaking change(s) were detected within your pull request.

{{- range .BreakingChanges}}
- {{.}}{{end}}

If you believe this detection to be incorrect please raise the concern with your reviewer.
If you intend to make this change you will need to wait for a [major release](https://www.terraform.io/plugin/sdkv2/best-practices/versioning#example-major-number-increments) window.
An `override-breaking-change` label can be added to allow merging.
Expand Down
6 changes: 4 additions & 2 deletions .ci/magician/cmd/generate_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var (
)

type Diff struct {
Repo source.Repo
Title string
Repo string
DiffStats string
}

Expand Down Expand Up @@ -201,7 +202,8 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
}
if diffStats != "" {
diffs = append(diffs, Diff{
Repo: repo,
Title: repo.Title,
Repo: repo.Name,
DiffStats: diffStats,
})
}
Expand Down
115 changes: 115 additions & 0 deletions .ci/magician/cmd/generate_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"magician/source"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExecGenerateComment(t *testing.T) {
Expand Down Expand Up @@ -127,3 +129,116 @@ func TestExecGenerateComment(t *testing.T) {
}
}
}

func TestFormatDiffComment(t *testing.T) {
cases := map[string]struct{
data diffCommentData
expectedStrings []string
notExpectedStrings []string
}{
"basic message": {
data: diffCommentData{},
expectedStrings: []string{"## Diff report", "hasn't generated any diffs"},
notExpectedStrings: []string{
"generated some diffs",
"## Breaking Change(s) Detected",
"## Errors",
"## Missing test report",
},
},
"errors are displayed": {
data: diffCommentData{
Errors: []Errors{
{
Title: "Other",
Errors: []string{"Error 1", "Error 2"},
},
},
},
expectedStrings: []string{"## Diff report", "## Errors", "Other", "- Error 1", "- Error 2"},
notExpectedStrings: []string{
"generated some diffs",
"## Breaking Change(s) Detected",
"## Missing test report",
},
},
"diffs are displayed": {
data: diffCommentData{
PrNumber: 1234567890,
Diffs: []Diff{
{
Title: "Repo 1",
Repo: "repo-1",
DiffStats: "+1 added, -1 removed",
},
},
},
expectedStrings: []string{
"## Diff report",
"generated some diffs",
"Repo 1",
"[Diff](https://github.com/modular-magician/repo-1/compare/auto-pr-1234567890-old..auto-pr-1234567890)",
"+1 added, -1 removed",
},
notExpectedStrings: []string{
"hasn't generated any diffs",
"## Breaking Change(s) Detected",
"## Errors",
"## Missing test report",
},
},
"breaking changes are displayed": {
data: diffCommentData{
BreakingChanges: []string{
"Breaking change 1",
"Breaking change 2",
},
},
expectedStrings: []string{
"## Diff report",
"## Breaking Change(s) Detected",
"major release",
"`override-breaking-change`",
"- Breaking change 1",
"- Breaking change 2",
},
notExpectedStrings: []string{
"generated some diffs",
"## Errors",
"## Missing test report",
},
},
"missing tests are displayed": {
data: diffCommentData{
MissingTests: "## Missing test report",
},
expectedStrings: []string{
"## Diff report",
"## Missing test report",
},
notExpectedStrings: []string{
"generated some diffs",
"## Breaking Change(s) Detected",
"## Errors",
},
},
}

for tn, tc := range cases {
tc := tc
t.Run(tn, func(t *testing.T) {
t.Parallel()

comment, err := formatDiffComment(tc.data)
assert.Nil(t, err)

for _, s := range tc.expectedStrings {
assert.Contains(t, comment, s)
}

for _, s := range tc.notExpectedStrings {
assert.NotContains(t, comment, s)
}
})
}
}

0 comments on commit 7bb79f1

Please sign in to comment.