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