261101.6 · d81d8f77dde7b2f932e94d374c52339ffce44bfc
Tests follow the code they cover
server_test.go and web_test.go kept their tests when server.go and web.go split. Tests now sit in the test file of the code they drive, and the shared helpers stay in the two original files.
Declarations moved unchanged, as a sorted comparison of every line outside the import blocks confirms. git blame -C -C traces them.
reading declarations
Changes to weblist_test.go. All files
@@ -0,0 +1,47 @@+package main++import (+ "net/http"+ "path/filepath"+ "strings"+ "testing"+)++// API where it answers, else from the cache.+func TestWebLists(t *testing.T) {+ api := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path == "/api/v1/users/org/repos" {+ w.Write([]byte(`[{"full_name":"org/tool","description":"a tool"}]`))+ return+ }+ http.NotFound(w, r)+ })+ f := newForge(t, api)+ f.repo(t, "acct/r", map[string]string{"README.md": "x\n"})+ w := testWeb(t, defaultDisk)+ read(t, w, "/"+f.host+"/acct/r/")++ rec := read(t, w, "/"+f.host+"/org")+ if body := rec.Body.String(); rec.Code != http.StatusOK || !strings.Contains(body, `href="/`+f.host+`/org/tool/"`) ||+ !strings.Contains(body, "a tool") || !strings.Contains(body, "the Forgejo API") {+ t.Errorf("API listing: %d\n%s", rec.Code, body)+ }+ rec = read(t, w, "/"+f.host+"/acct")+ if body := rec.Body.String(); rec.Code != http.StatusOK || !strings.Contains(body, `href="/`+f.host+`/acct/r/"`) ||+ !strings.Contains(body, "listed by the cache") {+ t.Errorf("cache listing: %d\n%s", rec.Code, body)+ }+ if rec := read(t, w, "/"+f.host+"/nobody"); rec.Code != http.StatusNotFound {+ t.Errorf("nothing there: %d\n%s", rec.Code, rec.Body)+ }+ // A failed clone leaves nothing in the cache. A forge that keeps+ // repos at one depth clones without a probe.+ depths[f.host] = 3+ t.Cleanup(func() { delete(depths, f.host) })+ if rec := read(t, w, "/"+f.host+"/acct/gone"); rec.Code != http.StatusBadGateway {+ t.Errorf("no repo: %d\n%s", rec.Code, rec.Body)+ }+ if exists(filepath.Join(w.root, f.host, "acct", "gone")) {+ t.Error("a failed read left its directory")+ }+}