aboutsummaryrefslogtreecommitdiff
path: root/tests/extras/go_embed_data/embed_test.go
blob: fdcd0fe0eb0308dde162358d0f36d8e4b0620110 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package go_embed_data

import (
	"io"
	"log"
	"os"
	"path/filepath"
	"testing"

	"github.com/bazelbuild/rules_go/go/tools/bazel"
)

func TestMain(m *testing.M) {
	// This test must run from the workspace root since it accesses files using
	// relative paths. We look at parent directories until we find AUTHORS,
	// then change to that directory.
	for {
		if _, err := os.Stat("AUTHORS"); err == nil {
			break
		}
		if err := os.Chdir(".."); err != nil {
			log.Fatal(err)
		}
		if wd, err := os.Getwd(); err != nil {
			log.Fatal(err)
		} else if wd == "/" {
			log.Fatal("could not locate workspace root")
		}
	}
	os.Exit(m.Run())
}

func TestCgo(t *testing.T) {
	if len(cgo) == 0 {
		t.Fatalf("cgo is empty")
	}
}

func TestEmpty(t *testing.T) {
	if len(empty) != 0 {
		t.Fatalf("empty is not empty")
	}
}

func TestSingle(t *testing.T) {
	checkFile(t, "AUTHORS", single)
}

func TestLocal(t *testing.T) {
	for path, data := range local {
		checkFile(t, path, data)
	}
}

func TestExternal(t *testing.T) {
	for path, data := range ext {
		checkFile(t, path, data)
	}
}

func TestFlat(t *testing.T) {
	for key := range flat {
		if filepath.Base(key) != key {
			t.Errorf("filename %q is not flat", key)
		}
	}
}

func TestString(t *testing.T) {
	for _, data := range str {
		var _ string = data // just check the type; contents covered by other tests.
	}
}

func TestUnpack(t *testing.T) {
	for _, data := range unpack {
		checkFile(t, "tests/extras/go_embed_data/BUILD.bazel", data)
	}
	for _, key := range []string{
		"from-zip/BUILD.bazel",
		// Note: Bazel's pkg_tar always adds a leading "./" to its outputs,
		// but tars generated from other sources can match the original
		// inputs more exactly.
		"./from-tar/BUILD.bazel",
	} {
		if _, ok := unpack[key]; !ok {
			t.Errorf("filename %q is not in unpacked set", key)
		}
	}
}

func checkFile(t *testing.T, rawPath string, data []byte) {
	path, err := bazel.Runfile(rawPath)
	if err != nil {
		t.Error(err)
		return
	}

	f, err := os.Open(path)
	if err != nil {
		t.Error(err)
		return
	}
	defer f.Close()

	count := 0
	buffer := make([]byte, 8192)
	for {
		n, err := f.Read(buffer)
		if err != nil && err != io.EOF {
			t.Error(err)
			return
		}
		if n == 0 {
			return
		}
		if n > len(data) {
			t.Errorf("%q: file on disk is longer than embedded data", path)
			return
		}

		for i := 0; i < n; i++ {
			if buffer[i] != data[i] {
				t.Errorf("data mismatch on file %q at offset %d", path, count+i)
				return
			}
		}
		count += n
		data = data[n:]
	}
}