1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-22 00:15:39 +00:00

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 package mtree
import ( import "testing"
"log"
"testing"
)
func TestCheck(t *testing.T) { func TestCheck(t *testing.T) {
dh, err := Walk(".", nil, append(DefaultKeywords, "sha1")) dh, err := Walk(".", nil, append(DefaultKeywords, "sha1"))
@ -15,6 +12,10 @@ func TestCheck(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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 checkSize = 9110
) )
// testing that the cksum function matches that of cksum(1) utility (silly POSIX crc32)
func TestCksum(t *testing.T) { func TestCksum(t *testing.T) {
fh, err := os.Open(checkFile) fh, err := os.Open(checkFile)
if err != nil { if err != nil {

View file

@ -84,3 +84,18 @@ const (
DotDotType // .. - A relative path step. keywords/options are ignored 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 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 package mtree
import ( import (
"fmt" "io/ioutil"
"os" "os"
"testing" "testing"
) )
var testFiles = []string{ var (
"testdata/source.mtree", 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) { func TestParser(t *testing.T) {
for _, file := range testFiles { for _, file := range testFiles {
@ -24,13 +33,36 @@ func TestParser(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) 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 { if err != nil {
t.Error(err) 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 ( import (
"io/ioutil" "io/ioutil"
"os"
"testing" "testing"
) )
@ -10,26 +11,27 @@ func TestWalk(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
numEntries = countTypes(dh)
//log.Fatalf("%#v", dh)
fh, err := ioutil.TempFile("", "walk.") fh, err := ioutil.TempFile("", "walk.")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(fh.Name())
defer fh.Close()
if _, err = dh.WriteTo(fh); err != nil { if _, err = dh.WriteTo(fh); err != nil {
t.Error(err) t.Fatal(err)
} }
fh.Close() if _, err := fh.Seek(0, 0); err != nil {
t.Fatal(fh.Name()) t.Fatal(err)
//os.Remove(fh.Name()) }
} if dh, err = ParseSpec(fh); err != nil {
t.Fatal(err)
func TestReadNames(t *testing.T) { }
names, err := readOrderedDirNames(".") for k, v := range countTypes(dh) {
if err != nil { if numEntries[k] != v {
t.Error(err) t.Errorf("for type %s: expected %d, got %d", k, numEntries[k], v)
}
} }
t.Errorf("names: %q", names)
} }