Search code examples
pythongitpre-commit-hookpre-commit.com

How to keep committing the code to github even after pytest-check hook fails?


Below is my .pre-commit-config.yaml file for my project.

# See https://pre-commit.com/hooks.html for more hooks
fail_fast: true
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.2.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
    -   id: check-added-large-files

-   repo: local
    hooks:
      - id: isort
        name: isort
        entry: isort
        language: python
        types: [python]
-   repo: local
    hooks:
    -   id: black
        name: black
        entry: black
        language: python
        types: [python]

-   repo: local
    hooks:
      - id: pytest-check
        name: pytest-check
        #entry: pytest tests/test_file2.py
        entry: pytest
        language: python
        #args: [--maxfail=1]
          #stages: [post-commit]
        pass_filenames: false
        always_run: false

I would like to achieve two things as described below.

  1. Sometimes, developer modify the business logic and test cases too and those gets fail due to some reason.And, if test cases keep on failing continuously then there is risk of loosing the code because pre-commit won't allow to commit the changes until all checks are passed. Hence, we should always want to be able to commit to feature branches even though test cases fail. Note : Please keep in mind that, I would like to achieve above scenario for pytest-check hook and not for other hooks.
  2. As per my current configuration of pre-commit-config.yaml file, it executes complete test suit. But i wants to execute it for specific test case file.

FYI. - I have already explored one approach to bypass pre-commit but it's applicable to all hook mentioned in the .pre-commit-config.yaml file.

How can we achieve all two scenario? Please suggest your input on the same?


Solution

  • Make your test into a shell script that executes, in effect,

    pytest tests/test_file2.py (or whatever you are doing now)
    true
    

    The last command could also be exit 0, or anything else that exits successfully.) That way the tests will run, with the side effects and output that you have now, but git will always treat them as successful.