From 88ad58d6cdc1684b72409353961aaab80cf56af2 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 22 Aug 2020 20:39:03 +0200 Subject: [PATCH] integration tests: Redirect directory diff into intermediate buffer --- cmd/restic/integration_helpers_test.go | 33 +++++++------------ cmd/restic/integration_helpers_unix_test.go | 15 +++++---- .../integration_helpers_windows_test.go | 9 ++--- cmd/restic/integration_test.go | 12 +++---- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/cmd/restic/integration_helpers_test.go b/cmd/restic/integration_helpers_test.go index 136ec6fe0..0069e27a9 100644 --- a/cmd/restic/integration_helpers_test.go +++ b/cmd/restic/integration_helpers_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "fmt" "io/ioutil" @@ -75,14 +76,13 @@ func sameModTime(fi1, fi2 os.FileInfo) bool { return fi1.ModTime().Equal(fi2.ModTime()) } -// directoriesEqualContents checks if both directories contain exactly the same -// contents. -func directoriesEqualContents(dir1, dir2 string) bool { +// directoriesContentsDiff returns a diff between both directories. If these +// contain exactly the same contents, then the diff is an empty string. +func directoriesContentsDiff(dir1, dir2 string) string { + var out bytes.Buffer ch1 := walkDir(dir1) ch2 := walkDir(dir2) - changes := false - var a, b *dirEntry for { var ok bool @@ -106,36 +106,27 @@ func directoriesEqualContents(dir1, dir2 string) bool { } if ch1 == nil { - fmt.Printf("+%v\n", b.path) - changes = true + fmt.Fprintf(&out, "+%v\n", b.path) } else if ch2 == nil { - fmt.Printf("-%v\n", a.path) - changes = true - } else if !a.equals(b) { + fmt.Fprintf(&out, "-%v\n", a.path) + } else if !a.equals(&out, b) { if a.path < b.path { - fmt.Printf("-%v\n", a.path) - changes = true + fmt.Fprintf(&out, "-%v\n", a.path) a = nil continue } else if a.path > b.path { - fmt.Printf("+%v\n", b.path) - changes = true + fmt.Fprintf(&out, "+%v\n", b.path) b = nil continue } else { - fmt.Printf("%%%v\n", a.path) - changes = true + fmt.Fprintf(&out, "%%%v\n", a.path) } } a, b = nil, nil } - if changes { - return false - } - - return true + return out.String() } type dirStat struct { diff --git a/cmd/restic/integration_helpers_unix_test.go b/cmd/restic/integration_helpers_unix_test.go index 2a06db63d..1130d2638 100644 --- a/cmd/restic/integration_helpers_unix_test.go +++ b/cmd/restic/integration_helpers_unix_test.go @@ -4,25 +4,26 @@ package main import ( "fmt" + "io" "io/ioutil" "os" "path/filepath" "syscall" ) -func (e *dirEntry) equals(other *dirEntry) bool { +func (e *dirEntry) equals(out io.Writer, other *dirEntry) bool { if e.path != other.path { - fmt.Fprintf(os.Stderr, "%v: path does not match (%v != %v)\n", e.path, e.path, other.path) + fmt.Fprintf(out, "%v: path does not match (%v != %v)\n", e.path, e.path, other.path) return false } if e.fi.Mode() != other.fi.Mode() { - fmt.Fprintf(os.Stderr, "%v: mode does not match (%v != %v)\n", e.path, e.fi.Mode(), other.fi.Mode()) + fmt.Fprintf(out, "%v: mode does not match (%v != %v)\n", e.path, e.fi.Mode(), other.fi.Mode()) return false } if !sameModTime(e.fi, other.fi) { - fmt.Fprintf(os.Stderr, "%v: ModTime does not match (%v != %v)\n", e.path, e.fi.ModTime(), other.fi.ModTime()) + fmt.Fprintf(out, "%v: ModTime does not match (%v != %v)\n", e.path, e.fi.ModTime(), other.fi.ModTime()) return false } @@ -30,17 +31,17 @@ func (e *dirEntry) equals(other *dirEntry) bool { stat2, _ := other.fi.Sys().(*syscall.Stat_t) if stat.Uid != stat2.Uid { - fmt.Fprintf(os.Stderr, "%v: UID does not match (%v != %v)\n", e.path, stat.Uid, stat2.Uid) + fmt.Fprintf(out, "%v: UID does not match (%v != %v)\n", e.path, stat.Uid, stat2.Uid) return false } if stat.Gid != stat2.Gid { - fmt.Fprintf(os.Stderr, "%v: GID does not match (%v != %v)\n", e.path, stat.Gid, stat2.Gid) + fmt.Fprintf(out, "%v: GID does not match (%v != %v)\n", e.path, stat.Gid, stat2.Gid) return false } if stat.Nlink != stat2.Nlink { - fmt.Fprintf(os.Stderr, "%v: Number of links do not match (%v != %v)\n", e.path, stat.Nlink, stat2.Nlink) + fmt.Fprintf(out, "%v: Number of links do not match (%v != %v)\n", e.path, stat.Nlink, stat2.Nlink) return false } diff --git a/cmd/restic/integration_helpers_windows_test.go b/cmd/restic/integration_helpers_windows_test.go index 9e3fbac9b..85285efb7 100644 --- a/cmd/restic/integration_helpers_windows_test.go +++ b/cmd/restic/integration_helpers_windows_test.go @@ -4,23 +4,24 @@ package main import ( "fmt" + "io" "io/ioutil" "os" ) -func (e *dirEntry) equals(other *dirEntry) bool { +func (e *dirEntry) equals(out io.Writer, other *dirEntry) bool { if e.path != other.path { - fmt.Fprintf(os.Stderr, "%v: path does not match (%v != %v)\n", e.path, e.path, other.path) + fmt.Fprintf(out, "%v: path does not match (%v != %v)\n", e.path, e.path, other.path) return false } if e.fi.Mode() != other.fi.Mode() { - fmt.Fprintf(os.Stderr, "%v: mode does not match (%v != %v)\n", e.path, e.fi.Mode(), other.fi.Mode()) + fmt.Fprintf(out, "%v: mode does not match (%v != %v)\n", e.path, e.fi.Mode(), other.fi.Mode()) return false } if !sameModTime(e.fi, other.fi) { - fmt.Fprintf(os.Stderr, "%v: ModTime does not match (%v != %v)\n", e.path, e.fi.ModTime(), other.fi.ModTime()) + fmt.Fprintf(out, "%v: ModTime does not match (%v != %v)\n", e.path, e.fi.ModTime(), other.fi.ModTime()) return false } diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index f162fb634..43cf8ed41 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -314,8 +314,8 @@ func TestBackup(t *testing.T) { restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i)) t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir) testRunRestore(t, env.gopts, restoredir, snapshotIDs[0]) - rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")), - "directories are not equal") + diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, "testdata")) + rtest.Assert(t, diff == "", "directories are not equal: %v", diff) } testRunCheck(t, env.gopts) @@ -856,8 +856,8 @@ func TestRestore(t *testing.T) { restoredir := filepath.Join(env.base, "restore") testRunRestoreLatest(t, env.gopts, restoredir, nil, nil) - rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata))), - "directories are not equal") + diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata))) + rtest.Assert(t, diff == "", "directories are not equal %v", diff) } func TestRestoreLatest(t *testing.T) { @@ -1276,8 +1276,8 @@ func TestHardLink(t *testing.T) { restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i)) t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir) testRunRestore(t, env.gopts, restoredir, snapshotIDs[0]) - rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")), - "directories are not equal") + diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, "testdata")) + rtest.Assert(t, diff == "", "directories are not equal %v", diff) linkResults := createFileSetPerHardlink(filepath.Join(restoredir, "testdata")) rtest.Assert(t, linksEqual(linkTests, linkResults),