-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_test.go
More file actions
40 lines (37 loc) · 962 Bytes
/
Copy pathlock_test.go
File metadata and controls
40 lines (37 loc) · 962 Bytes
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
package daemonit
import (
"fmt"
"os"
"os/user"
"testing"
)
func TestLockfile(t *testing.T) {
t.Run("lockfile", func(t *testing.T) {
var currentUser *user.User
var err error
if currentUser, err = user.Current(); err != nil {
panic(err)
}
expected := fmt.Sprintf("/tmp/testRun.%s.lock", currentUser.Username)
if filename := lockfile("testRun"); filename != expected {
t.Fatalf("Expected %v, got %v", expected, filename)
}
})
t.Run("lock", func(t *testing.T) {
// Lock file
var filename = lockfile("testRun")
if err := lock("testRun"); err != nil {
t.Fatalf("error locking daemon: %v", err)
}
if _, err := os.Lstat(filename); err != nil {
t.Fatalf("error checking lock file: %v", err)
}
if err := lock("testRun"); err == nil {
t.Fatalf("expected error, but nothing raised")
}
cleanupLock("testRun")
if _, err := os.Lstat(filename); !os.IsNotExist(err) {
t.Fatalf("expected file not to exist")
}
})
}