Coder Social home page Coder Social logo

the-bugging / istanbul-badges-readme Goto Github PK

View Code? Open in Web Editor NEW
94.0 4.0 32.0 5.03 MB

Creates and updates README testing coverage badges with your json-summary

Home Page: https://www.npmjs.com/package/istanbul-badges-readme

License: MIT License

JavaScript 2.76% TypeScript 96.84% Shell 0.40%
istanbul-badges-readme husky githooks jest readme coverage istanbul jest-badges-readme c8 nyc

istanbul-badges-readme's Introduction

Istanbul Badges Readme

Creates README badges from istanbul coverage report

Statements Branches Functions Lines
Statements Branches Functions Lines

Table of Contents

Running example

Example


Requirements

  • First, of course, you must have a test runner such as Jest and Mocha;
  • You must have json-summary as a coverageReporter in your tests configuration;
  • For example, if you are using Jest, configuration should either be within package.json or inside your jest config file i.e. jest.config.js or jestconfig.json as written below:
  "coverageReporters": ["json-summary"]

Installation

  • Install the library in your project as a devDependency:
  npm i -D istanbul-badges-readme
  • Add at least one of the below coverage hashes in your README file:

    • ![Statements](#statements#)
    • ![Branches](#branches#)
    • ![Functions](#functions#)
    • ![Lines](#lines#)
  • A simple example of all hashes being used in a table fashion markup:

| Statements                  | Branches                | Functions                 | Lines             |
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
| ![Statements](#statements#) | ![Branches](#branches#) | ![Functions](#functions#) | ![Lines](#lines#) |

Simple Usage

  • Simply run it from the CLI as follows:
  npx istanbul-badges-readme
  • Or add it to your package.json scripts as follows:
"scripts": {
  "make-badges": "istanbul-badges-readme",
}

Advanced Usage Arguments

Coverage Directory

  • Custom coverage directory? Use --coverageDir argument:
  npm run istanbul-badges-readme --coverageDir="./my-custom-coverage-directory"

Readme Directory

  • Custom readme directory? Use --readmeDir argument:
  npm run istanbul-badges-readme --readmeDir="./my-custom-readme-directory"

Silent

  • Want it without logging? Try silent mode with --silent argument:
  npm run istanbul-badges-readme --silent

Functions and Branches Labels

  • You may also create custom labeling for the badges using the corresponding hash and Label e.g. branchesLabel --branchesLabel='Branches are troublesome!':
  npm run istanbul-badges-readme --functionsLabel='Mis funciones!' --branchesLabel='Branches are troublesome!'

Badge Style

  • You can also change the badge styling, according to shields.io's own style reference. See more examples here.
  npm run istanbul-badges-readme --style="for-the-badges"

Badge Logo

  • There is an option to use a logo within the badge, as described on shields.io's own documentation which uses all icons available at simple-icons.
  npm run istanbul-badges-readme --logo="jest"

Configure Badge Color

  • You can configure the color threshold of the badges by passing the --colors argument. If you want red badges for a code coverage below 50% and yellow badges for a coverage below 60%, you'd do this:
  npm run istanbul-badges-readme --colors=red:50,yellow:60

Exit code

  • To exit with 1 code on validation errors (eg.: README doesn't exist, or coverage directory doesn't exist) or on editing errors (eg.: cannot write to README due to lack of permissions). The default exit code is 0. Set a different one by using --exitCode argument.
  npm run istanbul-badges-readme --exitCode=1

Usage as a part of your githooks

  • If you want to have this run on the pre-commit hook and update the commit in place, just install husky and add the pre-commit script to your package.json.
  1. Install Husky.
  npm install -D husky
  1. Add your pre-commit script:
  "husky": {
    "hooks": {
      "pre-commit": "npm run test && istanbul-badges-readme && git add 'README.md'"
    }
  }
  1. Git Commit and Push. Just use your workflow as usual. If your tests fail, no commit. If they pass, update the README.md and add the file to the commit. Nice!

Usage as a part of your CI

You may want to have peace of mind that contributors have run istanbul-badges-readme locally by performing a simple check in your CI.

The --ci argument will throw an error, code 0 by default unless exitCode is specified, thus not updating anything regarding coverage, if the badges generated do not match what is already in the README.md.

You can add this to your package.json as follows for exitCode 0:

"scripts": {
  "make-badges": "istanbul-badges-readme",
  "make-badges:ci": "npm run make-badges -- --ci",
}

Also if you wish a different exitCode:

"scripts": {
  "make-badges": "istanbul-badges-readme",
  "make-badges:ci": "npm run make-badges -- --ci --exitCode=1",
}

This is a useful addition/alternative to the githooks approach for some use cases such as larger codebases, slow computers etc, where it isn't always feasible to run all the tests and produce coverage on each commit.

Custom Badge Styles

  • DEFAULT STYLE Square style='square': Statements

  • Square flat style='square-flat': Statements

  • Plastic style='plastic': Statements

  • For the badge style='for-the-badge': Statements

See running examples

Examples folder

βœ”οΈ Tip

We use this in our pull request GitHub Action, check out a recent pull request to see it in action!


Contributors

Thanks goes to these wonderful people (emoji key):

Olavo Parno
Olavo Parno

πŸ€” πŸ’» ⚠️
nothingismagick
nothingismagick

πŸ€” πŸ› πŸ–‹
Dave Fisher
Dave Fisher

πŸ›
Martin Zagora
Martin Zagora

πŸ€” πŸ›
Victor Miti
Victor Miti

πŸ›
Stefan Huber
Stefan Huber

πŸ’¬ πŸ“–
Guilherme Ventura
Guilherme Ventura

πŸ€” πŸ’» πŸ›
Matt Hodges
Matt Hodges

πŸ›
Antoine Vendeville
Antoine Vendeville

πŸ›
Oleg Dutchenko
Oleg Dutchenko

πŸ›
Thomas
Thomas

πŸ€”
Troy Poulter
Troy Poulter

πŸ’» πŸ€” ⚠️
LiaoLiao
LiaoLiao

πŸ€”
David Mimnagh
David Mimnagh

πŸ€”
Micael Levi L. Cavalcante
Micael Levi L. Cavalcante

πŸ€” πŸ’» πŸ›
Richard Michael Coo
Richard Michael Coo

πŸ›
LetΓ­cia Vidal
LetΓ­cia Vidal

πŸ›
Yusuf
Yusuf

πŸ“–
Supun Tharinda Edirisuriya
Supun Tharinda Edirisuriya

πŸ“–
Nico Meyer
Nico Meyer

πŸ’» πŸ€”

This project follows the all-contributors specification. Contributions of any kind welcome!


License

Istanbul Badges Readme is MIT licensed.

istanbul-badges-readme's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar ic3m4n34 avatar leticiavna avatar micalevisk avatar olavoparno avatar troypoulter avatar venturalp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

istanbul-badges-readme's Issues

feature request: exit with non-zero code upon validation errors

Is your feature request related to a problem? Please describe.

Make the CLI exit with non-zero code if validation fails

Current behavior:

$ istanbul-badges-readme
Info: 0. Istanbul Badges Readme process started
Info: 1. Config check process started
- Readme file exists... βœ”οΈ.
Error: Coverage file does not exist in ./coverage/coverage-summary.json
Error: Please refer to the documentation
Error: https://github.com/olavoparno/istanbul-badges-readme/blob/master/README.md
Info: 0. Istanbul Badges Readme process finished
Done in 0.17s.
$ echo $?
0

Exiting with 0 prevent other tools (like GitHub Actions) to detect any validation errors of istanbul-badges-readme easily

Describe the solution you'd like

adding process.exitCode = 1; below will do the trick work but I'm pretty sure it will be a breaking change as some users could be relying on istanbul-badges-readme success exit code

https://github.com/olavoparno/istanbul-badges-readme/blob/805ab2e19931201fb80a949d1c9693ea29aac00d/src/factory.ts#L20-L24

thus, to avoid breaking changes, this feat could be optional and enabled via --exit-code flag, like git-diff do

Describe alternatives you've considered

In my use case, the coverage directory might not exists and I need to report this as an error. I'm doing the following:

$ [ -d './coverage' ] && istanbul-badges-readme || exit 1

but I think it would be cool if istanbul-badges-readme abstracts this for us :)

bug: getNewReadme fails on users with others process.argv items with 'ci' substring

Describe the bug
Hi! I was trying to run the script with npm following the steps in the documentation. However, it was always failing in this part of the process:

image

Since I'm not running with the ci flag or in a ci environment, i went into the codebase to search how was this error message thrown. I found that the getArgumentValue function searches a string with indexOf only:

https://github.com/olavoparno/istanbul-badges-readme/blob/eb418586322559b3a00f5cca9e73abda2edd9379/src/arguments.ts#L7-L12

The problem with this is that for users who have user names, or node path, or maybe any other line in the process.argv with the 'ci' substring, the item.indexof(argName) >= 0 returns true:

image

In my case, changing my user and nvm path worked.

To Reproduce
Steps to reproduce the behavior:

  1. Change or have a username/node path with 'ci' substring
  2. Try to run instanbul-badges-readme inside a repository
  3. Check error after editor process

Additional context
I was unsure if this is a node version related problem, but trying to run with v14.18.1 or 16.15.1 didnt work. Happy to help tho! Will submit a PR soon!

Initial replace

Wow! This tool is exactly what I needed. Thank you! There is one minor issue on the initial replace. Once I corrected it manually the parser updates it later correct.

Markdown

# Test

![Lines]

Check [here](https://github.com/olavoparno/istanbul-badges-readme) a cool tool.

first run

# Test

![Lines]
https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) a cool tool.

expected

# Test

![Lines](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg)

Check [here](https://github.com/olavoparno/istanbul-badges-readme) a cool tool.

doesReadmeHashExist doesn't take param --readmeDir

Describe the bug

Hello,

On my project I have 2 sub dir

- Matrix
| README.md
| - matrix-back
|        | - README.md
| - matrix-front
|        | - README.md

Each projects has a README.md.
I have a husky pre-hook in matrix-back who insert coverage into root folder.

If I don't have a statement into my matrix-back/README.md, the message Readme does not contain the needed hashes is displayed.

The issue is in the method validate.ts doesReadmeFileExist. The param doesn't take --readmeDir argument.

Additional context

> istanbul-badges-readme --readmeDir="./.."

Info: 0. Istanbul Badges Readme process started
Info: 1. Config check process started
- Readme file exists... βœ”οΈ.
- Coverage file exists... βœ”οΈ.
- Coverage hashes exist... βœ”οΈ.
readmePath= ./README.md
Error: Readme does not contain the needed hashes
Error: Please refer to the documentation

Thanks

Ability to add logo argument when calling the package

Is your feature request related to a problem? Please describe.
Nope, using it for jest coverage badges and would just like to add the Jest logo in

Describe the solution you'd like
Adding the jest logo into the resulting badge, so it appears like so:
image

Describe alternatives you've considered

Additional context

feature request: add custom --style argument

Is your feature request related to a problem? Please describe.
A badge has a few style: flat, plastic, flat-square, etc. Maybe it's a good choice to specify via --style custom argument.

flat-square:

flat:

Describe the solution you'd like
via --style argument

Describe alternatives you've considered

Additional context

fails to generate badge for 0% coverage

First of all, I'd like to thank you for this excellent package, this is absolutely great! Everything seems to work just fine, except I've noticed that if coverage is 0%, the badge isn't generated and hence substitution doesn't take place. For example, in my case, I had the following results:

=============================== Coverage summary ===============================
Statements   : 6.25% ( 1/16 )
Branches     : 0% ( 0/4 )
Functions    : 20% ( 1/5 )
Lines        : 7.14% ( 1/14 )
================================================================================
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        6.294s
Ran all test suites.

This resulted in

Statements Functions Lines Branches
Statements Functions Lines Branches

Thanks

How to create multiple badges for README.md

Hey!

I wanna create multiple coverage reports then I need two different markdown templates in README.md. How can I do that?

Example:

X Coverage Report Badges 

| Statements                                                               | Functions                                                                    | Lines                                                              | Code Quality                                                                                                                                                                                                                                  |
| ------------------------------------------------------------------------ | ---------------------------------------------------------------------------- | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ![Statements](https://img.shields.io/badge/Coverage-88.72%25-yellow.svg) | ![Functions](https://img.shields.io/badge/Coverage-91.94%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/Coverage-89.2%25-yellow.svg) | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/46159b0d28324e2485f32409d1b82960)](https://www.codacy.com?utm_source=gitlab.com&utm_medium=referral&utm_content=remoteteam/marketplace-core&utm_campaign=Badge_Grade) |

Y Coverage Report Badges

| Statements                                                               | Functions                                                                    | Lines                                                              | Code Quality                                                                                                                                                                                                                                  |
| ------------------------------------------------------------------------ | ---------------------------------------------------------------------------- | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ![Statements](https://img.shields.io/badge/Coverage-88.72%25-yellow.svg) | ![Functions](https://img.shields.io/badge/Coverage-91.94%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/Coverage-89.2%25-yellow.svg) | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/46159b0d28324e2485f32409d1b82960)](https://www.codacy.com?utm_source=gitlab.com&utm_medium=referral&utm_content=remoteteam/marketplace-core&utm_campaign=Badge_Grade) |

Error when running npm install with 1.2.2

Describe the bug

npm ERR! code ENOENT
npm ERR! syscall chmod
npm ERR! path /home/USER/PROJECT/node_modules/istanbul-badges-readme/lib/index.js
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, chmod '/home/USER/PROJECT/node_modules/istanbul-badges-readme/lib/index.js'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

To Reproduce

  1. Change dependency version from 1.2.1 to 1.2.2
  2. Run npm install

Additional context
1.2.1 works fine locally and in github actions. 1.2.2 fails in both environments.
Node v14.16.0
Npm 6.14.11

very small correction about README

I did run npm run istanbul-badges-readme after that readme says you can run npm run istanbul-badges-readme but I cannot. It says

npm ERR! Missing script: "istanbul-badges-readme"

This is a very small thing. Thanks for this great library!

bug: validation step ignore file permissions

Describe the bug

Even when the user doesn't have writting permission on the README file, logging saids everything went fine

To Reproduce

demo
demo.mp4

Additional context

The issue is that the following doesn't take account file permissions

https://github.com/olavoparno/istanbul-badges-readme/blob/e75dc4f33f60c4736625f7968871db2b59160d79/src/validate.ts#L8-L10

also, fs.exists has been deprecated in favor of fs.stat or fs.access

To fix that, I added the following line right before L10 from above:

fs.accessSync(getReadmePath(readmePath), fs.constants.R_OK | fs.constants.W_OK)

and got:

Info: 0. Istanbul Badges Readme process started
Info: 1. Config check process started
Error: Error: EACCES: permission denied, access './README.md'
Error: Please refer to the documentation
Error: https://github.com/olavoparno/istanbul-badges-readme/blob/master/README.md
Info: 0. Istanbul Badges Readme process finished

which is the expected output (but yeah, we should change that Error: Error: xD)

Option to use Alternative Titles as Badge Titles

As I don't really want to use the table layout but still need the context on which badge is which, using the already present alt attribute values (e.g. Statements, Branches, etc.) as titles for the badges (instead of Coverage for every badge) would be neat. 🐒

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.