Skip to content

Commit

Permalink
Merge pull request #9 from vbatts/fix_time_comparison
Browse files Browse the repository at this point in the history
Fix time comparison
  • Loading branch information
vbatts committed Jun 27, 2016
2 parents be2b057 + 6adcc98 commit d5aab78
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
44 changes: 44 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mtree

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -95,3 +96,46 @@ func ExampleCheck() {
// handle failed validity ...
}
}

// https://github.com/vbatts/go-mtree/issues/8
func TestTimeComparison(t *testing.T) {
dir, err := ioutil.TempDir("", "test-time.")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

// This is the format of time from FreeBSD
spec := `
/set type=file time=5.000000000
. type=dir
file time=5.000000000
..
`

fh, err := os.Create(filepath.Join(dir, "file"))
if err != nil {
t.Fatal(err)
}
// This is what mode we're checking for. Round integer of epoch seconds
epoch := time.Unix(5, 0)
if err := os.Chtimes(fh.Name(), epoch, epoch); err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}

dh, err := ParseSpec(bytes.NewBufferString(spec))
if err != nil {
t.Fatal(err)
}

res, err := Check(dir, dh, nil)
if err != nil {
t.Error(err)
}
if len(res.Failures) > 0 {
t.Fatal(res.Failures)
}
}
5 changes: 4 additions & 1 deletion keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ var (
}
timeKeywordFunc = func(path string, info os.FileInfo) (string, error) {
t := info.ModTime().UnixNano()
return fmt.Sprintf("time=%d.%d", (t / 1e9), (t % (t / 1e9))), nil
if t == 0 {
return "time=0.000000000", nil
}
return fmt.Sprintf("time=%d.%9.9d", (t / 1e9), (t % (t / 1e9))), nil
}
linkKeywordFunc = func(path string, info os.FileInfo) (string, error) {
if info.Mode()&os.ModeSymlink != 0 {
Expand Down

0 comments on commit d5aab78

Please sign in to comment.