package progtest
import (
)
type Fixture struct {
pipes [3]*pipe
dirCleanup func()
}
func ( *pipe) {
, := ioutil.ReadAll(.r)
if != nil {
panic()
}
.output <-
}
func () *Fixture {
, := testutil.InTestDir()
:= [3]*pipe{makePipe(false), makePipe(true), makePipe(true)}
return &Fixture{, }
}
func ( *Fixture) () {
.pipes[0].close()
.pipes[1].close()
.pipes[2].close()
.dirCleanup()
}
func ( *Fixture) () [3]*os.File {
return [3]*os.File{.pipes[0].r, .pipes[1].w, .pipes[2].w}
}
func ( *Fixture) ( string) {
, := .pipes[0].w.WriteString()
if != nil {
panic()
}
.pipes[0].w.Close()
.pipes[0].wClosed = true
}
func ( *Fixture) ( *testing.T, int, string) {
.Helper()
if := .pipes[].get(); != {
.Errorf("got out %q, want %q", , )
}
}
func ( *Fixture) ( *testing.T, int, string) {
.Helper()
if := .pipes[].get(); !strings.Contains(, ) {
.Errorf("got out %q, want string containing %q", , )
}
}
type pipe struct {
r, w *os.File
rClosed, wClosed bool
saved string
output chan []byte
}
func ( bool) *pipe {
, , := os.Pipe()
if != nil {
panic()
}
if ! {
return &pipe{r: , w: }
}
:= make(chan []byte, 1)
:= pipe{r: , w: , output: }
go captureOutput(&)
return &
}
func ( *pipe) () string {
if !.wClosed {
.w.Close()
.wClosed = true
if .output != nil {
.saved = string(<-.output)
}
}
return .saved
}
func ( *pipe) () {
if !.wClosed {
.w.Close()
.wClosed = true
if .output != nil {
.saved = string(<-.output)
}
}
if !.rClosed {
.r.Close()
.rClosed = true
}
if .output != nil {
close(.output)
.output = nil
}
}
func (, string) {
:= ioutil.WriteFile(, []byte(), 0600)
if != nil {
panic()
}
}
func ( ...string) []string {
return append([]string{"elvish"}, ...)
}
func ( *testing.T, *Fixture, int, string) {
.Helper()
if != 2 {
.Errorf("got exit %v, want 2", )
}
.TestOutSnippet(, 2, )
}