Welcome to mirror list, hosted at ThFree Co, Russian Federation.

root_test.go « projectroot « disk « serving « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8549d1029d3e7e174447aa0646c432ef7d1bc59 (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
package projectroot

import (
	"context"
	"os"
	"strconv"
	"testing"

	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/feature"
	"gitlab.com/gitlab-org/gitlab-pages/internal/vfs"
)

// In case this moch struct ever grows in complexity,
// consider using mockgen instead:
// e.g. internal/domain/mock/resolver_mock.go
type mockRoot struct {
	lstatCalledWith    string
	readlinkCalledWith string
	openCalledWith     string
}

func (m *mockRoot) Lstat(ctx context.Context, name string) (os.FileInfo, error) {
	m.lstatCalledWith = name
	return nil, nil
}

func (m *mockRoot) Readlink(ctx context.Context, name string) (string, error) {
	m.readlinkCalledWith = name
	return "", nil
}

func (m *mockRoot) Open(ctx context.Context, name string) (vfs.File, error) {
	m.openCalledWith = name
	return nil, nil
}

func TestProjectRoot(t *testing.T) {
	tests := map[string]struct {
		path           string
		rootDir        string
		expectedPath   string
		featureEnabled bool
	}{
		"when the root dir is provided": {
			path:           "some/path",
			rootDir:        "my_root_dir",
			expectedPath:   "my_root_dir/some/path",
			featureEnabled: true,
		},
		"when the root dir is not provided": {
			path:           "some/path",
			rootDir:        "",
			expectedPath:   "public/some/path",
			featureEnabled: true,
		},
		"when the feature is disabled": {
			path:           "some/path",
			rootDir:        "my_root_dir",
			expectedPath:   "public/some/path",
			featureEnabled: false,
		},
		"when the feature is disabled and no root path is provided": {
			path:           "some/path",
			rootDir:        "",
			expectedPath:   "public/some/path",
			featureEnabled: false,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			t.Setenv(feature.ConfigurableRoot.EnvVariable,
				strconv.FormatBool(test.featureEnabled))
			originalRoot := &mockRoot{}
			wrappedRoot := New(test.rootDir, originalRoot)

			wrappedRoot.Lstat(context.Background(), test.path)
			require.Equal(t, test.expectedPath, originalRoot.lstatCalledWith)

			wrappedRoot.Readlink(context.Background(), test.path)
			require.Equal(t, test.expectedPath, originalRoot.readlinkCalledWith)

			wrappedRoot.Open(context.Background(), test.path)
			require.Equal(t, test.expectedPath, originalRoot.openCalledWith)
		})
	}
}