Path: blob/main/components/gitpod-cli/cmd/git-commit-message-helper_test.go
2498 views
// Copyright (c) 2025 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"os"8"testing"910"github.com/google/go-cmp/cmp"11)1213func TestAddGitpodTrailer(t *testing.T) {14tests := []struct {15Name string16CommitMsg string17HostName string18Expected string19ExpectError bool20}{21{22Name: "adds trailer to simple message",23CommitMsg: "Initial commit",24HostName: "gitpod.io",25Expected: "Initial commit\n\nTool: gitpod/gitpod.io\n",26ExpectError: false,27},28{29Name: "doesn't duplicate existing trailer",30CommitMsg: "Initial commit\n\nTool: gitpod/gitpod.io\n",31HostName: "gitpod.io",32Expected: "Initial commit\n\nTool: gitpod/gitpod.io\n",33ExpectError: false,34},35{36Name: "preserves other trailers",37CommitMsg: "Initial commit\n\nSigned-off-by: Kyle <[email protected]>\n",38HostName: "gitpod.io",39Expected: "Initial commit\n\nSigned-off-by: Kyle <[email protected]>\nTool: gitpod/gitpod.io\n",40ExpectError: false,41},42}4344for _, tt := range tests {45t.Run(tt.Name, func(t *testing.T) {46tmpfile, err := os.CreateTemp("", "commit-msg-*")47if err != nil {48t.Fatal(err)49}50defer os.Remove(tmpfile.Name())5152if err := os.WriteFile(tmpfile.Name(), []byte(tt.CommitMsg), 0644); err != nil {53t.Fatal(err)54}5556err = addGitpodTrailer(tmpfile.Name(), tt.HostName)57if (err != nil) != tt.ExpectError {58t.Errorf("addGitpodTrailer() error = %v, wantErr %v", err, tt.ExpectError)59return60}6162got, err := os.ReadFile(tmpfile.Name())63if err != nil {64t.Fatal(err)65}6667equal := cmp.Equal(string(got), tt.Expected)68if !equal {69t.Fatalf(`Detected git command info was incorrect, got: %v, expected: %v.`, string(got), tt.Expected)70}71})72}73}747576