forked from shibukawa/extstat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextstat_test.go
More file actions
72 lines (63 loc) · 1.86 KB
/
extstat_test.go
File metadata and controls
72 lines (63 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package extstat
import (
"io/ioutil"
"os"
"runtime"
"testing"
"time"
)
var filePath string = "test.txt"
func TestNew(t *testing.T) {
err := ioutil.WriteFile(filePath, []byte("hello"), 0666)
if err != nil {
t.Error(err)
return
}
defer os.Remove(filePath)
now := time.Now()
btimeBefore := now.Add(+time.Second)
btimeAfter := now.Add(-time.Second)
mtimeBefore := now.Add(time.Second * 4)
mtimeAfter := now.Add(time.Second * 2)
atimeBefore := now.Add(time.Second * 7)
atimeAfter := now.Add(time.Second * 5)
t.Log("wait for changing mtime...")
time.Sleep(time.Second * 3)
fileForModify, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fileForModify.Close()
t.Error(err)
return
}
fileForModify.Write([]byte(" world"))
fileForModify.Close()
t.Log("wait for changing atime...")
time.Sleep(time.Second * 3)
content, err := ioutil.ReadFile(filePath)
t.Log(string(content))
if err != nil {
t.Error(err)
return
}
stat, err := os.Stat(filePath)
if err != nil {
t.Error(err)
return
}
extStat := New(stat)
// plan9 doesn't have correct ctime attribute
if runtime.GOOS != "plan9" {
if !extStat.BirthTime.Before(btimeBefore) || !extStat.BirthTime.After(btimeAfter) {
t.Error("btime is wrong:", btimeAfter, "<", extStat.BirthTime, "<", btimeBefore, " now: ", now)
}
}
if !extStat.ModTime.Before(mtimeBefore) || !extStat.ModTime.After(mtimeAfter) {
t.Error("mtime is wrong:", mtimeAfter, "<", extStat.ModTime, "<", mtimeBefore, " now: ", now)
}
if !extStat.ModTime.Before(mtimeBefore) || !extStat.ChangeTime.After(mtimeAfter) {
t.Error("mtime is wrong:", mtimeAfter, "<", extStat.ModTime, "<", mtimeBefore, " now: ", now)
}
if !extStat.AccessTime.Before(atimeBefore) || !extStat.AccessTime.After(atimeAfter) {
t.Error("atime is wrong:", atimeAfter, "<", extStat.AccessTime, "<", atimeBefore, " now: ", now)
}
}