mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 10:56:10 +01:00 
			
		
		
		
	Introduce path Clean/Join helper functions (#23495)
Since #23493 has conflicts with latest commits, this PR is my proposal for fixing #23371 Details are in the comments And refactor the `modules/options` module, to make it always use "filepath" to access local files. Benefits: * No need to do `util.CleanPath(strings.ReplaceAll(p, "\\", "/"))), "/")` any more (not only one before) * The function behaviors are clearly defined
This commit is contained in:
		| @@ -138,13 +138,75 @@ func TestMisc_IsReadmeFileName(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestCleanPath(t *testing.T) { | ||||
| 	cases := map[string]string{ | ||||
| 		"../../test": "test", | ||||
| 		"/test":      "/test", | ||||
| 		"/../test":   "/test", | ||||
| 	cases := []struct { | ||||
| 		elems    []string | ||||
| 		expected string | ||||
| 	}{ | ||||
| 		{[]string{}, ``}, | ||||
| 		{[]string{``}, ``}, | ||||
| 		{[]string{`..`}, `.`}, | ||||
| 		{[]string{`a`}, `a`}, | ||||
| 		{[]string{`/a/`}, `a`}, | ||||
| 		{[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`}, | ||||
| 		{[]string{`a\..\b`}, `a\..\b`}, | ||||
| 		{[]string{`a`, ``, `b`}, `a/b`}, | ||||
| 		{[]string{`a`, `..`, `b`}, `a/b`}, | ||||
| 		{[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`}, | ||||
| 	} | ||||
| 	for _, c := range cases { | ||||
| 		assert.Equal(t, c.expected, PathJoinRel(c.elems...), "case: %v", c.elems) | ||||
| 	} | ||||
|  | ||||
| 	for k, v := range cases { | ||||
| 		assert.Equal(t, v, CleanPath(k)) | ||||
| 	cases = []struct { | ||||
| 		elems    []string | ||||
| 		expected string | ||||
| 	}{ | ||||
| 		{[]string{}, ``}, | ||||
| 		{[]string{``}, ``}, | ||||
| 		{[]string{`..`}, `.`}, | ||||
| 		{[]string{`a`}, `a`}, | ||||
| 		{[]string{`/a/`}, `a`}, | ||||
| 		{[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`}, | ||||
| 		{[]string{`a\..\b`}, `b`}, | ||||
| 		{[]string{`a`, ``, `b`}, `a/b`}, | ||||
| 		{[]string{`a`, `..`, `b`}, `a/b`}, | ||||
| 		{[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`}, | ||||
| 	} | ||||
| 	for _, c := range cases { | ||||
| 		assert.Equal(t, c.expected, PathJoinRelX(c.elems...), "case: %v", c.elems) | ||||
| 	} | ||||
|  | ||||
| 	// for POSIX only, but the result is similar on Windows, because the first element must be an absolute path | ||||
| 	if isOSWindows() { | ||||
| 		cases = []struct { | ||||
| 			elems    []string | ||||
| 			expected string | ||||
| 		}{ | ||||
| 			{[]string{`C:\..`}, `C:\`}, | ||||
| 			{[]string{`C:\a`}, `C:\a`}, | ||||
| 			{[]string{`C:\a/`}, `C:\a`}, | ||||
| 			{[]string{`C:\..\a\`, `../b`, `c\..`, `d`}, `C:\a\b\d`}, | ||||
| 			{[]string{`C:\a/..\b`}, `C:\b`}, | ||||
| 			{[]string{`C:\a`, ``, `b`}, `C:\a\b`}, | ||||
| 			{[]string{`C:\a`, `..`, `b`}, `C:\a\b`}, | ||||
| 			{[]string{`C:\lfs`, `repo/..`, `user/../path`}, `C:\lfs\path`}, | ||||
| 		} | ||||
| 	} else { | ||||
| 		cases = []struct { | ||||
| 			elems    []string | ||||
| 			expected string | ||||
| 		}{ | ||||
| 			{[]string{`/..`}, `/`}, | ||||
| 			{[]string{`/a`}, `/a`}, | ||||
| 			{[]string{`/a/`}, `/a`}, | ||||
| 			{[]string{`/../a/`, `../b`, `c/..`, `d`}, `/a/b/d`}, | ||||
| 			{[]string{`/a\..\b`}, `/b`}, | ||||
| 			{[]string{`/a`, ``, `b`}, `/a/b`}, | ||||
| 			{[]string{`/a`, `..`, `b`}, `/a/b`}, | ||||
| 			{[]string{`/lfs`, `repo/..`, `user/../path`}, `/lfs/path`}, | ||||
| 		} | ||||
| 	} | ||||
| 	for _, c := range cases { | ||||
| 		assert.Equal(t, c.expected, FilePathJoinAbs(c.elems...), "case: %v", c.elems) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user