mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-25 11:22:16 +00:00
parent
090e753923
commit
b116418f05
13 changed files with 45 additions and 28 deletions
|
@ -14,6 +14,14 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// CleanPath ensure to clean the path
|
||||
func CleanPath(p string) string {
|
||||
if strings.HasPrefix(p, "/") {
|
||||
return path.Clean(p)
|
||||
}
|
||||
return path.Clean("/" + p)[1:]
|
||||
}
|
||||
|
||||
// EnsureAbsolutePath ensure that a path is absolute, making it
|
||||
// relative to absoluteBase if necessary
|
||||
func EnsureAbsolutePath(path, absoluteBase string) string {
|
||||
|
|
|
@ -136,3 +136,15 @@ func TestMisc_IsReadmeFileName(t *testing.T) {
|
|||
assert.Equal(t, testCase.idx, idx)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanPath(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"../../test": "test",
|
||||
"/test": "/test",
|
||||
"/../test": "/test",
|
||||
}
|
||||
|
||||
for k, v := range cases {
|
||||
assert.Equal(t, v, CleanPath(k))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue