nodece commented on code in PR #69:
URL: https://github.com/apache/pulsar-test-infra/pull/69#discussion_r962760646


##########
docbot/action_test.go:
##########
@@ -0,0 +1,326 @@
+package main
+
+import (
+       "context"
+       "fmt"
+       "os"
+       "strings"
+       "testing"
+
+       "github.com/google/go-github/v45/github"
+       "github.com/migueleliasweb/go-github-mock/src/mock"
+)
+
+func repoLabels() []*github.Label {
+       labels := []string{"doc-required", "doc-not-needed", "doc", 
"doc-complete", "doc-label-missing"}
+
+       result := make([]*github.Label, 0)
+       for _, label := range labels {
+               name := label
+               result = append(result, &github.Label{Name: &name})
+       }
+
+       return result
+}
+
+func mustNewActionConfig() *ActionConfig {
+       _ = os.Setenv("GITHUB_REPOSITORY", "apache/pulsar")
+       _ = os.Setenv("LABEL_WATCH_LIST", 
"doc,doc-required,doc-not-needed,doc-complete")
+       _ = os.Setenv("LABEL_MISSING", "doc-label-missing")
+
+       config, err := NewActionConfig()
+       if err != nil {
+               panic(err)
+       }
+
+       return config
+}
+
+func assertMessageLabel(t *testing.T, err error, message string) {
+       t.Helper()
+
+       if err == nil {
+               t.Fatal("Expect err not nil")
+       }
+
+       if err.Error() != message {
+               t.Fatal("Expect err equals " + message)
+       }
+}
+
+func TestSingleChecked(t *testing.T) {
+       id := int64(1)
+       body := fmt.Sprintf(`
+Check the box below or label this PR directly.
+
+Need to update docs?
+
+- [ ] %s
+(Your PR needs to update docs and you will update later)
+
+- [x] %s
+(Please explain why)
+
+- [ ] %s
+(Your PR contains doc changes)
+
+- [ ] %s
+(Docs have been already added)
+`, "`doc-required`", "`doc-not-needed`", "`doc`", "`doc-complete`")
+
+       mockedHTTPClient := mock.NewMockedHTTPClient(
+               mock.WithRequestMatch(
+                       mock.GetReposPullsByOwnerByRepoByPullNumber,
+                       github.PullRequest{
+                               ID:     &id,
+                               Body:   &body,
+                               Labels: nil,
+                       },
+               ), mock.WithRequestMatch(
+                       mock.GetReposLabelsByOwnerByRepo,
+                       repoLabels(),
+               ),
+               
mock.WithRequestMatch(mock.PostReposIssuesLabelsByOwnerByRepoByIssueNumber, 
nil),
+       )
+
+       config := mustNewActionConfig()
+       action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
+
+       err := action.Run(1, "opened")
+       if err != nil {
+               t.Fatal(err)
+       }
+}
+
+func TestMultipleChecked(t *testing.T) {
+       id := int64(1)
+       body := fmt.Sprintf(`
+Check the box below or label this PR directly.
+
+Need to update docs?
+
+- [ ] %s
+(Your PR needs to update docs and you will update later)
+
+- [x] %s
+(Please explain why)
+
+- [x] %s
+(Your PR contains doc changes)
+
+- [ ] %s
+(Docs have been already added)
+`, "`doc-required`", "`doc-not-needed`", "`doc`", "`doc-complete`")
+
+       mockedHTTPClient := mock.NewMockedHTTPClient(
+               mock.WithRequestMatch(
+                       mock.GetReposPullsByOwnerByRepoByPullNumber,
+                       github.PullRequest{
+                               ID:     &id,
+                               Body:   &body,
+                               Labels: nil,
+                       },
+               ), mock.WithRequestMatch(
+                       mock.GetReposLabelsByOwnerByRepo,
+                       repoLabels(),
+               ),
+               
mock.WithRequestMatch(mock.PostReposIssuesLabelsByOwnerByRepoByIssueNumber, 
nil),
+       )
+
+       const key = "ENABLE_LABEL_MULTIPLE"
+       value := os.Getenv(key)
+       defer func() {
+               // reset
+               _ = os.Setenv(key, value)
+       }()
+       _ = os.Setenv("ENABLE_LABEL_MULTIPLE", "true")
+
+       config := mustNewActionConfig()
+       action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
+
+       err := action.Run(1, "opened")
+       if err != nil {
+               t.Fatal(err)
+       }
+}
+
+func TestUnchecked(t *testing.T) {
+       id := int64(1)
+       body := fmt.Sprintf(`
+Check the box below or label this PR directly.
+
+Need to update docs?
+
+- [ ] %s
+(Your PR needs to update docs and you will update later)
+
+- [ ] %s
+(Please explain why)
+
+- [ ] %s
+(Your PR contains doc changes)
+
+- [ ] %s
+(Docs have been already added)
+`, "`doc-required`", "`doc-not-needed`", "`doc`", "`doc-complete`")
+
+       mockedHTTPClient := mock.NewMockedHTTPClient(
+               mock.WithRequestMatch(
+                       mock.GetReposPullsByOwnerByRepoByPullNumber,
+                       github.PullRequest{
+                               ID:     &id,
+                               Body:   &body,
+                               Labels: nil,
+                       },
+               ), mock.WithRequestMatch(
+                       mock.GetReposLabelsByOwnerByRepo,
+                       repoLabels(),
+               ),
+               
mock.WithRequestMatch(mock.PostReposIssuesLabelsByOwnerByRepoByIssueNumber, 
nil),
+               
mock.WithRequestMatch(mock.GetReposIssuesCommentsByOwnerByRepoByIssueNumber, 
nil),
+               
mock.WithRequestMatch(mock.PostReposIssuesCommentsByOwnerByRepoByIssueNumber, 
nil),
+       )
+
+       config := mustNewActionConfig()
+       action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
+
+       err := action.Run(1, "opened")
+       assertMessageLabel(t, err, MessageLabelMissing)
+}
+
+func TestMultipleChecked_WhenMultipleLabelsNotEnabled(t *testing.T) {
+       id := int64(1)
+       body := fmt.Sprintf(`
+Check the box below or label this PR directly.
+
+Need to update docs?
+
+- [ ] %s
+(Your PR needs to update docs and you will update later)
+
+- [x] %s
+(Please explain why)
+
+- [x] %s
+(Your PR contains doc changes)
+
+- [ ] %s
+(Docs have been already added)
+`, "`doc-required`", "`doc-not-needed`", "`doc`", "`doc-complete`")
+
+       mockedHTTPClient := mock.NewMockedHTTPClient(
+               mock.WithRequestMatch(
+                       mock.GetReposPullsByOwnerByRepoByPullNumber,
+                       github.PullRequest{
+                               ID:     &id,
+                               Body:   &body,
+                               Labels: nil,
+                       },
+               ), mock.WithRequestMatch(
+                       mock.GetReposLabelsByOwnerByRepo,
+                       repoLabels(),
+               ),
+               
mock.WithRequestMatch(mock.PostReposIssuesLabelsByOwnerByRepoByIssueNumber, 
nil),
+               
mock.WithRequestMatch(mock.GetReposIssuesCommentsByOwnerByRepoByIssueNumber, 
nil),
+               
mock.WithRequestMatch(mock.PostReposIssuesCommentsByOwnerByRepoByIssueNumber, 
nil),
+       )
+
+       config := mustNewActionConfig()
+       action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
+
+       err := action.Run(1, "opened")
+       assertMessageLabel(t, err, MessageLabelMultiple)
+}
+
+func TestSingleChecked_WhenLabelMissingExist(t *testing.T) {
+       id := int64(1)
+       body := fmt.Sprintf(`
+Check the box below or label this PR directly.
+
+Need to update docs?
+
+- [ ] %s
+(Your PR needs to update docs and you will update later)
+
+- [x] %s
+(Please explain why)
+
+- [ ] %s
+(Your PR contains doc changes)
+
+- [ ] %s
+(Docs have been already added)
+`, "`doc-required`", "`doc-not-needed`", "`doc`", "`doc-complete`")
+
+       labelMissing := "doc-label-missing"
+       mockedHTTPClient := mock.NewMockedHTTPClient(
+               mock.WithRequestMatch(
+                       mock.GetReposPullsByOwnerByRepoByPullNumber,
+                       github.PullRequest{
+                               ID:     &id,
+                               Body:   &body,
+                               Labels: []*github.Label{{Name: &labelMissing}},
+                       },
+               ), mock.WithRequestMatch(
+                       mock.GetReposLabelsByOwnerByRepo,
+                       repoLabels(),
+               ),
+               
mock.WithRequestMatch(mock.PostReposIssuesLabelsByOwnerByRepoByIssueNumber, 
nil),
+               
mock.WithRequestMatch(mock.DeleteReposIssuesLabelsByOwnerByRepoByIssueNumberByName,
 nil),
+       )
+
+       config := mustNewActionConfig()
+       action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
+
+       err := action.Run(1, "opened")
+       if err != nil {
+               t.Fatal(err)
+       }
+}
+
+func TestUnchecked_WhenLabelMissingExist(t *testing.T) {
+       id := int64(1)
+       body := fmt.Sprintf(`
+Check the box below or label this PR directly.
+
+Need to update docs?
+
+- [ ] %s
+(Your PR needs to update docs and you will update later)
+
+- [ ] %s
+(Please explain why)
+
+- [ ] %s
+(Your PR contains doc changes)
+
+- [ ] %s
+(Docs have been already added)
+`, "`doc-required`", "`doc-not-needed`", "`doc`", "`doc-complete`")
+
+       labelMissing := "doc-label-missing"
+       mockedHTTPClient := mock.NewMockedHTTPClient(
+               mock.WithRequestMatch(
+                       mock.GetReposPullsByOwnerByRepoByPullNumber,
+                       github.PullRequest{
+                               ID:     &id,
+                               Body:   &body,
+                               Labels: []*github.Label{{Name: &labelMissing}},
+                       },
+               ), mock.WithRequestMatch(
+                       mock.GetReposLabelsByOwnerByRepo,
+                       repoLabels(),
+               ),
+       )
+
+       config := mustNewActionConfig()
+       action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
+
+       err := action.Run(1, "opened")
+       assertMessageLabel(t, err, MessageLabelMissing)
+}
+
+func TestName(t *testing.T) {
+       split := strings.Split("docs, docs-required,", ",")
+       t.Log(split)
+}

Review Comment:
   Removed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to