summaryrefslogtreecommitdiff
path: root/xdelta3/go/src/xdelta/run.go
blob: 6523a1c194607e927b5799f0d3a4fef91966460b (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package xdelta

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
)

type Program struct {
	Path string
}

type Run struct {
	Cmd exec.Cmd
	Srcfile string
	Stdin io.WriteCloser
	Srcin io.WriteCloser
	Stdout io.ReadCloser
	Stderr io.ReadCloser
}

type Runner struct {
	Testdir string
}

func (r *Run) Wait() error {
	return r.Cmd.Wait()
}

func NewRunner() (*Runner, error) {
	if dir, err := ioutil.TempDir(tmpDir, "xrt"); err != nil {
		return nil, err
	} else {
		return &Runner{dir}, nil
	}
}

func (r *Runner) newTestGroup(name string) (*TestGroup) {
	tg := &TestGroup{Runner: r}
	tg.WaitGroup.Add(1)
	g0 := &Goroutine{tg, name, false}
	tg.running = append(tg.running, g0)
	tg.main = g0
	return tg
}

func (r *Runner) Cleanup() {
	os.RemoveAll(r.Testdir)
}

func (r *Runner) RunTest(name string, f func (t *TestGroup)) {
	t := r.newTestGroup(name)
	c := make(chan interface{})
	go func() {
		defer func() {
			c <- recover()
		}()
		fmt.Println("Testing", name, "...")
		f(t)
		c <- nil
	}()
	rec := <- c
	if t.errors == nil && rec == nil {
		fmt.Println("Success:", name)
	} else {
		fmt.Println("FAILED:", name, t.errors, rec)
	}
}