cmd: rewrite touch tests #1934

This commit is contained in:
Jakub Tasiemski 2018-02-02 14:46:56 +01:00 committed by Nick Craig-Wood
parent 295c3fabec
commit 1018e9bb27

View file

@ -6,56 +6,114 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fstest" "github.com/ncw/rclone/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
_ "github.com/ncw/rclone/backend/local" _ "github.com/ncw/rclone/backend/local"
) )
func TestTouch(t *testing.T) { var (
fstest.Initialise() t1 = fstest.Time("2017-02-03T04:05:06.499999999Z")
f, err := fs.NewFs("testfiles") )
err = Touch(f, "newFile")
require.NoError(t, err)
file, errFile := f.NewObject("newFile")
require.NoError(t, errFile)
err = file.Remove()
require.NoError(t, err)
notCreateNewFile = true func checkFile(t *testing.T, r fs.Fs, path string, content string) {
err = Touch(f, "fileWithCflag") layout := defaultLayout
if len(timeAsArgument) == len(layoutDateWithTime) {
layout = layoutDateWithTime
}
timeAtrFromFlags, err := time.Parse(layout, timeAsArgument)
require.NoError(t, err) require.NoError(t, err)
file, errFile = f.NewObject("fileWithCflag") file1 := fstest.NewItem(path, content, timeAtrFromFlags)
require.Error(t, errFile) fstest.CheckItems(t, r, file1)
notCreateNewFile = false }
timeAsArgument = "060102" // TestMain drives the tests
err = Touch(f, "oldFile") func TestMain(m *testing.M) {
require.NoError(t, err) fstest.TestMain(m)
file, err = f.NewObject("oldFile") }
require.NoError(t, err)
curretTime := time.Now()
require.NoError(t, err)
print(file.ModTime().Year() < curretTime.Year())
assert.Equal(t, true, file.ModTime().Year() < curretTime.Year())
err = file.Remove()
require.NoError(t, err)
timeAsArgument = "2006-01-02T15:04:05" func TestTouchOneFile(t *testing.T) {
err = Touch(f, "oldFile") r := fstest.NewRun(t)
require.NoError(t, err) defer r.Finalise()
file, err = f.NewObject("oldFile")
require.NoError(t, err)
assert.Equal(t, true, file.ModTime().Year() < curretTime.Year())
timeAsArgument = "" err := Touch(r.Fremote, "newFile")
err = Touch(f, "oldFile")
require.NoError(t, err) require.NoError(t, err)
file, err = f.NewObject("oldFile") _, err = r.Fremote.NewObject("newFile")
require.NoError(t, err)
timeBetween2007YearAndCurrent, errTime := time.Parse("060102", "121212")
require.NoError(t, errTime)
assert.Equal(t, true, file.ModTime().Year() > timeBetween2007YearAndCurrent.Year())
err = file.Remove()
require.NoError(t, err) require.NoError(t, err)
} }
func TestTouchWithNoCreateFlag(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
notCreateNewFile = true
err := Touch(r.Fremote, "newFile")
require.NoError(t, err)
_, err = r.Fremote.NewObject("newFile")
require.Error(t, err)
notCreateNewFile = false
}
func TestTouchWithTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
timeAsArgument = "060102"
srcFileName := "oldFile"
err := Touch(r.Fremote, srcFileName)
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, "")
}
func TestTouchWithLognerTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
timeAsArgument = "2006-01-02T15:04:05"
srcFileName := "oldFile"
err := Touch(r.Fremote, srcFileName)
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, "")
}
func TestTouchUpdateTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
srcFileName := "a"
content := "aaa"
file1 := r.WriteObject(srcFileName, content, t1)
fstest.CheckItems(t, r.Fremote, file1)
timeAsArgument = "121212"
err := Touch(r.Fremote, "a")
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, content)
}
func TestTouchUpdateTimestampWithCFlag(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
srcFileName := "a"
content := "aaa"
file1 := r.WriteObject(srcFileName, content, t1)
fstest.CheckItems(t, r.Fremote, file1)
notCreateNewFile = true
timeAsArgument = "121212"
err := Touch(r.Fremote, "a")
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, content)
notCreateNewFile = false
}
func TestTouchCreateMultipleDirAndFile(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
longPath := "a/b/c.txt"
err := Touch(r.Fremote, longPath)
require.NoError(t, err)
file1 := fstest.NewItem("a/b/c.txt", "", t1)
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{"a", "a/b"}, fs.ModTimeNotSupported)
}