// ValidateMain is called by the generated validate command. // ke: {"func": {"notest": true}} func ValidateMain(path string) { // Using process.Flags as the options means that the non-specified options are read from the // command flags. update := false options := &process.Flags{ Update: &update, Path: &path, } ctx, cancel, err := process.Initialise(context.Background(), options) if err != nil { fmt.Println(err.Error()) os.Exit(1) } // The log function outputs errors and messages to the console. We pass it in to permit testing. log := func(message string) { fmt.Println(message) } // The interrupt chanel signals that Ctrl+C or other OS interrupts have happened. interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) signal.Notify(interrupt, syscall.SIGTERM) exitStatus := validateMain(ctx, cancel, log, interrupt) wgctx.WaitAndExit(ctx, exitStatus) }
func initialise() context.Context { if systemContext == nil { ctx, _, err := process.Initialise(context.Background(), process.Options{Path: "kego.io/system"}) if err != nil { panic(err) } systemContext = ctx } return systemContext }
func TestReflectType(t *testing.T) { ctx, _, err := process.Initialise(context.Background(), process.Options{Path: "kego.io/system"}) require.NoError(t, err) checkReflectType(ctx, t, "kego.io/system", "type", "basic", "bool") checkReflectType(ctx, t, "kego.io/system", "type", "embed", "[]*system.Reference") checkReflectType(ctx, t, "kego.io/system", "type", "native", "*system.String") checkReflectType(ctx, t, "kego.io/system", "type", "interface", "bool") checkReflectType(ctx, t, "kego.io/system", "type", "fields", "map[string]system.RuleInterface") checkReflectType(ctx, t, "kego.io/system", "type", "rule", "*system.Type") }
func main() { // This turns off focus reporting mode that will print ā^[[Oā and ā^[[Iā // when the terminal window gets / loses focus // http://superuser.com/questions/931873/o-and-i-appearing-on-iterm2-when-focus-lost fmt.Print("\033[?1004l") ctx, cancel, err := process.Initialise(context.Background(), nil) if err != nil { fmt.Println(err) os.Exit(1) } env := envctx.FromContext(ctx) cmd := cmdctx.FromContext(ctx) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) signal.Notify(c, syscall.SIGTERM) go func() { <-c cancel() }() if err := process.GenerateAll(ctx, env.Path, map[string]bool{}); err != nil { fmt.Println(err.Error()) wgctx.WaitAndExit(ctx, 1) } if cmd.Validate { err := process.RunValidateCommand(ctx) if err != nil { if !cmd.Log { // in log mode, we have already written the output of the exec'ed ke command, // so we don't need to duplicate the error message. if v, ok := kerr.Source(err).(validate.ValidationCommandError); ok { fmt.Println(v.Description) wgctx.WaitAndExit(ctx, 4) // Exit code 4: validation error } fmt.Println(err.Error()) } wgctx.WaitAndExit(ctx, 1) } } if cmd.Edit { if err = server.Start(ctx, cancel); err != nil { fmt.Println(err.Error()) wgctx.WaitAndExit(ctx, 1) } } wgctx.WaitAndExit(ctx, 0) }
func TestNodeUnpack(t *testing.T) { ctx, _, err := process.Initialise(context.Background(), &process.Options{ Path: "kego.io/demo/site", }) require.NoError(t, err) j := `{"type":"system:package","aliases":{"images":"kego.io/demo/common/images","units":"kego.io/demo/common/units","words":"kego.io/demo/common/words"}}` _, err = node.Unmarshal(ctx, []byte(j)) require.NoError(t, err) }
func InitialiseAndGenerate(t *testing.T, cb *tests.ContextBuilder, name string, files map[string]string) (string, error) { path, _ := cb.TempPackage(name, files) ctx, _, err := process.Initialise(cb.Ctx(), &process.Options{ Path: path, }) if err != nil { return "", err } cb.SetCtx(ctx) source, err := generate.Structs(ctx, cb.Env()) if err != nil { return "", err } return string(source), nil }
func testUnpack(t *testing.T, path string) { ctx, _, err := process.Initialise(context.Background(), &process.Options{ Path: path, }) require.NoError(t, err) env := envctx.FromContext(ctx) files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive) bytes := scanner.ScanFilesToBytes(ctx, files) for b := range bytes { _, err := node.Unmarshal(ctx, b.Bytes) require.NoError(t, err, b.File) } }
func runKe(cb *tests.ContextBuilder, name string, files map[string]string) (string, error) { tests := false for name, _ := range files { if strings.HasSuffix(name, "_test.go") { // if we add a xxx_test.go file we should also run "go test" tests = true } } path, _ := cb.RealGopath().TempPackage(name, files) ctx, _, err := process.Initialise(context.Background(), &process.Options{ Path: path, }) if err != nil { return "", err } env := envctx.FromContext(ctx) if err := process.Generate(ctx, env); err != nil { return "", err } if err := process.RunValidateCommand(ctx); err != nil { return "", err } if tests { if out, err := exec.Command("go", "test", path).CombinedOutput(); err != nil { return "", fmt.Errorf("%s", string(out)) } } return env.Path, nil }