0

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?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Manoj Dhake
  • 227
  • 4
  • 16

1 Answers1

0

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.

alexis
  • 48,685
  • 16
  • 101
  • 161
  • you got any reference link? – Manoj Dhake Jun 15 '22 at 08:26
  • This approach looks good to me. Thanks @alexis. Adding the configuration here which i have added for executing custom script through .pre-commit.yaml file. - repo: local hooks: - id: custom-script-file name: custom-script-file entry: run_pytest.sh language: script types: [python] pass_filenames: false – Manoj Dhake Jun 15 '22 at 12:06