Merge pull request #2 from vbatts/testing

Tests pass
This commit is contained in:
Vincent Batts 2016-04-06 12:47:56 -04:00
commit b595e50fec
5 changed files with 76 additions and 25 deletions

View file

@ -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

View file

@ -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 {

View file

@ -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",
}

View file

@ -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
}

View file

@ -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)
}