showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
loop_test.go
2.0 KB · 86 lines
1package build23import (4 "context"5 "os"6 "path/filepath"7 "syscall"8 "testing"9 "time"1011 "daum.dev/showcase/internal/gitea"12)1314func inode(t *testing.T, p string) uint64 {15 t.Helper()16 fi, err := os.Stat(p)17 if err != nil {18 t.Fatal(err)19 }20 return fi.Sys().(*syscall.Stat_t).Ino21}2223func builds(t *testing.T, b *Builder) []string {24 t.Helper()25 es, _ := os.ReadDir(b.siteDir())26 var names []string27 for _, e := range es {28 if e.Name() != "current" {29 names = append(names, e.Name())30 }31 }32 return names33}3435func TestBuildLoop(t *testing.T) {36 b := testBuilder(t)37 ctx := context.Background()38 if b.Ready() {39 t.Fatal("ready before the first build")40 }41 tick := 042 now := b.Now()43 b.Now = func() time.Time { tick++; return now.Add(time.Duration(tick) * time.Second) }4445 if err := b.BuildOnce(ctx); err != nil {46 t.Fatal(err)47 }48 if !b.Ready() {49 t.Fatal("not ready after a build")50 }51 blob := func() string { return filepath.Join(b.Current(), "repo-a/blob/go.mod.html") }52 first := inode(t, blob())5354 // unchanged HEAD and header: git pages are hard-linked, not re-rendered55 if err := b.BuildOnce(ctx); err != nil {56 t.Fatal(err)57 }58 if inode(t, blob()) != first {59 t.Error("blob page was re-rendered although nothing changed")60 }6162 // a changed key re-renders; only the current and previous builds remain63 b.st.keys["repo-a"] = "changed"64 if err := b.BuildOnce(ctx); err != nil {65 t.Fatal(err)66 }67 if inode(t, blob()) == first {68 t.Error("blob page was reused although the repo changed")69 }70 if n := builds(t, b); len(n) != 2 {71 t.Errorf("want 2 builds kept, got %v", n)72 }7374 // a failing build leaves the current one in place and no temp dir behind75 cur, _ := os.Readlink(b.Current())76 b.Gitea = gitea.New("http://127.0.0.1:1", "daniel")77 if err := b.BuildOnce(ctx); err == nil {78 t.Fatal("build against a dead Gitea succeeded")79 }80 if now, _ := os.Readlink(b.Current()); now != cur {81 t.Error("current changed after a failed build")82 }83 if n := builds(t, b); len(n) != 2 {84 t.Errorf("failed build left files behind: %v", n)85 }86}