diff --git a/proxy.go b/proxy.go index 5c9a598..a42f6e3 100644 --- a/proxy.go +++ b/proxy.go @@ -247,22 +247,31 @@ func (p *Proxy) sendCommand(b []byte, waitPrompt ...bool) ([]byte, error) { } waitLoop: + for { + // wait until received prompt + bytesOut := <-p.stdout + debug(string(bytesOut)) + parts := bytes.Split(bytesOut, []byte("\n")) + for _, part := range parts { + if isPrompt(part) { + break waitLoop + } + } + + output = append(output, bytesOut...) + } + + // drain error channel +errLoop: for { select { case bytesErr := <-p.stderr: - debug(string(bytesErr)) - err = fmt.Errorf("%s", string(bytesErr)) - break waitLoop - case bytesOut := <-p.stdout: - debug(string(bytesOut)) - parts := bytes.Split(bytesOut, []byte("\n")) - for _, part := range parts { - if isPrompt(part) { - break waitLoop - } + if len(bytesErr) > 0 { + debug(string(bytesErr)) + err = fmt.Errorf("%s", string(bytesErr)) } - - output = append(output, bytesOut...) + default: + break errLoop } } diff --git a/proxy_test.go b/proxy_test.go index 325d879..3d171f5 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -2,19 +2,25 @@ package inkscape import ( "fmt" + "os" "testing" ) func TestConcurrent(t *testing.T) { tempFiles := make([]string, 0) - // defer func() { - // for _, t := range tempFiles { - // os.Remove(t) - // } - // }() + defer func() { + for _, t := range tempFiles { + os.Remove(t) + } + }() proxy := NewProxy(Verbose(true)) - proxy.Run() + err := proxy.Run() + if err != nil { + t.Error(err) + t.FailNow() + } + defer proxy.Close() for i := 0; i < 10; i++ { temp := fmt.Sprintf("%d.pdf", i) @@ -27,3 +33,21 @@ func TestConcurrent(t *testing.T) { }() } } + +func TestOpenFail(t *testing.T) { + proxy := NewProxy(Verbose(true)) + err := proxy.Run() + if err != nil { + t.Error(err) + t.FailNow() + } + defer proxy.Close() + + res, err := proxy.RawCommands("file-open:notexists.svg") + if err == nil { + t.Error("should fail", string(res)) + t.FailNow() + } + + t.Log(string(res)) +}