aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-23 01:20:25 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-23 01:20:25 +0200
commitb3b6e4bf6c9a14514bcfb8406d9cc86d9d18bd06 (patch)
tree65f792759394d1bc08e18d6987e4da1887990376
parentcac8795d33cd52d362e02590b4df4ccd9b96ff71 (diff)
downloaddna-backup-b3b6e4bf6c9a14514bcfb8406d9cc86d9d18bd06.tar.gz
dna-backup-b3b6e4bf6c9a14514bcfb8406d9cc86d9d18bd06.zip
better tests (util/io subtests + check log output)
-rw-r--r--repo_test.go7
-rw-r--r--utils/io_test.go81
2 files changed, 50 insertions, 38 deletions
diff --git a/repo_test.go b/repo_test.go
index 52da9ae..6ab7cb0 100644
--- a/repo_test.go
+++ b/repo_test.go
@@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
+ "strings"
"testing"
"github.com/n-peugnet/dna-backup/logger"
@@ -128,6 +129,9 @@ func TestReadFiles3(t *testing.T) {
}
func TestNoSuchFile(t *testing.T) {
+ var output bytes.Buffer
+ logger.SetOutput(&output)
+ defer logger.SetOutput(os.Stderr)
tmpDir := t.TempDir()
os.Symlink("./notexisting", filepath.Join(tmpDir, "linknotexisting"))
var buff bytes.Buffer
@@ -136,6 +140,9 @@ func TestNoSuchFile(t *testing.T) {
concatFiles(&files, utils.NopCloser(&buff))
testutils.AssertLen(t, 0, files, "Files")
testutils.AssertLen(t, 0, buff.Bytes(), "Buffer")
+ if !strings.Contains(output.String(), "linknotexisting") {
+ t.Errorf("log should contain a warning, actual %q", &output)
+ }
}
func TestLoadChunks(t *testing.T) {
diff --git a/utils/io_test.go b/utils/io_test.go
index 5f9c8c7..ad1a576 100644
--- a/utils/io_test.go
+++ b/utils/io_test.go
@@ -27,45 +27,50 @@ type wrapper struct {
w utils.WriteWrapper
}
-var wrappers = []wrapper{
- {"Zlib", utils.ZlibReader, utils.ZlibWriter},
- {"Nop", utils.NopReadWrapper, utils.NopWriteWrapper},
-}
-
func TestWrappers(t *testing.T) {
+ wrappers := []wrapper{
+ {"Zlib", utils.ZlibReader, utils.ZlibWriter},
+ {"Nop", utils.NopReadWrapper, utils.NopWriteWrapper},
+ }
for _, wrapper := range wrappers {
- var buff bytes.Buffer
- var err error
- w := wrapper.w(&buff)
- n, err := w.Write([]byte("test"))
- if err != nil {
- t.Error(wrapper.n, err)
- }
- if n != 4 {
- t.Error(wrapper.n, "expected: 4, actual:", n)
- }
- err = w.Close()
- if err != nil {
- t.Error(wrapper.n, err)
- }
- r, err := wrapper.r(&buff)
- if err != nil {
- t.Error(wrapper.n, err)
- }
- b := make([]byte, 4)
- n, err = r.Read(b)
- if n != 4 {
- t.Error(wrapper.n, "expected: 4, actual:", n)
- }
- if err != nil && err != io.EOF {
- t.Error(wrapper.n, err)
- }
- if !bytes.Equal(b, []byte("test")) {
- t.Errorf("%s, expected: %q, actual: %q", "test", wrapper.n, b)
- }
- n, err = r.Read(b)
- if err != io.EOF {
- t.Error(wrapper.n, err)
- }
+ t.Run(wrapper.n, func(t *testing.T) {
+ testWrapper(t, wrapper)
+ })
+ }
+}
+
+func testWrapper(t *testing.T, wrapper wrapper) {
+ var buff bytes.Buffer
+ var err error
+ w := wrapper.w(&buff)
+ n, err := w.Write([]byte("test"))
+ if err != nil {
+ t.Error(wrapper.n, err)
+ }
+ if n != 4 {
+ t.Error(wrapper.n, "expected: 4, actual:", n)
+ }
+ err = w.Close()
+ if err != nil {
+ t.Error(wrapper.n, err)
+ }
+ r, err := wrapper.r(&buff)
+ if err != nil {
+ t.Error(wrapper.n, err)
+ }
+ b := make([]byte, 4)
+ n, err = r.Read(b)
+ if n != 4 {
+ t.Error(wrapper.n, "expected: 4, actual:", n)
+ }
+ if err != nil && err != io.EOF {
+ t.Error(wrapper.n, err)
+ }
+ if !bytes.Equal(b, []byte("test")) {
+ t.Errorf("%s, expected: %q, actual: %q", "test", wrapper.n, b)
+ }
+ n, err = r.Read(b)
+ if err != io.EOF {
+ t.Error(wrapper.n, err)
}
}