code.gitea.io/gitea@v1.22.3/tests/integration/lfs_local_endpoint_test.go (about)

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