summaryrefslogtreecommitdiffstats
path: root/utils/file_test.go
blob: 6c7e3c46270d0012f70e025b1b79cc29b1a0b9ec (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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

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

func TestCopyDir(t *testing.T) {
	srcDir, err := ioutil.TempDir("", "src")
	require.NoError(t, err)
	defer os.RemoveAll(srcDir)

	dstParentDir, err := ioutil.TempDir("", "dstparent")
	require.NoError(t, err)
	defer os.RemoveAll(dstParentDir)

	dstDir := filepath.Join(dstParentDir, "dst")

	tempFile := "temp.txt"
	err = ioutil.WriteFile(filepath.Join(srcDir, tempFile), []byte("test file"), 0655)
	require.NoError(t, err)

	childDir := "child"
	err = os.Mkdir(filepath.Join(srcDir, childDir), 0777)
	require.NoError(t, err)

	childTempFile := "childtemp.txt"
	err = ioutil.WriteFile(filepath.Join(srcDir, childDir, childTempFile), []byte("test file"), 0755)
	require.NoError(t, err)

	err = CopyDir(srcDir, dstDir)
	assert.NoError(t, err)

	stat, err := os.Stat(filepath.Join(dstDir, tempFile))
	assert.NoError(t, err)
	assert.Equal(t, uint32(0655), uint32(stat.Mode()))
	assert.False(t, stat.IsDir())
	data, err := ioutil.ReadFile(filepath.Join(dstDir, tempFile))
	assert.NoError(t, err)
	assert.Equal(t, "test file", string(data))

	stat, err = os.Stat(filepath.Join(dstDir, childDir))
	assert.NoError(t, err)
	assert.True(t, stat.IsDir())

	stat, err = os.Stat(filepath.Join(dstDir, childDir, childTempFile))
	assert.NoError(t, err)
	assert.Equal(t, uint32(0755), uint32(stat.Mode()))
	assert.False(t, stat.IsDir())
	data, err = ioutil.ReadFile(filepath.Join(dstDir, childDir, childTempFile))
	assert.NoError(t, err)
	assert.Equal(t, "test file", string(data))

	err = CopyDir(srcDir, dstDir)
	assert.Error(t, err)
}