integration tests: Redirect directory diff into intermediate buffer

This commit is contained in:
Michael Eischer 2020-08-22 20:39:03 +02:00
parent 591a8c4cdf
commit 88ad58d6cd
4 changed files with 31 additions and 38 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -75,14 +76,13 @@ func sameModTime(fi1, fi2 os.FileInfo) bool {
return fi1.ModTime().Equal(fi2.ModTime()) return fi1.ModTime().Equal(fi2.ModTime())
} }
// directoriesEqualContents checks if both directories contain exactly the same // directoriesContentsDiff returns a diff between both directories. If these
// contents. // contain exactly the same contents, then the diff is an empty string.
func directoriesEqualContents(dir1, dir2 string) bool { func directoriesContentsDiff(dir1, dir2 string) string {
var out bytes.Buffer
ch1 := walkDir(dir1) ch1 := walkDir(dir1)
ch2 := walkDir(dir2) ch2 := walkDir(dir2)
changes := false
var a, b *dirEntry var a, b *dirEntry
for { for {
var ok bool var ok bool
@ -106,36 +106,27 @@ func directoriesEqualContents(dir1, dir2 string) bool {
} }
if ch1 == nil { if ch1 == nil {
fmt.Printf("+%v\n", b.path) fmt.Fprintf(&out, "+%v\n", b.path)
changes = true
} else if ch2 == nil { } else if ch2 == nil {
fmt.Printf("-%v\n", a.path) fmt.Fprintf(&out, "-%v\n", a.path)
changes = true } else if !a.equals(&out, b) {
} else if !a.equals(b) {
if a.path < b.path { if a.path < b.path {
fmt.Printf("-%v\n", a.path) fmt.Fprintf(&out, "-%v\n", a.path)
changes = true
a = nil a = nil
continue continue
} else if a.path > b.path { } else if a.path > b.path {
fmt.Printf("+%v\n", b.path) fmt.Fprintf(&out, "+%v\n", b.path)
changes = true
b = nil b = nil
continue continue
} else { } else {
fmt.Printf("%%%v\n", a.path) fmt.Fprintf(&out, "%%%v\n", a.path)
changes = true
} }
} }
a, b = nil, nil a, b = nil, nil
} }
if changes { return out.String()
return false
}
return true
} }
type dirStat struct { type dirStat struct {

View file

@ -4,25 +4,26 @@ package main
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
) )
func (e *dirEntry) equals(other *dirEntry) bool { func (e *dirEntry) equals(out io.Writer, other *dirEntry) bool {
if e.path != other.path { 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 return false
} }
if e.fi.Mode() != other.fi.Mode() { 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 return false
} }
if !sameModTime(e.fi, other.fi) { 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 return false
} }
@ -30,17 +31,17 @@ func (e *dirEntry) equals(other *dirEntry) bool {
stat2, _ := other.fi.Sys().(*syscall.Stat_t) stat2, _ := other.fi.Sys().(*syscall.Stat_t)
if stat.Uid != stat2.Uid { 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 return false
} }
if stat.Gid != stat2.Gid { 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 return false
} }
if stat.Nlink != stat2.Nlink { 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 return false
} }

View file

@ -4,23 +4,24 @@ package main
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
) )
func (e *dirEntry) equals(other *dirEntry) bool { func (e *dirEntry) equals(out io.Writer, other *dirEntry) bool {
if e.path != other.path { 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 return false
} }
if e.fi.Mode() != other.fi.Mode() { 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 return false
} }
if !sameModTime(e.fi, other.fi) { 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 return false
} }

View file

@ -314,8 +314,8 @@ func TestBackup(t *testing.T) {
restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i)) restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i))
t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir) t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir)
testRunRestore(t, env.gopts, restoredir, snapshotIDs[0]) testRunRestore(t, env.gopts, restoredir, snapshotIDs[0])
rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")), diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, "testdata"))
"directories are not equal") rtest.Assert(t, diff == "", "directories are not equal: %v", diff)
} }
testRunCheck(t, env.gopts) testRunCheck(t, env.gopts)
@ -856,8 +856,8 @@ func TestRestore(t *testing.T) {
restoredir := filepath.Join(env.base, "restore") restoredir := filepath.Join(env.base, "restore")
testRunRestoreLatest(t, env.gopts, restoredir, nil, nil) testRunRestoreLatest(t, env.gopts, restoredir, nil, nil)
rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata))), diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata)))
"directories are not equal") rtest.Assert(t, diff == "", "directories are not equal %v", diff)
} }
func TestRestoreLatest(t *testing.T) { func TestRestoreLatest(t *testing.T) {
@ -1276,8 +1276,8 @@ func TestHardLink(t *testing.T) {
restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i)) restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i))
t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir) t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir)
testRunRestore(t, env.gopts, restoredir, snapshotIDs[0]) testRunRestore(t, env.gopts, restoredir, snapshotIDs[0])
rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")), diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, "testdata"))
"directories are not equal") rtest.Assert(t, diff == "", "directories are not equal %v", diff)
linkResults := createFileSetPerHardlink(filepath.Join(restoredir, "testdata")) linkResults := createFileSetPerHardlink(filepath.Join(restoredir, "testdata"))
rtest.Assert(t, linksEqual(linkTests, linkResults), rtest.Assert(t, linksEqual(linkTests, linkResults),