Skip to content

Commit

Permalink
fixes to file exist func, tagset test.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzlichtbezirk committed Dec 24, 2023
1 parent cb8b54b commit c251d39
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 236 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Library to build and use data files packages.

[![Go](https://github.com/schwarzlichtbezirk/wpk/actions/workflows/go.yml/badge.svg)](https://github.com/schwarzlichtbezirk/wpk/actions/workflows/go.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/schwarzlichtbezirk/wpk.svg)](https://pkg.go.dev/github.com/schwarzlichtbezirk/wpk)
[![Go Report Card](https://goreportcard.com/badge/github.com/schwarzlichtbezirk/wpk)](https://goreportcard.com/report/github.com/schwarzlichtbezirk/wpk)
[![Hits-of-Code](https://hitsofcode.com/github/schwarzlichtbezirk/wpk?branch=master)](https://hitsofcode.com/github/schwarzlichtbezirk/wpk/view?branch=master)
Expand Down
24 changes: 18 additions & 6 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,28 @@ func Envfmt(p string) string {
})
}

// PathExists check up file or directory existence.
func PathExists(path string) (bool, error) {
var err error
if _, err = os.Stat(path); err == nil {
return true, nil
// DirExists check up directory existence.
func DirExists(path string) (bool, error) {
var stat, err = os.Stat(path)
if err == nil {
return stat.IsDir(), nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return true, err
return false, err
}

// FileExists check up file existence.
func FileExists(path string) (bool, error) {
var stat, err = os.Stat(path)
if err == nil {
return !stat.IsDir(), nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}

// TempPath returns filename located at temporary directory.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/edsrzf/mmap-go v1.1.0
github.com/h2non/filetype v1.1.3
github.com/yuin/gopher-lua v1.1.0
github.com/yuin/gopher-lua v1.1.1
gopkg.in/djherbis/times.v1 v1.3.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ=
github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q=
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE=
github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/djherbis/times.v1 v1.3.0 h1:uxMS4iMtH6Pwsxog094W0FYldiNnfY/xba00vq6C2+o=
Expand Down
273 changes: 86 additions & 187 deletions tagset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,195 +35,94 @@ func TestTagset(t *testing.T) {
str string
)

for _, check := range []struct {
cond func() bool
msg string
}{
{func() bool { return wpk.ToSlash(fkey1) != fkey || wpk.ToSlash(fkey2) != fkey },
"toslash test failed",
},
{func() bool { return tsi.TID() != wpk.TIDnone },
"tag ID in created iterator should be 'none'",
},
{func() bool { return ts.Num() != 4 },
"wrong number of tags",
},

// check up OFFSET
{func() bool { return !tsi.Next() },
"can not iterate to 'offset'",
},
{func() bool { return tsi.TID() != wpk.TIDoffset },
"tag #1 is not 'offset'",
},
{func() bool { tag = tsi.Tag(); return tag == nil },
"can not get 'offset' tag",
},
{func() bool { ov, ok = tsi.TagUint(wpk.TIDoffset); return !ok },
"can not convert 'offset' tag to value",
},
{func() bool { return ov != offset },
"'offset' tag is not equal to original value",
},

// check up SIZE
{func() bool { return !tsi.Next() },
"can not iterate to 'size'",
},
{func() bool { return tsi.TID() != wpk.TIDsize },
"tag #2 is not 'size'",
},
{func() bool { tag = tsi.Tag(); return tag == nil },
"can not get 'size' tag",
},
{func() bool { sv, ok = tsi.TagUint(wpk.TIDsize); return !ok },
"can not convert 'size' tag to value",
},
{func() bool { return sv != size },
"'size' tag is not equal to original value",
},

// check up PATH
{func() bool { return !tsi.Next() },
"can not iterate to 'path'",
},
{func() bool { return tsi.TID() != wpk.TIDpath },
"tag #3 is not 'path'",
},
{func() bool { return tsi.TagLen() != len(fkey) },
"length of 'path' tag does not equal to original length",
},
{func() bool { tag = tsi.Tag(); return tag == nil },
"can not get 'path' tag",
},
{func() bool { str, ok = tag.TagStr(); return !ok },
"can not convert 'path' tag to value",
},
{func() bool { return str != fkey },
"'path' tag is not equal to original value",
},

// check up FID
{func() bool { return !tsi.Next() },
"can not iterate to 'fid'",
},
{func() bool { return tsi.TID() != wpk.TIDfid },
"tag #4 is not 'fid'",
},
{func() bool { tag = tsi.Tag(); return tag == nil },
"can not get 'fid' tag",
},
{func() bool { fv, ok = tsi.TagUint(wpk.TIDfid); return !ok },
"can not convert 'fid' tag to value",
},
{func() bool { return fv != fid },
"'fid' tag is not equal to original value",
},

// check up valid iterations finish
{func() bool { return tsi.Failed() },
"content is broken",
},
{func() bool { return tsi.Next() },
"iterator does not finished",
},
{func() bool { return !tsi.Passed() },
"iterations does not reached till the end",
},
{func() bool { return tsi.TID() != wpk.TIDnone },
"tag ID in finished iterator should be 'none'",
},

// check up 'Has'
{func() bool {
return !(ts.Has(wpk.TIDoffset) && ts.Has(wpk.TIDsize) && ts.Has(wpk.TIDfid) && ts.Has(wpk.TIDpath))
},
"something does not pointed that should be present",
},
{func() bool { return ts.Has(wpk.TIDmd5) },
"'md5' tag is not set, but it's pointed that it present",
},

// check up helpers functions
{func() bool {
v, ok := ts.TagUint(wpk.TIDfid)
return !ok || v != fid
},
"FID getter does not work correctly",
},
{func() bool {
v, ok := ts.TagUint(wpk.TIDoffset)
return !ok || v != offset
},
"FOffset getter does not work correctly",
},
{func() bool {
v, ok := ts.TagUint(wpk.TIDsize)
return !ok || v != size
},
"FSize getter does not work correctly",
},
{func() bool { return ts.Path() != fkey },
"'Path' function does not work correctly",
},
{func() bool { return ts.Name() != path.Base(fkey) },
"'Name' function does not work correctly",
},

// check up 'Set' and 'Del'
{func() (ok bool) {
ts, ok = ts.SetOk(wpk.TIDpath, wpk.StrTag(fkey))
return ok
},
"content of 'path' tag should be replaced by 'Set'",
},
{func() bool { return ts.Num() != 4 },
"number of tags after replace 'path' must not be changed",
},
{func() bool { return ts.Path() != wpk.ToSlash(fkey2) },
"'Set' function does not work correctly",
},
{func() (ok bool) {
ts, ok = ts.SetOk(wpk.TIDmime, wpk.StrTag(mime))
return !ok
},
"content of 'mime' tag should be added by 'Set'",
},
{func() bool { return ts.Num() != 4+1 },
"number of tags after add 'mime' must be added by one",
},
{func() bool { tag, ok = ts.Get(wpk.TIDmime); return !ok },
"can not get 'mime' tag content",
},
{func() bool { str, _ = tag.TagStr(); return str != mime },
"'mime' tag is not equal to original value",
},
{func() (ok bool) {
ts, ok = ts.DelOk(wpk.TIDmime)
return !ok
},
"'mime' tag is not deleted",
},
{func() bool { return ts.Has(wpk.TIDmime) },
"'mime' tag must not be found after deletion",
},
{func() bool { return ts.Num() != 4 },
"number of tags after delete 'mime' must be restored",
},
{func() (ok bool) {
ts, ok = ts.DelOk(wpk.TIDmime)
return ok
},
"'mime' tag can not be deleted again",
},
{func() bool { return ts.Num() != 4 },
"number of tags after repeated delete 'mime' must be unchanged",
},
} {
if check.cond() {
t.Fatal(check.msg)
var assert = func(cond bool, msg string) {
if !cond {
t.Error(msg)
}
}

assert(wpk.ToSlash(fkey1) == fkey, "toslash test failed")
assert(wpk.ToSlash(fkey2) == fkey, "toslash test failed")
assert(tsi.TID() == wpk.TIDnone, "tag ID in created iterator should be 'none'")
assert(ts.Num() == 4, "wrong number of tags")

// check up OFFSET
assert(tsi.Next(), "can not iterate to 'offset'")
assert(tsi.TID() == wpk.TIDoffset, "tag #1 is not 'offset'")
tag = tsi.Tag()
assert(tag != nil, "can not get 'offset' tag")
ov, ok = tsi.TagUint(wpk.TIDoffset)
assert(ok, "can not convert 'offset' tag to value")
assert(ov == offset, "'offset' tag is not equal to original value")

// check up SIZE
assert(tsi.Next(), "can not iterate to 'size'")
assert(tsi.TID() == wpk.TIDsize, "tag #2 is not 'size'")
tag = tsi.Tag()
assert(tag != nil, "can not get 'size' tag")
sv, ok = tsi.TagUint(wpk.TIDsize)
assert(ok, "can not convert 'size' tag to value")
assert(sv == size, "'size' tag is not equal to original value")

// check up PATH
assert(tsi.Next(), "can not iterate to 'path'")
assert(tsi.TID() == wpk.TIDpath, "tag #3 is not 'path'")
assert(tsi.TagLen() == len(fkey), "length of 'path' tag does not equal to original length")
tag = tsi.Tag()
assert(tag != nil, "can not get 'path' tag")
str, ok = tag.TagStr()
assert(ok, "can not convert 'path' tag to value")
assert(str == fkey, "'path' tag is not equal to original value")

// check up FID
assert(tsi.Next(), "can not iterate to 'fid'")
assert(tsi.TID() == wpk.TIDfid, "tag #4 is not 'fid'")
tag = tsi.Tag()
assert(tag != nil, "can not get 'fid' tag")
fv, ok = tsi.TagUint(wpk.TIDfid)
assert(ok, "can not convert 'fid' tag to value")
assert(fv == fid, "'fid' tag is not equal to original value")

// check up valid iterations finish
assert(!tsi.Failed(), "content is broken")
assert(!tsi.Next(), "iterator does not finished")
assert(tsi.Passed(), "iterations does not reached till the end")
assert(tsi.TID() == wpk.TIDnone, "tag ID in finished iterator should be 'none'")

// check up 'Has'
assert(ts.Has(wpk.TIDoffset) && ts.Has(wpk.TIDsize) && ts.Has(wpk.TIDfid) && ts.Has(wpk.TIDpath),
"something does not pointed that should be present")
assert(!ts.Has(wpk.TIDmd5), "'md5' tag is not set, but it's pointed that it present")

// check up helpers functions
fv, ok = ts.TagUint(wpk.TIDfid)
assert(ok && fv == fid, "FID getter does not work correctly")
ov, ok = ts.TagUint(wpk.TIDoffset)
assert(ok && ov == offset, "FOffset getter does not work correctly")
sv, ok = ts.TagUint(wpk.TIDsize)
assert(ok && sv == size, "FSize getter does not work correctly")
assert(ts.Path() == fkey, "'Path' function does not work correctly")
assert(ts.Name() == path.Base(fkey), "'Name' function does not work correctly")

// check up 'Set' and 'Del'
ts, ok = ts.SetOk(wpk.TIDpath, wpk.StrTag(fkey))
assert(!ok, "content of 'path' tag should be replaced by 'Set'")
assert(ts.Num() == 4, "number of tags after replace 'path' must not be changed")
assert(ts.Path() == wpk.ToSlash(fkey2), "'Set' function does not work correctly")
ts, ok = ts.SetOk(wpk.TIDmime, wpk.StrTag(mime))
assert(ok, "content of 'mime' tag should be added by 'Set'")
assert(ts.Num() == 4+1, "number of tags after add 'mime' must be added by one")
tag, ok = ts.Get(wpk.TIDmime)
assert(ok, "can not get 'mime' tag content")
str, _ = tag.TagStr()
assert(str == mime, "'mime' tag is not equal to original value")
ts, ok = ts.DelOk(wpk.TIDmime)
assert(ok, "'mime' tag is not deleted")
assert(!ts.Has(wpk.TIDmime), "'mime' tag must not be found after deletion")
assert(ts.Num() == 4, "number of tags after delete 'mime' must be restored")
ts, ok = ts.DelOk(wpk.TIDmime)
assert(!ok, "'mime' tag can not be deleted again")
assert(ts.Num() == 4, "number of tags after repeated delete 'mime' must be unchanged")
}

func ExampleTagsetIterator_Next() {
Expand Down
4 changes: 2 additions & 2 deletions util/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func checkargs() int {
continue
}
fpath = wpk.ToSlash(wpk.Envfmt(fpath))
if ok, _ := wpk.PathExists(fpath); !ok {
if ok, _ := wpk.FileExists(fpath); !ok {
log.Printf("source file #%d '%s' does not exist", i+1, fpath)
ec++
continue
Expand All @@ -67,7 +67,7 @@ func checkargs() int {
log.Println("destination path does not specified")
ec++
}
if ok, _ := wpk.PathExists(DstPath); !ok {
if ok, _ := wpk.DirExists(DstPath); !ok {
if MkDst {
if err := os.MkdirAll(DstPath, os.ModePerm); err != nil {
log.Println(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions util/pack/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func checkargs() (ec int) { // returns error counter
if !strings.HasSuffix(fpath, "/") {
fpath += "/"
}
if ok, _ := wpk.PathExists(fpath); !ok {
if ok, _ := wpk.DirExists(fpath); !ok {
log.Printf("source path #%d '%s' does not exist", i+1, fpath)
ec++
continue
Expand All @@ -61,7 +61,7 @@ func checkargs() (ec int) { // returns error counter
if DstFile == "" {
log.Println("destination file does not specified")
ec++
} else if ok, _ := wpk.PathExists(path.Dir(DstFile)); !ok {
} else if ok, _ := wpk.DirExists(path.Dir(DstFile)); !ok {
log.Println("destination path does not exist")
ec++
}
Expand Down
Loading

0 comments on commit c251d39

Please sign in to comment.