コード例 #1
0
ファイル: annotate.go プロジェクト: litixsoft/lxb
func annotateSource() (rc int) {
	annotateFlags.Parse(os.Args[2:])
	if annotateFlags.NArg() == 0 {
		fmt.Fprintf(os.Stderr, "missing coverage file\n")
		return 1
	}

	var data []byte
	var err error
	if filename := annotateFlags.Arg(0); filename == "-" {
		data, err = ioutil.ReadAll(os.Stdin)
	} else {
		data, err = ioutil.ReadFile(filename)
	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to read coverage file: %s\n", err)
		return 1
	}

	packages, err := unmarshalJson(data)
	if err != nil {
		fmt.Fprintf(
			os.Stderr, "failed to unmarshal coverage data: %s\n", err)
		return 1
	}

	// Sort packages, functions by name.
	sort.Sort(packageList(packages))
	for _, pkg := range packages {
		sort.Sort(functionList(pkg.Functions))
	}

	a := &annotator{}
	a.fset = token.NewFileSet()
	a.files = make(map[string]*token.File)

	var regexps []*regexp.Regexp
	for _, arg := range annotateFlags.Args()[1:] {
		re, err := regexp.Compile(arg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "warning: failed to compile %q as a regular expression, ignoring\n", arg)
		} else {
			regexps = append(regexps, re)
		}
	}
	if len(regexps) == 0 {
		regexps = append(regexps, regexp.MustCompile("."))
	}
	for _, pkg := range packages {
		for _, fn := range pkg.Functions {
			if percentReached(fn) >= *annotateCeilingFlag {
				continue
			}
			name := pkg.Name + "/" + fn.Name
			for _, regexp := range regexps {
				if regexp.FindStringIndex(name) != nil {
					err := a.printFunctionSource(fn)
					if err != nil {
						fmt.Fprintf(os.Stderr, "warning: failed to annotate function %q\n", name)
					}
					break
				}
			}
		}
	}
	return
}
コード例 #2
0
ファイル: annotate.go プロジェクト: davecheney/gocov
func annotateSource() (rc int) {
	if flag.NArg() == 1 {
		fmt.Fprintf(os.Stderr, "missing coverage file and function patterns\n")
		return 1
	} else if flag.NArg() < 3 {
		fmt.Fprintf(os.Stderr, "missing function patterns\n")
		return 1
	}

	var data []byte
	var err error
	if filename := flag.Arg(1); filename == "-" {
		data, err = ioutil.ReadAll(os.Stdin)
	} else {
		data, err = ioutil.ReadFile(filename)
	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to read coverage file: %s\n", err)
		return 1
	}

	packages, err := unmarshalJson(data)
	if err != nil {
		fmt.Fprintf(
			os.Stderr, "failed to unmarshal coverage data: %s\n", err)
		return 1
	}

	// Sort packages, functions by name.
	sort.Sort(packageList(packages))
	for _, pkg := range packages {
		sort.Sort(functionList(pkg.Functions))
	}

	a := &annotator{}
	a.fset = token.NewFileSet()
	a.files = make(map[string]*token.File)

	var regexps []*regexp.Regexp
	for i := 2; i < flag.NArg(); i++ {
		arg := flag.Arg(i)
		re, err := regexp.Compile(arg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "warning: failed to compile %q as a regular expression, ignoring\n", arg)
		} else {
			regexps = append(regexps, re)
		}
	}
	if len(regexps) > 0 {
		for _, pkg := range packages {
			for _, fn := range pkg.Functions {
				name := pkg.Name + "/" + fn.Name
				for _, regexp := range regexps {
					if regexp.FindStringIndex(name) != nil {
						err := a.printFunctionSource(fn)
						if err != nil {
							fmt.Fprintf(os.Stderr, "warning: failed to annotate function %q\n", name)
						}
						break
					}
				}
			}
		}
	}
	return
}