summaryrefslogtreecommitdiff
path: root/pkg/ctime/ctime_test.go
blob: abfc627da6567e7f1bc08d91e0a316947e11a673 (plain)
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
package ctime

import (
	"io/ioutil"
	"os"
	"testing"
	"time"
)

func TestCreated(t *testing.T) {
	before := time.Now()

	fileA, err := ioutil.TempFile("", "ctime-test-")
	if err != nil {
		t.Error(err)
	}
	defer os.Remove(fileA.Name())

	fileB, err := ioutil.TempFile("", "ctime-test-")
	if err != nil {
		t.Error(err)
	}
	defer os.Remove(fileB.Name())

	after := time.Now()

	infoA, err := fileA.Stat()
	if err != nil {
		t.Error(err)
	}

	err = fileA.Close()
	if err != nil {
		t.Error(err)
	}

	infoB, err := fileB.Stat()
	if err != nil {
		t.Error(err)
	}

	err = fileB.Close()
	if err != nil {
		t.Error(err)
	}

	createdA := Created(infoA)
	beforeToCreateA := createdA.Sub(before)
	if beforeToCreateA.Nanoseconds() < -1000000000 {
		t.Errorf("created file A %s is %v nanoseconds before %s", createdA, -beforeToCreateA.Nanoseconds(), before)
	}

	createdB := Created(infoB)
	createAToCreateB := createdB.Sub(createdA)
	if createAToCreateB.Nanoseconds() < 0 {
		t.Errorf("created file B %s is %v nanoseconds before file A %s", createdB, -createAToCreateB.Nanoseconds(), createdA)
	}

	createBToAfter := after.Sub(createdB)
	if createBToAfter.Nanoseconds() < 0 {
		t.Errorf("created file B %s is %v nanoseconds after %s", createdB, -createBToAfter.Nanoseconds(), after)
	}
}