summaryrefslogtreecommitdiffstats
path: root/modules/assetfs/layered_test.go
blob: 58876d9be2c6254c56ad7c562b58dd93a4552aac (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
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package assetfs

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

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestLayered(t *testing.T) {
	dir := filepath.Join(t.TempDir(), "assetfs-layers")
	dir1 := filepath.Join(dir, "l1")
	dir2 := filepath.Join(dir, "l2")

	mkdir := func(elems ...string) {
		require.NoError(t, os.MkdirAll(filepath.Join(elems...), 0o755))
	}
	write := func(content string, elems ...string) {
		require.NoError(t, os.WriteFile(filepath.Join(elems...), []byte(content), 0o644))
	}

	// d1 & f1: only in "l1"; d2 & f2: only in "l2"
	// da & fa: in both "l1" and "l2"
	mkdir(dir1, "d1")
	mkdir(dir1, "da")
	mkdir(dir1, "da/sub1")

	mkdir(dir2, "d2")
	mkdir(dir2, "da")
	mkdir(dir2, "da/sub2")

	write("dummy", dir1, ".DS_Store")
	write("f1", dir1, "f1")
	write("fa-1", dir1, "fa")
	write("d1-f", dir1, "d1/f")
	write("da-f-1", dir1, "da/f")

	write("f2", dir2, "f2")
	write("fa-2", dir2, "fa")
	write("d2-f", dir2, "d2/f")
	write("da-f-2", dir2, "da/f")

	assets := Layered(Local("l1", dir1), Local("l2", dir2))

	f, err := assets.Open("f1")
	require.NoError(t, err)
	bs, err := io.ReadAll(f)
	require.NoError(t, err)
	assert.EqualValues(t, "f1", string(bs))
	_ = f.Close()

	assertRead := func(expected string, expectedErr error, elems ...string) {
		bs, err := assets.ReadFile(elems...)
		if err != nil {
			require.ErrorIs(t, err, expectedErr)
		} else {
			require.NoError(t, err)
			assert.Equal(t, expected, string(bs))
		}
	}
	assertRead("f1", nil, "f1")
	assertRead("f2", nil, "f2")
	assertRead("fa-1", nil, "fa")

	assertRead("d1-f", nil, "d1/f")
	assertRead("d2-f", nil, "d2/f")
	assertRead("da-f-1", nil, "da/f")

	assertRead("", fs.ErrNotExist, "no-such")

	files, err := assets.ListFiles(".", true)
	require.NoError(t, err)
	assert.EqualValues(t, []string{"f1", "f2", "fa"}, files)

	files, err = assets.ListFiles(".", false)
	require.NoError(t, err)
	assert.EqualValues(t, []string{"d1", "d2", "da"}, files)

	files, err = assets.ListFiles(".")
	require.NoError(t, err)
	assert.EqualValues(t, []string{"d1", "d2", "da", "f1", "f2", "fa"}, files)

	files, err = assets.ListAllFiles(".", true)
	require.NoError(t, err)
	assert.EqualValues(t, []string{"d1/f", "d2/f", "da/f", "f1", "f2", "fa"}, files)

	files, err = assets.ListAllFiles(".", false)
	require.NoError(t, err)
	assert.EqualValues(t, []string{"d1", "d2", "da", "da/sub1", "da/sub2"}, files)

	files, err = assets.ListAllFiles(".")
	require.NoError(t, err)
	assert.EqualValues(t, []string{
		"d1", "d1/f",
		"d2", "d2/f",
		"da", "da/f", "da/sub1", "da/sub2",
		"f1", "f2", "fa",
	}, files)

	assert.Empty(t, assets.GetFileLayerName("no-such"))
	assert.EqualValues(t, "l1", assets.GetFileLayerName("f1"))
	assert.EqualValues(t, "l2", assets.GetFileLayerName("f2"))
}