osutil_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Make sure it does not blow up
  78. CurrentUsername()
  79. }
  80. func TestCurrentUsernamePrefersEnvironmentVariable(t *testing.T) {
  81. // Some users/scripts expect that they can change the current username via environment variables
  82. if userBak, ok := os.LookupEnv("USER"); ok {
  83. defer os.Setenv("USER", userBak)
  84. } else {
  85. defer os.Unsetenv("USER")
  86. }
  87. if err := os.Setenv("USER", "__TESTING::USERNAME"); err != nil {
  88. t.Skip("Could not set the USER environment variable:", err)
  89. }
  90. assert.Equal(t, "__TESTING::USERNAME", CurrentUsername())
  91. }