-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathnewer_test.go
More file actions
112 lines (102 loc) · 3.06 KB
/
newer_test.go
File metadata and controls
112 lines (102 loc) · 3.06 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package target
import (
"os"
"path/filepath"
"testing"
"time"
// DEPRECATED: The ioutil package was deprecated in Go 1.16.
// TODO: Replace with os.MkdirTemp and os.WriteFile when minimum Go
// version is raised to 1.16+. See: https://go.dev/doc/go1.16#ioutil
"io/ioutil"
)
func TestNewestModTime(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("error creating temp dir: %s", err.Error())
}
defer os.RemoveAll(dir)
for _, name := range []string{"a", "b", "c", "d"} {
out := filepath.Join(dir, name)
if err := ioutil.WriteFile(out, []byte("hi!"), 0644); err != nil {
t.Fatalf("error writing file: %s", err.Error())
}
}
time.Sleep(10 * time.Millisecond)
outName := filepath.Join(dir, "c")
outfh, err := os.OpenFile(outName, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
t.Fatalf("error opening file to append: %s", err.Error())
}
if _, err := outfh.WriteString("\nbye!\n"); err != nil {
t.Fatalf("error appending to file: %s", err.Error())
}
if err := outfh.Close(); err != nil {
t.Fatalf("error closing file: %s", err.Error())
}
afi, err := os.Stat(filepath.Join(dir, "a"))
if err != nil {
t.Fatalf("error stating unmodified file: %s", err.Error())
}
cfi, err := os.Stat(outName)
if err != nil {
t.Fatalf("error stating modified file: %s", err.Error())
}
if afi.ModTime().Equal(cfi.ModTime()) {
t.Fatal("modified and unmodified file mtimes equal")
}
newest, err := NewestModTime(dir)
if err != nil {
t.Fatalf("error finding newest mod time: %s", err.Error())
}
if !newest.Equal(cfi.ModTime()) {
t.Fatal("expected newest mod time to match c")
}
}
func TestOldestModTime(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("error creating temp dir: %s", err.Error())
}
defer os.RemoveAll(dir)
for _, name := range []string{"a", "b", "c", "d"} {
out := filepath.Join(dir, name)
if err := ioutil.WriteFile(out, []byte("hi!"), 0644); err != nil {
t.Fatalf("error writing file: %s", err.Error())
}
}
time.Sleep(10 * time.Millisecond)
for _, name := range []string{"a", "b", "d"} {
outName := filepath.Join(dir, name)
outfh, err := os.OpenFile(outName, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
t.Fatalf("error opening file to append: %s", err.Error())
}
if _, err := outfh.WriteString("\nbye!\n"); err != nil {
t.Fatalf("error appending to file: %s", err.Error())
}
if err := outfh.Close(); err != nil {
t.Fatalf("error closing file: %s", err.Error())
}
}
afi, err := os.Stat(filepath.Join(dir, "a"))
if err != nil {
t.Fatalf("error stating unmodified file: %s", err.Error())
}
outName := filepath.Join(dir, "c")
cfi, err := os.Stat(outName)
if err != nil {
t.Fatalf("error stating modified file: %s", err.Error())
}
if afi.ModTime().Equal(cfi.ModTime()) {
t.Fatal("modified and unmodified file mtimes equal")
}
newest, err := OldestModTime(dir)
if err != nil {
t.Fatalf("error finding oldest mod time: %s", err.Error())
}
if !newest.Equal(cfi.ModTime()) {
t.Fatal("expected newest mod time to match c")
}
}