| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"os/exec" |
| 5 |
"path/filepath" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 8 |
"github.com/go-git/go-git/v5/storage/memory" |
| 9 |
|
| 10 |
. "gopkg.in/check.v1" |
| 11 |
) |
| 12 |
|
| 13 |
type loaderSuiteRepo struct { |
| 14 |
bare bool |
| 15 |
|
| 16 |
path string |
| 17 |
} |
| 18 |
|
| 19 |
type LoaderSuite struct { |
| 20 |
Repos map[string]loaderSuiteRepo |
| 21 |
} |
| 22 |
|
| 23 |
var _ = Suite(&LoaderSuite{ |
| 24 |
Repos: map[string]loaderSuiteRepo{ |
| 25 |
"repo": {path: "repo.git"}, |
| 26 |
"bare": {path: "bare.git", bare: true}, |
| 27 |
}, |
| 28 |
}) |
| 29 |
|
| 30 |
func (s *LoaderSuite) SetUpSuite(c *C) { |
| 31 |
if err := exec.Command("git", "--version").Run(); err != nil { |
| 32 |
c.Skip("git command not found") |
| 33 |
} |
| 34 |
|
| 35 |
dir := c.MkDir() |
| 36 |
|
| 37 |
for key, repo := range s.Repos { |
| 38 |
repo.path = filepath.Join(dir, repo.path) |
| 39 |
if repo.bare { |
| 40 |
c.Assert(exec.Command("git", "init", "--bare", repo.path).Run(), IsNil) |
| 41 |
} else { |
| 42 |
c.Assert(exec.Command("git", "init", repo.path).Run(), IsNil) |
| 43 |
} |
| 44 |
s.Repos[key] = repo |
| 45 |
} |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
func (s *LoaderSuite) endpoint(c *C, url string) *transport.Endpoint { |
| 50 |
ep, err := transport.NewEndpoint(url) |
| 51 |
c.Assert(err, IsNil) |
| 52 |
return ep |
| 53 |
} |
| 54 |
|
| 55 |
func (s *LoaderSuite) TestLoadNonExistent(c *C) { |
| 56 |
sto, err := DefaultLoader.Load(s.endpoint(c, "does-not-exist")) |
| 57 |
c.Assert(err, Equals, transport.ErrRepositoryNotFound) |
| 58 |
c.Assert(sto, IsNil) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *LoaderSuite) TestLoadNonExistentIgnoreHost(c *C) { |
| 62 |
sto, err := DefaultLoader.Load(s.endpoint(c, "https://github.com/does-not-exist")) |
| 63 |
c.Assert(err, Equals, transport.ErrRepositoryNotFound) |
| 64 |
c.Assert(sto, IsNil) |
| 65 |
} |
| 66 |
|
| 67 |
func (s *LoaderSuite) TestLoad(c *C) { |
| 68 |
sto, err := DefaultLoader.Load(s.endpoint(c, s.Repos["repo"].path)) |
| 69 |
c.Assert(err, IsNil) |
| 70 |
c.Assert(sto, NotNil) |
| 71 |
} |
| 72 |
|
| 73 |
func (s *LoaderSuite) TestLoadBare(c *C) { |
| 74 |
sto, err := DefaultLoader.Load(s.endpoint(c, s.Repos["bare"].path)) |
| 75 |
c.Assert(err, IsNil) |
| 76 |
c.Assert(sto, NotNil) |
| 77 |
} |
| 78 |
|
| 79 |
func (s *LoaderSuite) TestMapLoader(c *C) { |
| 80 |
ep, err := transport.NewEndpoint("file://test") |
| 81 |
sto := memory.NewStorage() |
| 82 |
c.Assert(err, IsNil) |
| 83 |
|
| 84 |
loader := MapLoader{ep.String(): sto} |
| 85 |
|
| 86 |
ep, err = transport.NewEndpoint("file://test") |
| 87 |
c.Assert(err, IsNil) |
| 88 |
|
| 89 |
loaderSto, err := loader.Load(ep) |
| 90 |
c.Assert(err, IsNil) |
| 91 |
c.Assert(sto, Equals, loaderSto) |
| 92 |
} |