Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed May 16, 2024
1 parent 7443447 commit abfe1e6
Showing 1 changed file with 0 additions and 150 deletions.
150 changes: 0 additions & 150 deletions cli/pkg/reporting/reporting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,159 +2,9 @@ package reporting

import (
"fmt"
"testing"
"time"

"github.com/diggerhq/digger/cli/pkg/core/utils"
"github.com/diggerhq/digger/libs/orchestrator"

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

func TestCommentPerRunStrategyReport(t *testing.T) {
timeOfRun := time.Now()
strategy := CommentPerRunStrategy{
TimeOfRun: timeOfRun,
}
existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time", false)("")

prNumber := 1
ciService := &MockCiService{
CommentsPerPr: map[int][]*orchestrator.Comment{
prNumber: {
{
Id: 1,
Body: &existingCommentForOtherRun,
},
},
},
}

report := "resource \"null_resource\" \"test\" {}"
reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1", false)
err := strategy.Report(ciService, prNumber, report, reportFormatter, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report2 := "resource \"null_resource\" \"test\" {}"
reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2", false)
err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report3 := "resource \"null_resource\" \"test\" {}"
reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3", false)
err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report4 := "resource \"null_resource\" \"test\" {}"
reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4", false)
err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true)

if err != nil {
t.Errorf("Error: %v", err)
}

assert.Equal(t, 2, len(ciService.CommentsPerPr[prNumber]))
assert.Equal(t, "<details><summary>Digger run report at "+timeOfRun.Format("2006-01-02 15:04:05 (MST)")+"</summary>\n <details><summary>run1</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n<details><summary>run2</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run3</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run4</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n</details>", *ciService.CommentsPerPr[prNumber][1].Body)
}

func TestLatestCommentStrategyReport(t *testing.T) {
timeOfRun := time.Now()
strategy := LatestRunCommentStrategy{
TimeOfRun: timeOfRun,
}
existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time", false)("")

prNumber := 1
ciService := &MockCiService{
CommentsPerPr: map[int][]*orchestrator.Comment{
prNumber: {
{
Id: 1,
Body: &existingCommentForOtherRun,
},
},
},
}

report := "resource \"null_resource\" \"test\" {}"
reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1", false)
err := strategy.Report(ciService, prNumber, report, reportFormatter, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report2 := "resource \"null_resource\" \"test\" {}"
reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2", false)
err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report3 := "resource \"null_resource\" \"test\" {}"
reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3", false)
err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report4 := "resource \"null_resource\" \"test\" {}"
reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4", false)
err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true)

if err != nil {
t.Errorf("Error: %v", err)
}

assert.Equal(t, 2, len(ciService.CommentsPerPr[prNumber]))
assert.Equal(t, "<details><summary>Digger latest run report</summary>\n <details><summary>run1</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n<details><summary>run2</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run3</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run4</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n</details>", *ciService.CommentsPerPr[prNumber][1].Body)
}

func TestMultipleCommentStrategyReport(t *testing.T) {
strategy := MultipleCommentsStrategy{}
existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time", false)("")

prNumber := 1
ciService := &MockCiService{
CommentsPerPr: map[int][]*orchestrator.Comment{
prNumber: {
{
Id: 1,
Body: &existingCommentForOtherRun,
},
},
},
}

report := "resource \"null_resource\" \"test\" {}"
reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1", false)
err := strategy.Report(ciService, prNumber, report, reportFormatter, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report2 := "resource \"null_resource\" \"test\" {}"
reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2", false)
err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report3 := "resource \"null_resource\" \"test\" {}"
reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3", false)
err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true)
if err != nil {
t.Errorf("Error: %v", err)
}
report4 := "resource \"null_resource\" \"test\" {}"
reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4", false)
err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true)

if err != nil {
t.Errorf("Error: %v", err)
}

assert.Equal(t, 5, len(ciService.CommentsPerPr[prNumber]))
assert.Equal(t, "<details><summary>run4</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>", *ciService.CommentsPerPr[prNumber][4].Body)
}

type MockCiService struct {
CommentsPerPr map[int][]*orchestrator.Comment
}
Expand Down

0 comments on commit abfe1e6

Please sign in to comment.