mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 08:21:11 +00:00 
			
		
		
		
	FORGEJO_* environment variables are set to the corresponding GITEA_* variable when the cli starts. This approach is intended to minimize the conflicts on rebase. All occurences of GITEA_* are left untouched in the codebase and they are only changed to FORGEJO_* if exposed to the user. (cherry picked from commite466f9d10e) (cherry picked from commite33e95931b) (cherry picked from commit2cfc6519b7) (cherry picked from commitaf8864373a) (cherry picked from commita0550ff339) (cherry picked from commit24dc0a5191) (cherry picked from commite255eea2b4) (cherry picked from commit0c4f5afa7a) (cherry picked from commit42fce708d0) (cherry picked from commite7278c3c22) (cherry picked from commit0fb9ed7e0e) (cherry picked from commita98308aa4d) (cherry picked from commitb8695fcbe0) (cherry picked from commit4aee8719f5) (cherry picked from commit1c503c1ba7) (cherry picked from commitcf1ed8551e) (cherry picked from commitc52459b088) (cherry picked from commit92cac277b2) (cherry picked from commit2c744f1118) (cherry picked from commit1482cfabe5)
		
			
				
	
	
		
			243 lines
		
	
	
	
		
			9.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			243 lines
		
	
	
	
		
			9.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2023 The Gitea Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: MIT
 | 
						|
 | 
						|
package setting
 | 
						|
 | 
						|
import (
 | 
						|
	"os"
 | 
						|
	"path/filepath"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/stretchr/testify/assert"
 | 
						|
)
 | 
						|
 | 
						|
type envVars map[string]string
 | 
						|
 | 
						|
func (e envVars) Getenv(key string) string {
 | 
						|
	return e[key]
 | 
						|
}
 | 
						|
 | 
						|
func TestInitWorkPathAndCommonConfig(t *testing.T) {
 | 
						|
	testInit := func(defaultWorkPath, defaultCustomPath, defaultCustomConf string) {
 | 
						|
		AppWorkPathMismatch = false
 | 
						|
		AppWorkPath = defaultWorkPath
 | 
						|
		appWorkPathBuiltin = defaultWorkPath
 | 
						|
		CustomPath = defaultCustomPath
 | 
						|
		customPathBuiltin = defaultCustomPath
 | 
						|
		CustomConf = defaultCustomConf
 | 
						|
		customConfBuiltin = defaultCustomConf
 | 
						|
	}
 | 
						|
 | 
						|
	fp := filepath.Join
 | 
						|
 | 
						|
	tmpDir := t.TempDir()
 | 
						|
	dirFoo := fp(tmpDir, "foo")
 | 
						|
	dirBar := fp(tmpDir, "bar")
 | 
						|
	dirXxx := fp(tmpDir, "xxx")
 | 
						|
	dirYyy := fp(tmpDir, "yyy")
 | 
						|
 | 
						|
	t.Run("Default", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("WorkDir(env)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirBar, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("WorkDir(env,arg)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirXxx})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("WorkDir(env)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirBar, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("WorkDir(env,arg)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirXxx})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("CustomPath(env)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom1/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("CustomPath(env,arg)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{CustomPath: "custom2"})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom2"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("CustomPath(env)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "custom1/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("CustomPath(env,arg)", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{CustomPath: "custom2"})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom2"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("CustomConf", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: "app1.ini"})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		cwd, _ := os.Getwd()
 | 
						|
		assert.Equal(t, fp(cwd, "app1.ini"), CustomConf)
 | 
						|
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: fp(dirBar, "app1.ini")})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirBar, "app1.ini"), CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("CustomConfOverrideWorkPath", func(t *testing.T) {
 | 
						|
		iniWorkPath := fp(tmpDir, "app-workpath.ini")
 | 
						|
		_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
 | 
						|
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
		assert.False(t, AppWorkPathMismatch)
 | 
						|
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
		assert.True(t, AppWorkPathMismatch)
 | 
						|
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirBar, CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
		assert.True(t, AppWorkPathMismatch)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("CustomConfOverrideWorkPath", func(t *testing.T) {
 | 
						|
		iniWorkPath := fp(tmpDir, "app-workpath.ini")
 | 
						|
		_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
 | 
						|
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
		assert.False(t, AppWorkPathMismatch)
 | 
						|
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
		assert.True(t, AppWorkPathMismatch)
 | 
						|
 | 
						|
		testInit(dirFoo, "", "")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirBar, CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
		assert.True(t, AppWorkPathMismatch)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("Builtin", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, dirBar, dirXxx)
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, dirBar, CustomPath)
 | 
						|
		assert.Equal(t, dirXxx, CustomConf)
 | 
						|
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom1/cfg.ini"), CustomConf)
 | 
						|
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirYyy, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirYyy, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirYyy, "custom1/cfg.ini"), CustomConf)
 | 
						|
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, dirYyy, CustomPath)
 | 
						|
		assert.Equal(t, fp(dirYyy, "cfg.ini"), CustomConf)
 | 
						|
 | 
						|
		iniWorkPath := fp(tmpDir, "app-workpath.ini")
 | 
						|
		_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("Builtin", func(t *testing.T) {
 | 
						|
		testInit(dirFoo, dirBar, dirXxx)
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, dirBar, CustomPath)
 | 
						|
		assert.Equal(t, dirXxx, CustomConf)
 | 
						|
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirFoo, "custom1/cfg.ini"), CustomConf)
 | 
						|
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirYyy, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirYyy, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, fp(dirYyy, "custom1/cfg.ini"), CustomConf)
 | 
						|
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
 | 
						|
		assert.Equal(t, dirFoo, AppWorkPath)
 | 
						|
		assert.Equal(t, dirYyy, CustomPath)
 | 
						|
		assert.Equal(t, fp(dirYyy, "cfg.ini"), CustomConf)
 | 
						|
 | 
						|
		iniWorkPath := fp(tmpDir, "app-workpath.ini")
 | 
						|
		_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
 | 
						|
		testInit(dirFoo, "custom1", "cfg.ini")
 | 
						|
		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
 | 
						|
		assert.Equal(t, dirXxx, AppWorkPath)
 | 
						|
		assert.Equal(t, fp(dirXxx, "custom1"), CustomPath)
 | 
						|
		assert.Equal(t, iniWorkPath, CustomConf)
 | 
						|
	})
 | 
						|
}
 |