From 7777d9a010353edd60fdeb0767a77d96b92a1cc9 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 5 Apr 2016 16:47:36 -0400 Subject: [PATCH 1/4] cksum: test is fine. commenting. Signed-off-by: Vincent Batts --- cksum_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cksum_test.go b/cksum_test.go index 5402c38..b8aa2f7 100644 --- a/cksum_test.go +++ b/cksum_test.go @@ -11,6 +11,7 @@ var ( checkSize = 9110 ) +// testing that the cksum function matches that of cksum(1) utility (silly POSIX crc32) func TestCksum(t *testing.T) { fh, err := os.Open(checkFile) if err != nil { From 34b9f4dc46c119efa90c6005b2210acbf7950e48 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 5 Apr 2016 17:16:44 -0400 Subject: [PATCH 2/4] hierarchy: testing works Signed-off-by: Vincent Batts --- hierarchy.go | 15 +++++++++++++++ mtree_test.go | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/hierarchy.go b/hierarchy.go index 2d4bec5..3c70b5e 100644 --- a/hierarchy.go +++ b/hierarchy.go @@ -84,3 +84,18 @@ const ( DotDotType // .. - A relative path step. keywords/options are ignored FullType // if the first word on the line has a `/` after the first character, it interpretted as a file pathname with options ) + +// String returns the name of the EntryType +func (et EntryType) String() string { + return typeNames[et] +} + +var typeNames = map[EntryType]string{ + SignatureType: "SignatureType", + BlankType: "BlankType", + CommentType: "CommentType", + SpecialType: "SpecialType", + RelativeType: "RelativeType", + DotDotType: "DotDotType", + FullType: "FullType", +} diff --git a/mtree_test.go b/mtree_test.go index b4f12bd..836fa6f 100644 --- a/mtree_test.go +++ b/mtree_test.go @@ -1,14 +1,23 @@ package mtree import ( - "fmt" + "io/ioutil" "os" "testing" ) -var testFiles = []string{ - "testdata/source.mtree", -} +var ( + testFiles = []string{"testdata/source.mtree"} + numEntries = map[EntryType]int{ + FullType: 0, + RelativeType: 45, + CommentType: 37, + SpecialType: 7, + DotDotType: 17, + BlankType: 34, + } + expectedLength = int64(7887) +) func TestParser(t *testing.T) { for _, file := range testFiles { @@ -24,13 +33,36 @@ func TestParser(t *testing.T) { if err != nil { t.Error(err) } - fmt.Printf("%q", dh) + gotNums := countTypes(dh) + for typ, num := range numEntries { + if gNum, ok := gotNums[typ]; ok { + if num != gNum { + t.Errorf("for type %s: expected %d, got %d", typ, num, gNum) + } + } + } - _, err = dh.WriteTo(os.Stdout) + i, err := dh.WriteTo(ioutil.Discard) if err != nil { t.Error(err) } + if i != expectedLength { + t.Errorf("expected to write %d, but wrote %d", expectedLength, i) + } }() } } + +func countTypes(dh *DirectoryHierarchy) map[EntryType]int { + nT := map[EntryType]int{} + for i := range dh.Entries { + typ := dh.Entries[i].Type + if _, ok := nT[typ]; !ok { + nT[typ] = 1 + } else { + nT[typ]++ + } + } + return nT +} From 9c6b4257f4ecd5b430ae0d7b5ff36b0c01ccb3c9 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 5 Apr 2016 18:13:27 -0400 Subject: [PATCH 3/4] check: now the simple test passes Signed-off-by: Vincent Batts --- check_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/check_test.go b/check_test.go index e0dccec..eac2e56 100644 --- a/check_test.go +++ b/check_test.go @@ -1,9 +1,6 @@ package mtree -import ( - "log" - "testing" -) +import "testing" func TestCheck(t *testing.T) { dh, err := Walk(".", nil, append(DefaultKeywords, "sha1")) @@ -15,6 +12,10 @@ func TestCheck(t *testing.T) { if err != nil { t.Fatal(err) } - //log.Fatalf("%#v", dh) - log.Fatalf("%#v", res) + + if len(res.Failures) > 0 { + t.Errorf("%#v", res) + } } + +// TODO make a directory, walk it, check it, modify it and ensure it fails From 99dcd25a7ea46003d8a0a4754dd9b270332c1979 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Wed, 6 Apr 2016 12:45:08 -0400 Subject: [PATCH 4/4] walk: test passes and validates parse Signed-off-by: Vincent Batts --- walk_test.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/walk_test.go b/walk_test.go index c37fb87..75c404a 100644 --- a/walk_test.go +++ b/walk_test.go @@ -2,6 +2,7 @@ package mtree import ( "io/ioutil" + "os" "testing" ) @@ -10,26 +11,27 @@ func TestWalk(t *testing.T) { if err != nil { t.Fatal(err) } - - //log.Fatalf("%#v", dh) + numEntries = countTypes(dh) fh, err := ioutil.TempFile("", "walk.") if err != nil { t.Fatal(err) } + defer os.Remove(fh.Name()) + defer fh.Close() if _, err = dh.WriteTo(fh); err != nil { - t.Error(err) + t.Fatal(err) } - fh.Close() - t.Fatal(fh.Name()) - //os.Remove(fh.Name()) -} - -func TestReadNames(t *testing.T) { - names, err := readOrderedDirNames(".") - if err != nil { - t.Error(err) + if _, err := fh.Seek(0, 0); err != nil { + t.Fatal(err) + } + if dh, err = ParseSpec(fh); err != nil { + t.Fatal(err) + } + for k, v := range countTypes(dh) { + if numEntries[k] != v { + t.Errorf("for type %s: expected %d, got %d", k, numEntries[k], v) + } } - t.Errorf("names: %q", names) }