Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/git-commit-message-helper_test.go
2498 views
1
// Copyright (c) 2025 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 cmd
6
7
import (
8
"os"
9
"testing"
10
11
"github.com/google/go-cmp/cmp"
12
)
13
14
func TestAddGitpodTrailer(t *testing.T) {
15
tests := []struct {
16
Name string
17
CommitMsg string
18
HostName string
19
Expected string
20
ExpectError bool
21
}{
22
{
23
Name: "adds trailer to simple message",
24
CommitMsg: "Initial commit",
25
HostName: "gitpod.io",
26
Expected: "Initial commit\n\nTool: gitpod/gitpod.io\n",
27
ExpectError: false,
28
},
29
{
30
Name: "doesn't duplicate existing trailer",
31
CommitMsg: "Initial commit\n\nTool: gitpod/gitpod.io\n",
32
HostName: "gitpod.io",
33
Expected: "Initial commit\n\nTool: gitpod/gitpod.io\n",
34
ExpectError: false,
35
},
36
{
37
Name: "preserves other trailers",
38
CommitMsg: "Initial commit\n\nSigned-off-by: Kyle <[email protected]>\n",
39
HostName: "gitpod.io",
40
Expected: "Initial commit\n\nSigned-off-by: Kyle <[email protected]>\nTool: gitpod/gitpod.io\n",
41
ExpectError: false,
42
},
43
}
44
45
for _, tt := range tests {
46
t.Run(tt.Name, func(t *testing.T) {
47
tmpfile, err := os.CreateTemp("", "commit-msg-*")
48
if err != nil {
49
t.Fatal(err)
50
}
51
defer os.Remove(tmpfile.Name())
52
53
if err := os.WriteFile(tmpfile.Name(), []byte(tt.CommitMsg), 0644); err != nil {
54
t.Fatal(err)
55
}
56
57
err = addGitpodTrailer(tmpfile.Name(), tt.HostName)
58
if (err != nil) != tt.ExpectError {
59
t.Errorf("addGitpodTrailer() error = %v, wantErr %v", err, tt.ExpectError)
60
return
61
}
62
63
got, err := os.ReadFile(tmpfile.Name())
64
if err != nil {
65
t.Fatal(err)
66
}
67
68
equal := cmp.Equal(string(got), tt.Expected)
69
if !equal {
70
t.Fatalf(`Detected git command info was incorrect, got: %v, expected: %v.`, string(got), tt.Expected)
71
}
72
})
73
}
74
}
75
76