forked from relops/cqlc
/
fixture.go
49 lines (42 loc) · 1.15 KB
/
fixture.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
39
40
41
42
43
44
45
46
47
48
49
package generator
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
func runFixture(name string, opts *Options) (ret string, err error) {
withFixture(name, func(path string) {
opts.Output = filepath.Join(path, "binding.go")
if err = Generate(opts, "test_version"); err != nil {
fmt.Println("Generate error:", err.Error())
return
}
// Shell to `go run encode.go`.
files, _ := filepath.Glob(filepath.Join(path, "*"))
args := []string{"run"}
args = append(args, files...)
c := exec.Command("go", args...)
out, _ := c.CombinedOutput()
ret = string(out)
})
return
}
// Sets up a Go project using a given fixture directory.
func withFixture(name string, fn func(string)) {
path, _ := ioutil.TempDir("", "")
os.RemoveAll(path)
defer os.RemoveAll(path)
src, _ := filepath.Abs("../test/.fixtures/" + name)
mustRun("cp", "-r", src, path)
fn(path)
}
// Executes a command that is expected run successfully. Otherwise dumps output and panics.
func mustRun(name string, args ...string) {
c := exec.Command(name, args...)
if err := c.Run(); err != nil {
fmt.Println(c.CombinedOutput())
panic("Fixture error: " + err.Error())
}
}