github.com/astaxie/beego@v1.12.3/utils/file_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package utils 16 17 import ( 18 "path/filepath" 19 "reflect" 20 "testing" 21 ) 22 23 var noExistedFile = "/tmp/not_existed_file" 24 25 func TestSelfPath(t *testing.T) { 26 path := SelfPath() 27 if path == "" { 28 t.Error("path cannot be empty") 29 } 30 t.Logf("SelfPath: %s", path) 31 } 32 33 func TestSelfDir(t *testing.T) { 34 dir := SelfDir() 35 t.Logf("SelfDir: %s", dir) 36 } 37 38 func TestFileExists(t *testing.T) { 39 if !FileExists("./file.go") { 40 t.Errorf("./file.go should exists, but it didn't") 41 } 42 43 if FileExists(noExistedFile) { 44 t.Errorf("Weird, how could this file exists: %s", noExistedFile) 45 } 46 } 47 48 func TestSearchFile(t *testing.T) { 49 path, err := SearchFile(filepath.Base(SelfPath()), SelfDir()) 50 if err != nil { 51 t.Error(err) 52 } 53 t.Log(path) 54 55 _, err = SearchFile(noExistedFile, ".") 56 if err == nil { 57 t.Errorf("err shouldnt be nil, got path: %s", SelfDir()) 58 } 59 } 60 61 func TestGrepFile(t *testing.T) { 62 _, err := GrepFile("", noExistedFile) 63 if err == nil { 64 t.Error("expect file-not-existed error, but got nothing") 65 } 66 67 path := filepath.Join(".", "testdata", "grepe.test") 68 lines, err := GrepFile(`^\s*[^#]+`, path) 69 if err != nil { 70 t.Error(err) 71 } 72 if !reflect.DeepEqual(lines, []string{"hello", "world"}) { 73 t.Errorf("expect [hello world], but receive %v", lines) 74 } 75 }