You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

path_test.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. type envVars map[string]string
  11. func (e envVars) Getenv(key string) string {
  12. return e[key]
  13. }
  14. func TestInitWorkPathAndCommonConfig(t *testing.T) {
  15. testInit := func(defaultWorkPath, defaultCustomPath, defaultCustomConf string) {
  16. AppWorkPathMismatch = false
  17. AppWorkPath = defaultWorkPath
  18. appWorkPathBuiltin = defaultWorkPath
  19. CustomPath = defaultCustomPath
  20. customPathBuiltin = defaultCustomPath
  21. CustomConf = defaultCustomConf
  22. customConfBuiltin = defaultCustomConf
  23. }
  24. fp := filepath.Join
  25. tmpDir := t.TempDir()
  26. dirFoo := fp(tmpDir, "foo")
  27. dirBar := fp(tmpDir, "bar")
  28. dirXxx := fp(tmpDir, "xxx")
  29. dirYyy := fp(tmpDir, "yyy")
  30. t.Run("Default", func(t *testing.T) {
  31. testInit(dirFoo, "", "")
  32. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
  33. assert.Equal(t, dirFoo, AppWorkPath)
  34. assert.Equal(t, fp(dirFoo, "custom"), CustomPath)
  35. assert.Equal(t, fp(dirFoo, "custom/conf/app.ini"), CustomConf)
  36. })
  37. t.Run("WorkDir(env)", func(t *testing.T) {
  38. testInit(dirFoo, "", "")
  39. InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{})
  40. assert.Equal(t, dirBar, AppWorkPath)
  41. assert.Equal(t, fp(dirBar, "custom"), CustomPath)
  42. assert.Equal(t, fp(dirBar, "custom/conf/app.ini"), CustomConf)
  43. })
  44. t.Run("WorkDir(env,arg)", func(t *testing.T) {
  45. testInit(dirFoo, "", "")
  46. InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirXxx})
  47. assert.Equal(t, dirXxx, AppWorkPath)
  48. assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
  49. assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf)
  50. })
  51. t.Run("CustomPath(env)", func(t *testing.T) {
  52. testInit(dirFoo, "", "")
  53. InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{})
  54. assert.Equal(t, dirFoo, AppWorkPath)
  55. assert.Equal(t, fp(dirBar, "custom1"), CustomPath)
  56. assert.Equal(t, fp(dirBar, "custom1/conf/app.ini"), CustomConf)
  57. })
  58. t.Run("CustomPath(env,arg)", func(t *testing.T) {
  59. testInit(dirFoo, "", "")
  60. InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{CustomPath: "custom2"})
  61. assert.Equal(t, dirFoo, AppWorkPath)
  62. assert.Equal(t, fp(dirFoo, "custom2"), CustomPath)
  63. assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf)
  64. })
  65. t.Run("CustomConf", func(t *testing.T) {
  66. testInit(dirFoo, "", "")
  67. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: "app1.ini"})
  68. assert.Equal(t, dirFoo, AppWorkPath)
  69. cwd, _ := os.Getwd()
  70. assert.Equal(t, fp(cwd, "app1.ini"), CustomConf)
  71. testInit(dirFoo, "", "")
  72. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: fp(dirBar, "app1.ini")})
  73. assert.Equal(t, dirFoo, AppWorkPath)
  74. assert.Equal(t, fp(dirBar, "app1.ini"), CustomConf)
  75. })
  76. t.Run("CustomConfOverrideWorkPath", func(t *testing.T) {
  77. iniWorkPath := fp(tmpDir, "app-workpath.ini")
  78. _ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
  79. testInit(dirFoo, "", "")
  80. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
  81. assert.Equal(t, dirXxx, AppWorkPath)
  82. assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
  83. assert.Equal(t, iniWorkPath, CustomConf)
  84. assert.False(t, AppWorkPathMismatch)
  85. testInit(dirFoo, "", "")
  86. InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
  87. assert.Equal(t, dirXxx, AppWorkPath)
  88. assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
  89. assert.Equal(t, iniWorkPath, CustomConf)
  90. assert.True(t, AppWorkPathMismatch)
  91. testInit(dirFoo, "", "")
  92. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirBar, CustomConf: iniWorkPath})
  93. assert.Equal(t, dirXxx, AppWorkPath)
  94. assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
  95. assert.Equal(t, iniWorkPath, CustomConf)
  96. assert.True(t, AppWorkPathMismatch)
  97. })
  98. t.Run("Builtin", func(t *testing.T) {
  99. testInit(dirFoo, dirBar, dirXxx)
  100. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
  101. assert.Equal(t, dirFoo, AppWorkPath)
  102. assert.Equal(t, dirBar, CustomPath)
  103. assert.Equal(t, dirXxx, CustomConf)
  104. testInit(dirFoo, "custom1", "cfg.ini")
  105. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
  106. assert.Equal(t, dirFoo, AppWorkPath)
  107. assert.Equal(t, fp(dirFoo, "custom1"), CustomPath)
  108. assert.Equal(t, fp(dirFoo, "custom1/cfg.ini"), CustomConf)
  109. testInit(dirFoo, "custom1", "cfg.ini")
  110. InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
  111. assert.Equal(t, dirYyy, AppWorkPath)
  112. assert.Equal(t, fp(dirYyy, "custom1"), CustomPath)
  113. assert.Equal(t, fp(dirYyy, "custom1/cfg.ini"), CustomConf)
  114. testInit(dirFoo, "custom1", "cfg.ini")
  115. InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
  116. assert.Equal(t, dirFoo, AppWorkPath)
  117. assert.Equal(t, dirYyy, CustomPath)
  118. assert.Equal(t, fp(dirYyy, "cfg.ini"), CustomConf)
  119. iniWorkPath := fp(tmpDir, "app-workpath.ini")
  120. _ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
  121. testInit(dirFoo, "custom1", "cfg.ini")
  122. InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
  123. assert.Equal(t, dirXxx, AppWorkPath)
  124. assert.Equal(t, fp(dirXxx, "custom1"), CustomPath)
  125. assert.Equal(t, iniWorkPath, CustomConf)
  126. })
  127. }