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 2 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
57 changes: 57 additions & 0 deletions cmd/show-coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

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

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

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")
}

var result map[string]interface{}
json.Unmarshal([]byte(string(dat)), &result)
line_counts := result["line_counts"].(map[string]interface{})
header := "Coverage: %.2f%% (%d/%d lines covered, %d missing)"
fmt.Println(fmt.Sprintf(header, result["covered_percent"], int(line_counts["covered"].(float64)), int(line_counts["total"].(float64)), int(line_counts["missed"].(float64))))
Copy link
Contributor

Choose a reason for hiding this comment

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

The body of this function is a bit dense. Do you mind extracting a few new functions to smooth out the read?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

if int(line_counts["missed"].(float64)) > 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 {
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))
}
}
}

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).