loading up the forgejo repo on tangled to test page performance
at forgejo 9.7 kB view raw
1// Copyright 2023 The Gitea Authors. All rights reserved. 2// SPDX-License-Identifier: MIT 3 4package setting 5 6import ( 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12) 13 14type envVars map[string]string 15 16func (e envVars) Getenv(key string) string { 17 return e[key] 18} 19 20func TestInitWorkPathAndCommonConfig(t *testing.T) { 21 testInit := func(defaultWorkPath, defaultCustomPath, defaultCustomConf string) { 22 AppWorkPathMismatch = false 23 AppWorkPath = defaultWorkPath 24 appWorkPathBuiltin = defaultWorkPath 25 CustomPath = defaultCustomPath 26 customPathBuiltin = defaultCustomPath 27 CustomConf = defaultCustomConf 28 customConfBuiltin = defaultCustomConf 29 } 30 31 fp := filepath.Join 32 33 tmpDir := t.TempDir() 34 dirFoo := fp(tmpDir, "foo") 35 dirBar := fp(tmpDir, "bar") 36 dirXxx := fp(tmpDir, "xxx") 37 dirYyy := fp(tmpDir, "yyy") 38 39 t.Run("Default", func(t *testing.T) { 40 testInit(dirFoo, "", "") 41 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{}) 42 assert.Equal(t, dirFoo, AppWorkPath) 43 assert.Equal(t, fp(dirFoo, "custom"), CustomPath) 44 assert.Equal(t, fp(dirFoo, "custom/conf/app.ini"), CustomConf) 45 }) 46 47 t.Run("WorkDir(env)", func(t *testing.T) { 48 testInit(dirFoo, "", "") 49 InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{}) 50 assert.Equal(t, dirBar, AppWorkPath) 51 assert.Equal(t, fp(dirBar, "custom"), CustomPath) 52 assert.Equal(t, fp(dirBar, "custom/conf/app.ini"), CustomConf) 53 }) 54 55 t.Run("WorkDir(env,arg)", func(t *testing.T) { 56 testInit(dirFoo, "", "") 57 InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirXxx}) 58 assert.Equal(t, dirXxx, AppWorkPath) 59 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 60 assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf) 61 }) 62 63 t.Run("WorkDir(env)", func(t *testing.T) { 64 testInit(dirFoo, "", "") 65 InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{}) 66 assert.Equal(t, dirBar, AppWorkPath) 67 assert.Equal(t, fp(dirBar, "custom"), CustomPath) 68 assert.Equal(t, fp(dirBar, "custom/conf/app.ini"), CustomConf) 69 }) 70 71 t.Run("WorkDir(env,arg)", func(t *testing.T) { 72 testInit(dirFoo, "", "") 73 InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirXxx}) 74 assert.Equal(t, dirXxx, AppWorkPath) 75 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 76 assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf) 77 }) 78 79 t.Run("CustomPath(env)", func(t *testing.T) { 80 testInit(dirFoo, "", "") 81 InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{}) 82 assert.Equal(t, dirFoo, AppWorkPath) 83 assert.Equal(t, fp(dirBar, "custom1"), CustomPath) 84 assert.Equal(t, fp(dirBar, "custom1/conf/app.ini"), CustomConf) 85 }) 86 87 t.Run("CustomPath(env,arg)", func(t *testing.T) { 88 testInit(dirFoo, "", "") 89 InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{CustomPath: "custom2"}) 90 assert.Equal(t, dirFoo, AppWorkPath) 91 assert.Equal(t, fp(dirFoo, "custom2"), CustomPath) 92 assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf) 93 }) 94 95 t.Run("CustomPath(env)", func(t *testing.T) { 96 testInit(dirFoo, "", "") 97 InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{}) 98 assert.Equal(t, dirFoo, AppWorkPath) 99 assert.Equal(t, fp(dirBar, "custom1"), CustomPath) 100 assert.Equal(t, fp(dirBar, "custom1/conf/app.ini"), CustomConf) 101 }) 102 103 t.Run("CustomPath(env,arg)", func(t *testing.T) { 104 testInit(dirFoo, "", "") 105 InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{CustomPath: "custom2"}) 106 assert.Equal(t, dirFoo, AppWorkPath) 107 assert.Equal(t, fp(dirFoo, "custom2"), CustomPath) 108 assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf) 109 }) 110 111 t.Run("CustomConf", func(t *testing.T) { 112 testInit(dirFoo, "", "") 113 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: "app1.ini"}) 114 assert.Equal(t, dirFoo, AppWorkPath) 115 cwd, _ := os.Getwd() 116 assert.Equal(t, fp(cwd, "app1.ini"), CustomConf) 117 118 testInit(dirFoo, "", "") 119 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: fp(dirBar, "app1.ini")}) 120 assert.Equal(t, dirFoo, AppWorkPath) 121 assert.Equal(t, fp(dirBar, "app1.ini"), CustomConf) 122 }) 123 124 t.Run("CustomConfOverrideWorkPath", func(t *testing.T) { 125 iniWorkPath := fp(tmpDir, "app-workpath.ini") 126 _ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644) 127 128 testInit(dirFoo, "", "") 129 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath}) 130 assert.Equal(t, dirXxx, AppWorkPath) 131 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 132 assert.Equal(t, iniWorkPath, CustomConf) 133 assert.False(t, AppWorkPathMismatch) 134 135 testInit(dirFoo, "", "") 136 InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath}) 137 assert.Equal(t, dirXxx, AppWorkPath) 138 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 139 assert.Equal(t, iniWorkPath, CustomConf) 140 assert.True(t, AppWorkPathMismatch) 141 142 testInit(dirFoo, "", "") 143 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirBar, CustomConf: iniWorkPath}) 144 assert.Equal(t, dirXxx, AppWorkPath) 145 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 146 assert.Equal(t, iniWorkPath, CustomConf) 147 assert.True(t, AppWorkPathMismatch) 148 }) 149 150 t.Run("CustomConfOverrideWorkPath", func(t *testing.T) { 151 iniWorkPath := fp(tmpDir, "app-workpath.ini") 152 _ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644) 153 154 testInit(dirFoo, "", "") 155 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath}) 156 assert.Equal(t, dirXxx, AppWorkPath) 157 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 158 assert.Equal(t, iniWorkPath, CustomConf) 159 assert.False(t, AppWorkPathMismatch) 160 161 testInit(dirFoo, "", "") 162 InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath}) 163 assert.Equal(t, dirXxx, AppWorkPath) 164 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 165 assert.Equal(t, iniWorkPath, CustomConf) 166 assert.True(t, AppWorkPathMismatch) 167 168 testInit(dirFoo, "", "") 169 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirBar, CustomConf: iniWorkPath}) 170 assert.Equal(t, dirXxx, AppWorkPath) 171 assert.Equal(t, fp(dirXxx, "custom"), CustomPath) 172 assert.Equal(t, iniWorkPath, CustomConf) 173 assert.True(t, AppWorkPathMismatch) 174 }) 175 176 t.Run("Builtin", func(t *testing.T) { 177 testInit(dirFoo, dirBar, dirXxx) 178 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{}) 179 assert.Equal(t, dirFoo, AppWorkPath) 180 assert.Equal(t, dirBar, CustomPath) 181 assert.Equal(t, dirXxx, CustomConf) 182 183 testInit(dirFoo, "custom1", "cfg.ini") 184 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{}) 185 assert.Equal(t, dirFoo, AppWorkPath) 186 assert.Equal(t, fp(dirFoo, "custom1"), CustomPath) 187 assert.Equal(t, fp(dirFoo, "custom1/cfg.ini"), CustomConf) 188 189 testInit(dirFoo, "custom1", "cfg.ini") 190 InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirYyy}.Getenv, ArgWorkPathAndCustomConf{}) 191 assert.Equal(t, dirYyy, AppWorkPath) 192 assert.Equal(t, fp(dirYyy, "custom1"), CustomPath) 193 assert.Equal(t, fp(dirYyy, "custom1/cfg.ini"), CustomConf) 194 195 testInit(dirFoo, "custom1", "cfg.ini") 196 InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": dirYyy}.Getenv, ArgWorkPathAndCustomConf{}) 197 assert.Equal(t, dirFoo, AppWorkPath) 198 assert.Equal(t, dirYyy, CustomPath) 199 assert.Equal(t, fp(dirYyy, "cfg.ini"), CustomConf) 200 201 iniWorkPath := fp(tmpDir, "app-workpath.ini") 202 _ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644) 203 testInit(dirFoo, "custom1", "cfg.ini") 204 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath}) 205 assert.Equal(t, dirXxx, AppWorkPath) 206 assert.Equal(t, fp(dirXxx, "custom1"), CustomPath) 207 assert.Equal(t, iniWorkPath, CustomConf) 208 }) 209 210 t.Run("Builtin", func(t *testing.T) { 211 testInit(dirFoo, dirBar, dirXxx) 212 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{}) 213 assert.Equal(t, dirFoo, AppWorkPath) 214 assert.Equal(t, dirBar, CustomPath) 215 assert.Equal(t, dirXxx, CustomConf) 216 217 testInit(dirFoo, "custom1", "cfg.ini") 218 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{}) 219 assert.Equal(t, dirFoo, AppWorkPath) 220 assert.Equal(t, fp(dirFoo, "custom1"), CustomPath) 221 assert.Equal(t, fp(dirFoo, "custom1/cfg.ini"), CustomConf) 222 223 testInit(dirFoo, "custom1", "cfg.ini") 224 InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirYyy}.Getenv, ArgWorkPathAndCustomConf{}) 225 assert.Equal(t, dirYyy, AppWorkPath) 226 assert.Equal(t, fp(dirYyy, "custom1"), CustomPath) 227 assert.Equal(t, fp(dirYyy, "custom1/cfg.ini"), CustomConf) 228 229 testInit(dirFoo, "custom1", "cfg.ini") 230 InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": dirYyy}.Getenv, ArgWorkPathAndCustomConf{}) 231 assert.Equal(t, dirFoo, AppWorkPath) 232 assert.Equal(t, dirYyy, CustomPath) 233 assert.Equal(t, fp(dirYyy, "cfg.ini"), CustomConf) 234 235 iniWorkPath := fp(tmpDir, "app-workpath.ini") 236 _ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644) 237 testInit(dirFoo, "custom1", "cfg.ini") 238 InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath}) 239 assert.Equal(t, dirXxx, AppWorkPath) 240 assert.Equal(t, fp(dirXxx, "custom1"), CustomPath) 241 assert.Equal(t, iniWorkPath, CustomConf) 242 }) 243}