1
0

file_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. package utils
  15. import (
  16. "path/filepath"
  17. "reflect"
  18. "testing"
  19. )
  20. var noExistedFile = "/tmp/not_existed_file"
  21. func TestSelfPath(t *testing.T) {
  22. path := SelfPath()
  23. if path == "" {
  24. t.Error("path cannot be empty")
  25. }
  26. t.Logf("SelfPath: %s", path)
  27. }
  28. func TestSelfDir(t *testing.T) {
  29. dir := SelfDir()
  30. t.Logf("SelfDir: %s", dir)
  31. }
  32. func TestFileExists(t *testing.T) {
  33. if !FileExists("./file.go") {
  34. t.Errorf("./file.go should exists, but it didn't")
  35. }
  36. if FileExists(noExistedFile) {
  37. t.Errorf("Weird, how could this file exists: %s", noExistedFile)
  38. }
  39. }
  40. func TestSearchFile(t *testing.T) {
  41. path, err := SearchFile(filepath.Base(SelfPath()), SelfDir())
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. t.Log(path)
  46. _, err = SearchFile(noExistedFile, ".")
  47. if err == nil {
  48. t.Errorf("err shouldnot be nil, got path: %s", SelfDir())
  49. }
  50. }
  51. func TestGrepFile(t *testing.T) {
  52. _, err := GrepFile("", noExistedFile)
  53. if err == nil {
  54. t.Error("expect file-not-existed error, but got nothing")
  55. }
  56. path := filepath.Join(".", "testdata", "grepe.test")
  57. lines, err := GrepFile(`^\s*[^#]+`, path)
  58. if err != nil {
  59. t.Error(err)
  60. }
  61. if !reflect.DeepEqual(lines, []string{"hello", "world"}) {
  62. t.Errorf("expect [hello world], but receive %v", lines)
  63. }
  64. }