From b0c7528ac6de816847e876aaca64d24eea37918e Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sat, 20 Sep 2025 04:16:50 +1000 Subject: [PATCH] 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 --- mtree_test.go | 90 ++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 55 deletions(-) diff --git a/mtree_test.go b/mtree_test.go index 35b2717..0afa06a 100644 --- a/mtree_test.go +++ b/mtree_test.go @@ -1,88 +1,68 @@ package mtree import ( + "bytes" "io" "os" "testing" "github.com/davecgh/go-spew/spew" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -var ( - testFiles = []struct { - Name string - Counts map[EntryType]int - Len int64 +func TestParser(t *testing.T) { + for _, test := range []struct { + name string + counts map[EntryType]int + size int64 }{ { - Name: "testdata/source.mtree", - Counts: map[EntryType]int{ - FullType: 0, + name: "testdata/source.mtree", + counts: map[EntryType]int{ + //FullType: 0, RelativeType: 45, CommentType: 37, SpecialType: 7, DotDotType: 17, BlankType: 34, }, - Len: int64(7887), + size: 7887, }, { - Name: "testdata/source.casync-mtree", - Counts: map[EntryType]int{ + name: "testdata/source.casync-mtree", + counts: map[EntryType]int{ FullType: 744, RelativeType: 56, - CommentType: 37, - SpecialType: 7, - DotDotType: 17, - BlankType: 34, }, - Len: int64(168439), + size: 168439, }, - } -) - -func TestParser(t *testing.T) { - for i, tf := range testFiles { - _ = i - func() { - fh, err := os.Open(tf.Name) - if err != nil { - t.Error(err) - return - } + } { + t.Run(test.name, func(t *testing.T) { + fh, err := os.Open(test.name) + require.NoError(t, err) defer fh.Close() - dh, err := ParseSpec(fh) - if err != nil { - t.Error(err) - } + var readSpecBuf bytes.Buffer + rdr := io.TeeReader(fh, &readSpecBuf) - if i == 1 { - spew.Dump(dh) - //buf, err := xml.MarshalIndent(dh, "", " ") - //if err == nil { - //t.Error(string(buf)) - //} - } + dh, err := ParseSpec(rdr) + require.NoErrorf(t, err, "parse spec %s", test.name) + + defer func() { + if t.Failed() { + t.Log(spew.Sdump(dh)) + } + }() gotNums := countTypes(dh) - for typ, num := range tf.Counts { - if gNum, ok := gotNums[typ]; ok { - if num != gNum { - t.Errorf("for type %s: expected %d, got %d", typ, num, gNum) - } - } - } + assert.Equal(t, test.counts, gotNums, "count of entry types mismatch") - i, err := dh.WriteTo(io.Discard) - if err != nil { - t.Error(err) - } - if i != tf.Len { - t.Errorf("expected to write %d, but wrote %d", tf.Len, i) - } - - }() + n, err := dh.WriteTo(io.Discard) + require.NoError(t, err, "write directory hierarchy representation") + assert.Equal(t, test.size, n, "output mtree spec should match input size") + // TODO: Verify that the output is equal to the input. + }) } }