pathutil_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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: "../../objects/info/..",
  28. wantVal: "objects",
  29. },
  30. {
  31. path: "/a/readme.txt",
  32. wantVal: "a/readme.txt",
  33. },
  34. {
  35. path: "/",
  36. wantVal: "",
  37. },
  38. {
  39. path: "/a/b/c/readme.txt",
  40. wantVal: "a/b/c/readme.txt",
  41. },
  42. // Windows-specific
  43. {
  44. path: `..\..\..\readme.txt`,
  45. wantVal: "readme.txt",
  46. },
  47. {
  48. path: `a\..\..\..\readme.txt`,
  49. wantVal: "readme.txt",
  50. },
  51. {
  52. path: `\..\a\b\..\c\..\readme.txt`,
  53. wantVal: "a/readme.txt",
  54. },
  55. {
  56. path: `\a\readme.txt`,
  57. wantVal: "a/readme.txt",
  58. },
  59. {
  60. path: `..\..\..\../README.md`,
  61. wantVal: "README.md",
  62. },
  63. {
  64. path: `\`,
  65. wantVal: "",
  66. },
  67. {
  68. path: `\a\b\c\readme.txt`,
  69. wantVal: `a/b/c/readme.txt`,
  70. },
  71. }
  72. for _, test := range tests {
  73. t.Run(test.path, func(t *testing.T) {
  74. assert.Equal(t, test.wantVal, Clean(test.path))
  75. })
  76. }
  77. }