Giter Site home page Giter Site logo

commit-hooks's Introduction

Testing & Commits

Table of Contents

Unit Testing

Commits

Pre-commit hooks

Pre-commit hooks will allow us to run something before a commit. In our case, we will be running pre-commit hooks to format code, lint code, and run unit tests. If any of the unit tests fail, the commit will not be able to finish. If your test(s) fail, fix them until they pass, then commit your changes.

  • Python

    • pre-commit
      • changes must be staged before pre-commit hooks can run
      • configurable to run post-commits also
  • JavaScript

Commits will be linted. If your commit message does not follow conventional commits, then you will receive an error. Here is conventional commits' specifications and guidelines for commit messages. Here is their github page.

Commitlint

Ex:

module.exports = {
  extends: ["@commitlint/config-conventional"],
  rules: {
    "type-enum": [
      2, // level: 0 - disable, 1 - warning, 2 - error
      "always", // applicable: always | never
      [
        "ci",
        "chore",
        "docs",
        "feat",
        "fix",
        "perf",
        "refactor",
        "revert",
        "style",
        "test",
      ],
    ],
  },
}

Test commitlint configs

echo "foo: test" | npx commitlint -V

Steps to set-up

  • Python

    1. Set-up your virtual environment if you have not already. You can use the requirements.txt found in the python directory here.
    2. Install pre-commit. Instructions here. Follow the quick start until you finish the "Quick start" section.
    3. Install commitlint. Instructions here. Configure commitlint via .commitlintrc.json.
    # Install commitlint cli and conventional config
    npm install --save-dev @commitlint/{config-conventional,cli}
    # For Windows:
    npm install --save-dev @commitlint/config-conventional @commitlint/cli
    
    # Configure commitlint to use conventional config
    echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
    1. Write your code and unit tests. Refer to /python/hello_world.py and /python/test_hello_world.py files. Basic explanation video for unittest in References.
    2. Configure your pre-commit-config.yaml to include commitlint and unit testing. Refer to the pre-commit-config.yaml in this repo. Documentation to set-up commitlint in pre-commit. Optional: add formatter (autopep8, black, etc) or linting (pylint, pylance, etc) to your pre-commit-config.yaml as well. This will only work if the directory has been initialized as a Git repo.
    3. Stage your files and commit using conventional commit messages.
  • JavaScript

    1. Install the correct packages
    npm install --save-dev jest husky @commitlint/cli @commitlint/config-conventional
    1. Set-up your scripts in your package.json. Make sure you have a prepare script for installing husky and test script for unit testing via jest.
     "scripts": {
      "prepare": "husky install",
      "test": "jest"
    }
    1. Activate husky hooks. This will only work if the directory has been initialized as a Git repo.
    npm run prepare
    1. Add test hook and commitlint to husky. Configure your /javascript/.commitlintrc.json to match your Python's.
    # Add test hook
    npx husky add .husky/pre-commit "npm test"
    
    # Add commitlint
    npx husky add .husky/commit-msg 'npx --no-install commitlint --edit '
    1. Write your code and unit tests. Refer to /javascript/hello-world.js and /javascript/__tests__/hello-world.test.js.
    2. Stage your files and commit using using conventional commit messages.

References

  • Unit testing video for Python using unittest

Glossary

lint

Lint, or a linter, is a static code analysis tool used to flag programming errors, bugs, stylistic errors, and suspicious constructs.

hook

Hooks allow us to do something and/or run code (or script) before something else.

commit-hooks's People

Watchers

James Cloos avatar Kendy Nguyen avatar

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.