Correct error comparison for sym data load in fs
This commit is contained in:
parent
89deda1268
commit
da45ee783b
@ -19,7 +19,7 @@ func main() {
|
||||
|
||||
ctx := context.Background()
|
||||
en := engine.NewDefaultEngine(dir)
|
||||
err := engine.Loop(root, &en, ctx)
|
||||
err := engine.Loop(&en, root, ctx, os.Stdin, os.Stdout)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "loop exited with error: %v", err)
|
||||
os.Exit(1)
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -23,6 +24,10 @@ func Loop(en *Engine, startSym string, ctx context.Context, reader io.Reader, wr
|
||||
bufReader := bufio.NewReader(reader)
|
||||
for running {
|
||||
in, err := bufReader.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
log.Printf("EOF found, that's all folks")
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot read input: %v\n", err)
|
||||
}
|
||||
@ -33,7 +38,6 @@ func Loop(en *Engine, startSym string, ctx context.Context, reader io.Reader, wr
|
||||
}
|
||||
b := bytes.NewBuffer(nil)
|
||||
en.WriteResult(b)
|
||||
//fmt.Println(b.String())
|
||||
writer.Write(b.Bytes())
|
||||
writer.Write([]byte{0x0a})
|
||||
}
|
||||
|
44
go/engine/loop_test.go
Normal file
44
go/engine/loop_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.defalsify.org/festive/cache"
|
||||
"git.defalsify.org/festive/resource"
|
||||
"git.defalsify.org/festive/state"
|
||||
)
|
||||
|
||||
func TestLoopCorrect(t *testing.T) {
|
||||
generateTestData(t)
|
||||
ctx := context.TODO()
|
||||
st := state.NewState(0)
|
||||
//rs := NewFsWrapper(dataDir, &st)
|
||||
rs := resource.NewFsResource(dataDir)
|
||||
ca := cache.NewCache().WithCacheSize(1024)
|
||||
|
||||
en := NewEngine(&st, &rs, ca)
|
||||
err := en.Init("root", ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
input := []string{
|
||||
"1",
|
||||
"0",
|
||||
"1",
|
||||
"0",
|
||||
}
|
||||
inputStr := strings.Join(input, "\n")
|
||||
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
|
||||
outputBuf := bytes.NewBuffer(nil)
|
||||
log.Printf("running with input: %s", inputBuf.Bytes())
|
||||
|
||||
err = Loop(&en, "root", ctx, inputBuf, outputBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -44,7 +45,7 @@ func(fs FsResource) FuncFor(sym string) (EntryFunc, error) {
|
||||
return fn, nil
|
||||
}
|
||||
_, err := fs.getFuncNoCtx(sym)
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unknown sym: %s", sym)
|
||||
}
|
||||
return fs.getFunc, nil
|
||||
@ -61,10 +62,11 @@ func(fs FsResource) getFunc(sym string, ctx context.Context) (string, error) {
|
||||
func(fs FsResource) getFuncNoCtx(sym string) (string, error) {
|
||||
fb := sym + ".txt"
|
||||
fp := path.Join(fs.Path, fb)
|
||||
log.Printf("getfunc search dir %s %s for %s", fs.Path, fp, sym)
|
||||
r, err := ioutil.ReadFile(fp)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed getting data for sym '%s': %v", sym, err)
|
||||
}
|
||||
s := string(r)
|
||||
return strings.TrimSpace(s), err
|
||||
return strings.TrimSpace(s), nil
|
||||
}
|
||||
|
@ -266,7 +266,6 @@ func(st *State) Up() (string, error) {
|
||||
if len(st.execPath) > 0 {
|
||||
sym = st.execPath[len(st.execPath)-1]
|
||||
}
|
||||
log.Printf("execpath %v", st.execPath)
|
||||
st.sizeIdx = 0
|
||||
return sym, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user