Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command "show-coverage" #424

Merged
merged 7 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions cmd/show-coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd

import (
"fmt"
"strings"
"io/ioutil"
"encoding/json"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func getLineCount(result map[string]interface{}, key string) int {
line_counts := result["line_counts"].(map[string]interface{})
return int(line_counts[key].(float64))
}

func printHeader(result map[string]interface{}) {
header := "Coverage: %.2f%% (%d/%d lines covered, %d missing)"
fmt.Println(fmt.Sprintf(header, result["covered_percent"], getLineCount(result, "covered"), getLineCount(result, "total"), getLineCount(result, "missed")))
}

func printUncoveredLinesFromFile(file map[string]interface{}) {
var uncovered_lines []int
var values []interface{}
json.Unmarshal([]byte(file["coverage"].(string)), &values)
for i, value := range values {
if value != nil && int(value.(float64)) == 0 {
uncovered_lines = append(uncovered_lines, i + 1)
}
}
uncovered_lines_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uncovered_lines)), ", "), "[]")
fmt.Println(fmt.Sprintf("%s: %s", file["name"], uncovered_lines_str))
}

var showCoverageCmd = &cobra.Command{
Use: "show-coverage",
Short: "Show coverage results in standard output",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("you must pass in one file with the coverage results")
}

dat, err := ioutil.ReadFile(args[0])
if err != nil {
return errors.New("could not open input file")
}

// Parse JSON from coverage file
var result map[string]interface{}
json.Unmarshal([]byte(string(dat)), &result)

printHeader(result)

// If there is any missed lines, print which are them, by file
if getLineCount(result, "missed") > 0 {

fmt.Println("Uncovered lines by file:")
files := result["source_files"].([]interface{})

for _, file_obj := range files {
file := file_obj.(map[string]interface{})
if file["covered_percent"].(float64) < 100 {
printUncoveredLinesFromFile(file)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about another one here? 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one what? 😁

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function!
I would say something like printUncoveredLines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, makes sense... done! :)


return nil
},
}

func init() {
RootCmd.AddCommand(showCoverageCmd)
}
37 changes: 37 additions & 0 deletions man/cc-test-reporter-show-coverage.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
% CC-TEST-REPORTER-SHOW-COVERAGE(1) User Manuals
% Meedan <hello@meedan.com>
% March 2020

# PROLOG

This is a sub-command of **cc-test-reporter**(1).

# SYNOPSIS

**cc-test-reporter-show-coverage** FILE

# DESCRIPTION

Show the coverage results from a Code Climate coverage JSON file.

# OPTIONS

## FILE

Input file. Must be a valid Code Climate coverage JSON file, for example, the
output of *cc-test-reporter format-coverage* or
*cc-test-reporter sum-coverage* command.

# INPUT VALIDATION

Must be a valid Code Climate coverage JSON file, for example, the
output of *cc-test-reporter format-coverage* or
*cc-test-reporter sum-coverage* command.

# ENVIRONMENT VARIABLES

None

# SEE ALSO

**cc-test-reporter-format-coverage**(1).