jesseduffield.lazygit/pkg/gui/command_log_panel_test.go
tmwatchanan 224f8e7e50
Allow copying streamed git output from the command log
Hook and push output is easy to read in lazygit but hard to reuse
elsewhere. Parse git output blocks from the command log view so copies
match what is shown, include the action and command above each block,
and avoid logging clipboard operations back into the stream.
2026-06-20 21:37:58 +07:00

90 lines
1.9 KiB
Go

package gui
import (
"testing"
"github.com/stretchr/testify/assert"
)
const gitOutputHeader = "Git output:"
func TestGitOutputBlocksFromCommandLogLines(t *testing.T) {
t.Parallel()
lines := []string{
"Push",
" git push",
"",
gitOutputHeader,
"line1",
"line2",
}
assert.Equal(t, []string{"Push\n git push\n\nGit output:\nline1\nline2"}, gitOutputBlocksFromCommandLogLines(lines, gitOutputHeader))
}
func TestGitOutputBlocksSkipCopyNotifications(t *testing.T) {
t.Parallel()
lines := []string{
"Push",
" git push",
gitOutputHeader,
"hook line",
" Copying 'hook line' to clipboard",
"hook line 2",
}
assert.Equal(t, []string{"Push\n git push\n\nGit output:\nhook line\nhook line 2"}, gitOutputBlocksFromCommandLogLines(lines, gitOutputHeader))
}
func TestGitOutputBlocksEndAtNextCommandLogEntry(t *testing.T) {
t.Parallel()
lines := []string{
"Push",
" git push",
gitOutputHeader,
"first command output",
"Stage file",
" git add foo",
"",
gitOutputHeader,
"second command output",
}
assert.Equal(t, []string{
"Push\n git push\n\nGit output:\nfirst command output",
"Stage file\n git add foo\n\nGit output:\nsecond command output",
}, gitOutputBlocksFromCommandLogLines(lines, gitOutputHeader))
}
func TestGitOutputBlocksMultipleBlocksJoined(t *testing.T) {
t.Parallel()
lines := []string{
"Push",
" git push",
gitOutputHeader,
"first command",
"Pull",
" git pull",
gitOutputHeader,
"second command",
}
blocks := gitOutputBlocksFromCommandLogLines(lines, gitOutputHeader)
assert.Equal(t, "Push\n git push\n\nGit output:\nfirst command\n\nPull\n git pull\n\nGit output:\nsecond command", joinGitOutputBlocks(blocks))
}
func joinGitOutputBlocks(blocks []string) string {
result := ""
for i, block := range blocks {
if i > 0 {
result += "\n\n"
}
result += block
}
return result
}