| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestExample(t *testing.T) { |
| 14 | t.Log("Starting go run main.go...") |
| 15 | start := time.Now() |
| 16 | |
| 17 | cmd := exec.Command("go", "run", "main.go") |
| 18 | cmd.Env = append(os.Environ(), "GOLOG_LOG_LEVEL=error") // reduce libp2p noise |
| 19 | |
| 20 | // Stream output to both test log and capture buffer for verification |
| 21 | // This ensures we see progress even if the process is killed |
| 22 | var buf bytes.Buffer |
| 23 | cmd.Stdout = io.MultiWriter(os.Stdout, &buf) |
| 24 | cmd.Stderr = io.MultiWriter(os.Stderr, &buf) |
| 25 | |
| 26 | err := cmd.Run() |
| 27 | |
| 28 | elapsed := time.Since(start) |
| 29 | t.Logf("Command completed in %v", elapsed) |
| 30 | |
| 31 | out := buf.String() |
| 32 | if err != nil { |
| 33 | t.Fatalf("running example (%v):\n%s", err, out) |
| 34 | } |
| 35 | |
| 36 | if !strings.Contains(out, "All done!") { |
| 37 | t.Errorf("example did not complete successfully, output:\n%s", out) |
| 38 | } |
| 39 | } |