testdata.go (2415B)
1 package testdata 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "os" 8 9 "git.defalsify.org/vise.git/db" 10 fsdb "git.defalsify.org/vise.git/db/fs" 11 "git.defalsify.org/vise.git/resource" 12 "git.defalsify.org/vise.git/logging" 13 ) 14 15 var ( 16 ctx = context.Background() 17 store = fsdb.NewFsDb() 18 out = outNew 19 logg = logging.NewVanilla().WithDomain("testdata") 20 ) 21 22 type echoFunc struct { 23 v string 24 } 25 26 func(e *echoFunc) get(ctx context.Context, nodeSym string, input []byte) (resource.Result, error) { 27 return resource.Result{ 28 Content: e.v, 29 }, nil 30 } 31 32 func outNew(sym string, b []byte, tpl string, data map[string]string) error { 33 logg.Debugf("testdata out", "sym", sym) 34 store.SetPrefix(db.DATATYPE_TEMPLATE) 35 err := store.Put(ctx, []byte(sym), []byte(tpl)) 36 if err != nil { 37 return err 38 } 39 store.SetPrefix(db.DATATYPE_BIN) 40 err = store.Put(ctx, []byte(sym), b) 41 if err != nil { 42 return err 43 } 44 store.SetPrefix(db.DATATYPE_STATICLOAD) 45 for k, v := range data { 46 logg.Debugf("testdata out staticload", "sym", sym, "k", k, "v", v) 47 err = store.Put(ctx, []byte(k), []byte(v)) 48 if err != nil { 49 return err 50 } 51 } 52 return nil 53 } 54 55 func generate() error { 56 err := os.MkdirAll(DataDir, 0755) 57 if err != nil { 58 return err 59 } 60 store = fsdb.NewFsDb() 61 store.Connect(ctx, DataDir) 62 store.SetLock(db.DATATYPE_TEMPLATE, false) 63 store.SetLock(db.DATATYPE_BIN, false) 64 store.SetLock(db.DATATYPE_MENU, false) 65 store.SetLock(db.DATATYPE_STATICLOAD, false) 66 67 fns := []genFunc{root, foo, bar, baz, long, lang, nothing, something, defaultCatch} 68 for _, fn := range fns { 69 err = fn() 70 if err != nil { 71 return err 72 } 73 } 74 return nil 75 } 76 77 // Generate outputs bytecode, templates and content symbols to a temporary directory. 78 // 79 // This directory can in turn be used as data source for the the resource.FsResource object. 80 func Generate() (string, error) { 81 dir, err := ioutil.TempDir("", "vise_testdata_") 82 if err != nil { 83 return "", err 84 } 85 DataDir = dir 86 dirLock = true 87 err = generate() 88 return dir, err 89 } 90 91 92 // Generate outputs bytecode, templates and content symbols to a specified directory. 93 // 94 // The directory must exist, and must not have been used already in the same code execution. 95 // 96 // This directory can in turn be used as data source for the the resource.FsResource object. 97 func GenerateTo(dir string) error { 98 if dirLock { 99 return fmt.Errorf("directory already overridden") 100 } 101 DataDir = dir 102 dirLock = true 103 return generate() 104 }