package testtrace
import (
"bytes"
"fmt"
"internal/trace/raw"
"internal/txtar"
"io"
)
func ParseFile (testPath string ) (io .Reader , *Expectation , error ) {
ar , err := txtar .ParseFile (testPath )
if err != nil {
return nil , nil , fmt .Errorf ("failed to read test file for %s: %v" , testPath , err )
}
if len (ar .Files ) != 2 {
return nil , nil , fmt .Errorf ("malformed test %s: wrong number of files" , testPath )
}
if ar .Files [0 ].Name != "expect" {
return nil , nil , fmt .Errorf ("malformed test %s: bad filename %s" , testPath , ar .Files [0 ].Name )
}
if ar .Files [1 ].Name != "trace" {
return nil , nil , fmt .Errorf ("malformed test %s: bad filename %s" , testPath , ar .Files [1 ].Name )
}
tr , err := raw .NewTextReader (bytes .NewReader (ar .Files [1 ].Data ))
if err != nil {
return nil , nil , fmt .Errorf ("malformed test %s: bad trace file: %v" , testPath , err )
}
var buf bytes .Buffer
tw , err := raw .NewWriter (&buf , tr .Version ())
if err != nil {
return nil , nil , fmt .Errorf ("failed to create trace byte writer: %v" , err )
}
for {
ev , err := tr .ReadEvent ()
if err == io .EOF {
break
}
if err != nil {
return nil , nil , fmt .Errorf ("malformed test %s: bad trace file: %v" , testPath , err )
}
if err := tw .WriteEvent (ev ); err != nil {
return nil , nil , fmt .Errorf ("internal error during %s: failed to write trace bytes: %v" , testPath , err )
}
}
exp , err := ParseExpectation (ar .Files [0 ].Data )
if err != nil {
return nil , nil , fmt .Errorf ("internal error during %s: failed to parse expectation %q: %v" , testPath , string (ar .Files [0 ].Data ), err )
}
return &buf , exp , nil
}
The pages are generated with Golds v0.7.0-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .