-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs_store_test.go
140 lines (106 loc) · 3.38 KB
/
fs_store_test.go
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
package main_test
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"runtime"
"testing"
"time"
main "github.com/lesomnus/vcpkg-cache-http"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
func NewTestFsStore(t *testing.T) (main.Store, error) {
return main.NewFsStore(t.TempDir())
}
type FsStoreSetup struct{}
func (s *FsStoreSetup) New(t *testing.T) (main.Store, error) {
return NewTestFsStore(t)
}
func TestFsStoreSuite(t *testing.T) {
suite.Run(t, &StoreTestSuite{Store: &FsStoreSetup{}})
}
func TestFsStoreNew(t *testing.T) {
t.Run("create store directory if it does not exist", func(t *testing.T) {
require := require.New(t)
root := filepath.Join(t.TempDir(), "foo", "bar")
_, err := main.NewFsStore(root)
require.NoError(err)
require.DirExists(root)
})
}
func TestFsStore(t *testing.T) {
t.Run("work directory is created if not exist", func(t *testing.T) {
require := require.New(t)
root := t.TempDir()
work := filepath.Join(root, "foo")
_, err := os.Stat(work)
require.ErrorIs(err, os.ErrNotExist)
_, err = main.NewFsStore(root, main.WithWorkDir(work))
require.NoError(err)
_, err = os.Stat(work)
require.NoError(err)
})
t.Run("work directory is created if it is removed", func(t *testing.T) {
require := require.New(t)
root := t.TempDir()
work := filepath.Join(root, "foo")
store, err := main.NewFsStore(root, main.WithWorkDir(work))
require.NoError(err)
err = os.RemoveAll(work)
require.NoError(err)
err = store.Put(context.Background(), DescriptionFoo, bytes.NewReader([]byte{}))
require.NoError(err)
_, err = os.Stat(work)
require.NoError(err)
})
t.Run("store cannot be closed if the work left", func(t *testing.T) {
require := require.New(t)
store, err := main.NewFsStore(t.TempDir())
require.NoError(err)
r, w := io.Pipe()
go store.Put(context.Background(), DescriptionFoo, r)
time.Sleep(time.Millisecond * 10)
err = store.Close()
require.ErrorContains(err, "not empty")
w.Close()
time.Sleep(time.Millisecond * 10)
err = store.Close()
require.NoError(err)
})
}
func TestNewFsStore(t *testing.T) {
if runtime.GOOS == "windows" {
// TODO:
// `NewFsStore` function checks whether a file can be created in the working directory.
// In this test, `NewFsStore`'s check failure is reproduced with Permission,
// but it does not work on Windows (probably because it uses a different form of access control than Unix).
t.Skip("Skipping test on Windows")
}
t.Run("it cannot create store directory", func(t *testing.T) {
require := require.New(t)
root := filepath.Join(t.TempDir(), "foo")
_, err := os.OpenFile(root, os.O_CREATE, 0744)
require.NoError(err)
_, err = main.NewFsStore(root)
require.ErrorContains(err, "create store directory")
})
t.Run("file cannot be created in work directory", func(t *testing.T) {
require := require.New(t)
work := filepath.Join(t.TempDir(), "foo")
err := os.Mkdir(work, 0544)
require.NoError(err)
_, err = main.NewFsStore(t.TempDir(), main.WithWorkDir(work))
require.ErrorIs(err, os.ErrPermission)
})
t.Run("file cannot be renamed from work to store directory", func(t *testing.T) {
require := require.New(t)
root := filepath.Join(t.TempDir(), "foo")
err := os.Mkdir(root, 0544)
require.NoError(err)
_, err = main.NewFsStore(root, main.WithWorkDir(t.TempDir()))
require.ErrorContains(err, "rename file")
})
}