From 229d843d30255bc3f769e6a0416da386c9cb748a Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Fri, 4 Sep 2020 11:55:37 -0400 Subject: [PATCH] fix: Add a test for PullRequest.ListChanges in Gitea Signed-off-by: Andrew Bayer --- scm/driver/gitea/pr_test.go | 27 ++++++++++++++--- .../gitea/testdata/pr_changes.json.golden | 13 ++++++++ scm/driver/gitea/testdata/pr_changes.patch | 30 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 scm/driver/gitea/testdata/pr_changes.json.golden create mode 100644 scm/driver/gitea/testdata/pr_changes.patch diff --git a/scm/driver/gitea/pr_test.go b/scm/driver/gitea/pr_test.go index ad52a3310..8ff70e54d 100644 --- a/scm/driver/gitea/pr_test.go +++ b/scm/driver/gitea/pr_test.go @@ -7,6 +7,7 @@ package gitea import ( "context" "encoding/json" + "github.com/stretchr/testify/assert" "io/ioutil" "testing" @@ -122,12 +123,30 @@ func TestPullRequestMerge(t *testing.T) { // pull request change sub-tests // -// TODO: Actually write a real test for generating changes from a patch func TestPullRequestChanges(t *testing.T) { + defer gock.Off() + + gock.New("https://try.gitea.io"). + Get("/api/v1/repos/go-gitea/gitea/pulls/1.patch"). + Reply(204). + Type("text/plain"). + File("testdata/pr_changes.patch") + + client, _ := New("https://try.gitea.io") - _, _, err := client.PullRequests.ListChanges(context.Background(), "go-gitea/gitea", 1, scm.ListOptions{}) - if err == scm.ErrNotSupported { - t.Errorf("Didn't expect Not Supported error") + got, _, err := client.PullRequests.ListChanges(context.Background(), "go-gitea/gitea", 1, scm.ListOptions{}) + if err != nil { + t.Error(err) + } + + want := []*scm.Change{} + raw, _ := ioutil.ReadFile("testdata/pr_changes.json.golden") + err = json.Unmarshal(raw, &want) + assert.NoError(t, err) + + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("Unexpected Results") + t.Log(diff) } } diff --git a/scm/driver/gitea/testdata/pr_changes.json.golden b/scm/driver/gitea/testdata/pr_changes.json.golden new file mode 100644 index 000000000..31290047c --- /dev/null +++ b/scm/driver/gitea/testdata/pr_changes.json.golden @@ -0,0 +1,13 @@ +[ + { + "Path": "README", + "Added": true, + "Additions": 1 + }, + { + "Path": "script.sh", + "PreviousPath": "script.sh", + "Additions": 1, + "Deletions": 1 + } +] diff --git a/scm/driver/gitea/testdata/pr_changes.patch b/scm/driver/gitea/testdata/pr_changes.patch new file mode 100644 index 000000000..a6b6c03e4 --- /dev/null +++ b/scm/driver/gitea/testdata/pr_changes.patch @@ -0,0 +1,30 @@ +From 3c716f1eb086c9039745e3cadf873c9cbbb481e5 Mon Sep 17 00:00:00 2001 +From: jenkins-x-bot +Date: Fri, 4 Sep 2020 15:16:56 +0000 +Subject: [PATCH] Adding for test PR + +--- + README | 1 + + script.sh | 2 +- + 2 files changed, 2 insertions(+), 1 deletion(-) + create mode 100644 README + +diff --git a/README b/README +new file mode 100644 +index 0000000..70c379b +--- /dev/null ++++ b/README +@@ -0,0 +1 @@ ++Hello world +\ No newline at end of file +diff --git a/script.sh b/script.sh +index 4915bcb..2c42f17 100644 +--- a/script.sh ++++ b/script.sh +@@ -1,3 +1,3 @@ + #!/bin/bash + +-echo "This passes" ++echo "This has changed but still passes" +-- +2.24.3