You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

path_test.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "net/url"
  6. "runtime"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestFileURLToPath(t *testing.T) {
  11. cases := []struct {
  12. url string
  13. expected string
  14. haserror bool
  15. windows bool
  16. }{
  17. // case 0
  18. {
  19. url: "",
  20. haserror: true,
  21. },
  22. // case 1
  23. {
  24. url: "http://test.io",
  25. haserror: true,
  26. },
  27. // case 2
  28. {
  29. url: "file:///path",
  30. expected: "/path",
  31. },
  32. // case 3
  33. {
  34. url: "file:///C:/path",
  35. expected: "C:/path",
  36. windows: true,
  37. },
  38. }
  39. for n, c := range cases {
  40. if c.windows && runtime.GOOS != "windows" {
  41. continue
  42. }
  43. u, _ := url.Parse(c.url)
  44. p, err := FileURLToPath(u)
  45. if c.haserror {
  46. assert.Error(t, err, "case %d: should return error", n)
  47. } else {
  48. assert.NoError(t, err, "case %d: should not return error", n)
  49. assert.Equal(t, c.expected, p, "case %d: should be equal", n)
  50. }
  51. }
  52. }
  53. func TestMisc_IsReadmeFileName(t *testing.T) {
  54. trueTestCases := []string{
  55. "readme",
  56. "README",
  57. "readME.mdown",
  58. "README.md",
  59. "readme.i18n.md",
  60. }
  61. falseTestCases := []string{
  62. "test.md",
  63. "wow.MARKDOWN",
  64. "LOL.mDoWn",
  65. "test",
  66. "abcdefg",
  67. "abcdefghijklmnopqrstuvwxyz",
  68. "test.md.test",
  69. "readmf",
  70. }
  71. for _, testCase := range trueTestCases {
  72. assert.True(t, IsReadmeFileName(testCase))
  73. }
  74. for _, testCase := range falseTestCases {
  75. assert.False(t, IsReadmeFileName(testCase))
  76. }
  77. type extensionTestcase struct {
  78. name string
  79. expected bool
  80. idx int
  81. }
  82. exts := []string{".md", ".txt", ""}
  83. testCasesExtensions := []extensionTestcase{
  84. {
  85. name: "readme",
  86. expected: true,
  87. idx: 2,
  88. },
  89. {
  90. name: "readme.md",
  91. expected: true,
  92. idx: 0,
  93. },
  94. {
  95. name: "README.md",
  96. expected: true,
  97. idx: 0,
  98. },
  99. {
  100. name: "ReAdMe.Md",
  101. expected: true,
  102. idx: 0,
  103. },
  104. {
  105. name: "readme.txt",
  106. expected: true,
  107. idx: 1,
  108. },
  109. {
  110. name: "readme.doc",
  111. expected: true,
  112. idx: 3,
  113. },
  114. {
  115. name: "readmee.md",
  116. },
  117. {
  118. name: "readme..",
  119. expected: true,
  120. idx: 3,
  121. },
  122. }
  123. for _, testCase := range testCasesExtensions {
  124. idx, ok := IsReadmeFileExtension(testCase.name, exts...)
  125. assert.Equal(t, testCase.expected, ok)
  126. assert.Equal(t, testCase.idx, idx)
  127. }
  128. }
  129. func TestCleanPath(t *testing.T) {
  130. cases := []struct {
  131. elems []string
  132. expected string
  133. }{
  134. {[]string{}, ``},
  135. {[]string{``}, ``},
  136. {[]string{`..`}, `.`},
  137. {[]string{`a`}, `a`},
  138. {[]string{`/a/`}, `a`},
  139. {[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`},
  140. {[]string{`a\..\b`}, `a\..\b`},
  141. {[]string{`a`, ``, `b`}, `a/b`},
  142. {[]string{`a`, `..`, `b`}, `a/b`},
  143. {[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`},
  144. }
  145. for _, c := range cases {
  146. assert.Equal(t, c.expected, PathJoinRel(c.elems...), "case: %v", c.elems)
  147. }
  148. cases = []struct {
  149. elems []string
  150. expected string
  151. }{
  152. {[]string{}, ``},
  153. {[]string{``}, ``},
  154. {[]string{`..`}, `.`},
  155. {[]string{`a`}, `a`},
  156. {[]string{`/a/`}, `a`},
  157. {[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`},
  158. {[]string{`a\..\b`}, `b`},
  159. {[]string{`a`, ``, `b`}, `a/b`},
  160. {[]string{`a`, `..`, `b`}, `a/b`},
  161. {[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`},
  162. }
  163. for _, c := range cases {
  164. assert.Equal(t, c.expected, PathJoinRelX(c.elems...), "case: %v", c.elems)
  165. }
  166. // for POSIX only, but the result is similar on Windows, because the first element must be an absolute path
  167. if isOSWindows() {
  168. cases = []struct {
  169. elems []string
  170. expected string
  171. }{
  172. {[]string{`C:\..`}, `C:\`},
  173. {[]string{`C:\a`}, `C:\a`},
  174. {[]string{`C:\a/`}, `C:\a`},
  175. {[]string{`C:\..\a\`, `../b`, `c\..`, `d`}, `C:\a\b\d`},
  176. {[]string{`C:\a/..\b`}, `C:\b`},
  177. {[]string{`C:\a`, ``, `b`}, `C:\a\b`},
  178. {[]string{`C:\a`, `..`, `b`}, `C:\a\b`},
  179. {[]string{`C:\lfs`, `repo/..`, `user/../path`}, `C:\lfs\path`},
  180. }
  181. } else {
  182. cases = []struct {
  183. elems []string
  184. expected string
  185. }{
  186. {[]string{`/..`}, `/`},
  187. {[]string{`/a`}, `/a`},
  188. {[]string{`/a/`}, `/a`},
  189. {[]string{`/../a/`, `../b`, `c/..`, `d`}, `/a/b/d`},
  190. {[]string{`/a\..\b`}, `/b`},
  191. {[]string{`/a`, ``, `b`}, `/a/b`},
  192. {[]string{`/a`, `..`, `b`}, `/a/b`},
  193. {[]string{`/lfs`, `repo/..`, `user/../path`}, `/lfs/path`},
  194. }
  195. }
  196. for _, c := range cases {
  197. assert.Equal(t, c.expected, FilePathJoinAbs(c.elems[0], c.elems[1:]...), "case: %v", c.elems)
  198. }
  199. }