Skip to content

Commit

Permalink
Merge pull request #7 from darcy-rayner/feature/sort-entries
Browse files Browse the repository at this point in the history
feat: Add option to control how entry list is sorted
  • Loading branch information
darcy-rayner authored Apr 20, 2018
2 parents 1137ffd + 682a4b7 commit 9f4a6b1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ schedule(istanbulCoverage({
// Set a custom failure message
customFailureMessage: "Coverage is a little low, take a look",

// How to sort the entries in the table
entrySortMethod: "alphabetical" // || "least-coverage" || "most-coverage" || "largest-file-size" ||"smallest-file-size" || "uncovered-lines"

// Add a maximum number of entries to display
numberOfEntries: 10,

Expand Down
1 change: 1 addition & 0 deletions src/config.model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe("makeCompleteConfiguration", () => {
coveragePath: "./coverage/coverage-summary.json",
reportFileSet: "all",
reportMode: "message",
entrySortMethod: "alphabetically",
numberOfEntries: 10,
threshold: {
statements: 100,
Expand Down
9 changes: 9 additions & 0 deletions src/config.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export type ReportFileSet = "created" | "modified" | "createdOrModified" | "all"
export type ReportMode = "fail" | "warn" | "message"
export type SortMethod =
| "alphabetically"
| "least-coverage"
| "most-coverage"
| "largest-file-size"
| "smallest-file-size"
| "uncovered-lines"

export interface CoverageThreshold {
statements: number
Expand All @@ -12,6 +19,7 @@ export interface Config {
customSuccessMessage?: string
customFailureMessage?: string
numberOfEntries: number
entrySortMethod: SortMethod
coveragePath: string
reportFileSet: ReportFileSet
threshold: CoverageThreshold
Expand All @@ -28,6 +36,7 @@ export function makeCompleteConfiguration(config?: Partial<Config>): Config {
coveragePath: "./coverage/coverage-summary.json",
reportFileSet: "all",
reportMode: "message",
entrySortMethod: "alphabetically",
numberOfEntries: 10,
threshold: {
statements: 100,
Expand Down
53 changes: 51 additions & 2 deletions src/coverage.mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
makeCoverageModel,
meetsThreshold,
parseCoverageCollection,
sortFiles,
} from "./coverage.model"

describe("combineEntries()", () => {
Expand Down Expand Up @@ -141,6 +142,54 @@ describe("makeCoverageModel", () => {
})
})

describe("parseCoverageCollection", () => {
it("")
describe("sortFiles", () => {
const coverage = {
file1: {
lines: { total: 50, covered: 25, skipped: 25, pct: 50 },
functions: { total: 100, covered: 50, skipped: 50, pct: 50 },
statements: { total: 100, covered: 50, skipped: 50, pct: 50 },
branches: { total: 100, covered: 50, skipped: 50, pct: 50 },
},
file2: {
lines: { total: 150, covered: 60, skipped: 40, pct: 60 },
functions: { total: 100, covered: 60, skipped: 40, pct: 60 },
statements: { total: 100, covered: 60, skipped: 40, pct: 60 },
branches: { total: 100, covered: 60, skipped: 40, pct: 60 },
},
file3: {
lines: { total: 100, covered: 700, skipped: 300, pct: 70 },
functions: { total: 100, covered: 70, skipped: 30, pct: 70 },
statements: { total: 100, covered: 70, skipped: 30, pct: 70 },
branches: { total: 100, covered: 70, skipped: 30, pct: 70 },
},
}
it("sorts files by their line coverage percentage in descending order", () => {
const output = sortFiles(["file1", "file2", "file3"], coverage, "most-coverage")
expect(output).toEqual(["file3", "file2", "file1"])
})
it("sorts files by their line coverage percentage in ascending order", () => {
const output = sortFiles(["file1", "file2", "file3"], coverage, "least-coverage")
expect(output).toEqual(["file1", "file2", "file3"])
})
it("skips files not in input file list", () => {
const output = sortFiles(["file1", "file3"], coverage, "most-coverage")
expect(output).toEqual(["file3", "file1"])
})
it("sorts files by the number of lines in descending order", () => {
const output = sortFiles(["file1", "file2", "file3"], coverage, "largest-file-size")
expect(output).toEqual(["file2", "file3", "file1"])
})
it("sorts files the number of lines in ascending order", () => {
const output = sortFiles(["file1", "file2", "file3"], coverage, "smallest-file-size")
expect(output).toEqual(["file1", "file3", "file2"])
})
it("sorts files by the number of uncovered lines in descending order", () => {
const output = sortFiles(["file1", "file2", "file3"], coverage, "uncovered-lines")
expect(output).toEqual(["file3", "file2", "file1"])
})

it("sorts files in alphabetical order", () => {
const output = sortFiles(["c", "b", "a"], coverage, "alphabetically")
expect(output).toEqual(["a", "b", "c"])
})
})
54 changes: 51 additions & 3 deletions src/coverage.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { METHODS } from "http"
import * as path from "path"
import { Config, CoverageThreshold } from "./config.model"
import { Config, CoverageThreshold, SortMethod } from "./config.model"
import FilesystemService from "./filesystem.service"

export interface CoverageItem {
Expand Down Expand Up @@ -79,8 +80,55 @@ export function parseCoverageCollection(coveragePath: string): CoverageCollectio
}
}

export function makeCoverageModel(numberOfEntries: number, files: string[], coverageCollection: CoverageCollection) {
const sortedFiles = files.sort((a, b) => a.localeCompare(b, "en-US"))
function sortFileByCoverageKey(
files: string[],
coverageCollection: CoverageCollection,
ascending: boolean,
key: string
) {
return files
.map(file => {
return { file, entry: coverageCollection[file] }
})
.sort((a, b) => (ascending ? a.entry.lines[key] - b.entry.lines[key] : b.entry.lines[key] - a.entry.lines[key]))
.map(entry => entry.file)
}

function sortFilesAlphabetically(files: string[]): string[] {
return files.sort((a, b) => a.localeCompare(b, "en-US"))
}

/**
* Sorts a list of files by their total line coverage.
* @param files The files list
* @param coverageCollection The collection of file coverages.
* @param method The method to use while sorting
* @returns The sorted list of file names.
*/
export function sortFiles(files: string[], coverageCollection: CoverageCollection, method: SortMethod) {
switch (method) {
case "alphabetically":
return sortFilesAlphabetically(files)
case "least-coverage":
return sortFileByCoverageKey(files, coverageCollection, true, "pct")
case "most-coverage":
return sortFileByCoverageKey(files, coverageCollection, false, "pct")
case "largest-file-size":
return sortFileByCoverageKey(files, coverageCollection, false, "total")
case "smallest-file-size":
return sortFileByCoverageKey(files, coverageCollection, true, "total")
case "uncovered-lines":
return sortFileByCoverageKey(files, coverageCollection, false, "skipped")
}
}

export function makeCoverageModel(
numberOfEntries: number,
files: string[],
coverageCollection: CoverageCollection,
sortMethod: SortMethod = "alphabetically"
) {
const sortedFiles = sortFiles(files, coverageCollection, sortMethod)

const displayedFiles = sortedFiles.slice(0, Math.min(sortedFiles.length, numberOfEntries))
const displayedEntries = displayedFiles.map(file => coverageCollection[file])
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ export function istanbulCoverage(config?: Partial<Config>): Promise<void> {
return
}

const coverageModel = makeCoverageModel(combinedConfig.numberOfEntries, files, coverage)
const coverageModel = makeCoverageModel(
combinedConfig.numberOfEntries,
files,
coverage,
combinedConfig.entrySortMethod
)
sendPRComment(combinedConfig, coverageModel.total)

const report = generateReport(gitRoot, gitBranch, coverageModel, combinedConfig.reportFileSet)
Expand Down

0 comments on commit 9f4a6b1

Please sign in to comment.