osutil_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. )
  10. func TestIsFile(t *testing.T) {
  11. tests := []struct {
  12. path string
  13. expVal bool
  14. }{
  15. {
  16. path: "osutil.go",
  17. expVal: true,
  18. }, {
  19. path: "../osutil",
  20. expVal: false,
  21. }, {
  22. path: "not_found",
  23. expVal: false,
  24. },
  25. }
  26. for _, test := range tests {
  27. t.Run("", func(t *testing.T) {
  28. assert.Equal(t, test.expVal, IsFile(test.path))
  29. })
  30. }
  31. }
  32. func TestIsDir(t *testing.T) {
  33. tests := []struct {
  34. path string
  35. expVal bool
  36. }{
  37. {
  38. path: "osutil.go",
  39. expVal: false,
  40. }, {
  41. path: "../osutil",
  42. expVal: true,
  43. }, {
  44. path: "not_found",
  45. expVal: false,
  46. },
  47. }
  48. for _, test := range tests {
  49. t.Run("", func(t *testing.T) {
  50. assert.Equal(t, test.expVal, IsDir(test.path))
  51. })
  52. }
  53. }
  54. func TestIsExist(t *testing.T) {
  55. tests := []struct {
  56. path string
  57. expVal bool
  58. }{
  59. {
  60. path: "osutil.go",
  61. expVal: true,
  62. }, {
  63. path: "../osutil",
  64. expVal: true,
  65. }, {
  66. path: "not_found",
  67. expVal: false,
  68. },
  69. }
  70. for _, test := range tests {
  71. t.Run("", func(t *testing.T) {
  72. assert.Equal(t, test.expVal, IsExist(test.path))
  73. })
  74. }
  75. }
  76. func TestCurrentUsername(t *testing.T) {
  77. if oldUser, ok := os.LookupEnv("USER"); ok {
  78. defer func() { _ = os.Setenv("USER", oldUser) }()
  79. } else {
  80. defer func() { _ = os.Unsetenv("USER") }()
  81. }
  82. if err := os.Setenv("USER", "__TESTING::USERNAME"); err != nil {
  83. t.Skip("Could not set the USER environment variable:", err)
  84. }
  85. assert.Equal(t, "__TESTING::USERNAME", CurrentUsername())
  86. }