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.

lfs_local_endpoint_test.go 2.2KB

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