1package vfstest
2
3import (
4	"runtime"
5	"testing"
6
7	"github.com/stretchr/testify/require"
8)
9
10// TestTouchAndDelete checks that writing a zero byte file and immediately
11// deleting it is not racy. See https://github.com/rclone/rclone/issues/1181
12func TestTouchAndDelete(t *testing.T) {
13	run.skipIfNoFUSE(t)
14	run.checkDir(t, "")
15
16	run.createFile(t, "touched", "")
17	run.rm(t, "touched")
18
19	run.checkDir(t, "")
20}
21
22// TestRenameOpenHandle checks that a file with open writers is successfully
23// renamed after all writers close. See https://github.com/rclone/rclone/issues/2130
24func TestRenameOpenHandle(t *testing.T) {
25	run.skipIfNoFUSE(t)
26	if runtime.GOOS == "windows" {
27		t.Skip("Skipping test on Windows")
28	}
29
30	run.checkDir(t, "")
31
32	// create file
33	example := []byte("Some Data")
34	path := run.path("rename")
35	file, err := osCreate(path)
36	require.NoError(t, err)
37
38	// write some data
39	_, err = file.Write(example)
40	require.NoError(t, err)
41	err = file.Sync()
42	require.NoError(t, err)
43
44	// attempt to rename open file
45	err = run.os.Rename(path, path+"bla")
46	require.NoError(t, err)
47
48	// close open writers to allow rename on remote to go through
49	err = file.Close()
50	require.NoError(t, err)
51
52	run.waitForWriters()
53
54	// verify file was renamed properly
55	run.checkDir(t, "renamebla 9")
56
57	// cleanup
58	run.rm(t, "renamebla")
59	run.checkDir(t, "")
60}
61