diff options
author | n-peugnet <n.peugnet@free.fr> | 2021-09-09 18:55:06 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2021-09-09 18:55:06 +0200 |
commit | bea4f87f1e4528630d326699a9c7fd6133ee7806 (patch) | |
tree | 49bff8fed5e9e1a7d4546ad41454e6ee07f6fa25 /repo_test.go | |
parent | 3e866db2accc6ed5aa65befdf21474dbc7b1ec18 (diff) | |
download | dna-backup-bea4f87f1e4528630d326699a9c7fd6133ee7806.tar.gz dna-backup-bea4f87f1e4528630d326699a9c7fd6133ee7806.zip |
better commit and restore tests (almost complete)
added some file path management function for prfixes and trailing slash
Diffstat (limited to 'repo_test.go')
-rw-r--r-- | repo_test.go | 71 |
1 files changed, 50 insertions, 21 deletions
diff --git a/repo_test.go b/repo_test.go index 5127263..cd8a88e 100644 --- a/repo_test.go +++ b/repo_test.go @@ -231,41 +231,70 @@ func TestBsdiff(t *testing.T) { func TestCommit(t *testing.T) { dest := t.TempDir() source := path.Join("testdata", "logs") + expected := path.Join("testdata", "repo_8k") repo := NewRepo(dest) repo.Commit(source) - recipe := loadRecipe(path.Join(dest, "00000", recipeName)) - log.Println(recipe) + assertSameTree(t, assertCompatibleRepoFile, expected, dest, "Commit") } func TestRestore(t *testing.T) { dest := t.TempDir() source := path.Join("testdata", "repo_8k") + expected := path.Join("testdata", "logs") repo := NewRepo(source) repo.Restore(dest) - destFiles := listFiles(dest) - sourceFiles := listFiles(path.Join("testdata", "logs")) - sfCount := len(sourceFiles) - if sfCount <= 0 { - t.Fatalf("No source files: %d", sfCount) + assertSameTree(t, assertSameFile, expected, dest, "Restore") +} + +func assertSameTree(t *testing.T, apply func(t *testing.T, expected string, actual string, prefix string), expected string, actual string, prefix string) { + actualFiles := listFiles(actual) + expectedFiles := listFiles(expected) + efCount := len(expectedFiles) + if efCount <= 0 { + t.Fatalf("No expected files: %d", efCount) } - dfCount := len(destFiles) - if sfCount != dfCount { - t.Fatalf("Incorrect number for destination files: %d, should be %d", dfCount, sfCount) + afCount := len(actualFiles) + if efCount != afCount { + t.Fatalf("Incorrect number of files: %d, should be %d", afCount, efCount) } - for i, sf := range sourceFiles { - sfContent, err := os.ReadFile(sf.Path) - if err != nil { - t.Fatalf("Error reading from source file '%s': %s", sf.Path, err) + for i, ef := range expectedFiles { + af := actualFiles[i] + efRelPath := ef.Path[len(expected):] + afRelPath := af.Path[len(actual):] + if efRelPath != afRelPath { + t.Fatalf("File path '%s' does not match '%s'", afRelPath, efRelPath) } - df := destFiles[i] - dfContent, err := os.ReadFile(df.Path) - if err != nil { - t.Fatalf("Error reading from source file '%s': %s", df.Path, err) - } - if bytes.Compare(sfContent, dfContent) != 0 { - t.Errorf("File content of '%s' does not match '%s'", df.Path, sf.Path) + apply(t, ef.Path, af.Path, prefix) + } +} + +func assertCompatibleRepoFile(t *testing.T, expected string, actual string, prefix string) { + if path.Base(expected) == filesName { + eFiles := loadFileList(expected) + aFiles := loadFileList(actual) + assertLen(t, len(eFiles), aFiles, prefix) + for i := 0; i < len(eFiles); i++ { + if eFiles[i] != aFiles[i] { + t.Fatal(prefix, "file entry do not match:", aFiles[i], ", expected:", eFiles[i]) + } } + } else if path.Base(expected) == recipeName { + // TODO: check recipies equality + } else { + assertSameFile(t, expected, actual, prefix) + } +} + +func assertSameFile(t *testing.T, expected string, actual string, prefix string) { + efContent, err := os.ReadFile(expected) + if err != nil { + t.Fatalf("%s Error reading from expected file '%s': %s", prefix, expected, err) + } + afContent, err := os.ReadFile(actual) + if err != nil { + t.Fatalf("%s Error reading from expected file '%s': %s", prefix, actual, err) } + assertSameSlice(t, efContent, afContent, prefix+" files") } func assertLen(t *testing.T, expected int, actual interface{}, prefix string) { |