Debian Codegraph @

plumbing/transport/internal/common/common_test.go

raw
1 package common
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/go-git/go-git/v5/plumbing/transport"
8 . "gopkg.in/check.v1"
9 )
10
11 func Test(t *testing.T) { TestingT(t) }
12
13 type CommonSuite struct{}
14
15 var _ = Suite(&CommonSuite{})
16
17 func (s *CommonSuite) TestIsRepoNotFoundErrorForUnknownSource(c *C) {
18 msg := "unknown system is complaining of something very sad :("
19
20 isRepoNotFound := isRepoNotFoundError(msg)
21
22 c.Assert(isRepoNotFound, Equals, false)
23 }
24
25 func (s *CommonSuite) TestIsRepoNotFoundError(c *C) {
26 msg := "no such repository : some error stuf"
27
28 isRepoNotFound := isRepoNotFoundError(msg)
29
30 c.Assert(isRepoNotFound, Equals, true)
31 }
32
33 func (s *CommonSuite) TestCheckNotFoundError(c *C) {
34 firstErrLine := make(chan string, 1)
35
36 session := session{
37 firstErrLine: firstErrLine,
38 }
39
40 firstErrLine <- ""
41
42 err := session.checkNotFoundError()
43
44 c.Assert(err, IsNil)
45 }
46
47 func TestAdvertisedReferencesWithRemoteError(t *testing.T) {
48 tests := []struct {
49 name string
50 stderr string
51 wantErr error
52 }{
53 {
54 name: "unknown error",
55 stderr: "something",
56 wantErr: fmt.Errorf("unknown error: something"),
57 },
58 {
59 name: "GitLab: repository not found",
60 stderr: `remote:
61 remote: ========================================================================
62 remote:
63 remote: ERROR: The project you were looking for could not be found or you don't have permission to view it.
64
65 remote:
66 remote: ========================================================================
67 remote:`,
68 wantErr: transport.ErrRepositoryNotFound,
69 },
70 }
71
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 client := NewClient(MockCommander{stderr: tt.stderr})
75 sess, err := client.NewUploadPackSession(nil, nil)
76 if err != nil {
77 t.Fatalf("unexpected error: %s", err)
78 }
79
80 _, err = sess.AdvertisedReferences()
81
82 if tt.wantErr != nil {
83 if tt.wantErr != err {
84 if tt.wantErr.Error() != err.Error() {
85 t.Fatalf("expected a different error: got '%s', expected '%s'", err, tt.wantErr)
86 }
87 }
88 } else if err != nil {
89 t.Fatalf("unexpected error: %s", err)
90 }
91 })
92 }
93 }