Beego/utils/file_test.go

77 lines
1.8 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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"
2020-08-05 06:34:54 +00:00
"github.com/stretchr/testify/assert"
2013-12-09 05:30:19 +00:00
)
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") {
t.Errorf("./file.go should exists, but it didn't")
2013-12-09 08:01:24 +00:00
}
if FileExists(noExistedFile) {
2016-08-17 14:56:21 +00:00
t.Errorf("Weird, how could this file exists: %s", noExistedFile)
2013-12-09 08:01:24 +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)
2016-08-17 14:56:21 +00:00
_, err = SearchFile(noExistedFile, ".")
2013-12-09 08:01:24 +00:00
if err == nil {
2017-04-28 15:37:40 +00:00
t.Errorf("err shouldnt be nil, got path: %s", SelfDir())
2013-12-09 08:01:24 +00:00
}
}
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)
2020-08-05 06:34:54 +00:00
assert.Nil(t, err)
2013-12-09 08:01:24 +00:00
if !reflect.DeepEqual(lines, []string{"hello", "world"}) {
t.Errorf("expect [hello world], but receive %v", lines)
}
}