Example #1
0
func parseProfile(fileName string) error {
	profiles, err := cover.ParseProfiles(fileName)
	if err != nil {
		return err
	}

	prof, err := discover.ParseProfile(profiles)
	if err != nil {
		return err
	}

	for _, f := range prof.Files {
		prof.Trim(f)

		// If we filtered out all decls, don't print at all
		if len(f.Decls) == 0 {
			continue
		}

		fn := filepath.Base(prof.Fset.File(f.Pos()).Name())
		importPath := prof.ImportPaths[f]
		if importPath == "" {
			return fmt.Errorf("No import path found for %q", fn)
		}

		if err := outputFile(importPath, fn, prof.Fset, f); err != nil {
			return err
		}
	}
	return nil
}
Example #2
0
func convertProfiles(filenames ...string) error {
	var ps gocovutil.Packages
	for i := range filenames {
		converter := converter{
			packages: make(map[string]*gocov.Package),
		}
		profiles, err := cover.ParseProfiles(filenames[i])
		if err != nil {
			return err
		}
		for _, p := range profiles {
			if err := converter.convertProfile(p); err != nil {
				return err
			}
		}

		for _, pkg := range converter.packages {
			ps.AddPackage(pkg)
		}
	}
	bytes, err := marshalJson(ps)
	if err != nil {
		return err
	}
	fmt.Println(string(bytes))
	return nil
}
Example #3
0
func parseCover(fn string) ([]*SourceFile, error) {
	profs, err := cover.ParseProfiles(fn)
	if err != nil {
		return nil, fmt.Errorf("Error parsing coverage: %v", err)
	}

	var rv []*SourceFile
	for _, prof := range profs {
		path, err := findFile(prof.FileName)
		if err != nil {
			log.Fatalf("Can't find %v", err)
		}
		fb, err := ioutil.ReadFile(path)
		if err != nil {
			log.Fatalf("Error reading %v: %v", path, err)
		}
		sf := &SourceFile{
			Name:     getCoverallsSourceFileName(path),
			Source:   string(fb),
			Coverage: make([]interface{}, 1+bytes.Count(fb, []byte{'\n'})),
		}

		for _, block := range prof.Blocks {
			for i := block.StartLine; i <= block.EndLine; i++ {
				count, _ := sf.Coverage[i-1].(int)
				sf.Coverage[i-1] = count + block.Count
			}
		}

		rv = append(rv, sf)
	}

	return rv, nil
}
Example #4
0
// htmlOutput reads the profile data from profile and generates an HTML
// coverage report, writing it to outfile. If outfile is empty,
// it writes the report to a temporary file and opens it in a web browser.
func htmlOutput(profile, outfile string) error {
	profiles, err := cover.ParseProfiles(profile)
	if err != nil {
		return err
	}

	var d templateData

	for _, profile := range profiles {
		fn := profile.FileName
		if profile.Mode == "set" {
			d.Set = true
		}
		file, err := findFile(fn)
		if err != nil {
			return err
		}
		src, err := ioutil.ReadFile(file)
		if err != nil {
			return fmt.Errorf("can't read %q: %v", fn, err)
		}
		var buf bytes.Buffer
		err = htmlGen(&buf, src, profile.Boundaries(src))
		if err != nil {
			return err
		}
		d.Files = append(d.Files, &templateFile{
			Name:     fn,
			Body:     template.HTML(buf.String()),
			Coverage: percentCovered(profile),
		})
	}

	var out *os.File
	if outfile == "" {
		var dir string
		dir, err = ioutil.TempDir("", "cover")
		if err != nil {
			return err
		}
		out, err = os.Create(filepath.Join(dir, "coverage.html"))
	} else {
		out, err = os.Create(outfile)
	}
	err = htmlTemplate.Execute(out, d)
	if err == nil {
		err = out.Close()
	}
	if err != nil {
		return err
	}

	if outfile == "" {
		if !startBrowser("file://" + out.Name()) {
			fmt.Fprintf(os.Stderr, "HTML output written to %s\n", out.Name())
		}
	}

	return nil
}
Example #5
0
func parseGoCoverageResults(target *core.BuildTarget, coverage *core.TestCoverage, filename string) error {
	profiles, err := cover.ParseProfiles(filename)
	if err != nil {
		return err
	}
	for _, profile := range profiles {
		coverage.Files[profile.FileName] = parseBlocks(profile.Blocks)
	}
	coverage.Tests[target.Label] = coverage.Files
	return nil
}
Example #6
0
func main() {
	flag.Parse()

	var profiles []*cover.Profile

	for _, file := range flag.Args() {
		profs, err := cover.ParseProfiles(file)
		if err != nil {
			log.Fatalf("failed to parse profiles: %v", err)
		}
		profiles = append(profiles, profs...)
	}
	fmt.Printf("coverage: %.1f%%\n", percentCovered(profiles))
}
Example #7
0
func (c *CoverProfile) Aggregate(inputFile string) error {
	profiles, err := cover.ParseProfiles(inputFile)
	if err != nil {
		return err
	} else if c.profiles == nil {
		c.profiles = profiles
		return nil
	}
	for _, newProfile := range profiles {
		found := false
		for pIdx, p := range c.profiles {
			// Find the matching file
			if newProfile.FileName == p.FileName {
				found = true
				if p.Mode != newProfile.Mode {
					return fmt.Errorf("Mismatching count mode: %s vs %s", p.Mode, newProfile.Mode)
				}
				// Now merge block profiles
				for _, newBlock := range newProfile.Blocks {
					// So... find the matching block
					for bIdx, b := range p.Blocks {
						// We can't compare the blocks as a whole, since the count will differ
						if c.compareBlock(&newBlock, &b) {
							// Depending on the mode, we need to toggle or sum the count
							if p.Mode == "set" {
								if newBlock.Count > 0 {
									c.profiles[pIdx].Blocks[bIdx].Count = newBlock.Count
								}
							} else {
								c.profiles[pIdx].Blocks[bIdx].Count += newBlock.Count
							}
							// We found the correct block, go for the next new block
							break
						}
					}
				}
				// We processed the correct file, now process the next one
				break
			}
		}
		if found == false {
			c.profiles = append(c.profiles, newProfile)
		}
	}
	return nil
}
Example #8
0
func main() {
	flag.Parse()

	var merged []*cover.Profile

	for _, file := range flag.Args() {
		profiles, err := cover.ParseProfiles(file)
		if err != nil {
			log.Fatalf("failed to parse profiles: %v", err)
		}
		for _, p := range profiles {
			merged = addProfile(merged, p)
		}
	}

	dumpProfiles(merged, os.Stdout)
}
Example #9
0
func funcOutput(profile, outputFile string) error {
	profiles, err := cover.ParseProfiles(profile)
	if err != nil {
		return err
	}

	var out *bufio.Writer
	if outputFile == "" {
		out = bufio.NewWriter(os.Stdout)
	} else {
		fd, err := os.Create(outputFile)
		if err != nil {
			return err
		}
		defer fd.Close()
		out = bufio.NewWriter(fd)
	}
	defer out.Flush()

	tabber := tabwriter.NewWriter(out, 1, 8, 1, '\t', 0)
	defer tabber.Flush()

	var total, covered int64
	for _, profile := range profiles {
		fn := profile.FileName
		file, err := findFile(fn)
		if err != nil {
			return err
		}
		funcs, err := findFuncs(file)
		if err != nil {
			return err
		}
		// Now match up functions and profile blocks.
		for _, f := range funcs {
			c, t := f.coverage(profile)
			fmt.Fprintf(tabber, "%s:%d:\t%s\t%.1f%%\n", fn, f.startLine, f.name, 100.0*float64(c)/float64(t))
			total += t
			covered += c
		}
	}
	fmt.Fprintf(tabber, "total:\t(statements)\t%.1f%%\n", 100.0*float64(covered)/float64(total))

	return nil
}
Example #10
0
func calculateCoverage(coverprofile string) (float64, error) {
	profiles, err := cover.ParseProfiles(coverprofile)
	if err != nil {
		return 0.0, wraperr(err, "cannot parse coverage profile file %s", coverprofile)
	}
	total := 0
	covered := 0
	for _, profile := range profiles {
		for _, block := range profile.Blocks {
			total += block.NumStmt
			if block.Count > 0 {
				covered += block.NumStmt
			}
		}
	}
	if total == 0 {
		return 0.0, nil
	}
	return float64(covered) / float64(total) * 100, nil
}
Example #11
0
func (m *gocoverdir) calculateCoverage() (float64, error) {
	profiles, err := cover.ParseProfiles(m.args.coverprofile)
	if err != nil {
		return 0.0, err
	}
	total := 0
	covered := 0
	for _, profile := range profiles {
		for _, block := range profile.Blocks {
			total += block.NumStmt
			if block.Count > 0 {
				covered += block.NumStmt
			}
		}
	}
	if total == 0 {
		return 0.0, nil
	}
	return float64(covered) / float64(total) * 100, nil
}
Example #12
0
func main() {
	flag.Parse()

	var merged []*cover.Profile
	var mode string = ""

	for _, file := range flag.Args() {
		profiles, err := cover.ParseProfiles(file)
		if err != nil {
			log.Fatalf("failed to parse profiles: %v", err)
		}
		for _, p := range profiles {
			if mode == "" {
				mode = p.Mode
			} else if p.Mode != mode {
				log.Fatalf("all profiles must be the same mode")
			}
			merged = addProfile(merged, p)
		}
	}

	dumpProfiles(merged, os.Stdout)
}
Example #13
0
func main() {
	outputProfiles := map[string]*cover.Profile{}
	for _, input := range os.Args[1:] {
		inputProfiles, err := cover.ParseProfiles(input)
		if err != nil {
			panic(fmt.Sprintf("Error parsing %s: %v", input, err))
		}
		for _, ip := range inputProfiles {
			op := outputProfiles[ip.FileName]
			if op == nil {
				outputProfiles[ip.FileName] = ip
			} else {
				outputProfiles[ip.FileName] = merge(op, ip)
			}
		}
	}
	profiles := make([]*cover.Profile, 0, len(outputProfiles))
	for _, profile := range outputProfiles {
		profiles = append(profiles, profile)
	}
	sort.Sort(byFileName(profiles))
	print(profiles)
}
Example #14
0
func runPackageTests(pkg string) (out string, cov []*cover.Profile, err error) {
	coverFile, err := ioutil.TempFile("", "gotestcover-")
	if err != nil {
		return "", nil, err
	}
	defer coverFile.Close()
	defer os.Remove(coverFile.Name())
	var args []string
	args = append(args, "test")

	if flagA {
		args = append(args, "-a")
	}
	if flagX {
		args = append(args, "-x")
	}
	if flagRace {
		args = append(args, "-race")
	}
	if flagTags != "" {
		args = append(args, "-tags", flagTags)
	}

	if flagV {
		args = append(args, "-v")
	}
	if flagCount != 0 {
		args = append(args, "-count", fmt.Sprint(flagCount))
	}
	if flagCPU != "" {
		args = append(args, "-cpu", flagCPU)
	}
	if flagParallel != "" {
		args = append(args, "-parallel", flagParallel)
	}
	if flagRun != "" {
		args = append(args, "-run", flagRun)
	}
	if flagShort {
		args = append(args, "-short")
	}
	if flagTimeout != "" {
		args = append(args, "-timeout", flagTimeout)
	}
	args = append(args, "-cover")
	if flagCoverMode != "" {
		args = append(args, "-covermode", flagCoverMode)
	}
	args = append(args, "-coverprofile", coverFile.Name())

	args = append(args, pkg)

	args = append(args, argsAfter("--")...)

	cmdOut, err := runGoCommand(args...)
	if err != nil {
		return "", nil, err
	}

	cov, err = cover.ParseProfiles(coverFile.Name())
	if err != nil {
		return "", nil, err
	}

	return string(cmdOut), cov, nil
}
Example #15
0
func (r *reader) ReadFile(path string) ([]*cover.Profile, error) {
	return cover.ParseProfiles(path)
}