1package fs_test
2
3import (
4	"os"
5	"path/filepath"
6	"runtime"
7	"testing"
8	"time"
9
10	"gotest.tools/assert"
11	"gotest.tools/fs"
12)
13
14func TestFromDir(t *testing.T) {
15	dir := fs.NewDir(t, "test-from-dir", fs.FromDir("testdata/copy-test"))
16	defer dir.Remove()
17
18	expected := fs.Expected(t,
19		fs.WithFile("1", "1\n"),
20		fs.WithDir("a",
21			fs.WithFile("1", "1\n"),
22			fs.WithFile("2", "2\n"),
23			fs.WithDir("b",
24				fs.WithFile("1", "1\n"))))
25
26	assert.Assert(t, fs.Equal(dir.Path(), expected))
27}
28
29func TestFromDirSymlink(t *testing.T) {
30	dir := fs.NewDir(t, "test-from-dir", fs.FromDir("testdata/copy-test-with-symlink"))
31	defer dir.Remove()
32
33	currentdir, err := os.Getwd()
34	assert.NilError(t, err)
35
36	link2 := filepath.FromSlash("../2")
37	link3 := "/some/inexistent/link"
38	if runtime.GOOS == "windows" {
39		link3 = filepath.Join(filepath.VolumeName(currentdir), link3)
40	}
41
42	expected := fs.Expected(t,
43		fs.WithFile("1", "1\n"),
44		fs.WithDir("a",
45			fs.WithFile("1", "1\n"),
46			fs.WithFile("2", "2\n"),
47			fs.WithDir("b",
48				fs.WithFile("1", "1\n"),
49				fs.WithSymlink("2", link2),
50				fs.WithSymlink("3", link3),
51				fs.WithSymlink("4", "5"),
52			)))
53
54	assert.Assert(t, fs.Equal(dir.Path(), expected))
55}
56
57func TestWithTimestamps(t *testing.T) {
58	stamp := time.Date(2011, 11, 11, 5, 55, 55, 0, time.UTC)
59	tmpFile := fs.NewFile(t, t.Name(), fs.WithTimestamps(stamp, stamp))
60	defer tmpFile.Remove()
61
62	stat, err := os.Stat(tmpFile.Path())
63	assert.NilError(t, err)
64	assert.DeepEqual(t, stat.ModTime(), stamp)
65}
66