exists_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package filesystemutils
  5. import (
  6. "os"
  7. "path"
  8. "testing"
  9. )
  10. func TestIsExists(t *testing.T) {
  11. temp, err := os.MkdirTemp("", "test*")
  12. if err != nil {
  13. t.Fatalf("create temp directory failed: %s", temp)
  14. }
  15. defer func() {
  16. _ = os.RemoveAll(temp)
  17. }()
  18. testDir := path.Join(temp, "test_dir")
  19. err = os.Mkdir(testDir, 0700)
  20. if err != nil {
  21. t.Fatalf("create temp/test_dir failed: %s", temp)
  22. }
  23. testFile := path.Join(temp, "test_file")
  24. f, err := os.OpenFile(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  25. if err != nil {
  26. t.Fatalf("create temp/test_file failed: %s", temp)
  27. }
  28. _ = f.Close()
  29. testNotExists := path.Join(temp, "test_not_exists")
  30. // IsExists 测试
  31. if !IsExists(testFile) {
  32. t.Errorf("IsExists(%s) -> true: false", testFile)
  33. }
  34. if !IsExists(testDir) {
  35. t.Errorf("IsExists(%s) -> true: false", testDir)
  36. }
  37. if IsExists(testNotExists) {
  38. t.Errorf("IsExists(%s) -> false: true", testNotExists)
  39. }
  40. // IsFile 测试
  41. if !IsFile(testFile) {
  42. t.Errorf("IsFile(%s) -> true: false", testFile)
  43. }
  44. if IsFile(testDir) {
  45. t.Errorf("IsFile(%s) -> false: true", testDir)
  46. }
  47. if IsFile(testNotExists) {
  48. t.Errorf("IsFile(%s) -> false: true", testNotExists)
  49. }
  50. // IsExistsAndFile 测试
  51. if exists, file := IsExistsAndFile(testFile); !(exists && file) {
  52. t.Errorf("IsExistsAndFile(%s) -> true, true: %v, %v", testDir, exists, file)
  53. }
  54. if exists, file := IsExistsAndFile(testDir); !(exists && !file) {
  55. t.Errorf("IsExistsAndFile(%s) -> true, false: %v, %v", testDir, exists, file)
  56. }
  57. if exists, file := IsExistsAndFile(testNotExists); !(!exists && !file) {
  58. t.Errorf("IsExistsAndFile(%s) -> false, false: %v, %v", testDir, exists, file)
  59. }
  60. // IsDir 测试
  61. if IsDir(testFile) {
  62. t.Errorf("IsDir(%s) -> false: true", testFile)
  63. }
  64. if !IsDir(testDir) {
  65. t.Errorf("IsDir(%s) -> true: false", testDir)
  66. }
  67. if IsDir(testNotExists) {
  68. t.Errorf("IsDir(%s) -> false: true", testNotExists)
  69. }
  70. // IsExistsAndDir 测试
  71. if exists, dir := IsExistsAndDir(testFile); !(exists && !dir) {
  72. t.Errorf("IsExistsAndDir(%s) -> true, false: %v, %v", testDir, exists, dir)
  73. }
  74. if exists, dir := IsExistsAndDir(testDir); !(exists && dir) {
  75. t.Errorf("IsExistsAndDir(%s) -> true, true: %v, %v", testDir, exists, dir)
  76. }
  77. if exists, dir := IsExistsAndDir(testNotExists); !(!exists && !dir) {
  78. t.Errorf("IsExistsAndDir(%s) -> false, false: %v, %v", testDir, exists, dir)
  79. }
  80. }