Skip to content

Commit

Permalink
Fix failing pre-commit hook fails on directories (#17)
Browse files Browse the repository at this point in the history
* Add test for get flagged lines

* Skip PII flag check for directories
  • Loading branch information
letmerecall authored Feb 7, 2023
1 parent 3c439aa commit 3db5d4a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pii_check/pii_check_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_payload(content, enabled_entity_list, blocked_list):
def get_flagged_lines(files):
flagged = []
for file in files:
if os.path.exists(file):
if os.path.exists(file) and not os.path.isdir(file):
with open(file, "r") as fp:
lines = fp.readlines()
start_flag = False
Expand Down
4 changes: 4 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest==7.2.1
pytest-check==2.1.2
python-dotenv==0.19.0
requests==2.28.1
Empty file added tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/test_data/dir_with_files/file_with_pii.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Credit card number: 1234 5678 9101 1123
10 changes: 10 additions & 0 deletions tests/test_data/dir_with_files/file_with_pii_flag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PII_CHECK:OFF
Some content in between the flags. Ideally this content won't be checked for PII.
Below is a dummy PII to check this
Credit card number: 1234 5678 9101 1123
CVV: 123
PII_CHECK:ON

Some content where the check will be performed.
Credit card number: 1234 5678 9101 1123
CVV: 123
1 change: 1 addition & 0 deletions tests/test_data/dir_with_files/file_without_pii.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here's some content.
1 change: 1 addition & 0 deletions tests/test_data/symlink_of_dir_with_files
13 changes: 13 additions & 0 deletions tests/test_get_flagged_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest_check as check
from pii_check.pii_check_hook import get_flagged_lines


def test_get_flagged_lines():
files = [
"tests/test_data/dir_with_files/file_with_pii.txt", "tests/test_data/dir_with_files/file_without_pii.txt",
"tests/test_data/dir_with_files/file_with_pii_flag_on", "tests/test_data/dir_with_files/file_with_pii_flag_off",
"tests/test_data/dir_with_files/file_with_pii_flag", "tests/test_data/symlink_of_dir_with_files"
]
res = get_flagged_lines(files)
check.equal(res, [(1, 6, 'tests/test_data/dir_with_files/file_with_pii_flag')])

0 comments on commit 3db5d4a

Please # to comment.