From da45ee783b8afaff461638549de67b39bed180b8 Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 10 Apr 2023 08:07:08 +0100 Subject: [PATCH] Correct error comparison for sym data load in fs --- go/dev/interactive.go | 2 +- go/engine/loop.go | 6 +++++- go/engine/loop_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++ go/resource/fs.go | 6 ++++-- go/state/state.go | 1 - 5 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 go/engine/loop_test.go diff --git a/go/dev/interactive.go b/go/dev/interactive.go index e771da2..acee162 100644 --- a/go/dev/interactive.go +++ b/go/dev/interactive.go @@ -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) diff --git a/go/engine/loop.go b/go/engine/loop.go index d151c72..8d6bad8 100644 --- a/go/engine/loop.go +++ b/go/engine/loop.go @@ -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}) } diff --git a/go/engine/loop_test.go b/go/engine/loop_test.go new file mode 100644 index 0000000..4179956 --- /dev/null +++ b/go/engine/loop_test.go @@ -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) + } +} diff --git a/go/resource/fs.go b/go/resource/fs.go index 585ca3b..66514b1 100644 --- a/go/resource/fs.go +++ b/go/resource/fs.go @@ -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 } diff --git a/go/state/state.go b/go/state/state.go index cc2c759..5f21436 100644 --- a/go/state/state.go +++ b/go/state/state.go @@ -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 }