forked from codecrafters-io/tester-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage_harness.go
38 lines (33 loc) · 1.16 KB
/
stage_harness.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package tester_utils
// StageHarness is passed to your Stage's TestFunc.
//
// If the program is a long-lived program that must be alive during the duration of the test (like a Redis server),
// do something like this at the start of your test function:
//
// if err := stageHarness.Executable.Start(); err != nil {
// return err
// }
// stageHarness.RegisterTeardownFunc(func() { stageHarness.Executable.Kill() })
//
// If the program is a script that must be executed and then checked for output (like a Git command), use it like this:
//
// result, err := stageHarness.Executable.Run("cat-file", "-p", "sha")
// if err != nil {
// return err
// }
type StageHarness struct {
// Logger is to be used for all logs generated from the test function.
Logger *Logger
// Executable is the program to be tested.
Executable *Executable
// teardownFuncs are run once the error has been reported to the user
teardownFuncs []func()
}
func (s *StageHarness) RegisterTeardownFunc(teardownFunc func()) {
s.teardownFuncs = append(s.teardownFuncs, teardownFunc)
}
func (s StageHarness) RunTeardownFuncs() {
for _, teardownFunc := range s.teardownFuncs {
teardownFunc()
}
}