mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
pr diff copied the server's patch bytes straight to stdout, so a pull request could change the terminal title, clear the screen or move the cursor of whoever viewed its diff. Add terminal.SafeWriter, which filters streamed server text like terminal.SafeText when stdout is a terminal, and use it for pr diff. Piped output stays byte-exact, so `tea pr diff N | git apply` keeps working. Signed-off-by: bircni <bircni@icloud.com>
28 lines
913 B
Go
28 lines
913 B
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package app
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPullDiffStripsTerminalControlsOnlyOnATerminal(t *testing.T) {
|
|
const diff = "+hello \x1b]0;PWNED\x07 \x1b[31mred\x1b[0m\r\n-old\tline\n"
|
|
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
require.Equal(t, "/api/v1/repos/alice/project/pulls/2.diff", r.URL.Path)
|
|
_, _ = w.Write([]byte(diff))
|
|
}))
|
|
defer server.Close()
|
|
for tty, want := range map[bool]string{false: diff, true: "+hello ]0;PWNED [31mred[0m\n-old\tline\n"} {
|
|
deps, out, _ := serverDeps(t, server.URL)
|
|
deps.Streams.OutTTY = tty
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "diff", "2", "-R", "alice/project"}))
|
|
require.Equal(t, want, out.String(), "tty=%t", tty)
|
|
}
|
|
}
|