Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for script.Exec() to be used like exec.Command() #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func Echo(s string) *Pipe {
//
// Use [Pipe.Exec] to send the contents of an existing pipe to the command's
// standard input.
func Exec(cmdLine string) *Pipe {
return NewPipe().Exec(cmdLine)
func Exec(cmdLine string, args ...string) *Pipe {
return NewPipe().Exec(cmdLine, args...)
}

// File creates a pipe that reads from the file path.
Expand Down Expand Up @@ -375,9 +375,10 @@ func (p *Pipe) Error() error {
// If the command writes to its standard error stream, this will also go to the
// pipe, along with its standard output. However, the standard error text can
// instead be redirected to a supplied writer, using [Pipe.WithStderr].
func (p *Pipe) Exec(cmdLine string) *Pipe {
func (p *Pipe) Exec(cmdLine string, args ...string) *Pipe {
return p.Filter(func(r io.Reader, w io.Writer) error {
args, err := shell.Fields(cmdLine, nil)
parsedArgs, err := shell.Fields(cmdLine, nil)
args = append(parsedArgs, args...)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,21 @@ func TestExecRunsGoHelpAndGetsUsageMessage(t *testing.T) {
}
}

func TestExecRunsGoHelpVariantAndGetsUsageMessage(t *testing.T) {
t.Parallel()
p := script.Exec("go", "help")
if p.Error() != nil {
t.Fatal(p.Error())
}
output, err := p.String()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "Usage") {
t.Fatalf("want output containing the word 'Usage', got %q", output)
}
}

func TestFileOutputsContentsOfSpecifiedFile(t *testing.T) {
t.Parallel()
want := "This is the first line in the file.\nHello, world.\nThis is another line in the file.\n"
Expand Down