1
0

osutil_test.go 2.3 KB

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