forked from knieriem/hgo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers_test.go
73 lines (62 loc) · 1.61 KB
/
helpers_test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package hgo_test
import (
"flag"
"io/ioutil"
"os"
"os/exec"
"testing"
"time"
)
var (
keepTmpDirs = flag.Bool("test.keeptmp", false,
"don't remove temporary dirs after use")
// tmpDirs is used by makeTmpDir and removeTmpDirs to record and clean up
// temporary directories used during testing.
tmpDirs []string
)
// Convert time to OS X compatible `touch -t` time
func appleTime(t string) string {
ti, _ := time.Parse(time.RFC3339, t)
return ti.Local().Format("200601021504.05")
}
func createRepo(t testing.TB, commands []string) string {
dir := makeTmpDir(t)
for _, command := range commands {
cmd := exec.Command("bash", "-c", command)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Command %q failed. Output was:\n\n%s", command, out)
}
}
return dir
}
// removeTmpDirs removes all temporary directories created by makeTmpDir
// (unless the -test.keeptmp flag is true, in which case they are retained).
func removeTmpDirs(t testing.TB) {
if *keepTmpDirs {
return
}
for _, dir := range tmpDirs {
err := os.RemoveAll(dir)
if err != nil {
t.Fatalf("tearDown: RemoveAll(%q) failed: %s", dir, err)
}
}
tmpDirs = nil
}
// makeTmpDir creates a temporary directory and returns its path. The
// directory is added to the list of directories to be removed when the
// currently running test ends (assuming the test calls removeTmpDirs() after
// execution).
func makeTmpDir(t testing.TB) string {
dir, err := ioutil.TempDir("", "hgo-")
if err != nil {
t.Fatal(err)
}
if *keepTmpDirs {
t.Logf("Using temp dir %s.", dir)
}
tmpDirs = append(tmpDirs, dir)
return dir
}