-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmtimehash_test.go
More file actions
90 lines (75 loc) · 2.09 KB
/
mtimehash_test.go
File metadata and controls
90 lines (75 loc) · 2.09 KB
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
package mtimehash
import (
"os"
"path"
"path/filepath"
"slices"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestProcess(t *testing.T) {
fileToContent := map[string]string{
"a.txt": "aaa",
"b.txt": "bbb",
"c.txt": "aaa",
}
files := setupFiles(t, fileToContent)
t.Run("happy path", func(t *testing.T) {
require.NoError(t, Process(slices.Values(files), 1000000000))
mtimes := getMtimes(t, files)
assert.Equal(t, map[string]int64{
"a.txt": 259627185,
"b.txt": 613142970,
"c.txt": 259627185,
}, mtimes)
})
t.Run("low maxUnixTime", func(t *testing.T) {
require.NoError(t, Process(slices.Values(files), 2))
mtimes := getMtimes(t, files)
assert.Equal(t, map[string]int64{
"a.txt": 1,
"b.txt": 0,
"c.txt": 1,
}, mtimes)
})
t.Run("errors", func(t *testing.T) {
var badFiles []string
badFiles = append(badFiles, "nonexistent.txt")
dirPath := filepath.Join(t.TempDir(), "dir")
require.NoError(t, os.Mkdir(dirPath, 0o777))
badFiles = append(badFiles, dirPath)
nonReadableFilePath := filepath.Join(t.TempDir(), "non-readable.txt")
require.NoError(t, os.WriteFile(nonReadableFilePath, []byte("non-readable"), 0o000))
badFiles = append(badFiles, nonReadableFilePath)
err := Process(slices.Values(slices.Concat(badFiles, files)), 1000000000)
assert.Error(t, err)
mtimes := getMtimes(t, files)
assert.Equal(t, map[string]int64{
"a.txt": 259627185,
"b.txt": 613142970,
"c.txt": 259627185,
}, mtimes)
})
}
func setupFiles(t *testing.T, files map[string]string) []string {
t.Helper()
tempDir := t.TempDir()
var filePaths []string
for name, content := range files {
filePath := filepath.Join(tempDir, name)
require.NoError(t, os.WriteFile(filePath, []byte(content), 0o666))
filePaths = append(filePaths, filePath)
}
return filePaths
}
func getMtimes(t *testing.T, files []string) map[string]int64 {
t.Helper()
mtimes := make(map[string]int64)
for _, file := range files {
s, err := os.Stat(file)
require.NoError(t, err)
mtimes[path.Base(file)] = s.ModTime().Unix()
}
return mtimes
}