code.gitea.io/gitea@v1.22.3/modules/setting/path_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package setting
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type envVars map[string]string
    15  
    16  func (e envVars) Getenv(key string) string {
    17  	return e[key]
    18  }
    19  
    20  func 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("CustomPath(env)", func(t *testing.T) {
    64  		testInit(dirFoo, "", "")
    65  		InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{})
    66  		assert.Equal(t, dirFoo, AppWorkPath)
    67  		assert.Equal(t, fp(dirBar, "custom1"), CustomPath)
    68  		assert.Equal(t, fp(dirBar, "custom1/conf/app.ini"), CustomConf)
    69  	})
    70  
    71  	t.Run("CustomPath(env,arg)", func(t *testing.T) {
    72  		testInit(dirFoo, "", "")
    73  		InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{CustomPath: "custom2"})
    74  		assert.Equal(t, dirFoo, AppWorkPath)
    75  		assert.Equal(t, fp(dirFoo, "custom2"), CustomPath)
    76  		assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf)
    77  	})
    78  
    79  	t.Run("CustomConf", func(t *testing.T) {
    80  		testInit(dirFoo, "", "")
    81  		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: "app1.ini"})
    82  		assert.Equal(t, dirFoo, AppWorkPath)
    83  		cwd, _ := os.Getwd()
    84  		assert.Equal(t, fp(cwd, "app1.ini"), CustomConf)
    85  
    86  		testInit(dirFoo, "", "")
    87  		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: fp(dirBar, "app1.ini")})
    88  		assert.Equal(t, dirFoo, AppWorkPath)
    89  		assert.Equal(t, fp(dirBar, "app1.ini"), CustomConf)
    90  	})
    91  
    92  	t.Run("CustomConfOverrideWorkPath", func(t *testing.T) {
    93  		iniWorkPath := fp(tmpDir, "app-workpath.ini")
    94  		_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
    95  
    96  		testInit(dirFoo, "", "")
    97  		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
    98  		assert.Equal(t, dirXxx, AppWorkPath)
    99  		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
   100  		assert.Equal(t, iniWorkPath, CustomConf)
   101  		assert.False(t, AppWorkPathMismatch)
   102  
   103  		testInit(dirFoo, "", "")
   104  		InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
   105  		assert.Equal(t, dirXxx, AppWorkPath)
   106  		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
   107  		assert.Equal(t, iniWorkPath, CustomConf)
   108  		assert.True(t, AppWorkPathMismatch)
   109  
   110  		testInit(dirFoo, "", "")
   111  		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirBar, CustomConf: iniWorkPath})
   112  		assert.Equal(t, dirXxx, AppWorkPath)
   113  		assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
   114  		assert.Equal(t, iniWorkPath, CustomConf)
   115  		assert.True(t, AppWorkPathMismatch)
   116  	})
   117  
   118  	t.Run("Builtin", func(t *testing.T) {
   119  		testInit(dirFoo, dirBar, dirXxx)
   120  		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
   121  		assert.Equal(t, dirFoo, AppWorkPath)
   122  		assert.Equal(t, dirBar, CustomPath)
   123  		assert.Equal(t, dirXxx, CustomConf)
   124  
   125  		testInit(dirFoo, "custom1", "cfg.ini")
   126  		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
   127  		assert.Equal(t, dirFoo, AppWorkPath)
   128  		assert.Equal(t, fp(dirFoo, "custom1"), CustomPath)
   129  		assert.Equal(t, fp(dirFoo, "custom1/cfg.ini"), CustomConf)
   130  
   131  		testInit(dirFoo, "custom1", "cfg.ini")
   132  		InitWorkPathAndCommonConfig(envVars{"GITEA_WORK_DIR": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
   133  		assert.Equal(t, dirYyy, AppWorkPath)
   134  		assert.Equal(t, fp(dirYyy, "custom1"), CustomPath)
   135  		assert.Equal(t, fp(dirYyy, "custom1/cfg.ini"), CustomConf)
   136  
   137  		testInit(dirFoo, "custom1", "cfg.ini")
   138  		InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
   139  		assert.Equal(t, dirFoo, AppWorkPath)
   140  		assert.Equal(t, dirYyy, CustomPath)
   141  		assert.Equal(t, fp(dirYyy, "cfg.ini"), CustomConf)
   142  
   143  		iniWorkPath := fp(tmpDir, "app-workpath.ini")
   144  		_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
   145  		testInit(dirFoo, "custom1", "cfg.ini")
   146  		InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
   147  		assert.Equal(t, dirXxx, AppWorkPath)
   148  		assert.Equal(t, fp(dirXxx, "custom1"), CustomPath)
   149  		assert.Equal(t, iniWorkPath, CustomConf)
   150  	})
   151  }