Skip to content

Allow custom commands to write to stdout and stderr #139

@matthewmueller

Description

@matthewmueller

Hey there, I've had a couple cases where it'd be nice to be able to write to stdout and stderr from inside a custom command.

For example:

func Test(t *testing.T) {
	p := testscript.Params{
		Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
			"render": render,
		},
	}
	testscript.Run(t, p)
}

func render(ts *testscript.TestScript, neg bool, args []string) {
    html, err := render.Render(ts.MkAbs(args))
    ts.Check(err)
    _ = html
}
render 'index.gohtml'

-- index.gohtml --
<html>
  <body>
    <h1>{{.}}</h1>
  </body>
</html>

Unfortunately I haven't found a way to use the command's results within the test. The workaround I use now is to write a file and then read from it.

What I'd like to be able to do is write a testscript like this:

render 'index.gohtml'
stdout '<h1>'

-- index.gohtml --
<html>
  <body>
    <h1>{{.}}</h1>
  </body>
</html>

And the failure case:

! render 'index.gohtml'
stderr 'bad HTML'

-- index.gohtml --
<html

Thanks for extracting and improving this package!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions