-
Notifications
You must be signed in to change notification settings - Fork 11
/
main_test.go
80 lines (70 loc) · 2.06 KB
/
main_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
74
75
76
77
78
79
80
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
func equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}
func TempRoot(t testing.TB) string {
if runtime.GOOS == "darwin" {
// on macOS $TMPDIR cannot be used for `nix --store`
pwd, err := os.Getwd()
if err != nil {
ok(t, err)
}
return pwd
} else {
return os.TempDir()
}
}
func buildNixFile(t testing.TB, tempdir string, nixFile string, expectedBuilds int) int {
store := path.Join(tempdir, "store")
output := path.Join(tempdir, path.Base(nixFile))
err := os.MkdirAll(output, 0700)
ok(t, err)
buildFlags := fmt.Sprintf("--store '%s' -o '%s'", store, path.Join(output, "result"))
flags := []string{"-build-flags", buildFlags, nixFile, "-I", "nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixpkgs-unstable.tar.gz", "--store", store}
fmt.Printf("nix-build-uncached %s\n", strings.Join(flags, " "))
err = realMain(flags)
ok(t, err)
files, err := ioutil.ReadDir(output)
ok(t, err)
return len(files)
}
func TestFoo(t *testing.T) {
tempdir, err := ioutil.TempDir(TempRoot(t), "test")
ok(t, err)
asset := os.Getenv("TEST_ASSETS")
if asset == "" {
asset = "test"
}
builds := buildNixFile(t, tempdir, path.Join(asset, "test-skip-cached.nix"), 1)
equals(t, builds, 1)
builds = buildNixFile(t, tempdir, path.Join(asset, "test-many-drvs.nix"), 1)
// depends a bit ulimit, but we should be able to build at least 100
if 100 > builds {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\t: %#v > %#v\033[39m\n\n", filepath.Base(file), line, 100, builds)
t.FailNow()
}
}