osutil_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2020 The Gogs 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 osutil
  5. import (
  6. "os"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestIsFile(t *testing.T) {
  12. tests := []struct {
  13. path string
  14. want bool
  15. }{
  16. {
  17. path: "osutil.go",
  18. want: true,
  19. }, {
  20. path: "../osutil",
  21. want: false,
  22. }, {
  23. path: "not_found",
  24. want: false,
  25. },
  26. }
  27. for _, test := range tests {
  28. t.Run("", func(t *testing.T) {
  29. assert.Equal(t, test.want, IsFile(test.path))
  30. })
  31. }
  32. }
  33. func TestIsDir(t *testing.T) {
  34. tests := []struct {
  35. path string
  36. want bool
  37. }{
  38. {
  39. path: "osutil.go",
  40. want: false,
  41. }, {
  42. path: "../osutil",
  43. want: true,
  44. }, {
  45. path: "not_found",
  46. want: false,
  47. },
  48. }
  49. for _, test := range tests {
  50. t.Run("", func(t *testing.T) {
  51. assert.Equal(t, test.want, IsDir(test.path))
  52. })
  53. }
  54. }
  55. func TestIsExist(t *testing.T) {
  56. tests := []struct {
  57. path string
  58. expVal bool
  59. }{
  60. {
  61. path: "osutil.go",
  62. expVal: true,
  63. }, {
  64. path: "../osutil",
  65. expVal: true,
  66. }, {
  67. path: "not_found",
  68. expVal: false,
  69. },
  70. }
  71. for _, test := range tests {
  72. t.Run("", func(t *testing.T) {
  73. assert.Equal(t, test.expVal, IsExist(test.path))
  74. })
  75. }
  76. }
  77. func TestCurrentUsername(t *testing.T) {
  78. if oldUser, ok := os.LookupEnv("USER"); ok {
  79. defer func() { t.Setenv("USER", oldUser) }()
  80. } else {
  81. defer func() { _ = os.Unsetenv("USER") }()
  82. }
  83. t.Setenv("USER", "__TESTING::USERNAME")
  84. assert.Equal(t, "__TESTING::USERNAME", CurrentUsername())
  85. }
  86. func TestIsSymlink(t *testing.T) {
  87. // Create a temporary file
  88. tempFile, err := os.CreateTemp("", "symlink-test-*")
  89. require.NoError(t, err, "create temporary file")
  90. tempFilePath := tempFile.Name()
  91. _ = tempFile.Close()
  92. defer func() { _ = os.Remove(tempFilePath) }()
  93. // Create a temporary symlink
  94. tempSymlinkPath := tempFilePath + "-symlink"
  95. err = os.Symlink(tempFilePath, tempSymlinkPath)
  96. require.NoError(t, err, "create temporary symlink")
  97. defer func() { _ = os.Remove(tempSymlinkPath) }()
  98. tests := []struct {
  99. name string
  100. path string
  101. want bool
  102. }{
  103. {
  104. name: "non-existent path",
  105. path: "not_found",
  106. want: false,
  107. },
  108. {
  109. name: "regular file",
  110. path: tempFilePath,
  111. want: false,
  112. },
  113. {
  114. name: "symlink",
  115. path: tempSymlinkPath,
  116. want: true,
  117. },
  118. }
  119. for _, test := range tests {
  120. t.Run(test.name, func(t *testing.T) {
  121. assert.Equal(t, test.want, IsSymlink(test.path))
  122. })
  123. }
  124. }