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

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