File: sync_test.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (243 lines) | stat: -rw-r--r-- 5,279 bytes parent folder | download | duplicates (4)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package safe

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

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

type call struct {
	method string
	path   string
}

// recordingOpener opens files normally through the OS package but
// it records all of the calls for testing.
type recordingOpener struct {
	wrappedOpen func(string) (file, error)
	calls       []call
}

func (r *recordingOpener) recordedCalls(trimPrefix string) []call {
	var calls []call
	for _, c := range r.calls {
		calls = append(calls, call{
			method: c.method,
			path:   strings.TrimPrefix(c.path, trimPrefix),
		})
	}

	return calls
}

func (r *recordingOpener) open(path string) (file, error) {
	r.record("open", path)

	f, err := r.wrappedOpen(path)
	if err != nil {
		return nil, err
	}

	return &recordingFile{
		record: func(method string) { r.record(method, path) },
		file:   f,
	}, nil
}

func (r *recordingOpener) record(method, path string) {
	r.calls = append(r.calls, call{
		method: method,
		path:   path,
	})
}

type recordingFile struct {
	record func(string)
	file
}

func (r *recordingFile) Sync() error {
	r.record("sync")
	return r.file.Sync()
}

func (r *recordingFile) Close() error {
	r.record("close")
	return r.file.Close()
}

// recordingSyncer returns a new Syncer and a recordingOpener
// that records the open calls made by the Sync* functions.
func recordingSyncer() (Syncer, *recordingOpener) {
	syncer := NewSyncer()
	recorder := &recordingOpener{wrappedOpen: syncer.open}
	syncer.open = recorder.open

	return syncer, recorder
}

func createTestDirectoryHierarchy(tb testing.TB) (string, string) {
	tb.Helper()

	tmpDir := tb.TempDir()
	rootDir := filepath.Join(tmpDir, "root")
	for _, dir := range []string{
		"a/c",
		"a/d",
		"b/e",
		"b/f",
	} {
		require.NoError(tb, os.MkdirAll(filepath.Join(rootDir, dir), fs.ModePerm))
	}

	for _, path := range []string{
		"file_1",
		"file_2",
		"a/file_3",
		"a/file_4",
		"b/file_5",
		"b/file_6",
		"a/c/file_7",
		"a/c/file_8",
		"a/d/file_9",
		"a/d/file_10",
		"b/e/file_11",
		"b/e/file_12",
		"b/f/file_13",
		"b/f/file_14",
	} {
		require.NoError(tb, os.WriteFile(
			filepath.Join(rootDir, path), nil, fs.ModePerm,
		))
	}

	return tmpDir, rootDir
}

func expectedCalls(paths ...string) []call {
	var calls []call
	for _, path := range paths {
		calls = append(calls, []call{
			{method: "open", path: path},
			{method: "sync", path: path},
			{method: "close", path: path},
			// The second close is the deferred file close.
			{method: "close", path: path},
		}...)
	}

	return calls
}

func TestSync(t *testing.T) {
	for _, tc := range []struct {
		desc          string
		path          string
		expectedCalls []call
	}{
		{
			desc: "file",
			path: "/root/a/file_3",
		},
		{
			desc: "directory",
			path: "/root/a/c",
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			tmpDir, _ := createTestDirectoryHierarchy(t)

			syncer, recorder := recordingSyncer()
			require.NoError(t, syncer.Sync(filepath.Join(tmpDir, tc.path)))
			require.Equal(t, expectedCalls(tc.path), recorder.recordedCalls(tmpDir))
		})
	}
}

func TestSyncParent(t *testing.T) {
	tmpDir, _ := createTestDirectoryHierarchy(t)

	t.Run("parent exists", func(t *testing.T) {
		syncer, recorder := recordingSyncer()
		require.NoError(t, syncer.SyncParent(filepath.Join(tmpDir, "root", "file_1")))
		require.Equal(t,
			expectedCalls("/root"),
			recorder.recordedCalls(tmpDir),
		)
	})

	t.Run("trailing slash ignored", func(t *testing.T) {
		syncer, recorder := recordingSyncer()
		require.NoError(t, syncer.SyncParent(filepath.Join(tmpDir, "root", "file_1")+string(os.PathSeparator)))
		require.Equal(t,
			expectedCalls("/root"),
			recorder.recordedCalls(tmpDir),
		)
	})

	t.Run("non-existent child", func(t *testing.T) {
		// Since we are fsyncing just the parent, we don't really need to verify whether the
		// child itself exists.
		syncer, recorder := recordingSyncer()
		require.NoError(t, syncer.SyncParent(filepath.Join(tmpDir, "root", "non-existent")))
		require.Equal(t,
			expectedCalls("/root"),
			recorder.recordedCalls(tmpDir),
		)
	})

	t.Run("non-existent parent", func(t *testing.T) {
		syncer, recorder := recordingSyncer()

		path := filepath.Join(tmpDir, "root", "non-existent-parent", "non-existent-child")
		require.ErrorIs(
			t,
			syncer.SyncParent(path),
			fs.ErrNotExist,
		)

		require.Equal(t,
			[]call{{method: "open", path: "/root/non-existent-parent"}},
			recorder.recordedCalls(tmpDir),
		)
	})
}

func TestSyncHierarchy(t *testing.T) {
	tmpDir, rootDir := createTestDirectoryHierarchy(t)
	syncer, recorder := recordingSyncer()
	require.NoError(t, syncer.SyncHierarchy(rootDir, "a/c/file_7"))
	require.Equal(t,
		expectedCalls(
			"/root/a/c/file_7",
			"/root/a/c",
			"/root/a",
			"/root",
		),
		recorder.recordedCalls(tmpDir),
	)
}

func TestSyncRecursive(t *testing.T) {
	tmpDir, rootDir := createTestDirectoryHierarchy(t)
	syncer, recorder := recordingSyncer()
	require.NoError(t, syncer.SyncRecursive(filepath.Join(rootDir, "a")))
	require.Equal(t,
		expectedCalls(
			"/root/a",
			"/root/a/c",
			"/root/a/c/file_7",
			"/root/a/c/file_8",
			"/root/a/d",
			"/root/a/d/file_10",
			"/root/a/d/file_9",
			"/root/a/file_3",
			"/root/a/file_4",
		),
		recorder.recordedCalls(tmpDir),
	)
}