lib/model: Introduce must test utility (#5586)

* lib/model: Introduce must test utility

* nice
This commit is contained in:
Simon Frei 2019-03-09 19:45:36 +01:00 committed by Audrius Butkevicius
parent 27ff20faa3
commit 189e44488e
5 changed files with 58 additions and 168 deletions

View File

@ -35,15 +35,9 @@ func TestRecvOnlyRevertDeletes(t *testing.T) {
testOs.MkdirAll("_recvonly/.stfolder", 0755) testOs.MkdirAll("_recvonly/.stfolder", 0755)
testOs.MkdirAll("_recvonly/ignDir", 0755) testOs.MkdirAll("_recvonly/ignDir", 0755)
testOs.MkdirAll("_recvonly/unknownDir", 0755) testOs.MkdirAll("_recvonly/unknownDir", 0755)
if err := ioutil.WriteFile("_recvonly/ignDir/ignFile", []byte("hello\n"), 0644); err != nil { must(t, ioutil.WriteFile("_recvonly/ignDir/ignFile", []byte("hello\n"), 0644))
t.Fatal(err) must(t, ioutil.WriteFile("_recvonly/unknownDir/unknownFile", []byte("hello\n"), 0644))
} must(t, ioutil.WriteFile("_recvonly/.stignore", []byte("ignDir\n"), 0644))
if err := ioutil.WriteFile("_recvonly/unknownDir/unknownFile", []byte("hello\n"), 0644); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile("_recvonly/.stignore", []byte("ignDir\n"), 0644); err != nil {
t.Fatal(err)
}
knownFiles := setupKnownFiles(t, []byte("hello\n")) knownFiles := setupKnownFiles(t, []byte("hello\n"))
@ -166,15 +160,11 @@ func TestRecvOnlyRevertNeeds(t *testing.T) {
// Update the file. // Update the file.
newData := []byte("totally different data\n") newData := []byte("totally different data\n")
if err := ioutil.WriteFile("_recvonly/knownDir/knownFile", newData, 0644); err != nil { must(t, ioutil.WriteFile("_recvonly/knownDir/knownFile", newData, 0644))
t.Fatal(err)
}
// Rescan. // Rescan.
if err := m.ScanFolder("ro"); err != nil { must(t, m.ScanFolder("ro"))
t.Fatal(err)
}
// We now have a newer file than the rest of the cluster. Global state should reflect this. // We now have a newer file than the rest of the cluster. Global state should reflect this.
@ -268,13 +258,9 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
// Create a file and modify another // Create a file and modify another
file := "_recvonly/foo" file := "_recvonly/foo"
if err := ioutil.WriteFile(file, []byte("hello\n"), 0644); err != nil { must(t, ioutil.WriteFile(file, []byte("hello\n"), 0644))
t.Fatal(err)
}
if err := ioutil.WriteFile("_recvonly/knownDir/knownFile", []byte("bye\n"), 0644); err != nil { must(t, ioutil.WriteFile("_recvonly/knownDir/knownFile", []byte("bye\n"), 0644))
t.Fatal(err)
}
m.ScanFolder("ro") m.ScanFolder("ro")
@ -286,9 +272,7 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
// Remove the file again and undo the modification // Remove the file again and undo the modification
testOs.Remove(file) testOs.Remove(file)
if err := ioutil.WriteFile("_recvonly/knownDir/knownFile", oldData, 0644); err != nil { must(t, ioutil.WriteFile("_recvonly/knownDir/knownFile", oldData, 0644))
t.Fatal(err)
}
folderFs.Chtimes("knownDir/knownFile", knownFiles[1].ModTime(), knownFiles[1].ModTime()) folderFs.Chtimes("knownDir/knownFile", knownFiles[1].ModTime(), knownFiles[1].ModTime())
m.ScanFolder("ro") m.ScanFolder("ro")
@ -303,9 +287,7 @@ func setupKnownFiles(t *testing.T, data []byte) []protocol.FileInfo {
testOs := &fatalOs{t} testOs := &fatalOs{t}
testOs.MkdirAll("_recvonly/knownDir", 0755) testOs.MkdirAll("_recvonly/knownDir", 0755)
if err := ioutil.WriteFile("_recvonly/knownDir/knownFile", data, 0644); err != nil { must(t, ioutil.WriteFile("_recvonly/knownDir/knownFile", data, 0644))
t.Fatal(err)
}
t0 := time.Now().Add(-1 * time.Minute) t0 := time.Now().Add(-1 * time.Minute)
testOs.Chtimes("_recvonly/knownDir/knownFile", t0, t0) testOs.Chtimes("_recvonly/knownDir/knownFile", t0, t0)

View File

@ -79,18 +79,12 @@ func createFile(t *testing.T, name string, fs fs.Filesystem) protocol.FileInfo {
t.Helper() t.Helper()
f, err := fs.Create(name) f, err := fs.Create(name)
if err != nil { must(t, err)
t.Fatal(err)
}
f.Close() f.Close()
fi, err := fs.Stat(name) fi, err := fs.Stat(name)
if err != nil { must(t, err)
t.Fatal(err)
}
file, err := scanner.CreateFileInfo(fi, name, fs) file, err := scanner.CreateFileInfo(fi, name, fs)
if err != nil { must(t, err)
t.Fatal(err)
}
return file return file
} }
@ -323,9 +317,7 @@ func TestWeakHash(t *testing.T) {
} }
f, err := ffs.Create("weakhash") f, err := ffs.Create("weakhash")
if err != nil { must(t, err)
t.Fatal(err)
}
defer f.Close() defer f.Close()
_, err = io.CopyN(f, rand.Reader, size) _, err = io.CopyN(f, rand.Reader, size)
if err != nil { if err != nil {
@ -652,23 +644,15 @@ func TestIssue3164(t *testing.T) {
ignDir := filepath.Join("issue3164", "oktodelete") ignDir := filepath.Join("issue3164", "oktodelete")
subDir := filepath.Join(ignDir, "foobar") subDir := filepath.Join(ignDir, "foobar")
if err := ffs.MkdirAll(subDir, 0777); err != nil { must(t, ffs.MkdirAll(subDir, 0777))
t.Fatal(err) must(t, ioutil.WriteFile(filepath.Join(tmpDir, subDir, "file"), []byte("Hello"), 0644))
} must(t, ioutil.WriteFile(filepath.Join(tmpDir, ignDir, "file"), []byte("Hello"), 0644))
if err := ioutil.WriteFile(filepath.Join(tmpDir, subDir, "file"), []byte("Hello"), 0644); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, ignDir, "file"), []byte("Hello"), 0644); err != nil {
t.Fatal(err)
}
file := protocol.FileInfo{ file := protocol.FileInfo{
Name: "issue3164", Name: "issue3164",
} }
matcher := ignore.New(ffs) matcher := ignore.New(ffs)
if err := matcher.Parse(bytes.NewBufferString("(?d)oktodelete"), ""); err != nil { must(t, matcher.Parse(bytes.NewBufferString("(?d)oktodelete"), ""))
t.Fatal(err)
}
dbUpdateChan := make(chan dbUpdateJob, 1) dbUpdateChan := make(chan dbUpdateJob, 1)
@ -757,13 +741,9 @@ func TestDeleteIgnorePerms(t *testing.T) {
defer file.Close() defer file.Close()
stat, err := file.Stat() stat, err := file.Stat()
if err != nil { must(t, err)
t.Fatal(err)
}
fi, err := scanner.CreateFileInfo(stat, name, ffs) fi, err := scanner.CreateFileInfo(stat, name, ffs)
if err != nil { must(t, err)
t.Fatal(err)
}
ffs.Chmod(name, 0600) ffs.Chmod(name, 0600)
scanChan := make(chan string) scanChan := make(chan string)
finished := make(chan struct{}) finished := make(chan struct{})
@ -776,9 +756,7 @@ func TestDeleteIgnorePerms(t *testing.T) {
<-finished <-finished
case <-finished: case <-finished:
} }
if err != nil { must(t, err)
t.Fatal(err)
}
} }
func TestCopyOwner(t *testing.T) { func TestCopyOwner(t *testing.T) {

View File

@ -2343,13 +2343,9 @@ func TestIssue3028(t *testing.T) {
// Create two files that we'll delete, one with a name that is a prefix of the other. // Create two files that we'll delete, one with a name that is a prefix of the other.
if err := ioutil.WriteFile("testdata/testrm", []byte("Hello"), 0644); err != nil { must(t, ioutil.WriteFile("testdata/testrm", []byte("Hello"), 0644))
t.Fatal(err)
}
defer testOs.Remove("testdata/testrm") defer testOs.Remove("testdata/testrm")
if err := ioutil.WriteFile("testdata/testrm2", []byte("Hello"), 0644); err != nil { must(t, ioutil.WriteFile("testdata/testrm2", []byte("Hello"), 0644))
t.Fatal(err)
}
defer testOs.Remove("testdata/testrm2") defer testOs.Remove("testdata/testrm2")
// Create a model and default folder // Create a model and default folder
@ -2831,26 +2827,17 @@ func TestIssue2571(t *testing.T) {
}() }()
for _, dir := range []string{"toLink", "linkTarget"} { for _, dir := range []string{"toLink", "linkTarget"} {
err := testFs.MkdirAll(dir, 0775) must(t, testFs.MkdirAll(dir, 0775))
if err != nil {
t.Fatal(err)
}
fd, err := testFs.Create(filepath.Join(dir, "a")) fd, err := testFs.Create(filepath.Join(dir, "a"))
if err != nil { must(t, err)
t.Fatal(err)
}
fd.Close() fd.Close()
} }
m := setupModel(w) m := setupModel(w)
if err := testFs.RemoveAll("toLink"); err != nil { must(t, testFs.RemoveAll("toLink"))
t.Fatal(err)
}
if err := osutil.DebugSymlinkForTestsOnly(filepath.Join(testFs.URI(), "linkTarget"), filepath.Join(testFs.URI(), "toLink")); err != nil { must(t, osutil.DebugSymlinkForTestsOnly(filepath.Join(testFs.URI(), "linkTarget"), filepath.Join(testFs.URI(), "toLink")))
t.Fatal(err)
}
m.ScanFolder("default") m.ScanFolder("default")
@ -2879,25 +2866,17 @@ func TestIssue4573(t *testing.T) {
os.Remove(w.ConfigPath()) os.Remove(w.ConfigPath())
}() }()
err := testFs.MkdirAll("inaccessible", 0755) must(t, testFs.MkdirAll("inaccessible", 0755))
if err != nil {
t.Fatal(err)
}
defer testFs.Chmod("inaccessible", 0777) defer testFs.Chmod("inaccessible", 0777)
file := filepath.Join("inaccessible", "a") file := filepath.Join("inaccessible", "a")
fd, err := testFs.Create(file) fd, err := testFs.Create(file)
if err != nil { must(t, err)
t.Fatal(err)
}
fd.Close() fd.Close()
m := setupModel(w) m := setupModel(w)
err = testFs.Chmod("inaccessible", 0000) must(t, testFs.Chmod("inaccessible", 0000))
if err != nil {
t.Fatal(err)
}
m.ScanFolder("default") m.ScanFolder("default")
@ -2931,8 +2910,7 @@ func TestInternalScan(t *testing.T) {
for _, dir := range baseDirs { for _, dir := range baseDirs {
sub := filepath.Join(dir, "subDir") sub := filepath.Join(dir, "subDir")
for _, dir := range []string{dir, sub} { for _, dir := range []string{dir, sub} {
err := testFs.MkdirAll(dir, 0775) if err := testFs.MkdirAll(dir, 0775); err != nil {
if err != nil {
t.Fatalf("%v: %v", dir, err) t.Fatalf("%v: %v", dir, err)
} }
} }
@ -2942,9 +2920,7 @@ func TestInternalScan(t *testing.T) {
for _, dir := range []string{dir, sub} { for _, dir := range []string{dir, sub} {
file := filepath.Join(dir, "a") file := filepath.Join(dir, "a")
fd, err := testFs.Create(file) fd, err := testFs.Create(file)
if err != nil { must(t, err)
t.Fatal(err)
}
fd.Close() fd.Close()
testCases[file] = func(f protocol.FileInfo) bool { testCases[file] = func(f protocol.FileInfo) bool {
return !f.Deleted return !f.Deleted
@ -2955,15 +2931,11 @@ func TestInternalScan(t *testing.T) {
m := setupModel(w) m := setupModel(w)
for _, dir := range baseDirs { for _, dir := range baseDirs {
if err := testFs.RemoveAll(dir); err != nil { must(t, testFs.RemoveAll(dir))
t.Fatal(err)
}
} }
fd, err := testFs.Create("dirToFile") fd, err := testFs.Create("dirToFile")
if err != nil { must(t, err)
t.Fatal(err)
}
fd.Close() fd.Close()
m.ScanFolder("default") m.ScanFolder("default")
@ -3029,10 +3001,7 @@ func TestRemoveDirWithContent(t *testing.T) {
defaultFs.MkdirAll("dirwith", 0755) defaultFs.MkdirAll("dirwith", 0755)
content := filepath.Join("dirwith", "content") content := filepath.Join("dirwith", "content")
fd, err := defaultFs.Create(content) fd, err := defaultFs.Create(content)
if err != nil { must(t, err)
t.Fatal(err)
return
}
fd.Close() fd.Close()
m := setupModel(defaultCfgWrapper) m := setupModel(defaultCfgWrapper)
@ -3099,17 +3068,10 @@ func TestIssue4475(t *testing.T) {
// This should result in the directory being recreated and added to the // This should result in the directory being recreated and added to the
// db locally. // db locally.
err := testFs.MkdirAll("delDir", 0755) must(t, testFs.MkdirAll("delDir", 0755))
if err != nil {
t.Fatal(err)
}
m.ScanFolder("default") m.ScanFolder("default")
if err = testFs.RemoveAll("delDir"); err != nil {
t.Fatal(err)
}
m.ScanFolder("default") m.ScanFolder("default")
if fcfg, ok := m.cfg.Folder("default"); !ok || !fcfg.SharedWith(device1) { if fcfg, ok := m.cfg.Folder("default"); !ok || !fcfg.SharedWith(device1) {
@ -3158,9 +3120,7 @@ func TestVersionRestore(t *testing.T) {
// We verify that the content matches at the expected filenames // We verify that the content matches at the expected filenames
// after the restore operation. // after the restore operation.
dir, err := ioutil.TempDir("", "") dir, err := ioutil.TempDir("", "")
if err != nil { must(t, err)
t.Fatal(err)
}
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
fcfg := config.NewFolderConfiguration(myID, "default", "default", fs.FilesystemTypeBasic, dir) fcfg := config.NewFolderConfiguration(myID, "default", "default", fs.FilesystemTypeBasic, dir)
@ -3178,9 +3138,7 @@ func TestVersionRestore(t *testing.T) {
m.ScanFolder("default") m.ScanFolder("default")
sentinel, err := time.ParseInLocation(versioner.TimeFormat, "20200101-010101", locationLocal) sentinel, err := time.ParseInLocation(versioner.TimeFormat, "20200101-010101", locationLocal)
if err != nil { must(t, err)
t.Fatal(err)
}
sentinelTag := sentinel.Format(versioner.TimeFormat) sentinelTag := sentinel.Format(versioner.TimeFormat)
for _, file := range []string{ for _, file := range []string{
@ -3206,9 +3164,7 @@ func TestVersionRestore(t *testing.T) {
file = filepath.FromSlash(file) file = filepath.FromSlash(file)
} }
dir := filepath.Dir(file) dir := filepath.Dir(file)
if err := filesystem.MkdirAll(dir, 0755); err != nil { must(t, filesystem.MkdirAll(dir, 0755))
t.Fatal(err)
}
if fd, err := filesystem.Create(file); err != nil { if fd, err := filesystem.Create(file); err != nil {
t.Fatal(err) t.Fatal(err)
} else if _, err := fd.Write([]byte(file)); err != nil { } else if _, err := fd.Write([]byte(file)); err != nil {
@ -3221,9 +3177,7 @@ func TestVersionRestore(t *testing.T) {
} }
versions, err := m.GetFolderVersions("default") versions, err := m.GetFolderVersions("default")
if err != nil { must(t, err)
t.Fatal(err)
}
expectedVersions := map[string]int{ expectedVersions := map[string]int{
"file.txt": 1, "file.txt": 1,
"existing": 1, "existing": 1,
@ -3273,9 +3227,7 @@ func TestVersionRestore(t *testing.T) {
} }
ferr, err := m.RestoreFolderVersions("default", restore) ferr, err := m.RestoreFolderVersions("default", restore)
if err != nil { must(t, err)
t.Fatal(err)
}
if err, ok := ferr["something"]; len(ferr) > 1 || !ok || err != "cannot replace a non-file" { if err, ok := ferr["something"]; len(ferr) > 1 || !ok || err != "cannot replace a non-file" {
t.Fatalf("incorrect error or count: %d %s", len(ferr), ferr) t.Fatalf("incorrect error or count: %d %s", len(ferr), ferr)
@ -3327,9 +3279,7 @@ func TestVersionRestore(t *testing.T) {
taggedArchivedName := filepath.Join(".stversions", taggedName) taggedArchivedName := filepath.Join(".stversions", taggedName)
fd, err := filesystem.Open(taggedArchivedName) fd, err := filesystem.Open(taggedArchivedName)
if err != nil { must(t, err)
t.Fatal(err)
}
defer fd.Close() defer fd.Close()
content, err := ioutil.ReadAll(fd) content, err := ioutil.ReadAll(fd)
@ -3373,9 +3323,7 @@ func TestPausedFolders(t *testing.T) {
pausedConfig := wrapper.RawCopy() pausedConfig := wrapper.RawCopy()
pausedConfig.Folders[0].Paused = true pausedConfig.Folders[0].Paused = true
w, err := m.cfg.Replace(pausedConfig) w, err := m.cfg.Replace(pausedConfig)
if err != nil { must(t, err)
t.Fatal(err)
}
w.Wait() w.Wait()
if err := m.ScanFolder("default"); err != ErrFolderPaused { if err := m.ScanFolder("default"); err != ErrFolderPaused {
@ -3412,9 +3360,7 @@ func TestIssue4094(t *testing.T) {
} }
cfg.Folders = []config.FolderConfiguration{fcfg} cfg.Folders = []config.FolderConfiguration{fcfg}
p, err := wrapper.Replace(cfg) p, err := wrapper.Replace(cfg)
if err != nil { must(t, err)
t.Fatal(err)
}
p.Wait() p.Wait()
if err := m.SetIgnores(fcfg.ID, []string{"foo"}); err != nil { if err := m.SetIgnores(fcfg.ID, []string{"foo"}); err != nil {
@ -3451,9 +3397,7 @@ func TestIssue4903(t *testing.T) {
} }
cfg.Folders = []config.FolderConfiguration{fcfg} cfg.Folders = []config.FolderConfiguration{fcfg}
p, err := wrapper.Replace(cfg) p, err := wrapper.Replace(cfg)
if err != nil { must(t, err)
t.Fatal(err)
}
p.Wait() p.Wait()
if err := fcfg.CheckPath(); err != config.ErrPathMissing { if err := fcfg.CheckPath(); err != config.ErrPathMissing {

View File

@ -482,9 +482,7 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
payload := []byte("hello") payload := []byte("hello")
if err := ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777); err != nil { must(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777))
t.Fatal(err)
}
received := make(chan protocol.FileInfo) received := make(chan protocol.FileInfo)
fc.mut.Lock() fc.mut.Lock()
@ -521,9 +519,7 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
payload = []byte("bye") payload = []byte("bye")
buf = make([]byte, len(payload)) buf = make([]byte, len(payload))
if err := ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777); err != nil { must(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777))
t.Fatal(err)
}
_, err = m.Request(device1, "default", "foo", int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false) _, err = m.Request(device1, "default", "foo", int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false)
if err == nil { if err == nil {
@ -573,9 +569,7 @@ func TestParentDeletion(t *testing.T) {
} }
// Delete parent dir // Delete parent dir
if err := testFs.RemoveAll(parent); err != nil { must(t, testFs.RemoveAll(parent))
t.Fatal(err)
}
// Scan only the child dir (not the parent) // Scan only the child dir (not the parent)
if err := m.ScanFolderSubdirs("default", []string{child}); err != nil { if err := m.ScanFolderSubdirs("default", []string{child}); err != nil {
@ -783,9 +777,7 @@ func TestRequestRemoteRenameChanged(t *testing.T) {
} }
for _, n := range [2]string{a, b} { for _, n := range [2]string{a, b} {
if err := equalContents(filepath.Join(tmpDir, n), data[n]); err != nil { must(t, equalContents(filepath.Join(tmpDir, n), data[n]))
t.Fatal(err)
}
} }
var gotA, gotB, gotConfl bool var gotA, gotB, gotConfl bool
@ -912,9 +904,7 @@ func TestRequestRemoteRenameConflict(t *testing.T) {
} }
for _, n := range [2]string{a, b} { for _, n := range [2]string{a, b} {
if err := equalContents(filepath.Join(tmpDir, n), data[n]); err != nil { must(t, equalContents(filepath.Join(tmpDir, n), data[n]))
t.Fatal(err)
}
} }
fd, err := tfs.OpenFile(b, fs.OptReadWrite, 0644) fd, err := tfs.OpenFile(b, fs.OptReadWrite, 0644)

View File

@ -21,40 +21,38 @@ type fatalOs struct {
fatal fatal
} }
func (f *fatalOs) must(fn func() error) { func must(f fatal, err error) {
f.Helper() f.Helper()
if err := fn(); err != nil { if err != nil {
f.Fatal(err) f.Fatal(err)
} }
} }
func (f *fatalOs) Chmod(name string, mode os.FileMode) { func (f *fatalOs) Chmod(name string, mode os.FileMode) {
f.Helper() f.Helper()
f.must(func() error { return os.Chmod(name, mode) }) must(f, os.Chmod(name, mode))
} }
func (f *fatalOs) Chtimes(name string, atime time.Time, mtime time.Time) { func (f *fatalOs) Chtimes(name string, atime time.Time, mtime time.Time) {
f.Helper() f.Helper()
f.must(func() error { return os.Chtimes(name, atime, mtime) }) must(f, os.Chtimes(name, atime, mtime))
} }
func (f *fatalOs) Create(name string) *os.File { func (f *fatalOs) Create(name string) *os.File {
f.Helper() f.Helper()
file, err := os.Create(name) file, err := os.Create(name)
if err != nil { must(f, err)
f.Fatal(err)
}
return file return file
} }
func (f *fatalOs) Mkdir(name string, perm os.FileMode) { func (f *fatalOs) Mkdir(name string, perm os.FileMode) {
f.Helper() f.Helper()
f.must(func() error { return os.Mkdir(name, perm) }) must(f, os.Mkdir(name, perm))
} }
func (f *fatalOs) MkdirAll(name string, perm os.FileMode) { func (f *fatalOs) MkdirAll(name string, perm os.FileMode) {
f.Helper() f.Helper()
f.must(func() error { return os.MkdirAll(name, perm) }) must(f, os.MkdirAll(name, perm))
} }
func (f *fatalOs) Remove(name string) { func (f *fatalOs) Remove(name string) {
@ -73,14 +71,12 @@ func (f *fatalOs) RemoveAll(name string) {
func (f *fatalOs) Rename(oldname, newname string) { func (f *fatalOs) Rename(oldname, newname string) {
f.Helper() f.Helper()
f.must(func() error { return os.Rename(oldname, newname) }) must(f, os.Rename(oldname, newname))
} }
func (f *fatalOs) Stat(name string) os.FileInfo { func (f *fatalOs) Stat(name string) os.FileInfo {
f.Helper() f.Helper()
info, err := os.Stat(name) info, err := os.Stat(name)
if err != nil { must(f, err)
f.Fatal(err)
}
return info return info
} }