From 71d7c1466e23057e3af569b1ded8fadcf0a96981 Mon Sep 17 00:00:00 2001 From: lash Date: Sun, 2 Apr 2023 10:11:09 +0100 Subject: [PATCH] Remove duplicate rendertemplate code in vm test --- go/dev/interactive.go | 49 ++++++++++++++++++++++ go/dev/{ => testdata}/generate_testdata.go | 0 go/engine/default.go | 12 ++++++ go/engine/engine.go | 3 ++ go/engine/engine_test.go | 23 ++-------- go/resource/fs.go | 22 ++++++---- go/resource/fs_test.go | 3 +- go/resource/render.go | 24 +++++++++++ go/vm/vm_test.go | 18 +------- 9 files changed, 109 insertions(+), 45 deletions(-) create mode 100644 go/dev/interactive.go rename go/dev/{ => testdata}/generate_testdata.go (100%) create mode 100644 go/engine/default.go create mode 100644 go/resource/render.go diff --git a/go/dev/interactive.go b/go/dev/interactive.go new file mode 100644 index 0000000..c3d50f1 --- /dev/null +++ b/go/dev/interactive.go @@ -0,0 +1,49 @@ +package main + +import ( + "bufio" + "bytes" + "context" + "flag" + "fmt" + "strings" + "os" + + "git.defalsify.org/festive/engine" +) + +func main() { + var dir string + var root string + flag.StringVar(&dir, "d", ".", "resource dir to read from") + flag.StringVar(&root, "root", "root", "entry point symbol") + flag.Parse() + fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) + + ctx := context.Background() + en := engine.NewDefaultEngine(dir) + err := en.Init("root", ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "cannot init: %v\n", err) + os.Exit(1) + } + + running := true + for running { + reader := bufio.NewReader(os.Stdin) + in, err := reader.ReadString('\n') + if err != nil { + fmt.Fprintf(os.Stderr, "cannot read input: %v", err) + os.Exit(1) + } + in = strings.TrimSpace(in) + err = en.Exec([]byte(in), ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "execution terminated: %v", err) + os.Exit(1) + } + b := bytes.NewBuffer(nil) + en.WriteResult(b) + fmt.Println(b.String()) + } +} diff --git a/go/dev/generate_testdata.go b/go/dev/testdata/generate_testdata.go similarity index 100% rename from go/dev/generate_testdata.go rename to go/dev/testdata/generate_testdata.go diff --git a/go/engine/default.go b/go/engine/default.go new file mode 100644 index 0000000..a2753cc --- /dev/null +++ b/go/engine/default.go @@ -0,0 +1,12 @@ +package engine + +import ( + "git.defalsify.org/festive/state" + "git.defalsify.org/festive/resource" +) + +func NewDefaultEngine(dir string) Engine { + st := state.NewState(0) + rs := resource.NewFsResource(dir) + return NewEngine(&st, &rs) +} diff --git a/go/engine/engine.go b/go/engine/engine.go index 10a6bec..5d174d6 100644 --- a/go/engine/engine.go +++ b/go/engine/engine.go @@ -43,6 +43,9 @@ func(en *Engine) Init(sym string, ctx context.Context) error { if err != nil { return err } + if len(code) == 0 { + return fmt.Errorf("no code found at resource %s", en.rs) + } return en.st.AppendCode(code) } diff --git a/go/engine/engine_test.go b/go/engine/engine_test.go index c83fb9d..695041a 100644 --- a/go/engine/engine_test.go +++ b/go/engine/engine_test.go @@ -6,7 +6,6 @@ import ( "fmt" "io/ioutil" "path" - "text/template" "testing" "git.defalsify.org/festive/resource" @@ -24,8 +23,8 @@ type FsWrapper struct { st *state.State } -func NewFsWrapper(path string, st *state.State, ctx context.Context) FsWrapper { - rs := resource.NewFsResource(path, ctx) +func NewFsWrapper(path string, st *state.State) FsWrapper { + rs := resource.NewFsResource(path) return FsWrapper { &rs, st, @@ -33,21 +32,7 @@ func NewFsWrapper(path string, st *state.State, ctx context.Context) FsWrapper { } func (r FsWrapper) RenderTemplate(sym string, values map[string]string) (string, error) { - v, err := r.GetTemplate(sym) - if err != nil { - return "", err - } - tp, err := template.New("tester").Option("missingkey=error").Parse(v) - if err != nil { - return "", err - } - - b := bytes.NewBuffer([]byte{}) - err = tp.Execute(b, values) - if err != nil { - return "", err - } - return b.String(), err + return resource.DefaultRenderTemplate(r, sym, values) } func(fs FsWrapper) one(ctx context.Context) (string, error) { @@ -84,7 +69,7 @@ func TestEngineInit(t *testing.T) { st := state.NewState(17).WithCacheSize(1024) generateTestData(t) ctx := context.TODO() - rs := NewFsWrapper(dataDir, &st, ctx) + rs := NewFsWrapper(dataDir, &st) en := NewEngine(&st, &rs) err := en.Init("root", ctx) if err != nil { diff --git a/go/resource/fs.go b/go/resource/fs.go index 1c71a8f..3888683 100644 --- a/go/resource/fs.go +++ b/go/resource/fs.go @@ -1,22 +1,24 @@ package resource import ( - "context" "fmt" "io/ioutil" "path" + "path/filepath" "strings" ) type FsResource struct { Path string - ctx context.Context } -func NewFsResource(path string, ctx context.Context) (FsResource) { +func NewFsResource(path string) (FsResource) { + absPath, err := filepath.Abs(path) + if err != nil { + panic(err) + } return FsResource{ - Path: path, - ctx: ctx, + Path: absPath, } } @@ -28,13 +30,19 @@ func(fs FsResource) GetTemplate(sym string) (string, error) { } func(fs FsResource) RenderTemplate(sym string, values map[string]string) (string, error) { - return "", nil + return DefaultRenderTemplate(&fs, sym, values) } func(fs FsResource) GetCode(sym string) ([]byte, error) { - return []byte{}, nil + fb := sym + ".bin" + fp := path.Join(fs.Path, fb) + return ioutil.ReadFile(fp) } func(fs FsResource) FuncFor(sym string) (EntryFunc, error) { return nil, fmt.Errorf("not implemented") } + +func(rs FsResource) String() string { + return fmt.Sprintf("fs resource at path: %s", rs.Path) +} diff --git a/go/resource/fs_test.go b/go/resource/fs_test.go index fd9573f..29c5706 100644 --- a/go/resource/fs_test.go +++ b/go/resource/fs_test.go @@ -1,11 +1,10 @@ package resource import ( - "context" "testing" ) func TestNewFs(t *testing.T) { - n := NewFsResource("./testdata", context.TODO()) + n := NewFsResource("./testdata") _ = n } diff --git a/go/resource/render.go b/go/resource/render.go new file mode 100644 index 0000000..b3b63ca --- /dev/null +++ b/go/resource/render.go @@ -0,0 +1,24 @@ +package resource + +import ( + "bytes" + "text/template" +) + +func DefaultRenderTemplate(r Resource, sym string, values map[string]string) (string, error) { + v, err := r.GetTemplate(sym) + if err != nil { + return "", err + } + tp, err := template.New("tester").Option("missingkey=error").Parse(v) + if err != nil { + return "", err + } + + b := bytes.NewBuffer([]byte{}) + err = tp.Execute(b, values) + if err != nil { + return "", err + } + return b.String(), err +} diff --git a/go/vm/vm_test.go b/go/vm/vm_test.go index e2c60eb..4753716 100644 --- a/go/vm/vm_test.go +++ b/go/vm/vm_test.go @@ -6,10 +6,8 @@ import ( "fmt" "log" "testing" - "text/template" "git.defalsify.org/festive/resource" -// "git.defalsify.org/festive/router" "git.defalsify.org/festive/state" ) @@ -53,21 +51,7 @@ func (r *TestResource) GetTemplate(sym string) (string, error) { } func (r *TestResource) RenderTemplate(sym string, values map[string]string) (string, error) { - v, err := r.GetTemplate(sym) - if err != nil { - return "", err - } - tp, err := template.New("tester").Option("missingkey=error").Parse(v) - if err != nil { - return "", err - } - - b := bytes.NewBuffer([]byte{}) - err = tp.Execute(b, values) - if err != nil { - return "", err - } - return b.String(), err + return resource.DefaultRenderTemplate(r, sym, values) } func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) {