1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2025-10-03 20:21:01 +00:00

mtree test: correctly check number of types

It seems that this test actually had a bug that was not caught until I
tried to refactor it. The new test has behaviour that makes more sense
to me.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai 2025-09-20 04:16:50 +10:00
parent 3252a4ad82
commit b0c7528ac6
No known key found for this signature in database
GPG key ID: 2897FAD2B7E9446F

View file

@ -1,88 +1,68 @@
package mtree package mtree
import ( import (
"bytes"
"io" "io"
"os" "os"
"testing" "testing"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
var ( func TestParser(t *testing.T) {
testFiles = []struct { for _, test := range []struct {
Name string name string
Counts map[EntryType]int counts map[EntryType]int
Len int64 size int64
}{ }{
{ {
Name: "testdata/source.mtree", name: "testdata/source.mtree",
Counts: map[EntryType]int{ counts: map[EntryType]int{
FullType: 0, //FullType: 0,
RelativeType: 45, RelativeType: 45,
CommentType: 37, CommentType: 37,
SpecialType: 7, SpecialType: 7,
DotDotType: 17, DotDotType: 17,
BlankType: 34, BlankType: 34,
}, },
Len: int64(7887), size: 7887,
}, },
{ {
Name: "testdata/source.casync-mtree", name: "testdata/source.casync-mtree",
Counts: map[EntryType]int{ counts: map[EntryType]int{
FullType: 744, FullType: 744,
RelativeType: 56, RelativeType: 56,
CommentType: 37,
SpecialType: 7,
DotDotType: 17,
BlankType: 34,
}, },
Len: int64(168439), size: 168439,
}, },
} } {
) t.Run(test.name, func(t *testing.T) {
fh, err := os.Open(test.name)
func TestParser(t *testing.T) { require.NoError(t, err)
for i, tf := range testFiles {
_ = i
func() {
fh, err := os.Open(tf.Name)
if err != nil {
t.Error(err)
return
}
defer fh.Close() defer fh.Close()
dh, err := ParseSpec(fh) var readSpecBuf bytes.Buffer
if err != nil { rdr := io.TeeReader(fh, &readSpecBuf)
t.Error(err)
}
if i == 1 { dh, err := ParseSpec(rdr)
spew.Dump(dh) require.NoErrorf(t, err, "parse spec %s", test.name)
//buf, err := xml.MarshalIndent(dh, "", " ")
//if err == nil { defer func() {
//t.Error(string(buf)) if t.Failed() {
//} t.Log(spew.Sdump(dh))
} }
}()
gotNums := countTypes(dh) gotNums := countTypes(dh)
for typ, num := range tf.Counts { assert.Equal(t, test.counts, gotNums, "count of entry types mismatch")
if gNum, ok := gotNums[typ]; ok {
if num != gNum {
t.Errorf("for type %s: expected %d, got %d", typ, num, gNum)
}
}
}
i, err := dh.WriteTo(io.Discard) n, err := dh.WriteTo(io.Discard)
if err != nil { require.NoError(t, err, "write directory hierarchy representation")
t.Error(err) assert.Equal(t, test.size, n, "output mtree spec should match input size")
} // TODO: Verify that the output is equal to the input.
if i != tf.Len { })
t.Errorf("expected to write %d, but wrote %d", tf.Len, i)
}
}()
} }
} }