- Non-TTY widgets/prompts, flag parsing, secret non-leak and malicious IDs - Real hello workspace rebuild latency line, debounce and ignored bin/tmp - scripts/check-phase1.sh runs vet/test/race across root, hello, base, greeter, optional
463 lines
12 KiB
Go
463 lines
12 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"
|
|
)
|
|
|
|
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()
|
|
}
|
|
|
|
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, 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, &out, 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, "rebuild:") != 2 {
|
|
t.Fatalf("want 2 rebuild latency lines, got %q", got)
|
|
}
|
|
|
|
cancel()
|
|
select {
|
|
case err := <-done:
|
|
if err != nil && err != context.Canceled {
|
|
t.Fatalf("watch exit: %v", err)
|
|
}
|
|
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, 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, &out, 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, 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, &out, 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, 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, &out, 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, 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, &out, 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, "rebuild:") < 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 strings.HasPrefix(line, "rebuild:") && len(strings.TrimSpace(strings.TrimPrefix(line, "rebuild:"))) > 0 {
|
|
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)
|
|
}
|