mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-03-11 17:48:44 -05:00
refactor: add centralized ResolvePath for rootpath-relative paths
This commit is contained in:
@@ -473,6 +473,16 @@ func InitDefaultConfig() {
|
||||
PluginsDir.setDefault(filepath.Join(ServiceRootpath.GetString(), "plugins"))
|
||||
}
|
||||
|
||||
// ResolvePath resolves a path relative to service.rootpath.
|
||||
// If the path is already absolute, it is returned as-is (cleaned).
|
||||
// If the path is relative (or empty), it is joined with service.rootpath.
|
||||
func ResolvePath(p string) string {
|
||||
if filepath.IsAbs(p) {
|
||||
return filepath.Clean(p)
|
||||
}
|
||||
return filepath.Join(ServiceRootpath.GetString(), p)
|
||||
}
|
||||
|
||||
func GetConfigValueFromFile(configKey string) string {
|
||||
if !strings.HasSuffix(configKey, ".file") {
|
||||
configKey += ".file"
|
||||
|
||||
@@ -32,3 +32,44 @@ func TestGetRootpathLocation(t *testing.T) {
|
||||
result := getRootpathLocation()
|
||||
assert.Equal(t, expected, result)
|
||||
}
|
||||
|
||||
func TestResolvePath(t *testing.T) {
|
||||
// Save and restore rootpath
|
||||
original := ServiceRootpath.GetString()
|
||||
defer ServiceRootpath.Set(original)
|
||||
ServiceRootpath.Set("/var/lib/vikunja")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "absolute path returned as-is",
|
||||
input: "/etc/vikunja/config.yml",
|
||||
expected: "/etc/vikunja/config.yml",
|
||||
},
|
||||
{
|
||||
name: "relative path joined with rootpath",
|
||||
input: "files",
|
||||
expected: "/var/lib/vikunja/files",
|
||||
},
|
||||
{
|
||||
name: "relative subdir path joined with rootpath",
|
||||
input: "data/vikunja.db",
|
||||
expected: "/var/lib/vikunja/data/vikunja.db",
|
||||
},
|
||||
{
|
||||
name: "empty string returns rootpath",
|
||||
input: "",
|
||||
expected: "/var/lib/vikunja",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ResolvePath(tt.input)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user