2015-02-25 18:33:01 +00:00
|
|
|
package pidfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewAndRemove(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "test-pidfile")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create test directory")
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:09:55 +00:00
|
|
|
path := filepath.Join(dir, "testfile")
|
|
|
|
file, err := New(path)
|
2015-02-25 18:33:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create test file", err)
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:09:55 +00:00
|
|
|
_, err = New(path)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Test file creation not blocked")
|
|
|
|
}
|
|
|
|
|
2015-02-25 18:33:01 +00:00
|
|
|
if err := file.Remove(); err != nil {
|
|
|
|
t.Fatal("Could not delete created test file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveInvalidPath(t *testing.T) {
|
2015-07-25 08:35:07 +00:00
|
|
|
file := PIDFile{path: filepath.Join("foo", "bar")}
|
2015-02-25 18:33:01 +00:00
|
|
|
|
|
|
|
if err := file.Remove(); err == nil {
|
|
|
|
t.Fatal("Non-existing file doesn't give an error on delete")
|
|
|
|
}
|
|
|
|
}
|