TestRunBuildsAndReloads is the end-to-end happy path: a tiny module that prints a version string, Run() watches it, we rewrite the version, and the child respawns with the new output.
(t *testing.T)
| 13 | // prints a version string, Run() watches it, we rewrite the version, and the |
| 14 | // child respawns with the new output. |
| 15 | func TestRunBuildsAndReloads(t *testing.T) { |
| 16 | dir := t.TempDir() |
| 17 | writeModule(t, dir, `package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "os/signal" |
| 22 | "syscall" |
| 23 | ) |
| 24 | |
| 25 | const version = "v1" |
| 26 | |
| 27 | func main() { |
| 28 | fmt.Println("started:" + version) |
| 29 | ch := make(chan struct{}) |
| 30 | go func() { |
| 31 | c := make(chan any, 1) |
| 32 | _ = c |
| 33 | _ = signal.Ignore |
| 34 | _ = syscall.SIGTERM |
| 35 | }() |
| 36 | // Stay alive until killed. |
| 37 | sig := make(chan any) |
| 38 | go func() { <-sig }() |
| 39 | <-ch |
| 40 | } |
| 41 | `) |
| 42 | // Replace with a version that handles SIGTERM cleanly. |
| 43 | writeModule(t, dir, `package main |
| 44 | |
| 45 | import ( |
| 46 | "fmt" |
| 47 | "os" |
| 48 | "os/signal" |
| 49 | "syscall" |
| 50 | ) |
| 51 | |
| 52 | const version = "v1" |
| 53 | |
| 54 | func main() { |
| 55 | fmt.Println("started:" + version) |
| 56 | ch := make(chan os.Signal, 1) |
| 57 | signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT) |
| 58 | <-ch |
| 59 | } |
| 60 | `) |
| 61 | |
| 62 | ctx, cancel := context.WithCancel(context.Background()) |
| 63 | defer cancel() |
| 64 | |
| 65 | out := &safeBuf{} |
| 66 | errb := &safeBuf{} |
| 67 | done := make(chan error, 1) |
| 68 | go func() { |
| 69 | done <- Run(ctx, Options{ |
| 70 | WorkDir: dir, |
| 71 | Package: ".", |
| 72 | Debounce: 80 * time.Millisecond, |
nothing calls this directly
no test coverage detected