| 1 |
// Copyright 2023 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package main |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"io/fs" |
| 14 |
"net/http" |
| 15 |
"net/http/httptest" |
| 16 |
"net/url" |
| 17 |
"os" |
| 18 |
"path" |
| 19 |
"path/filepath" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/alecthomas/kong" |
| 24 |
"github.com/getkin/kin-openapi/openapi3" |
| 25 |
"github.com/google/go-cmp/cmp" |
| 26 |
"github.com/google/go-github/v60/github" |
| 27 |
) |
| 28 |
|
| 29 |
func TestUpdateGo(t *testing.T) { |
| 30 |
t.Run("valid", func(t *testing.T) { |
| 31 |
res := runTest(t, "testdata/update-go/valid", "update-go") |
| 32 |
res.assertOutput("", "") |
| 33 |
res.assertNoErr() |
| 34 |
res.checkGolden() |
| 35 |
}) |
| 36 |
|
| 37 |
t.Run("invalid", func(t *testing.T) { |
| 38 |
res := runTest(t, "testdata/update-go/invalid", "update-go") |
| 39 |
res.assertOutput("", "") |
| 40 |
res.assertErr(` |
| 41 |
no operations defined for AService.NoOperation |
| 42 |
no operations defined for AService.NoComment |
| 43 |
ambiguous operation "GET /ambiguous/{}" could match any of: [GET /ambiguous/{id} GET /ambiguous/{name}] |
| 44 |
could not find operation "GET /missing/{id}" in openapi_operations.yaml |
| 45 |
duplicate operation: GET /a/{a_id} |
| 46 |
`) |
| 47 |
res.checkGolden() |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
func TestUnused(t *testing.T) { |
| 52 |
res := runTest(t, "testdata/unused", "unused") |
| 53 |
res.assertOutput(` |
| 54 |
Found 3 unused operations |
| 55 |
|
| 56 |
GET /a/{a_id} |
| 57 |
doc: https://docs.github.com/rest/a/a#overridden-get-a |
| 58 |
|
| 59 |
POST /a/{a_id} |
| 60 |
doc: https://docs.github.com/rest/a/a#update-a |
| 61 |
|
| 62 |
GET /undocumented/{undocumented_id} |
| 63 |
`, "") |
| 64 |
} |
| 65 |
|
| 66 |
func TestUpdateOpenAPI(t *testing.T) { |
| 67 |
testServer := newTestServer(t, "main", map[string]interface{}{ |
| 68 |
"api.github.com/api.github.com.json": openapi3.T{ |
| 69 |
Paths: openapi3.NewPaths( |
| 70 |
openapi3.WithPath("/a/{a_id}", &openapi3.PathItem{ |
| 71 |
Get: &openapi3.Operation{ |
| 72 |
ExternalDocs: &openapi3.ExternalDocs{ |
| 73 |
URL: "https://docs.github.com/rest/reference/a", |
| 74 |
}, |
| 75 |
}, |
| 76 |
})), |
| 77 |
}, |
| 78 |
"ghec/ghec.json": openapi3.T{ |
| 79 |
Paths: openapi3.NewPaths( |
| 80 |
openapi3.WithPath("/a/b/{a_id}", &openapi3.PathItem{ |
| 81 |
Get: &openapi3.Operation{ |
| 82 |
ExternalDocs: &openapi3.ExternalDocs{ |
| 83 |
URL: "https://docs.github.com/rest/reference/a", |
| 84 |
}, |
| 85 |
}, |
| 86 |
})), |
| 87 |
}, |
| 88 |
"ghes-3.9/ghes-3.9.json": openapi3.T{ |
| 89 |
Paths: openapi3.NewPaths( |
| 90 |
openapi3.WithPath("/a/b/{a_id}", &openapi3.PathItem{ |
| 91 |
Get: &openapi3.Operation{ |
| 92 |
ExternalDocs: &openapi3.ExternalDocs{ |
| 93 |
URL: "https://docs.github.com/rest/reference/a", |
| 94 |
}, |
| 95 |
}, |
| 96 |
})), |
| 97 |
}, |
| 98 |
"ghes-3.10/ghes-3.10.json": openapi3.T{ |
| 99 |
Paths: openapi3.NewPaths( |
| 100 |
openapi3.WithPath("/a/b/{a_id}", &openapi3.PathItem{ |
| 101 |
Get: &openapi3.Operation{ |
| 102 |
ExternalDocs: &openapi3.ExternalDocs{ |
| 103 |
URL: "https://docs.github.com/rest/reference/a", |
| 104 |
}, |
| 105 |
}, |
| 106 |
})), |
| 107 |
}, |
| 108 |
"ghes-2.22/ghes-2.22.json": openapi3.T{ |
| 109 |
Paths: openapi3.NewPaths( |
| 110 |
openapi3.WithPath("/a/b/{a_id}", &openapi3.PathItem{ |
| 111 |
Get: &openapi3.Operation{ |
| 112 |
ExternalDocs: &openapi3.ExternalDocs{ |
| 113 |
URL: "https://docs.github.com/rest/reference/a", |
| 114 |
}, |
| 115 |
}, |
| 116 |
})), |
| 117 |
}, |
| 118 |
}) |
| 119 |
|
| 120 |
res := runTest(t, "testdata/update-openapi", "update-openapi", "--github-url", testServer.URL) |
| 121 |
res.assertOutput("", "") |
| 122 |
res.assertNoErr() |
| 123 |
res.checkGolden() |
| 124 |
} |
| 125 |
|
| 126 |
func TestFormat(t *testing.T) { |
| 127 |
res := runTest(t, "testdata/format", "format") |
| 128 |
res.assertOutput("", "") |
| 129 |
res.assertNoErr() |
| 130 |
res.checkGolden() |
| 131 |
} |
| 132 |
|
| 133 |
func updateGoldenDir(t *testing.T, origDir, resultDir, goldenDir string) { |
| 134 |
t.Helper() |
| 135 |
if os.Getenv("UPDATE_GOLDEN") == "" { |
| 136 |
return |
| 137 |
} |
| 138 |
assertNilError(t, os.RemoveAll(goldenDir)) |
| 139 |
assertNilError(t, filepath.WalkDir(resultDir, func(path string, d fs.DirEntry, err error) error { |
| 140 |
if err != nil || d.IsDir() { |
| 141 |
return err |
| 142 |
} |
| 143 |
relName := mustRel(t, resultDir, path) |
| 144 |
origName := filepath.Join(origDir, relName) |
| 145 |
_, err = os.Stat(origName) |
| 146 |
if err != nil { |
| 147 |
if os.IsNotExist(err) { |
| 148 |
err = os.MkdirAll(filepath.Dir(filepath.Join(goldenDir, relName)), d.Type()) |
| 149 |
if err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
return copyFile(path, filepath.Join(goldenDir, relName)) |
| 153 |
} |
| 154 |
return err |
| 155 |
} |
| 156 |
resContent, err := os.ReadFile(path) |
| 157 |
if err != nil { |
| 158 |
return err |
| 159 |
} |
| 160 |
origContent, err := os.ReadFile(origName) |
| 161 |
if err != nil { |
| 162 |
return err |
| 163 |
} |
| 164 |
if bytes.Equal(resContent, origContent) { |
| 165 |
return nil |
| 166 |
} |
| 167 |
return copyFile(path, filepath.Join(goldenDir, relName)) |
| 168 |
})) |
| 169 |
} |
| 170 |
|
| 171 |
func checkGoldenDir(t *testing.T, origDir, resultDir, goldenDir string) { |
| 172 |
t.Helper() |
| 173 |
golden := true |
| 174 |
t.Cleanup(func() { |
| 175 |
t.Helper() |
| 176 |
if !golden { |
| 177 |
t.Log("To regenerate golden files run `UPDATE_GOLDEN=1 script/test.sh`") |
| 178 |
} |
| 179 |
}) |
| 180 |
updateGoldenDir(t, origDir, resultDir, goldenDir) |
| 181 |
checked := map[string]bool{} |
| 182 |
_, err := os.Stat(goldenDir) |
| 183 |
if err == nil { |
| 184 |
assertNilError(t, filepath.Walk(goldenDir, func(wantPath string, info fs.FileInfo, err error) error { |
| 185 |
relPath := mustRel(t, goldenDir, wantPath) |
| 186 |
if err != nil || info.IsDir() { |
| 187 |
return err |
| 188 |
} |
| 189 |
if !assertEqualFiles(t, wantPath, filepath.Join(resultDir, relPath)) { |
| 190 |
golden = false |
| 191 |
} |
| 192 |
checked[relPath] = true |
| 193 |
return nil |
| 194 |
})) |
| 195 |
} |
| 196 |
assertNilError(t, filepath.Walk(origDir, func(wantPath string, info fs.FileInfo, err error) error { |
| 197 |
relPath := mustRel(t, origDir, wantPath) |
| 198 |
if err != nil || info.IsDir() || checked[relPath] { |
| 199 |
return err |
| 200 |
} |
| 201 |
if !assertEqualFiles(t, wantPath, filepath.Join(resultDir, relPath)) { |
| 202 |
golden = false |
| 203 |
} |
| 204 |
checked[relPath] = true |
| 205 |
return nil |
| 206 |
})) |
| 207 |
assertNilError(t, filepath.Walk(resultDir, func(resultPath string, info fs.FileInfo, err error) error { |
| 208 |
relPath := mustRel(t, resultDir, resultPath) |
| 209 |
if err != nil || info.IsDir() || checked[relPath] { |
| 210 |
return err |
| 211 |
} |
| 212 |
golden = false |
| 213 |
return fmt.Errorf("found unexpected file:\n%s", relPath) |
| 214 |
})) |
| 215 |
} |
| 216 |
|
| 217 |
func mustRel(t *testing.T, base, target string) string { |
| 218 |
t.Helper() |
| 219 |
rel, err := filepath.Rel(base, target) |
| 220 |
assertNilError(t, err) |
| 221 |
return rel |
| 222 |
} |
| 223 |
|
| 224 |
func copyDir(t *testing.T, dst, src string) error { |
| 225 |
fmt.Println("dst", dst) |
| 226 |
dst, err := filepath.Abs(dst) |
| 227 |
if err != nil { |
| 228 |
return err |
| 229 |
} |
| 230 |
return filepath.Walk(src, func(srcPath string, info fs.FileInfo, err error) error { |
| 231 |
if err != nil || info.IsDir() { |
| 232 |
return err |
| 233 |
} |
| 234 |
dstPath := filepath.Join(dst, mustRel(t, src, srcPath)) |
| 235 |
err = copyFile(srcPath, dstPath) |
| 236 |
return err |
| 237 |
}) |
| 238 |
} |
| 239 |
|
| 240 |
func copyFile(src, dst string) (errOut error) { |
| 241 |
srcDirStat, err := os.Stat(filepath.Dir(src)) |
| 242 |
if err != nil { |
| 243 |
return err |
| 244 |
} |
| 245 |
err = os.MkdirAll(filepath.Dir(dst), srcDirStat.Mode()) |
| 246 |
if err != nil { |
| 247 |
return err |
| 248 |
} |
| 249 |
dstFile, err := os.Create(dst) |
| 250 |
if err != nil { |
| 251 |
return err |
| 252 |
} |
| 253 |
defer func() { |
| 254 |
e := dstFile.Close() |
| 255 |
if errOut == nil { |
| 256 |
errOut = e |
| 257 |
} |
| 258 |
}() |
| 259 |
srcFile, err := os.Open(src) |
| 260 |
if err != nil { |
| 261 |
return err |
| 262 |
} |
| 263 |
defer func() { |
| 264 |
e := srcFile.Close() |
| 265 |
if errOut == nil { |
| 266 |
errOut = e |
| 267 |
} |
| 268 |
}() |
| 269 |
_, err = io.Copy(dstFile, srcFile) |
| 270 |
return err |
| 271 |
} |
| 272 |
|
| 273 |
type testRun struct { |
| 274 |
t *testing.T |
| 275 |
workDir string |
| 276 |
srcDir string |
| 277 |
stdOut bytes.Buffer |
| 278 |
stdErr bytes.Buffer |
| 279 |
err error |
| 280 |
} |
| 281 |
|
| 282 |
func (r testRun) checkGolden() { |
| 283 |
r.t.Helper() |
| 284 |
checkGoldenDir(r.t, r.srcDir, r.workDir, filepath.Join("testdata", "golden", r.t.Name())) |
| 285 |
} |
| 286 |
|
| 287 |
func (r testRun) assertOutput(stdout, stderr string) { |
| 288 |
r.t.Helper() |
| 289 |
assertEqualStrings(r.t, strings.TrimSpace(stdout), strings.TrimSpace(r.stdOut.String())) |
| 290 |
assertEqualStrings(r.t, strings.TrimSpace(stderr), strings.TrimSpace(r.stdErr.String())) |
| 291 |
} |
| 292 |
|
| 293 |
func (r testRun) assertNoErr() { |
| 294 |
r.t.Helper() |
| 295 |
assertNilError(r.t, r.err) |
| 296 |
} |
| 297 |
|
| 298 |
func (r testRun) assertErr(want string) { |
| 299 |
r.t.Helper() |
| 300 |
if r.err == nil { |
| 301 |
r.t.Error("expected error") |
| 302 |
return |
| 303 |
} |
| 304 |
if strings.TrimSpace(r.err.Error()) != strings.TrimSpace(want) { |
| 305 |
r.t.Errorf("unexpected error:\nwant:\n%s\ngot:\n%s", want, r.err.Error()) |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
func runTest(t *testing.T, srcDir string, args ...string) testRun { |
| 310 |
t.Helper() |
| 311 |
srcDir = filepath.FromSlash(srcDir) |
| 312 |
res := testRun{ |
| 313 |
t: t, |
| 314 |
workDir: t.TempDir(), |
| 315 |
srcDir: srcDir, |
| 316 |
} |
| 317 |
err := copyDir(t, res.workDir, srcDir) |
| 318 |
if err != nil { |
| 319 |
t.Error(err) |
| 320 |
return res |
| 321 |
} |
| 322 |
res.err = run( |
| 323 |
append(args, "-C", res.workDir), |
| 324 |
[]kong.Option{kong.Writers(&res.stdOut, &res.stdErr)}, |
| 325 |
) |
| 326 |
return res |
| 327 |
} |
| 328 |
|
| 329 |
func newTestServer(t *testing.T, ref string, files map[string]interface{}) *httptest.Server { |
| 330 |
t.Helper() |
| 331 |
jsonHandler := func(wantQuery url.Values, val interface{}) http.HandlerFunc { |
| 332 |
return func(w http.ResponseWriter, r *http.Request) { |
| 333 |
gotQuery := r.URL.Query() |
| 334 |
queryDiff := cmp.Diff(wantQuery, gotQuery) |
| 335 |
if queryDiff != "" { |
| 336 |
t.Errorf("query mismatch for %s (-want +got):\n%s", r.URL.Path, queryDiff) |
| 337 |
} |
| 338 |
w.WriteHeader(200) |
| 339 |
err := json.NewEncoder(w).Encode(val) |
| 340 |
if err != nil { |
| 341 |
panic(err) |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
repoPath := "/api/v3/repos/github/rest-api-description" |
| 346 |
emptyQuery := url.Values{} |
| 347 |
refQuery := url.Values{"ref": []string{ref}} |
| 348 |
mux := http.NewServeMux() |
| 349 |
server := httptest.NewServer(mux) |
| 350 |
mux.HandleFunc( |
| 351 |
path.Join(repoPath, "commits", ref), |
| 352 |
jsonHandler(emptyQuery, &github.RepositoryCommit{SHA: github.String("s")}), |
| 353 |
) |
| 354 |
var descriptionsContent []*github.RepositoryContent |
| 355 |
for name, content := range files { |
| 356 |
descriptionsContent = append(descriptionsContent, &github.RepositoryContent{ |
| 357 |
Name: github.String(path.Base(path.Dir(name))), |
| 358 |
}) |
| 359 |
mux.HandleFunc( |
| 360 |
path.Join(repoPath, "contents/descriptions", path.Dir(name)), |
| 361 |
jsonHandler(refQuery, []*github.RepositoryContent{ |
| 362 |
{ |
| 363 |
Name: github.String(path.Base(name)), |
| 364 |
DownloadURL: github.String(server.URL + "/dl/" + name), |
| 365 |
}, |
| 366 |
}), |
| 367 |
) |
| 368 |
mux.HandleFunc( |
| 369 |
path.Join("/dl", name), |
| 370 |
jsonHandler(emptyQuery, content), |
| 371 |
) |
| 372 |
} |
| 373 |
mux.HandleFunc( |
| 374 |
path.Join(repoPath, "contents/descriptions"), |
| 375 |
jsonHandler(refQuery, descriptionsContent), |
| 376 |
) |
| 377 |
t.Cleanup(server.Close) |
| 378 |
t.Setenv("GITHUB_TOKEN", "fake token") |
| 379 |
return server |
| 380 |
} |
| 381 |
|
| 382 |
func assertEqualStrings(t *testing.T, want, got string) { |
| 383 |
t.Helper() |
| 384 |
diff := cmp.Diff(want, got) |
| 385 |
if diff != "" { |
| 386 |
t.Error(diff) |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
func assertEqualFiles(t *testing.T, want, got string) bool { |
| 391 |
t.Helper() |
| 392 |
wantBytes, err := os.ReadFile(want) |
| 393 |
if !assertNilError(t, err) { |
| 394 |
return false |
| 395 |
} |
| 396 |
wantBytes = bytes.ReplaceAll(wantBytes, []byte("\r\n"), []byte("\n")) |
| 397 |
gotBytes, err := os.ReadFile(got) |
| 398 |
if !assertNilError(t, err) { |
| 399 |
return false |
| 400 |
} |
| 401 |
gotBytes = bytes.ReplaceAll(gotBytes, []byte("\r\n"), []byte("\n")) |
| 402 |
if !bytes.Equal(wantBytes, gotBytes) { |
| 403 |
diff := cmp.Diff(string(wantBytes), string(gotBytes)) |
| 404 |
t.Errorf("files %q and %q differ: %s", want, got, diff) |
| 405 |
return false |
| 406 |
} |
| 407 |
return true |
| 408 |
} |
| 409 |
|
| 410 |
func assertNilError(t *testing.T, err error) bool { |
| 411 |
t.Helper() |
| 412 |
if err != nil { |
| 413 |
t.Error(err) |
| 414 |
return false |
| 415 |
} |
| 416 |
return true |
| 417 |
} |