fix: preserve stdin taskfile bytes (#2950)

Signed-off-by: cuishuang <imcusg@gmail.com>
This commit is contained in:
cui fliter
2026-08-10 02:55:17 +08:00
committed by GitHub
parent f174912975
commit d2b02e3c69
2 changed files with 67 additions and 11 deletions

View File

@@ -1,8 +1,7 @@
package taskfile package taskfile
import ( import (
"bufio" "io"
"fmt"
"os" "os"
"github.com/go-task/task/v3/internal/execext" "github.com/go-task/task/v3/internal/execext"
@@ -29,15 +28,7 @@ func (node *StdinNode) Remote() bool {
} }
func (node *StdinNode) Read() ([]byte, error) { func (node *StdinNode) Read() ([]byte, error) {
var stdin []byte return io.ReadAll(os.Stdin)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
stdin = fmt.Appendln(stdin, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return stdin, nil
} }
func (node *StdinNode) ResolveEntrypoint(entrypoint string) (string, error) { func (node *StdinNode) ResolveEntrypoint(entrypoint string) (string, error) {

View File

@@ -0,0 +1,65 @@
package taskfile
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStdinNodeReadPreservesInput(t *testing.T) { //nolint:paralleltest // replaces process-wide stdin
node, err := NewStdinNode("")
require.NoError(t, err)
tests := []struct {
name string
input []byte
}{
{
name: "line longer than 64 KiB",
input: []byte(strings.Repeat("a", 64*1024+1)),
},
{
name: "no trailing newline",
input: []byte("version: '3'"),
},
{
name: "CRLF line endings",
input: []byte("version: '3'\r\ntasks:\r\n"),
},
{
name: "empty input",
input: []byte{},
},
}
for _, tt := range tests { //nolint:paralleltest // subtests replace process-wide stdin
t.Run(tt.name, func(t *testing.T) {
replaceStdin(t, tt.input)
got, err := node.Read()
require.NoError(t, err)
assert.Equal(t, tt.input, got)
})
}
}
func replaceStdin(t *testing.T, input []byte) {
t.Helper()
path := filepath.Join(t.TempDir(), "stdin")
require.NoError(t, os.WriteFile(path, input, 0o600))
stdin, err := os.Open(path)
require.NoError(t, err)
original := os.Stdin
os.Stdin = stdin
t.Cleanup(func() {
os.Stdin = original
require.NoError(t, stdin.Close())
})
}