Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/pkg/serverapi/publicapi_test.go
2500 views
1
// Copyright (c) 2024 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package serverapi
6
7
import (
8
"encoding/json"
9
"fmt"
10
"strings"
11
"testing"
12
13
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
14
"github.com/google/go-cmp/cmp"
15
"github.com/google/go-cmp/cmp/cmpopts"
16
)
17
18
func generateArray(prefix string, number int, length int, withDot bool) []string {
19
var arr []string
20
for i := 0; i < number; i++ {
21
lengthyPart := strings.Repeat("a", length)
22
arr = append(arr, fmt.Sprintf("%s_%d_%s", prefix, i, lengthyPart))
23
}
24
if withDot {
25
arr = append(arr, "...")
26
}
27
return arr
28
}
29
30
func TestCapGitStatusLength(t *testing.T) {
31
32
tests := []struct {
33
name string
34
input *v1.GitStatus
35
expected *v1.GitStatus
36
}{
37
{
38
name: "Short GitStatus",
39
input: &v1.GitStatus{
40
Branch: "main",
41
LatestCommit: "abc123",
42
TotalUncommitedFiles: 2,
43
TotalUnpushedCommits: 3,
44
TotalUntrackedFiles: 4,
45
UncommitedFiles: []string{"file1.txt", "file2.txt"},
46
UnpushedCommits: []string{"commit1", "commit2", "commit3"},
47
UntrackedFiles: []string{"file3.txt", "file4.txt", "file5.txt", "file6.txt"},
48
},
49
expected: &v1.GitStatus{
50
Branch: "main",
51
LatestCommit: "abc123",
52
TotalUncommitedFiles: 2,
53
TotalUnpushedCommits: 3,
54
TotalUntrackedFiles: 4,
55
UncommitedFiles: []string{"file1.txt", "file2.txt"},
56
UnpushedCommits: []string{"commit1", "commit2", "commit3"},
57
UntrackedFiles: []string{"file3.txt", "file4.txt", "file5.txt", "file6.txt"},
58
},
59
},
60
{
61
name: "Long GitStatus",
62
input: &v1.GitStatus{
63
Branch: "main",
64
LatestCommit: "abc123",
65
TotalUncommitedFiles: 2,
66
TotalUnpushedCommits: 3,
67
TotalUntrackedFiles: 4,
68
UncommitedFiles: []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt", "file6.txt"},
69
UnpushedCommits: []string{"commit1", "commit2", "commit3", "commit4", "commit5", "commit6", "commit7"},
70
UntrackedFiles: generateArray("file", 800, 10, false),
71
},
72
expected: &v1.GitStatus{
73
Branch: "main",
74
LatestCommit: "abc123",
75
TotalUncommitedFiles: 2,
76
TotalUnpushedCommits: 3,
77
TotalUntrackedFiles: 4,
78
UncommitedFiles: []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt", "file6.txt"},
79
UnpushedCommits: []string{"commit1", "commit2", "commit3", "commit4", "commit5", "commit6", "commit7"},
80
UntrackedFiles: generateArray("file", 166, 10, true),
81
},
82
},
83
{
84
name: "Long paths in GitStatus",
85
input: &v1.GitStatus{
86
Branch: "main",
87
LatestCommit: "abc123",
88
TotalUncommitedFiles: 2,
89
TotalUnpushedCommits: 3,
90
TotalUntrackedFiles: 4,
91
UncommitedFiles: []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt", "file6.txt"},
92
UnpushedCommits: []string{"commit1", "commit2", "commit3", "commit4", "commit5", "commit6", "commit7"},
93
UntrackedFiles: generateArray("file", 50, 200, false),
94
},
95
expected: &v1.GitStatus{
96
Branch: "main",
97
LatestCommit: "abc123",
98
TotalUncommitedFiles: 2,
99
TotalUnpushedCommits: 3,
100
TotalUntrackedFiles: 4,
101
UncommitedFiles: []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt", "file6.txt"},
102
UnpushedCommits: []string{"commit1", "commit2", "commit3", "commit4", "commit5", "commit6", "commit7"},
103
UntrackedFiles: generateArray("file", 17, 200, true),
104
},
105
},
106
{
107
name: "Empty GitStatus",
108
input: &v1.GitStatus{
109
Branch: "",
110
LatestCommit: "",
111
TotalUncommitedFiles: 0,
112
TotalUnpushedCommits: 0,
113
TotalUntrackedFiles: 0,
114
UncommitedFiles: nil,
115
UnpushedCommits: nil,
116
UntrackedFiles: nil,
117
},
118
expected: &v1.GitStatus{
119
Branch: "",
120
LatestCommit: "",
121
TotalUncommitedFiles: 0,
122
TotalUnpushedCommits: 0,
123
TotalUntrackedFiles: 0,
124
UncommitedFiles: nil,
125
UnpushedCommits: nil,
126
UntrackedFiles: nil,
127
},
128
},
129
}
130
131
for _, tt := range tests {
132
t.Run(tt.name, func(t *testing.T) {
133
got := capGitStatusLength(tt.input)
134
if diff := cmp.Diff(tt.expected, got, cmpopts.IgnoreUnexported(v1.GitStatus{})); diff != "" {
135
t.Errorf("CapGitStatusLength (-want +got):\n%s", diff)
136
}
137
138
bytes, err := json.Marshal(tt.input)
139
if err != nil {
140
t.Error(err)
141
}
142
if len(bytes) > GIT_STATUS_API_LIMIT_BYTES {
143
t.Errorf("JSON size exceeds GIT_STATUS_API_LIMIT_BYTES: %d (%d)", len(bytes), GIT_STATUS_API_LIMIT_BYTES)
144
}
145
})
146
}
147
}
148
149