computed_test.go 2.4 KB

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