-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_remote_test.go
More file actions
113 lines (102 loc) · 3.37 KB
/
Copy pathcheck_remote_test.go
File metadata and controls
113 lines (102 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/google/go-github/v56/github"
"github.com/haproxytech/check-commit/v5/junit"
gitlab "gitlab.com/gitlab-org/api/client-go"
)
func TestGithubAPICommitDataSingleFilesFetch(t *testing.T) {
filesCalls := 0
mux := http.NewServeMux()
mux.HandleFunc("/repos/own/repo/pulls/7/commits", func(w http.ResponseWriter, _ *http.Request) {
_, _ = fmt.Fprint(w, `[
{"sha":"1111111111111111","commit":{"sha":"1111111111111111","message":"first subject"}},
{"sha":"2222222222222222","commit":{"sha":"2222222222222222","message":"second subject"}}
]`)
})
mux.HandleFunc("/repos/own/repo/pulls/7/files", func(w http.ResponseWriter, _ *http.Request) {
filesCalls++
_, _ = fmt.Fprint(w, `[{"filename":"a.txt","patch":"+added line"}]`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
client := github.NewClient(nil)
base, _ := url.Parse(srv.URL + "/")
client.BaseURL = base
commits, diffs, err := githubAPICommitData(context.Background(), client,
"own/repo", "refs/pull/7/merge", "pull_request", &junit.JunitSuiteDummy{})
if err != nil {
t.Fatal(err)
}
if len(commits) != 2 {
t.Fatalf("commits = %d, want 2", len(commits))
}
if filesCalls != 1 {
t.Errorf("files endpoint called %d times, want 1", filesCalls)
}
if len(diffs) != 1 {
t.Fatalf("diffs entries = %d, want 1", len(diffs))
}
if diffs[0].Hash != "" {
t.Errorf("API diffs are not attributable, hash = %q", diffs[0].Hash)
}
if diffs[0].Files["a.txt"] != "+added line\n" {
t.Errorf("diff content = %v", diffs[0].Files)
}
}
func TestGithubAPICommitDataFailureIsUnavailable(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "boom", http.StatusInternalServerError)
}))
defer srv.Close()
client := github.NewClient(nil)
base, _ := url.Parse(srv.URL + "/")
client.BaseURL = base
_, _, err := githubAPICommitData(context.Background(), client,
"own/repo", "refs/pull/7/merge", "pull_request", &junit.JunitSuiteDummy{})
if !errors.Is(err, errCommitDataUnavailable) {
t.Fatalf("expected errCommitDataUnavailable, got %v", err)
}
}
func TestGitlabAPICommitDataSingleDiffsFetch(t *testing.T) {
diffsCalls := 0
mux := http.NewServeMux()
mux.HandleFunc("/api/v4/projects/42/merge_requests/7/commits", func(w http.ResponseWriter, _ *http.Request) {
_, _ = fmt.Fprint(w, `[
{"id":"aaaaaaaaaaaaaaaa","short_id":"aaaaaaaa","message":"first subject"},
{"id":"bbbbbbbbbbbbbbbb","short_id":"bbbbbbbb","message":"second subject"}
]`)
})
mux.HandleFunc("/api/v4/projects/42/merge_requests/7/diffs", func(w http.ResponseWriter, _ *http.Request) {
diffsCalls++
_, _ = fmt.Fprint(w, `[{"new_path":"a.txt","diff":"+added line"}]`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
client, err := gitlab.NewClient("", gitlab.WithBaseURL(srv.URL))
if err != nil {
t.Fatal(err)
}
commits, diffs, err := gitlabAPICommitData(client, "7", "42", &junit.JunitSuiteDummy{})
if err != nil {
t.Fatal(err)
}
if len(commits) != 2 {
t.Fatalf("commits = %d, want 2", len(commits))
}
if diffsCalls != 1 {
t.Errorf("diffs endpoint called %d times, want 1", diffsCalls)
}
if len(diffs) != 1 {
t.Fatalf("diffs entries = %d, want 1", len(diffs))
}
if diffs[0].Files["a.txt"] != "+added line\n" {
t.Errorf("diff content = %v", diffs[0].Files)
}
}