Home

overlay @main - refs - log -
-
https://git.jolheiser.com/overlay.git
Overlay FS
overlay / overlay_test.go
- raw
  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
132
133
134
135
136
137
138
139
package overlay

import (
	"embed"
	"io/fs"
	"os"
	"strings"
	"testing"
)

//go:embed _test/embed
var embedded embed.FS

func TestMain(m *testing.M) {
	os.Exit(m.Run())
}

func TestOverlay(t *testing.T) {
	tt := []struct {
		Name     string
		File     string
		Expected string
	}{
		{
			Name:     "Embed",
			File:     "test1.txt",
			Expected: "test1",
		},
		{
			Name:     "Disk",
			File:     "test2.txt",
			Expected: "test3",
		},
	}

	x, err := New("_test/disk", embedded, WithSub("_test/embed"), WithCaching(false))
	if err != nil {
		t.Log(err)
		t.FailNow()
	}

	for _, tc := range tt {
		t.Run(tc.Name, func(t *testing.T) {
			fi, err := x.Open(tc.File)
			if err != nil {
				t.Log(err)
				t.FailNow()
			}
			defer fi.Close()

			contents, err := x.ReadFile(tc.File)
			if err != nil {
				t.Log(err)
				t.FailNow()
			}

			if !strings.EqualFold(string(contents), tc.Expected) {
				t.Logf("fs did not match:\n\tgot: %s\n\texpected: %s\n", string(contents), tc.Expected)
				t.FailNow()
			}
		})
	}
}

func TestWalk(t *testing.T) {
	expected := []string{"test1.txt", "test2.txt"}

	x, err := New("_test/disk", embedded, WithSub("_test/embed"), WithCaching(false))
	if err != nil {
		t.Log(err)
		t.FailNow()
	}

	found := make([]string, 0)
	if err := fs.WalkDir(x, ".", func(walkPath string, walkEntry fs.DirEntry, walkErr error) error {
		if walkErr != nil {
			return walkErr
		}

		if walkEntry.IsDir() {
			return nil
		}

		found = append(found, walkEntry.Name())

		return nil
	}); err != nil {
		t.Log(err)
		t.FailNow()
	}

	for _, e := range expected {
		exists := false
		for _, f := range found {
			if e == f {
				exists = true
				break
			}
		}
		if !exists {
			t.Logf("could not find %s in %s\n", e, found)
			t.Fail()
		}
	}
}

var emptyFS embed.FS

func TestInvalid(t *testing.T) {
	_, err := New("/var/lib/myapp/assets/custom", emptyFS)
	if err != nil {
		t.Log("invalid FS should not error explicitly")
		t.FailNow()
	}
}

func BenchmarkCache(b *testing.B) {
	x, err := New("_test/disk", embedded)
	if err != nil {
		b.Log(err)
		b.FailNow()
	}

	for idx := 0; idx < b.N; idx++ {
		x.exists("test2.txt")
	}
}

func BenchmarkNoCache(b *testing.B) {
	x, err := New("_test/disk", embedded, WithCaching(false))
	if err != nil {
		b.Log(err)
		b.FailNow()
	}

	for idx := 0; idx < b.N; idx++ {
		x.exists("test2.txt")
	}
}