(t *testing.T)
| 72 | } |
| 73 | |
| 74 | func TestHandler(t *testing.T) { |
| 75 | testprog := testprogleader |
| 76 | testprog = append(testprog, getPackage()...) |
| 77 | testprog = append(testprog, testprogtrailer...) |
| 78 | tempDir, err := os.MkdirTemp("", "test_handler") |
| 79 | if err != nil { |
| 80 | log.Fatalf("can't create temp dir. %q", err) |
| 81 | } |
| 82 | defer os.RemoveAll(tempDir) |
| 83 | |
| 84 | gofile := filepath.Join(tempDir, "gofile.go") |
| 85 | if err := os.WriteFile(gofile, testprog, 0666); err != nil { |
| 86 | t.Fatalf("can't create go file. %q", err) |
| 87 | } |
| 88 | |
| 89 | outfile := filepath.Join(tempDir, "outfile.out") |
| 90 | arg := time.Now().UTC().String() |
| 91 | err = exec.Command("go", "run", gofile, outfile, arg).Run() |
| 92 | if err == nil { |
| 93 | t.Fatalf("completed normally, should have failed") |
| 94 | } |
| 95 | |
| 96 | data, err := os.ReadFile(outfile) |
| 97 | if err != nil { |
| 98 | t.Fatalf("can't read output file %s. %q", outfile, err) |
| 99 | } |
| 100 | |
| 101 | if string(data) != arg { |
| 102 | t.Fatalf("bad data. Expected %q, got %q", data, arg) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // getPackage returns the name of the current package, which makes running this |
| 107 | // test in a fork simpler |
nothing calls this directly
no test coverage detected