Files
summercms/internal/dev/watch_test.go
Jakub Zych 2baec56420 test(cabana,flare,dev,summer): json, reported errors and dev serve
- Docker: admin:token:create and list --json, -q keeps the token
- flare generate and test-push failures print once and are Reported
- summer dev starts serve with the forwarded globals, reports rebuilds,
  failures and restarts, and stops serve with SIGTERM returning nil
- delegates carry two examples and say the global options pass through
2026-10-21 19:38:19 +02:00

623 lines
18 KiB
Go

package dev
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
"git.golem15.com/golem15/summercms/internal/build"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
type safeBuffer struct {
mu sync.Mutex
b bytes.Buffer
}
func (s *safeBuffer) Write(p []byte) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.b.Write(p)
}
func (s *safeBuffer) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.b.String()
}
// testOutput is a non-terminal bonfire console writing to w.
func testOutput(w io.Writer) bonfire.Output {
return bonfire.NewOutput(strings.NewReader(""), w, w)
}
func TestWatchRebuildsOnSourceEdit(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
var out safeBuffer
var builds atomic.Int32
var starts atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 40 * time.Millisecond,
Build: func(ctx context.Context, appDir string, w io.Writer) error {
builds.Add(1)
fmt.Fprintln(w, "built hello in 1ms")
return nil
},
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
if got := strings.Join(args, " "); got != "serve -v --env staging" {
return nil, fmt.Errorf("start args = %q, want serve and the forwarded globals", got)
}
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
}
return cmd, nil
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, testOutput(&out), []string{"-v", "--env", "staging"}, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
if builds.Load() != 1 {
t.Fatalf("initial builds = %d", builds.Load())
}
if err := os.WriteFile(filepath.Join(dir, "plugin.go"), []byte("package p\n// edited\n"), 0o644); err != nil {
t.Fatal(err)
}
waitUntil(t, "restart after edit", func() bool { return starts.Load() == 2 })
if builds.Load() != 2 {
t.Fatalf("builds after edit = %d", builds.Load())
}
if err := os.WriteFile(filepath.Join(dir, "plugins.gen.go"), []byte("package main\n// generated\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n// generated\n"), 0o644); err != nil {
t.Fatal(err)
}
time.Sleep(200 * time.Millisecond)
if starts.Load() != 2 || builds.Load() != 2 {
t.Fatalf("generated files triggered rebuild starts=%d builds=%d", starts.Load(), builds.Load())
}
got := out.String()
if strings.Count(got, "Rebuilt in ") != 2 {
t.Fatalf("want 2 rebuild latency lines, got %q", got)
}
cancel()
select {
case err := <-done:
if err != nil {
t.Fatalf("watch returned %v after Ctrl+C, want nil (exit 0)", err)
}
if !strings.Contains(out.String(), "Dev report") || !strings.Contains(out.String(), "Restarts 1") {
t.Fatalf("missing dev report:\n%s", out.String())
}
case <-time.After(3 * time.Second):
t.Fatal("watch did not exit on cancel")
}
}
func TestWatchKeepsChildOnBuildFailure(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
var out safeBuffer
var builds atomic.Int32
var starts atomic.Int32
var pid atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 40 * time.Millisecond,
Build: func(ctx context.Context, appDir string, w io.Writer) error {
n := builds.Add(1)
if n >= 2 {
return fmt.Errorf("compile failed")
}
return nil
},
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
}
pid.Store(int32(cmd.Process.Pid))
return cmd, nil
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
if err := os.WriteFile(filepath.Join(dir, "plugin.go"), []byte("package p\n// bad\n"), 0o644); err != nil {
t.Fatal(err)
}
waitUntil(t, "failed rebuild", func() bool { return builds.Load() >= 2 })
time.Sleep(80 * time.Millisecond)
if starts.Load() != 1 {
t.Fatalf("build failure restarted child: starts=%d", starts.Load())
}
if !strings.Contains(out.String(), "compile failed") {
t.Fatalf("missing build error: %s", out.String())
}
proc, err := os.FindProcess(int(pid.Load()))
if err != nil {
t.Fatal(err)
}
if err := proc.Signal(syscall.Signal(0)); err != nil {
t.Fatalf("child was reaped after failed build: %v", err)
}
cancel()
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("watch did not exit")
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if err := proc.Signal(syscall.Signal(0)); err != nil {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatal("child still running after cancel")
}
func TestWatchDebouncesRapidEdits(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
var out safeBuffer
var builds atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 80 * time.Millisecond,
Build: func(ctx context.Context, appDir string, w io.Writer) error {
builds.Add(1)
return nil
},
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
}
return cmd, nil
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial build", func() bool { return builds.Load() == 1 })
for i := 0; i < 5; i++ {
if err := os.WriteFile(filepath.Join(dir, "plugin.go"), []byte(fmt.Sprintf("package p\n// %d\n", i)), 0o644); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
}
waitUntil(t, "debounced rebuild", func() bool { return builds.Load() >= 2 })
time.Sleep(150 * time.Millisecond)
if builds.Load() != 2 {
t.Fatalf("rapid edits should coalesce, builds=%d", builds.Load())
}
cancel()
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("watch did not exit")
}
}
func TestWatchIgnoresBinAndTmp(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
if err := os.MkdirAll(filepath.Join(dir, "bin"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(dir, "tmp"), 0o755); err != nil {
t.Fatal(err)
}
var out safeBuffer
var builds atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 40 * time.Millisecond,
Build: func(ctx context.Context, appDir string, w io.Writer) error {
builds.Add(1)
return nil
},
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
}
return cmd, nil
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial build", func() bool { return builds.Load() == 1 })
if err := os.WriteFile(filepath.Join(dir, "bin", "hello.go"), []byte("package main\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "tmp", "scratch.go"), []byte("package tmp\n"), 0o644); err != nil {
t.Fatal(err)
}
time.Sleep(200 * time.Millisecond)
if builds.Load() != 1 {
t.Fatalf("bin/tmp edits triggered rebuild: builds=%d", builds.Load())
}
cancel()
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("watch did not exit")
}
}
func TestWatchHelloWorkspaceRebuildLatency(t *testing.T) {
dir := copyHelloApp(t)
var out safeBuffer
var builds atomic.Int32
var starts atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 80 * time.Millisecond,
Build: func(ctx context.Context, appDir string, w io.Writer) error {
builds.Add(1)
if err := build.App(ctx, appDir, w); err != nil {
return err
}
return nil
},
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
}
return cmd, nil
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntilTimeout(t, 60*time.Second, "initial real rebuild", func() bool { return starts.Load() == 1 })
if builds.Load() != 1 {
t.Fatalf("initial builds = %d", builds.Load())
}
if !rebuildLinePresent(out.String()) {
t.Fatalf("missing rebuild latency line after initial build:\n%s", out.String())
}
plugin := filepath.Join(dir, "plugins", "greeter", "plugin.go")
src, err := os.ReadFile(plugin)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(plugin, append(src, []byte("\n// watch-edit\n")...), 0o644); err != nil {
t.Fatal(err)
}
waitUntilTimeout(t, 60*time.Second, "rebuild after source edit", func() bool { return starts.Load() == 2 })
got := out.String()
if strings.Count(got, "Rebuilt in ") < 2 {
t.Fatalf("want rebuild latency lines after restart, got %q", got)
}
if !rebuildLinePresent(got) {
t.Fatalf("rebuild line missing duration:\n%s", got)
}
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n// generated\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "plugins.gen.go"), []byte("package main\n// generated\n"), 0o644); err != nil {
t.Fatal(err)
}
time.Sleep(400 * time.Millisecond)
if starts.Load() != 2 || builds.Load() != 2 {
t.Fatalf("generated-file edits looped: starts=%d builds=%d", starts.Load(), builds.Load())
}
cancel()
select {
case err := <-done:
if err != nil && err != context.Canceled {
t.Fatalf("watch exit: %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("watch did not exit on cancel")
}
}
func rebuildLinePresent(s string) bool {
for _, line := range strings.Split(s, "\n") {
line = strings.TrimSpace(line)
if _, d, ok := strings.Cut(line, "Rebuilt in "); ok && strings.TrimSpace(d) != "" {
return true
}
}
return false
}
func waitUntilTimeout(t *testing.T, d time.Duration, name string, ok func() bool) {
t.Helper()
deadline := time.Now().Add(d)
for time.Now().Before(deadline) {
if ok() {
return
}
time.Sleep(25 * time.Millisecond)
}
t.Fatalf("timed out waiting for %s", name)
}
func copyHelloApp(t *testing.T) string {
t.Helper()
src, err := filepath.Abs(filepath.Join("..", "..", "examples", "hello"))
if err != nil {
t.Fatal(err)
}
framework, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
t.Fatal(err)
}
dst := t.TempDir()
if err := filepath.WalkDir(src, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
if rel == "bin" || strings.HasPrefix(rel, "bin"+string(os.PathSeparator)) {
if d.IsDir() {
return filepath.SkipDir
}
return nil
}
target := filepath.Join(dst, rel)
if d.IsDir() {
return os.MkdirAll(target, 0o755)
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
if filepath.Base(path) == "go.mod" {
data = rewriteFrameworkReplace(data, framework)
}
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return err
}
return os.WriteFile(target, data, 0o644)
}); err != nil {
t.Fatal(err)
}
return dst
}
func rewriteFrameworkReplace(data []byte, framework string) []byte {
const module = "git.golem15.com/golem15/summercms"
lines := strings.Split(string(data), "\n")
for i, line := range lines {
trimmed := strings.TrimSpace(line)
switch {
case strings.HasPrefix(trimmed, "replace "+module+" =>"):
indent := line[:len(line)-len(strings.TrimLeft(line, " \t"))]
lines[i] = indent + "replace " + module + " => " + framework
case strings.HasPrefix(trimmed, module+" =>"):
indent := line[:len(line)-len(strings.TrimLeft(line, " \t"))]
lines[i] = indent + module + " => " + framework
}
}
return []byte(strings.Join(lines, "\n"))
}
func writeWatchApp(t *testing.T, dir string) {
t.Helper()
write(t, filepath.Join(dir, "summer.yaml"), "module: example.com/app\nbinary: hello\nplugins:\n - id: a.b\n module: example.com/app/plugins/demo\n")
write(t, filepath.Join(dir, "plugin.go"), "package p\n")
write(t, filepath.Join(dir, "main.go"), "package main\n")
write(t, filepath.Join(dir, "plugins.gen.go"), "package main\n")
}
func write(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
func waitUntil(t *testing.T, name string, ok func() bool) {
t.Helper()
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
if ok() {
return
}
time.Sleep(15 * time.Millisecond)
}
t.Fatalf("timed out waiting for %s", name)
}
// fakeServe writes an executable that records its arguments in
// $DEV_ARGS_FILE and, on SIGTERM, writes "term" to $DEV_TERM_FILE and exits
// 0, like serve's graceful stop.
func fakeServe(t *testing.T, path string) (argsFile, termFile string) {
t.Helper()
dir := t.TempDir()
argsFile, termFile = filepath.Join(dir, "args"), filepath.Join(dir, "term")
t.Setenv("DEV_ARGS_FILE", argsFile)
t.Setenv("DEV_TERM_FILE", termFile)
script := "#!/bin/sh\n" +
"printf '%s\\n' \"$*\" > \"$DEV_ARGS_FILE\"\n" +
"trap 'echo term > \"$DEV_TERM_FILE\"; exit 0' TERM\n" +
"while :; do sleep 0.05; done\n"
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
return argsFile, termFile
}
func readTrimmed(path string) string {
b, _ := os.ReadFile(path)
return strings.TrimSpace(string(b))
}
// TestStartBinServe: summer dev starts bin/<binary> with "serve" first and
// then the forwarded global options, through the real startBin.
func TestStartBinServe(t *testing.T) {
bin := filepath.Join(t.TempDir(), "bin", "hello")
argsFile, _ := fakeServe(t, bin)
var out safeBuffer
cmd, err := startBin(t.Context(), bin, []string{"serve", "-vv", "--env=staging"}, &out, &out)
if err != nil {
t.Fatal(err)
}
waitUntil(t, "args written", func() bool { return readTrimmed(argsFile) != "" })
_ = cmd.Process.Signal(syscall.SIGTERM)
_ = cmd.Wait()
if got := readTrimmed(argsFile); got != "serve -vv --env=staging" {
t.Fatalf("child args = %q", got)
}
// Through watch: the default Start runs serve with the globals summer
// dev was given.
dir := t.TempDir()
writeWatchApp(t, dir)
argsFile, _ = fakeServe(t, filepath.Join(dir, "bin", "hello"))
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
done := make(chan error, 1)
opts := Options{Debounce: 40 * time.Millisecond, Build: func(context.Context, string, io.Writer) error { return nil }}
go func() { done <- watch(ctx, dir, testOutput(&out), []string{"-q", "--no-ansi"}, opts) }()
waitUntil(t, "child started", func() bool { return readTrimmed(argsFile) != "" })
if got := readTrimmed(argsFile); got != "serve -q --no-ansi" {
t.Fatalf("watch started the child with %q", got)
}
if !strings.Contains(out.String(), "running bin/hello serve -q --no-ansi") {
t.Errorf("watch line lacks the child command:\n%s", out.String())
}
cancel()
<-done
}
// TestDevReport: the dev report counts successful and failed rebuilds and
// restarts; a failed build keeps the running child.
func TestDevReport(t *testing.T) {
r := devReport{rebuilds: 2, failed: 1, restarts: 1, started: time.Now().Add(-90 * time.Second)}
d := r.details()
if d[0].Value != "3 (1 failed)" || !d[0].Warn || d[1].Value != "1" || d[2].Value != "1m30s" {
t.Fatalf("details = %+v", d)
}
if clean := (devReport{rebuilds: 1, started: time.Now()}).details(); clean[0].Value != "1 (0 failed)" || clean[0].Warn {
t.Fatalf("clean details = %+v", clean)
}
dir := t.TempDir()
writeWatchApp(t, dir)
var out safeBuffer
var builds, starts atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 40 * time.Millisecond,
Build: func(context.Context, string, io.Writer) error {
if builds.Add(1) == 2 {
return fmt.Errorf("syntax error")
}
return nil
},
Start: func(context.Context, string, []string, io.Writer, io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
return cmd, cmd.Start()
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
write(t, filepath.Join(dir, "plugin.go"), "package p\n// broken\n")
waitUntil(t, "failed build", func() bool { return builds.Load() == 2 })
waitUntil(t, "build failure line", func() bool { return strings.Contains(out.String(), "Build failed: syntax error") })
write(t, filepath.Join(dir, "plugin.go"), "package p\n// fixed\n")
waitUntil(t, "restart", func() bool { return starts.Load() == 2 })
cancel()
if err := <-done; err != nil {
t.Fatalf("watch returned %v", err)
}
got := out.String()
for _, want := range []string{"Dev report", "Rebuilds 3 (1 failed)", "Restarts 1", "Uptime"} {
if !strings.Contains(got, want) {
t.Errorf("dev report lacks %q:\n%s", want, got)
}
}
}
// TestDevGracefulNil: when summer dev is stopped it sends SIGTERM to the
// running serve, which stops gracefully, prints the dev report and returns
// nil (exit 0).
func TestDevGracefulNil(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
argsFile, termFile := fakeServe(t, filepath.Join(dir, "bin", "hello"))
var out safeBuffer
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
done := make(chan error, 1)
opts := Options{Debounce: 40 * time.Millisecond, Build: func(context.Context, string, io.Writer) error { return nil }}
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "child started", func() bool { return readTrimmed(argsFile) == "serve" })
cancel()
select {
case err := <-done:
if err != nil {
t.Fatalf("watch returned %v after stop, want nil", err)
}
case <-time.After(5 * time.Second):
t.Fatal("watch did not stop")
}
if readTrimmed(termFile) != "term" {
t.Fatal("serve did not get SIGTERM")
}
if !strings.Contains(out.String(), "Dev report") || !strings.Contains(out.String(), "Restarts 0") {
t.Fatalf("no dev report:\n%s", out.String())
}
}