1package vfstest
2
3import (
4	"os"
5	"runtime"
6	"testing"
7	"time"
8
9	"github.com/rclone/rclone/vfs"
10	"github.com/stretchr/testify/assert"
11	"github.com/stretchr/testify/require"
12)
13
14// TestFileModTime tests mod times on files
15func TestFileModTime(t *testing.T) {
16	run.skipIfNoFUSE(t)
17
18	run.createFile(t, "file", "123")
19
20	mtime := time.Date(2012, time.November, 18, 17, 32, 31, 0, time.UTC)
21	err := run.os.Chtimes(run.path("file"), mtime, mtime)
22	require.NoError(t, err)
23
24	info, err := run.os.Stat(run.path("file"))
25	require.NoError(t, err)
26
27	// avoid errors because of timezone differences
28	assert.Equal(t, info.ModTime().Unix(), mtime.Unix())
29
30	run.rm(t, "file")
31}
32
33// run.os.Create without opening for write too
34func osCreate(name string) (vfs.OsFiler, error) {
35	return run.os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
36}
37
38// run.os.Create with append
39func osAppend(name string) (vfs.OsFiler, error) {
40	return run.os.OpenFile(name, os.O_WRONLY|os.O_APPEND, 0666)
41}
42
43// TestFileModTimeWithOpenWriters tests mod time on open files
44func TestFileModTimeWithOpenWriters(t *testing.T) {
45	run.skipIfNoFUSE(t)
46	if runtime.GOOS == "windows" {
47		t.Skip("Skipping test on Windows")
48	}
49
50	mtime := time.Date(2012, time.November, 18, 17, 32, 31, 0, time.UTC)
51	filepath := run.path("cp-archive-test")
52
53	f, err := osCreate(filepath)
54	require.NoError(t, err)
55
56	_, err = f.Write([]byte{104, 105})
57	require.NoError(t, err)
58
59	err = run.os.Chtimes(filepath, mtime, mtime)
60	require.NoError(t, err)
61
62	err = f.Close()
63	require.NoError(t, err)
64
65	run.waitForWriters()
66
67	info, err := run.os.Stat(filepath)
68	require.NoError(t, err)
69
70	// avoid errors because of timezone differences
71	assert.Equal(t, info.ModTime().Unix(), mtime.Unix())
72
73	run.rm(t, "cp-archive-test")
74}
75