Skip to content

Commit 7d15fd6

Browse files
committed
Test for sort imports
Sorting imports has been an unwritten coding style of our codebase. This commit makes it into a test rule. Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent 8d66f64 commit 7d15fd6

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ VENV=venv
2727
ACTIVATE?=. ${VENV}/bin/activate;
2828
VIRTUALENV_PYZ=etc/thirdparty/virtualenv.pyz
2929
BLACK_ARGS=-l 100 .
30+
ISORT_ARGS=.
3031
# Do not depend on Python to generate the SECRET_KEY
3132
GET_SECRET_KEY=`base64 /dev/urandom | head -c50`
3233
# Customize with `$ make envfile ENV_FILE=/etc/vulnerablecode/.env`
@@ -61,14 +62,20 @@ envfile:
6162
@echo SECRET_KEY=\"${GET_SECRET_KEY}\" > ${ENV_FILE}
6263

6364
check:
65+
@echo "-> Run isort validation"
66+
@${ACTIVATE} isort --check-only ${ISORT_ARGS}
6467
@echo "-> Run black validation"
6568
@${ACTIVATE} black --check ${BLACK_ARGS}
6669

6770
black:
6871
@echo "-> Apply black code formatter"
6972
${VENV}/bin/black ${BLACK_ARGS}
7073

71-
valid: black
74+
isort:
75+
@echo "-> Apply isort code formatter"
76+
${VENV}/bin/isort ${ISORT_ARGS}
77+
78+
valid: isort black
7279

7380
clean:
7481
@echo "-> Clean the Python env"

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ addopts = [
4343
"-rfExXw",
4444
"--strict",
4545
"--doctest-modules"
46-
]
46+
]
47+
48+
[tool.isort]
49+
profile = "black"
50+
line_length = 100
51+
force_single_line = true
52+
skip_gitignore = true

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ django-environ==0.4.5
1919
defusedxml==0.7.1
2020

2121
Markdown==3.3.4
22+
isort==5.7.0

vulnerabilities/tests/test_basics.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
2222

23-
from os.path import dirname
24-
from os.path import join
2523
import subprocess
2624
import sys
2725
import unittest
26+
from os.path import dirname, join
2827

2928
root_dir = dirname(dirname(dirname(__file__)))
3029
bin_dir = dirname(sys.executable)
@@ -36,4 +35,16 @@ def test_codestyle(self):
3635
try:
3736
subprocess.check_output(args.split(), cwd=root_dir)
3837
except Exception as e:
39-
raise Exception(f"Black style check failed, please format the code using black") from e
38+
raise Exception(
39+
"Black style check failed, please format the code using black -l 100 . "
40+
"Alternatively, run ``make valid``"
41+
) from e
42+
43+
args = join(bin_dir, "isort --check-only .")
44+
try:
45+
subprocess.check_output(args.split(), cwd=root_dir)
46+
except Exception as e:
47+
raise Exception(
48+
"Unsorted imports, please sort your imports using isort. "
49+
"Alternatively, run ``make valid``"
50+
) from e

0 commit comments

Comments
 (0)