Skip to content

Commit

Permalink
migrate to testify
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenkoehler committed Aug 8, 2023
1 parent 36bc62e commit 1f0f558
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 38 deletions.
42 changes: 22 additions & 20 deletions digest/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/soerenkoehler/go-chdiff/digest"
"github.com/soerenkoehler/go-util-test/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type testCase struct {
Expand Down Expand Up @@ -60,12 +62,10 @@ func verifyDigest(

digest := digest.Calculate(createData(t, testdata), algorithm)

if len(*digest.Entries) != len(testdata) {
t.Fatal("Digest size must match number of input data points")
}
require.Equal(t, len(*digest.Entries), len(testdata))

for _, dataPoint := range testdata {
verifyDataPoint(t, dataPoint, digest.Entries)
assert.Equal(t, dataPoint.hash, (*digest.Entries)[dataPoint.path])
}
}

Expand All @@ -78,24 +78,26 @@ func createData(
for _, dataPoint := range testdata {
file := path.Join(root, dataPoint.path)
data.CreateRandomFile(file, dataPoint.size, dataPoint.seed)
// in := rand.New(rand.NewSource(2))
// io.CopyN(os.Stdout, in, 16)
}

return root
}

func verifyDataPoint(
t *testing.T,
dataPoint testCase,
hashes *digest.FileHashes) {

expectedPath := dataPoint.path
expectedHash := dataPoint.hash

actualHash := (*hashes)[expectedPath]
if actualHash != expectedHash {
t.Errorf("hash mismatch\nexpected: %v\nactual: %v\ntest file: %v",
expectedHash,
actualHash,
expectedPath)
}
}
// func verifyDataPoint(
// t *testing.T,
// dataPoint testCase,
// hashes *digest.FileHashes) {

// expectedPath := dataPoint.path
// expectedHash := dataPoint.hash

// actualHash := (*hashes)[expectedPath]
// if actualHash != expectedHash {
// t.Errorf("hash mismatch\nexpected: %v\nactual: %v\ntest file: %v",
// expectedHash,
// actualHash,
// expectedPath)
// }
// }
6 changes: 3 additions & 3 deletions digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"time"

"github.com/soerenkoehler/go-chdiff/digest"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)

func TestInvalidHash(t *testing.T) {
require.PanicsWithError(t, "invalid hash bad-hash", func() {
assert.PanicsWithError(t, "invalid hash bad-hash", func() {
digest := digest.NewDigest("path", time.Now())
digest.AddFileHash("file", "bad-hash")
})
}

func TestHashTypeMismatch(t *testing.T) {
require.PanicsWithError(t, "hash type mismatch old=1 new=2", func() {
assert.PanicsWithError(t, "hash type mismatch old=1 new=2", func() {
digest := digest.NewDigest("path", time.Now())
digest.AddFileHash("file", createRandomHash(32))
digest.AddFileHash("file", createRandomHash(64))
Expand Down
3 changes: 2 additions & 1 deletion digest/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func Load(digestPath, digestFile string) (Digest, error) {
lines := bufio.NewScanner(input)
for lines.Scan() {
normalized := strings.Replace(lines.Text(), SEPARATOR_TEXT, SEPARATOR_BINARY, 1)
println(normalized)
tokens := strings.SplitN(normalized, SEPARATOR_BINARY, 2)
if len(tokens) != 2 {
return Digest{}, fmt.Errorf("invalid digest file")
Expand All @@ -42,7 +43,7 @@ func Save(digest Digest, digestFile string) error {
output, err := os.Create(digestFile)
if err == nil {
for k, v := range *digest.Entries {
fmt.Fprintf(output, "%v%v%v", v, SEPARATOR_BINARY, k)
fmt.Fprintf(output, "%v%v%v\n", v, SEPARATOR_BINARY, k)
}
os.Chtimes(digestFile, digest.Location.Time, digest.Location.Time)
}
Expand Down
17 changes: 6 additions & 11 deletions digest/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/soerenkoehler/go-chdiff/digest"
"github.com/stretchr/testify/assert"
)

func TestLoadNonexistantFile(t *testing.T) {
path := "../testdata/digest/file/path-without-digest"
file := filepath.Join(path, "test-digest.txt")
expectedError := fmt.Sprintf("lstat %v: no such file or directory", file)
_, err := digest.Load(path, file)
if err == nil || err.Error() != expectedError {
t.Fatalf("\nexpected: %v\n actual: %v", expectedError, err)
}
assert.EqualError(t, err, fmt.Sprintf("lstat %v: no such file or directory", file))
}

func TestSaveLoad256(t *testing.T) {
Expand All @@ -33,13 +31,10 @@ func testSaveLoad(t *testing.T, hashsize int) {
digestTime := time.Now()
digestFile := filepath.Join(digestPath, "test-digest.txt")
expected := digest.NewDigest(digestPath, digestTime)
expected.AddFileHash("file", createRandomHash(hashsize))
expected.AddFileHash("file1", createRandomHash(hashsize))
expected.AddFileHash("file2", createRandomHash(hashsize))
digest.Save(expected, digestFile)
actual, err := digest.Load(digestPath, digestFile)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(expected, actual) {
t.Fatal(cmp.Diff(expected, actual))
}
assert.Nil(t, err)
assert.True(t, cmp.Equal(expected, actual), cmp.Diff(expected, actual))
}
5 changes: 2 additions & 3 deletions digest/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"time"

"github.com/soerenkoehler/go-chdiff/digest"
"github.com/stretchr/testify/assert"
)

func TestNewDigestTruncatesTime(t *testing.T) {
digest := digest.NewDigest("",
time.Date(1999, 12, 31, 23, 59, 58, 999999, time.Local))
if digest.Location.Time.Nanosecond() != 0 {
t.Fatal("NewDigest() should truncate nanoseconds")
}
assert.Equal(t, 0, digest.Location.Time.Nanosecond())
}

0 comments on commit 1f0f558

Please sign in to comment.