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
|
package save
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cli/commands/cmdtest"
"gitlab.com/gitlab-org/cli/internal/run"
"gitlab.com/gitlab-org/cli/pkg/git"
)
func Test_stackAmendCmd(t *testing.T) {
tests := []struct {
desc string
args []string
files []string
amendedFiles []string
description string
expected string
wantErr bool
editorMessage string
}{
{
desc: "amending regular files",
args: []string{"testfile", "randomfile"},
files: []string{"testfile", "randomfile"},
amendedFiles: []string{"otherfile"},
description: "this is a commit message",
expected: "Amended stack item with description: \"this is a commit message\".\n",
},
{
desc: "with no message",
args: []string{"testfile", "randomfile"},
files: []string{"testfile", "randomfile"},
amendedFiles: []string{"otherfile"},
description: "",
editorMessage: "amended description",
expected: "Amended stack item with description: \"amended description\".\n",
},
{
desc: "with no amended changes",
args: []string{"."},
files: []string{"oldfile"},
amendedFiles: []string{},
description: "this is a commit message",
expected: "no changes to save",
wantErr: true,
},
{
desc: "not on a stack branch",
args: []string{"asdf"},
files: []string{"asdf"},
amendedFiles: []string{"otherfile"},
description: "this is a commit message",
expected: "Could not find stack ref for branch",
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
ios, _, _, _ := cmdtest.InitIOStreams(true, "")
f := cmdtest.InitFactory(ios, nil)
dir := git.InitGitRepoWithCommit(t)
err := git.SetLocalConfig("glab.currentstack", "cool-test-feature")
require.Nil(t, err)
createTemporaryFiles(t, dir, tc.files)
var saveArgs []string
saveArgs = append(saveArgs, "-m")
saveArgs = append(saveArgs, "\"original save message\"")
saveArgs = append(saveArgs, tc.args...)
getText := getMockEditor(tc.editorMessage, &[]string{})
_, err = runSaveCommand(nil, getText, true, strings.Join(saveArgs, " "))
require.Nil(t, err)
createTemporaryFiles(t, dir, tc.amendedFiles)
if tc.desc == "not on a stack branch" {
checkout := git.GitCommand("checkout", "-b", "randobranch")
_, err := run.PrepareCmd(checkout).Output()
require.Nil(t, err)
}
output, err := amendFunc(f, tc.args, getText, tc.description)
if tc.wantErr {
require.ErrorContains(t, err, tc.expected)
} else {
require.Nil(t, err)
require.Equal(t, tc.expected, output)
}
})
}
}
|