1
0

computed_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package conf
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "gogs.io/gogs/internal/testutil"
  8. )
  9. func TestIsProdMode(t *testing.T) {
  10. before := App.RunMode
  11. defer func() {
  12. App.RunMode = before
  13. }()
  14. tests := []struct {
  15. mode string
  16. want bool
  17. }{
  18. {mode: "dev", want: false},
  19. {mode: "test", want: false},
  20. {mode: "prod", want: true},
  21. {mode: "Prod", want: true},
  22. {mode: "PROD", want: true},
  23. }
  24. for _, test := range tests {
  25. t.Run("", func(t *testing.T) {
  26. App.RunMode = test.mode
  27. assert.Equal(t, test.want, IsProdMode())
  28. })
  29. }
  30. }
  31. func TestWorkDirHelper(_ *testing.T) {
  32. if !testutil.WantHelperProcess() {
  33. return
  34. }
  35. fmt.Fprintln(os.Stdout, WorkDir())
  36. }
  37. func TestWorkDir(t *testing.T) {
  38. tests := []struct {
  39. env string
  40. want string
  41. }{
  42. {env: "GOGS_WORK_DIR=/tmp", want: "/tmp"},
  43. {env: "", want: WorkDir()},
  44. }
  45. for _, test := range tests {
  46. t.Run("", func(t *testing.T) {
  47. out, err := testutil.Exec("TestWorkDirHelper", test.env)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. assert.Equal(t, test.want, out)
  52. })
  53. }
  54. }
  55. func TestCustomDirHelper(_ *testing.T) {
  56. if !testutil.WantHelperProcess() {
  57. return
  58. }
  59. fmt.Fprintln(os.Stdout, CustomDir())
  60. }
  61. func TestCustomDir(t *testing.T) {
  62. tests := []struct {
  63. env string
  64. want string
  65. }{
  66. {env: "GOGS_CUSTOM=/tmp", want: "/tmp"},
  67. {env: "", want: CustomDir()},
  68. }
  69. for _, test := range tests {
  70. t.Run("", func(t *testing.T) {
  71. out, err := testutil.Exec("TestCustomDirHelper", test.env)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. assert.Equal(t, test.want, out)
  76. })
  77. }
  78. }
  79. func TestHomeDirHelper(_ *testing.T) {
  80. if !testutil.WantHelperProcess() {
  81. return
  82. }
  83. fmt.Fprintln(os.Stdout, HomeDir())
  84. }
  85. func TestHomeDir(t *testing.T) {
  86. tests := []struct {
  87. envs []string
  88. want string
  89. }{
  90. {envs: []string{"HOME=/tmp"}, want: "/tmp"},
  91. {envs: []string{`USERPROFILE=C:\Users\Joe`}, want: `C:\Users\Joe`},
  92. {envs: []string{`HOMEDRIVE=C:`, `HOMEPATH=\Users\Joe`}, want: `C:\Users\Joe`},
  93. {envs: nil, want: ""},
  94. }
  95. for _, test := range tests {
  96. t.Run("", func(t *testing.T) {
  97. out, err := testutil.Exec("TestHomeDirHelper", test.envs...)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. assert.Equal(t, test.want, out)
  102. })
  103. }
  104. }