Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup environment for pre-commit

How can I source a bash script that contains exports for environment variables?

env.sh

...
export VARIABLE=test
...

test_file.py

...
os.environ['VARIABLE'] # Throws KeyError
...

How can I use pre-commit to run env.sh to setup an environment that the following pytest hook can use?

-   repo: local
    hooks:
    -   id: tests
        name: run tests
        entry: pytest -v
        language: system
        types: [python]
like image 976
user1222324562 Avatar asked Apr 10 '26 18:04

user1222324562


1 Answers

pre-commit-config

  • change language to python (optional, but it's so that pre-commit creates a venv)
  • entry - we will invoke a bash script instead of invoking pytest directly

Ex.

 - repo: local
   hooks:
   - id: tests
     name: run tests
     entry: bash tests.sh
     language: python

tests.sh

source env.sh
pytest -v .
like image 146
SW_user2953243 Avatar answered Apr 13 '26 11:04

SW_user2953243



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!