pathutil_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package pathutil
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestClean(t *testing.T) {
  10. tests := []struct {
  11. path string
  12. wantVal string
  13. }{
  14. {
  15. path: "../../../readme.txt",
  16. wantVal: "readme.txt",
  17. },
  18. {
  19. path: "a/../../../readme.txt",
  20. wantVal: "readme.txt",
  21. },
  22. {
  23. path: "/../a/b/../c/../readme.txt",
  24. wantVal: "a/readme.txt",
  25. },
  26. {
  27. path: "/a/readme.txt",
  28. wantVal: "a/readme.txt",
  29. },
  30. {
  31. path: "/",
  32. wantVal: "",
  33. },
  34. {
  35. path: "/a/b/c/readme.txt",
  36. wantVal: "a/b/c/readme.txt",
  37. },
  38. // Windows-specific
  39. {
  40. path: `..\..\..\readme.txt`,
  41. wantVal: "readme.txt",
  42. },
  43. {
  44. path: `a\..\..\..\readme.txt`,
  45. wantVal: "readme.txt",
  46. },
  47. {
  48. path: `\..\a\b\..\c\..\readme.txt`,
  49. wantVal: "a/readme.txt",
  50. },
  51. {
  52. path: `\a\readme.txt`,
  53. wantVal: "a/readme.txt",
  54. },
  55. {
  56. path: `..\..\..\../README.md`,
  57. wantVal: "README.md",
  58. },
  59. {
  60. path: `\`,
  61. wantVal: "",
  62. },
  63. {
  64. path: `\a\b\c\readme.txt`,
  65. wantVal: `a/b/c/readme.txt`,
  66. },
  67. }
  68. for _, test := range tests {
  69. t.Run(test.path, func(t *testing.T) {
  70. assert.Equal(t, test.wantVal, Clean(test.path))
  71. })
  72. }
  73. }