Skip to content

Commit

Permalink
increase windows test coverage, and remove platformSpecificStat nonsense
Browse files Browse the repository at this point in the history
  • Loading branch information
djherbis committed Oct 27, 2018
1 parent db2d930 commit ba9a5d4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 30 deletions.
22 changes: 20 additions & 2 deletions ctime_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
"unsafe"
)

// Stat returns the Timespec for the given filename.
func Stat(name string) (Timespec, error) {
ts, err := platformSpecficStat(name)
if err == nil {
return ts, err
}

return stat(name, os.Stat)
}

// Lstat returns the Timespec for the given filename, and does not follow Symlinks.
func Lstat(name string) (Timespec, error) {
ts, err := platformSpecficLstat(name)
if err == nil {
return ts, err
}

return stat(name, os.Lstat)
}

type timespecEx struct {
atime
mtime
Expand All @@ -33,8 +53,6 @@ func statFile(h syscall.Handle) (Timespec, error) {
return t, nil
}

const hasPlatformSpecificStat = true

func platformSpecficLstat(name string) (Timespec, error) {
if findProcErr != nil {
return nil, findProcErr
Expand Down
22 changes: 0 additions & 22 deletions times.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,6 @@ func Get(fi os.FileInfo) Timespec {

type statFunc func(string) (os.FileInfo, error)

// Lstat returns the Timespec for the given filename, and does not follow Symlinks.
func Lstat(name string) (Timespec, error) {
if hasPlatformSpecificStat {
if ts, err := platformSpecficLstat(name); err == nil {
return ts, nil
}
}

return stat(name, os.Lstat)
}

// Stat returns the Timespec for the given filename.
func Stat(name string) (Timespec, error) {
if hasPlatformSpecificStat {
if ts, err := platformSpecficStat(name); err == nil {
return ts, nil
}
}

return stat(name, os.Stat)
}

func stat(name string, sf statFunc) (Timespec, error) {
fi, err := sf(name)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions times_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func TestStatErr(t *testing.T) {
}
}

func TestLstatErr(t *testing.T) {
_, err := Lstat("badfile?")
if err == nil {
t.Error("expected an error")
}
}

func TestCheat(t *testing.T) {
// not all times are available for all platforms
// this allows us to get 100% test coverage for platforms which do not have
Expand Down
75 changes: 75 additions & 0 deletions times_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package times

import (
"errors"
"os"
"syscall"
"testing"
"time"
)

func TestStatFile(t *testing.T) {
fileTest(t, func(f *os.File) {
ts, err := StatFile(f)
if err != nil {
t.Error(err.Error())
}
timespecTest(ts, newInterval(time.Now(), time.Second), t)
})
}

func TestStatFileErr(t *testing.T) {
fileTest(t, func(f *os.File) {
f.Close()

_, err := StatFile(f)
if err == nil {
t.Error("got nil err, but err was expected!")
}
})
}

func TestStatFileProcErr(t *testing.T) {
fileTest(t, func(f *os.File) {
findProcErr = errors.New("fake error")
defer func() { findProcErr = nil }()

_, err := StatFile(f)
if err == nil {
t.Error("got nil err, but err was expected!")
}
})
}

func TestStatBadNameErr(t *testing.T) {
_, err := platformSpecficStat(string([]byte{0}))
if err != syscall.EINVAL {
t.Error(err)
}
}

func TestStatProcErrFallback(t *testing.T) {
fileTest(t, func(f *os.File) {
findProcErr = errors.New("fake error")
defer func() { findProcErr = nil }()

ts, err := Stat(f.Name())
if err != nil {
t.Error(err.Error())
}
timespecTest(ts, newInterval(time.Now(), time.Second), t)
})
}

func TestLstatProcErrFallback(t *testing.T) {
fileTest(t, func(f *os.File) {
findProcErr = errors.New("fake error")
defer func() { findProcErr = nil }()

ts, err := Lstat(f.Name())
if err != nil {
t.Error(err.Error())
}
timespecTest(ts, newInterval(time.Now(), time.Second), t)
})
}
13 changes: 7 additions & 6 deletions use_generic_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

package times

const hasPlatformSpecificStat = false
import "os"

// do not use, only here to prevent "undefined" method error.
func platformSpecficStat(name string) (Timespec, error) {
return nil, nil
// Stat returns the Timespec for the given filename.
func Stat(name string) (Timespec, error) {
return stat(name, os.Stat)
}

func platformSpecficLstat(name string) (Timespec, error) {
return nil, nil
// Lstat returns the Timespec for the given filename, and does not follow Symlinks.
func Lstat(name string) (Timespec, error) {
return stat(name, os.Lstat)
}

0 comments on commit ba9a5d4

Please sign in to comment.