Beispiel #1
0
func (i *Instrumentable) instrumentTo(processed map[string]bool, istest bool, outdir, relpath string, f func(file *patch.PatchableFile) patch.Patches) error {
	if processed[relpath] {
		return nil
	}
	processed[i.pkg.ImportPath] = true
	imps := i.pkg.Imports
	if istest {
		imps = append(imps, i.pkg.TestImports...)
		imps = append(imps, i.pkg.XTestImports...)
	}
	for _, imp := range imps {
		if i.relevantImport(imp) {
			pkg, err := i.doimport(imp)
			if err != nil {
				return err
			}
			if build.IsLocalImport(imp) {
				imp = "./" + filepath.Join(relpath, imp)
			}
			if err := pkg.instrumentTo(processed, false, outdir, imp, f); err != nil {
				return err
			}
		}
	}
	if !istest {
		pkg := patch.NewPatchablePkg()
		if err := pkg.ParseFiles(i.Files()...); err != nil {
			return err
		}
		if err := i.instrumentPatchable(outdir, relpath, pkg, f); err != nil {
			return err
		}
	} else {
		pkg := patch.NewPatchablePkg()
		if err := pkg.ParseFiles(i.TestFiles()...); err != nil {
			return err
		}
		if err := i.instrumentPatchable(outdir, relpath, pkg, f); err != nil {
			return err
		}
		pkg = patch.NewPatchablePkg()
		if err := pkg.ParseFiles(i.XTestFiles()...); err != nil {
			return err
		}
		if err := i.instrumentPatchable(outdir, relpath, pkg, f); err != nil {
			return err
		}
	}
	return nil
}
Beispiel #2
0
func (i *Instrumentable) instrumentInline(processed map[string]bool, f func(file *patch.PatchableFile) patch.Patches) error {
	if processed[i.id()] {
		return nil
	}
	processed[i.id()] = true
	imps := i.pkg.Imports
	if true {
		imps = append(imps, i.pkg.TestImports...)
		imps = append(imps, i.pkg.XTestImports...)
	}
	for _, imp := range imps {
		if i.relevantImport(imp) {
			pkg, err := i.doimport(imp)
			if err != nil {
				return err
			}
			if err := pkg.instrumentInline(processed, f); err != nil {
				return err
			}
		}
	}
	pkg := patch.NewPatchablePkg()
	if err := pkg.ParseFiles(i.Files()...); err != nil {
		return err
	}
	for path, file := range pkg.Files {
		patches := f(file)
		outfile, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0644)
		if err != nil {
			return err
		}
		if _, err := file.FprintPatched(outfile, file.All(), patches); err != nil {
			return err
		}
		if err := outfile.Close(); err != nil {
			return err
		}
	}
	return nil
}