mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 11:36:54 +00:00
Merge 7e1df7a0cb into b1a8062750
This commit is contained in:
commit
c770053448
2 changed files with 41 additions and 0 deletions
24
pre_commit_hooks/detect_datetime_now.py
Normal file
24
pre_commit_hooks/detect_datetime_now.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import os
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description='Check if datetime.now is being used.')
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
for filename in args.filenames:
|
||||
if os.path.isfile(filename):
|
||||
with open(filename, 'rb') as f:
|
||||
if b'datetime.now()' in f.read():
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
17
tests/detect_datetime_now_test.py
Normal file
17
tests/detect_datetime_now_test.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import pytest
|
||||
|
||||
from pre_commit_hooks.detect_datetime_now import main
|
||||
|
||||
TESTS = (
|
||||
('from datetime import datetime\ncurrent_date = datetime.now()', 1),
|
||||
('from datetime import datetime', 0),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('input_s', 'expected_return_value'), TESTS)
|
||||
def test_datetime_now_usage(input_s, expected_return_value, tmpdir):
|
||||
"""Test behavior with no datetime.now being used on code."""
|
||||
path = tmpdir.join('test_script.py')
|
||||
path.write(input_s)
|
||||
using_datetime = main([path.strpath])
|
||||
assert using_datetime == expected_return_value
|
||||
Loading…
Add table
Add a link
Reference in a new issue