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 #1 from vbatts/check_results

Check results
This commit is contained in:
Vincent Batts 2016-04-05 16:21:46 -04:00
commit e7673b617d
4 changed files with 29 additions and 13 deletions

View file

@ -7,12 +7,25 @@ import (
"sort" "sort"
) )
// Result of a Check
type Result struct { type Result struct {
// XXX perhaps this is a list of the failed files and keywords? Failures []Failure // list of any failures in the Check
} }
var ErrNotAllClear = fmt.Errorf("some keyword check failed validation") // Failure of a particular keyword for a path
type Failure struct {
Path string
Keyword string
Expected string
Got string
}
// String returns a "pretty" formatting for a Failure
func (f Failure) String() string {
return fmt.Sprintf("%q: keyword %q: expected %s; got %s", f.Path, f.Keyword, f.Expected, f.Got)
}
// Check a root directory path for a DirectoryHierarchy
func Check(root string, dh *DirectoryHierarchy) (*Result, error) { func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
creator := dhCreator{DH: dh} creator := dhCreator{DH: dh}
curDir, err := os.Getwd() curDir, err := os.Getwd()
@ -25,7 +38,7 @@ func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
} }
sort.Sort(byPos(creator.DH.Entries)) sort.Sort(byPos(creator.DH.Entries))
var failed bool var result Result
for _, e := range creator.DH.Entries { for _, e := range creator.DH.Entries {
switch e.Type { switch e.Type {
case SpecialType: case SpecialType:
@ -57,16 +70,11 @@ func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
return nil, err return nil, err
} }
if string(kv) != curKeyVal { if string(kv) != curKeyVal {
failed = true failure := Failure{Path: e.Path(), Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()}
fmt.Printf("%q: keyword %q: expected %s; got %s\n", e.Path(), kv.Keyword(), kv.Value(), KeyVal(curKeyVal).Value()) result.Failures = append(result.Failures, failure)
} }
} }
} }
} }
return &result, nil
if failed {
return nil, ErrNotAllClear
}
return nil, nil
} }

View file

@ -98,8 +98,11 @@ func main() {
isErr = true isErr = true
return return
} }
if res != nil { if res != nil && len(res.Failures) > 0 {
fmt.Printf("%#v\n", res) defer os.Exit(1)
for _, failure := range res.Failures {
fmt.Println(failure)
}
} }
} }
} }

View file

@ -46,6 +46,7 @@ type Entry struct {
Type EntryType Type EntryType
} }
// Path provides the full path of the file, despite RelativeType or FullType
func (e Entry) Path() string { func (e Entry) Path() string {
if e.Parent == nil || e.Type == FullType { if e.Parent == nil || e.Type == FullType {
return e.Name return e.Name

View file

@ -53,6 +53,9 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
case len(strings.Fields(str)) > 0 && strings.Fields(str)[0] == "..": case len(strings.Fields(str)) > 0 && strings.Fields(str)[0] == "..":
e.Type = DotDotType e.Type = DotDotType
e.Raw = str e.Raw = str
if creator.curDir != nil {
creator.curDir = creator.curDir.Parent
}
// nothing else to do here // nothing else to do here
case len(strings.Fields(str)) > 0: case len(strings.Fields(str)) > 0:
// collapse any escaped newlines // collapse any escaped newlines
@ -74,6 +77,7 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
} }
e.Name = f[0] e.Name = f[0]
e.Keywords = f[1:] e.Keywords = f[1:]
e.Parent = creator.curDir
for i := range e.Keywords { for i := range e.Keywords {
kv := KeyVal(e.Keywords[i]) kv := KeyVal(e.Keywords[i])
if kv.Keyword() == "type" { if kv.Keyword() == "type" {