Skip to content

Commit

Permalink
Merge pull request mandiant#847 from Arker123/pre-commit
Browse files Browse the repository at this point in the history
Implement pre-commit for linting automation mandiant#825
  • Loading branch information
williballenthin committed Aug 1, 2023
2 parents 79809d2 + de76629 commit b65259e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
- name: Install dependencies
run: pip install -e .[dev]
- name: Lint with isort
run: isort --profile black --length-sort --line-width 120 -c .
run: pre-commit run isort
- name: Lint with black
run: black -l 120 --check .
run: pre-commit run black
- name: Check types with mypy
run: mypy --config-file .github/mypy/mypy.ini --check-untyped-defs floss/ scripts/ tests/
run: pre-commit run mypy

tests:
name: Tests in ${{ matrix.python-version }} on ${{ matrix.os }}
Expand Down
59 changes: 59 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
repos:
- repo: local
hooks:
- id: isort
name: isort
stages: [commit, push]
language: system
entry: isort
args:
- "--length-sort"
- "--profile"
- "black"
- "--line-length=120"
- "."
always_run: true
pass_filenames: false

- repo: local
hooks:
- id: black
name: black
stages: [commit, push]
language: system
entry: black
args:
- "--line-length=120"
- "."
always_run: true
pass_filenames: false

- repo: local
hooks:
- id: mypy
name: mypy
stages: [push]
language: system
entry: mypy
args:
- "--check-untyped-defs"
- "--config-file=.github/mypy/mypy.ini"
- "floss/"
- "scripts/"
- "tests/"
always_run: true
pass_filenames: false

- repo: local
hooks:
- id: pytest
name: pytest
stages: []
language: system
pass_filenames: false
always_run: true
entry: pytest
args:
- "--verbose"
- "tests/"

29 changes: 29 additions & 0 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,35 @@ Or use the manual option:
- `$ cd /local/path/to/src`
- `$ git submodule update --init tests/data`

We use the following tools to ensure consistent code style and formatting:

- [black](https://github.com/psf/black) code formatter
- [isort](https://pypi.org/project/isort/) code formatter
- [mypy](https://mypy-lang.org/) type checking

We use [pre-commit](https://pre-commit.com/) so that its trivial to run the same linters & configuration locally as in CI.

Run all linters liks:
❯ pre-commit run --all-files
isort....................................................................Passed
black....................................................................Passed
mypy.....................................................................Passed

Or run a single linter like:
❯ pre-commit run --all-files isort
isort....................................................................Passed

Importantly, you can configure pre-commit to run automatically before every commit by running:

❯ pre-commit install --hook-type pre-commit
pre-commit installed at .git/hooks/pre-commit

❯ pre-commit install --hook-type pre-push
pre-commit installed at .git/hooks/pre-push

This way you can ensure that you don't commit code style or formatting offenses.
You can always temporarily skip the checks by using the `-n`/`--no-verify` git option.


### Step 4: Building standalone executables

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
install_requires=requirements,
extras_require={
"dev": [
"pre-commit==2.21.0",
"pyyaml==6.0.1",
"pytest==7.4.0",
"pytest-sugar==0.9.4",
Expand Down

0 comments on commit b65259e

Please sign in to comment.