-
Notifications
You must be signed in to change notification settings - Fork 6
/
touch_test.go
48 lines (40 loc) · 935 Bytes
/
touch_test.go
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
package main
import (
"bytes"
"io/ioutil"
"os"
"testing"
)
func TestTouch(t *testing.T) {
file, err := ioutil.TempFile("/tmp", "test-touch-")
if err != nil {
t.Fatal(err)
}
filename := file.Name()
defer file.Close()
defer os.Remove(filename)
if _, err := file.Write([]byte("hello")); err != nil {
t.Fatal(err)
}
if err := Touch(filename); err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
} else if !bytes.Equal(data, []byte("hello")) {
t.Errorf("The contents of the temporary file were modified after being touched; they were %#v", data)
}
if err := os.Remove(filename); err != nil {
t.Fatal(err)
}
if err := Touch(filename); err != nil {
t.Fatal(err)
}
data, err = ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
} else if !bytes.Equal(data, nil) {
t.Errorf("The contents of the touched file were not empty; got %#v", data)
}
}