Esempio n. 1
0
func Initialise(ctx context.Context, overrides OptionsInterface) (context.Context, context.CancelFunc, error) {
	if overrides == nil {
		overrides = Flags{}
	}
	options := overrides.getOptions()

	ctx, cancel := context.WithCancel(ctx)
	ctx = jsonctx.AutoContext(ctx)
	ctx = wgctx.NewContext(ctx)
	ctx = sysctx.NewContext(ctx)

	cmd := &cmdctx.Cmd{}

	vos := vosctx.FromContext(ctx)

	path := ""
	cmd.Edit = options.Edit
	cmd.Validate = options.Validate
	cmd.Update = options.Update
	cmd.Log = options.Log
	cmd.Debug = options.Debug
	cmd.Port = options.Port
	if options.Path == "" {
		dir, err := vos.Getwd()
		if err != nil {
			return nil, nil, kerr.Wrap("OKOLXAMBSJ", err)
		}
		p, err := packages.GetPackageFromDir(ctx, dir)
		if err != nil {
			return nil, nil, kerr.Wrap("ADNJKTLAWY", err)
		}
		path = p
	} else {
		path = options.Path
	}

	ctx = cmdctx.NewContext(ctx, cmd)

	pcache, err := parser.Parse(ctx, path)
	if err != nil {
		return nil, nil, kerr.Wrap("EBMBIBIKUF", err)
	}

	ctx = envctx.NewContext(ctx, pcache.Env)

	return ctx, cancel, nil
}
Esempio n. 2
0
File: scan_test.go Progetto: kego/ke
func walkFile(path string, t *testing.T) error {

	b, err := ioutil.ReadFile(path)
	if err != nil {
		return kerr.Wrap("GJULIOKHIR", err)
	}

	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
	if err != nil {
		return kerr.Wrap("HMRIYHYYYH", err)
	}

	pkg, err := packages.GetPackageFromDir(context.Background(), filepath.Dir(path))
	if err != nil {
		return kerr.Wrap("FIPPWAKAGK", err)
	}

	// ignore anything that starts with kego.io/demo/
	if strings.HasPrefix(pkg, "kego.io/demo/") {
		return nil
	}

	def := getPkgDef(pkg)
	if strings.HasSuffix(path, "_test.go") {
		def.tested = true
	}

	kerrName := ""
	assertName := ""
	requireName := ""
	for _, is := range file.Imports {
		importPath, _ := strconv.Unquote(is.Path.Value)
		switch importPath {
		case "github.com/davelondon/kerr":
			if is.Name != nil {
				kerrName, _ = strconv.Unquote(is.Name.Name)
			} else {
				kerrName = "kerr"
			}
		case "github.com/davelondon/ktest/assert":
			if is.Name != nil {
				assertName, _ = strconv.Unquote(is.Name.Name)
			} else {
				assertName = "assert"
			}
		case "github.com/davelondon/ktest/require":
			if is.Name != nil {
				requireName, _ = strconv.Unquote(is.Name.Name)
			} else {
				requireName = "require"
			}
		}
	}

	// visitor implements ast.Visitor
	v := &visitor{
		Bytes:       b,
		t:           t,
		kerrName:    kerrName,
		assertName:  assertName,
		requireName: requireName,
		filePath:    path,
		fset:        fset,
		pkg:         pkg,
	}
	ast.Walk(v, file)

	return nil
}