| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 package gitiles |
| 16 |
| 17 import ( |
| 18 "fmt" |
| 19 "net/http" |
| 20 "net/http/httptest" |
| 21 "testing" |
| 22 |
| 23 "golang.org/x/net/context" |
| 24 |
| 25 . "github.com/smartystreets/goconvey/convey" |
| 26 ) |
| 27 |
| 28 func TestRepoURL(t *testing.T) { |
| 29 t.Parallel() |
| 30 Convey("Malformed", t, func() { |
| 31 f := func(arg string) { |
| 32 So(ValidateRepoURL(arg), ShouldNotBeNil) |
| 33 _, err := NormalizeRepoURL(arg) |
| 34 So(err, ShouldNotBeNil) |
| 35 } |
| 36 |
| 37 f("wtf/\\is\this") |
| 38 f("https://example.com/repo.git") |
| 39 f("http://bad-protocol.googlesource.com/repo.git") |
| 40 f("https://a.googlesource.com") |
| 41 f("https://a.googlesource.com/") |
| 42 f("a.googlesource.com/no-protocol.git") |
| 43 f("https://a.googlesource.com/no-protocol#fragment") |
| 44 }) |
| 45 |
| 46 Convey("OK", t, func() { |
| 47 f := func(arg, exp string) { |
| 48 So(ValidateRepoURL(arg), ShouldBeNil) |
| 49 act, err := NormalizeRepoURL(arg) |
| 50 So(err, ShouldBeNil) |
| 51 So(act, ShouldEqual, exp) |
| 52 } |
| 53 |
| 54 f("https://chromium.googlesource.com/repo.git", |
| 55 "https://chromium.googlesource.com/a/repo") |
| 56 f("https://chromium.googlesource.com/repo/", |
| 57 "https://chromium.googlesource.com/a/repo") |
| 58 f("https://chromium.googlesource.com/a/repo", |
| 59 "https://chromium.googlesource.com/a/repo") |
| 60 f("https://chromium.googlesource.com/parent/repo.git/", |
| 61 "https://chromium.googlesource.com/a/parent/repo") |
| 62 }) |
| 63 } |
| 64 |
| 65 func TestNew(t *testing.T) { |
| 66 Convey("New with bad url", t, func() { |
| 67 _, err := New(nil, "bad://repo.url") |
| 68 So(err, ShouldNotBeNil) |
| 69 }) |
| 70 Convey("New with good url", t, func() { |
| 71 c, err := New(nil, "https://chromium.googlesource.com/repo.git") |
| 72 So(err, ShouldBeNil) |
| 73 So(c.RepoURL, ShouldEqual, "https://chromium.googlesource.com/a/
repo") |
| 74 }) |
| 75 } |
| 76 |
| 77 func TestLog(t *testing.T) { |
| 78 t.Parallel() |
| 79 ctx := context.Background() |
| 80 |
| 81 Convey("Log w/o pages", t, func() { |
| 82 srv, c := newMockClient(func(w http.ResponseWriter, r *http.Requ
est) { |
| 83 w.WriteHeader(200) |
| 84 w.Header().Set("Content-Type", "application/json") |
| 85 fmt.Fprintf(w, ")]}'\n{\"log\": [%s, %s]}\n", fake_commi
t1_str, fake_commit2_str) |
| 86 }) |
| 87 defer srv.Close() |
| 88 |
| 89 Convey("Return All", func() { |
| 90 commits, err := c.Log(ctx, "master..8de6836858c99e48f3c5
8164ab717bda728e95dd", 10) |
| 91 So(err, ShouldBeNil) |
| 92 So(len(commits), ShouldEqual, 2) |
| 93 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 94 So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfa
ab61c4d30a35adce5f40") |
| 95 }) |
| 96 |
| 97 Convey("DO not exceed limit", func() { |
| 98 commits, err := c.Log(ctx, "master..8de6836858c99e48f3c5
8164ab717bda728e95dd", 1) |
| 99 So(err, ShouldBeNil) |
| 100 So(len(commits), ShouldEqual, 1) |
| 101 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 102 }) |
| 103 }) |
| 104 |
| 105 Convey("Log Paging", t, func() { |
| 106 cursor_sent := "" |
| 107 srv, c := newMockClient(func(w http.ResponseWriter, r *http.Requ
est) { |
| 108 w.WriteHeader(200) |
| 109 w.Header().Set("Content-Type", "application/json") |
| 110 if next := r.URL.Query().Get("s"); next == "" { |
| 111 fmt.Fprintf(w, ")]}'\n{\"log\": [%s], \"next\":
\"next_cursor_value\"}\n", fake_commit1_str) |
| 112 } else { |
| 113 cursor_sent = next |
| 114 fmt.Fprintf(w, ")]}'\n{\"log\": [%s]}\n", fake_c
ommit2_str) |
| 115 } |
| 116 }) |
| 117 defer srv.Close() |
| 118 |
| 119 Convey("Page till no cursor", func() { |
| 120 commits, err := c.Log(ctx, "master..8de6836858c99e48f3c5
8164ab717bda728e95dd", 10) |
| 121 So(err, ShouldBeNil) |
| 122 So(cursor_sent, ShouldEqual, "next_cursor_value") |
| 123 So(len(commits), ShouldEqual, 2) |
| 124 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 125 So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfa
ab61c4d30a35adce5f40") |
| 126 }) |
| 127 |
| 128 Convey("Page till limit", func() { |
| 129 commits, err := c.Log(ctx, "master", 1) |
| 130 So(err, ShouldBeNil) |
| 131 So(cursor_sent, ShouldEqual, "") |
| 132 So(len(commits), ShouldEqual, 1) |
| 133 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 134 }) |
| 135 }) |
| 136 } |
| 137 |
| 138 //////////////////////////////////////////////////////////////////////////////// |
| 139 |
| 140 var ( |
| 141 fake_commit1_str = `{ |
| 142 "commit": "0b2c5409e58a71c691b05454b55cc5580cc822d1", |
| 143 "tree": "3c6f95bc757698cd6aca3c49f88f640fd145ea69", |
| 144 "parents": [ "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40" ], |
| 145 "author": { |
| 146 "name": "Author 1", |
| 147 "email": "author1@example.com", |
| 148 "time": "Mon Jul 17 15:02:43 2017" |
| 149 }, |
| 150 "committer": { |
| 151 "name": "Commit Bot", |
| 152 "email": "commit-bot@chromium.org", |
| 153 "time": "Mon Jul 17 15:02:43 2017" |
| 154 }, |
| 155 "message": "Import wpt@d96d68ed964f9bfc2bb248c2d2fab7a8870dc685\
\n\\nCr-Commit-Position: refs/heads/master@{#487078}" |
| 156 }` |
| 157 fake_commit2_str = `{ |
| 158 "commit": "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40", |
| 159 "tree": "1ba2335c07915c31597b97a8d824aecc85a996f6", |
| 160 "parents": ["8de6836858c99e48f3c58164ab717bda728e95dd"], |
| 161 "author": { |
| 162 "name": "Author 2", |
| 163 "email": "author-2@example.com", |
| 164 "time": "Mon Jul 17 15:01:13 2017" |
| 165 }, |
| 166 "committer": { |
| 167 "name": "Commit Bot", |
| 168 "email": "commit-bot@chromium.org", |
| 169 "time": "Mon Jul 17 15:01:13 2017" |
| 170 }, |
| 171 "message": "[Web Payments] User weak ptr in Payment Request\u002
7s error callback\\n\\nBug: 742329\\nReviewed-on: https://chromium-review.google
source.com/570982\\nCr-Commit-Position: refs/heads/master@{#487077}" |
| 172 }` |
| 173 ) |
| 174 |
| 175 //////////////////////////////////////////////////////////////////////////////// |
| 176 |
| 177 func newMockClient(handler func(w http.ResponseWriter, r *http.Request)) (*httpt
est.Server, *Client) { |
| 178 srv := httptest.NewServer(http.HandlerFunc(handler)) |
| 179 return srv, &Client{Client: http.DefaultClient, RepoURL: srv.URL} |
| 180 } |
| OLD | NEW |