gitea源码

lfs_local_endpoint_test.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/url"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "code.gitea.io/gitea/modules/lfs"
  10. "code.gitea.io/gitea/tests"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func str2url(raw string) *url.URL {
  14. u, _ := url.Parse(raw)
  15. return u
  16. }
  17. func TestDetermineLocalEndpoint(t *testing.T) {
  18. defer tests.PrepareTestEnv(t)()
  19. root := t.TempDir()
  20. rootdotgit := t.TempDir()
  21. os.Mkdir(filepath.Join(rootdotgit, ".git"), 0o700)
  22. lfsroot := t.TempDir()
  23. // Test cases
  24. cases := []struct {
  25. cloneurl string
  26. lfsurl string
  27. expected *url.URL
  28. }{
  29. // case 0
  30. {
  31. cloneurl: root,
  32. lfsurl: "",
  33. expected: str2url("file://" + root),
  34. },
  35. // case 1
  36. {
  37. cloneurl: root,
  38. lfsurl: lfsroot,
  39. expected: str2url("file://" + lfsroot),
  40. },
  41. // case 2
  42. {
  43. cloneurl: "https://git.com/repo.git",
  44. lfsurl: lfsroot,
  45. expected: str2url("file://" + lfsroot),
  46. },
  47. // case 3
  48. {
  49. cloneurl: rootdotgit,
  50. lfsurl: "",
  51. expected: str2url("file://" + filepath.Join(rootdotgit, ".git")),
  52. },
  53. // case 4
  54. {
  55. cloneurl: "",
  56. lfsurl: rootdotgit,
  57. expected: str2url("file://" + filepath.Join(rootdotgit, ".git")),
  58. },
  59. // case 5
  60. {
  61. cloneurl: rootdotgit,
  62. lfsurl: rootdotgit,
  63. expected: str2url("file://" + filepath.Join(rootdotgit, ".git")),
  64. },
  65. // case 6
  66. {
  67. cloneurl: "file://" + root,
  68. lfsurl: "",
  69. expected: str2url("file://" + root),
  70. },
  71. // case 7
  72. {
  73. cloneurl: "file://" + root,
  74. lfsurl: "file://" + lfsroot,
  75. expected: str2url("file://" + lfsroot),
  76. },
  77. // case 8
  78. {
  79. cloneurl: root,
  80. lfsurl: "file://" + lfsroot,
  81. expected: str2url("file://" + lfsroot),
  82. },
  83. // case 9
  84. {
  85. cloneurl: "",
  86. lfsurl: "/does/not/exist",
  87. expected: nil,
  88. },
  89. // case 10
  90. {
  91. cloneurl: "",
  92. lfsurl: "file:///does/not/exist",
  93. expected: str2url("file:///does/not/exist"),
  94. },
  95. }
  96. for n, c := range cases {
  97. ep := lfs.DetermineEndpoint(c.cloneurl, c.lfsurl)
  98. assert.Equal(t, c.expected, ep, "case %d: error should match", n)
  99. }
  100. }