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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2021 The Gitea 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 integrations
  5. import (
  6. "fmt"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "code.gitea.io/gitea/modules/lfs"
  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 prepareTestEnv(t)()
  20. root, _ := os.MkdirTemp("", "lfs_test")
  21. defer os.RemoveAll(root)
  22. rootdotgit, _ := os.MkdirTemp("", "lfs_test")
  23. defer os.RemoveAll(rootdotgit)
  24. os.Mkdir(filepath.Join(rootdotgit, ".git"), 0o700)
  25. lfsroot, _ := os.MkdirTemp("", "lfs_test")
  26. defer os.RemoveAll(lfsroot)
  27. // Test cases
  28. cases := []struct {
  29. cloneurl string
  30. lfsurl string
  31. expected *url.URL
  32. }{
  33. // case 0
  34. {
  35. cloneurl: root,
  36. lfsurl: "",
  37. expected: str2url(fmt.Sprintf("file://%s", root)),
  38. },
  39. // case 1
  40. {
  41. cloneurl: root,
  42. lfsurl: lfsroot,
  43. expected: str2url(fmt.Sprintf("file://%s", lfsroot)),
  44. },
  45. // case 2
  46. {
  47. cloneurl: "https://git.com/repo.git",
  48. lfsurl: lfsroot,
  49. expected: str2url(fmt.Sprintf("file://%s", lfsroot)),
  50. },
  51. // case 3
  52. {
  53. cloneurl: rootdotgit,
  54. lfsurl: "",
  55. expected: str2url(fmt.Sprintf("file://%s", filepath.Join(rootdotgit, ".git"))),
  56. },
  57. // case 4
  58. {
  59. cloneurl: "",
  60. lfsurl: rootdotgit,
  61. expected: str2url(fmt.Sprintf("file://%s", filepath.Join(rootdotgit, ".git"))),
  62. },
  63. // case 5
  64. {
  65. cloneurl: rootdotgit,
  66. lfsurl: rootdotgit,
  67. expected: str2url(fmt.Sprintf("file://%s", filepath.Join(rootdotgit, ".git"))),
  68. },
  69. // case 6
  70. {
  71. cloneurl: fmt.Sprintf("file://%s", root),
  72. lfsurl: "",
  73. expected: str2url(fmt.Sprintf("file://%s", root)),
  74. },
  75. // case 7
  76. {
  77. cloneurl: fmt.Sprintf("file://%s", root),
  78. lfsurl: fmt.Sprintf("file://%s", lfsroot),
  79. expected: str2url(fmt.Sprintf("file://%s", lfsroot)),
  80. },
  81. // case 8
  82. {
  83. cloneurl: root,
  84. lfsurl: fmt.Sprintf("file://%s", lfsroot),
  85. expected: str2url(fmt.Sprintf("file://%s", lfsroot)),
  86. },
  87. // case 9
  88. {
  89. cloneurl: "",
  90. lfsurl: "/does/not/exist",
  91. expected: nil,
  92. },
  93. // case 10
  94. {
  95. cloneurl: "",
  96. lfsurl: "file:///does/not/exist",
  97. expected: str2url("file:///does/not/exist"),
  98. },
  99. }
  100. for n, c := range cases {
  101. ep := lfs.DetermineEndpoint(c.cloneurl, c.lfsurl)
  102. assert.Equal(t, c.expected, ep, "case %d: error should match", n)
  103. }
  104. }