mirror of
https://github.com/go-task/task.git
synced 2026-08-29 10:08:27 +02:00
fix: preserve stdin taskfile bytes (#2950)
Signed-off-by: cuishuang <imcusg@gmail.com>
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
65
taskfile/node_stdin_test.go
Normal file
65
taskfile/node_stdin_test.go
Normal 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())
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user