2013-12-09 05:30:19 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2013-12-09 08:01:24 +00:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2013-12-09 05:30:19 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-12-09 08:01:24 +00:00
|
|
|
var noExistedFile = "/tmp/not_existed_file"
|
|
|
|
|
2013-12-09 05:30:19 +00:00
|
|
|
func TestSelfPath(t *testing.T) {
|
|
|
|
path := SelfPath()
|
|
|
|
if path == "" {
|
|
|
|
t.Error("path cannot be empty")
|
|
|
|
}
|
|
|
|
t.Logf("SelfPath: %s", path)
|
|
|
|
}
|
2013-12-09 08:01:24 +00:00
|
|
|
|
|
|
|
func TestSelfDir(t *testing.T) {
|
|
|
|
dir := SelfDir()
|
|
|
|
t.Logf("SelfDir: %s", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileExists(t *testing.T) {
|
2013-12-11 14:18:45 +00:00
|
|
|
if !FileExists("./file.go") {
|
2013-12-12 11:30:44 +00:00
|
|
|
t.Errorf("./file.go should exists, but it didn't")
|
2013-12-09 08:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if FileExists(noExistedFile) {
|
|
|
|
t.Errorf("Wierd, how could this file exists: %s", noExistedFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 11:30:44 +00:00
|
|
|
func TestSearchFile(t *testing.T) {
|
|
|
|
path, err := SearchFile(filepath.Base(SelfPath()), SelfDir())
|
2013-12-09 08:01:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
t.Log(path)
|
|
|
|
|
2013-12-12 11:30:44 +00:00
|
|
|
path, err = SearchFile(noExistedFile, ".")
|
2013-12-09 08:01:24 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("err shouldnot be nil, got path: %s", SelfDir())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:18:45 +00:00
|
|
|
func TestGrepFile(t *testing.T) {
|
|
|
|
_, err := GrepFile("", noExistedFile)
|
2013-12-09 08:01:24 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expect file-not-existed error, but got nothing")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(".", "testdata", "grepe.test")
|
2013-12-11 14:18:45 +00:00
|
|
|
lines, err := GrepFile(`^\s*[^#]+`, path)
|
2013-12-09 08:01:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(lines, []string{"hello", "world"}) {
|
|
|
|
t.Errorf("expect [hello world], but receive %v", lines)
|
|
|
|
}
|
|
|
|
}
|