-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreserve_owner_test.go
71 lines (50 loc) · 1.51 KB
/
preserve_owner_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
//go:build !windows
// +build !windows
package aferocopy
import (
"errors"
"syscall"
"testing"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.nhat.io/aferomock"
)
func TestPreserveOwner_statFail(t *testing.T) {
t.Parallel()
srcFs := aferomock.MockFs(func(fs *aferomock.Fs) {
fs.On("Stat", mock.Anything).
Return(nil, errors.New("stat error"))
})(t)
actual := preserveOwner(srcFs, "resources", nil, "", nil)
expected := errors.New("stat error")
assert.Equal(t, expected, actual)
}
func TestPreserveOwner_chownSuccess(t *testing.T) {
t.Parallel()
const src = "resources/fixtures/data/case00/README.md"
srcFs := afero.NewOsFs()
info, err := srcFs.Stat(src)
require.NoError(t, err)
stat, ok := info.Sys().(*syscall.Stat_t)
require.True(t, ok)
expectedUID := int(stat.Uid)
expectedGID := int(stat.Gid)
destFs := aferomock.MockFs(func(fs *aferomock.Fs) {
fs.On("Chown", src, expectedUID, expectedGID).
Return(nil)
})(t)
err = preserveOwner(srcFs, src, destFs, src, nil)
require.NoError(t, err)
}
func TestPreserveOwner_chownFail(t *testing.T) {
t.Parallel()
destFs := aferomock.MockFs(func(fs *aferomock.Fs) {
fs.On("Chown", mock.Anything, mock.Anything, mock.Anything).
Return(errors.New("chown error"))
})(t)
actual := preserveOwner(afero.NewOsFs(), "resources/fixtures/data/case00/README.md", destFs, "", nil)
expected := errors.New("chown error")
assert.Equal(t, expected, actual)
}