Exemplo n.º 1
0
// profileToReport is a helper function that converts the merged coverage
// report to the Report JSON format expected by the coverage server.
func profileToReport(profiles []*cover.Profile) *client.Report {
	report := client.Report{}
	report.Files = make([]*client.File, len(profiles), len(profiles))

	for i, profile := range profiles {
		file := client.File{
			Mode:     profile.Mode,
			FileName: profile.FileName,
		}

		file.Blocks = make([]*client.Block, len(profile.Blocks), len(profile.Blocks))
		for ii, block := range profile.Blocks {
			file.Blocks[ii] = &client.Block{
				StartLine: block.StartLine,
				StartCol:  block.StartCol,
				EndLine:   block.EndLine,
				EndCol:    block.EndCol,
				NumStmt:   block.NumStmt,
				Count:     block.Count,
			}
		}

		covered, total, percent := percentCovered(profile)
		file.Lines = total
		file.Covered = covered
		file.Coverage = percent

		report.Files[i] = &file
		report.Lines += file.Lines
		report.Covered += file.Covered
	}
	if report.Lines != 0 {
		report.Coverage = float64(report.Covered) / float64(report.Lines) * float64(100)
	}
	return &report
}
Exemplo n.º 2
0
func findFileReferences(report *client.Report, base string) {
	var files []*client.File

	// normalize the file path based on the working directory
	// also ignore any files outside of the directory
	for _, file := range report.Files {
		fileName := file.FileName
		var prefix string

		if path.IsAbs(fileName) {
			if !strings.HasPrefix(fileName, base) {
				logrus.Warningf("File referenced in coverage not found at %s", fileName)
				continue
			}

			prefix = base
		} else if _, err := os.Stat(fileName); err == nil {
			logrus.Debugf("File found at relative path %s", fileName)

			prefix = ""
		} else {
			var err error
			prefix, err = coverage.PathPrefix(fileName, base)

			if err != nil {
				// See if file is on disk
				logrus.Warningf("File referenced in coverage not found at %s", fileName)
				continue
			}

			logrus.Debugf("Found common path at %s", prefix)
		}

		// Add the file to the report
		file.FileName = strings.TrimPrefix(fileName, prefix)
		files = append(files, file)
	}

	report.Files = files
}