mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 11:36:54 +00:00
add Tests and cleanup testing issues
This commit is contained in:
parent
564a82ca89
commit
c78ebd0d85
4 changed files with 49 additions and 6 deletions
|
|
@ -147,3 +147,9 @@
|
|||
entry: trailing-whitespace-fixer
|
||||
language: python
|
||||
files: \.(asciidoc|adoc|coffee|cpp|css|c|ejs|erb|groovy|h|haml|hh|hpp|hxx|html|in|j2|jade|json|js|less|markdown|md|ml|mli|pp|py|rb|rs|R|scala|scss|sh|slim|tex|tmpl|ts|txt|yaml|yml)$
|
||||
- id: no-commit-to-branch
|
||||
name: Dont commit to branch
|
||||
entry: no-commit-to-branch
|
||||
language: python
|
||||
files: .*
|
||||
always_run: true
|
||||
|
|
|
|||
|
|
@ -56,6 +56,8 @@ Add this to your `.pre-commit-config.yaml`
|
|||
- `forbid-new-submodules` - Prevent addition of new git submodules.
|
||||
- `name-tests-test` - Assert that files in tests/ end in `_test.py`.
|
||||
- Use `args: ['--django']` to match `test*.py` instead.
|
||||
- `no-commit-to-branch` - Protect specific branches from direct checkins.
|
||||
- Use `args: -b <branch> ` to set the branch. `master` is the default if no argument is set.
|
||||
- `pyflakes` - Run pyflakes on your python files.
|
||||
- `pretty-format-json` - Checks that all your JSON files are pretty. "Pretty"
|
||||
here means that keys are sorted and indented. You can configure this with
|
||||
|
|
|
|||
|
|
@ -5,6 +5,13 @@ import sys
|
|||
|
||||
import util
|
||||
|
||||
def is_on_branch(protected):
|
||||
retval = False
|
||||
branch = util.cmd_output('git', 'symbolic-ref', 'HEAD')
|
||||
chunks = branch.strip().split('/')
|
||||
if chunks[2] == protected:
|
||||
retval = True
|
||||
return retval
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
|
|
@ -12,12 +19,10 @@ def main(argv=None):
|
|||
parser.add_argument('filenames', nargs='*', help='filenames to check.')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retval = 0
|
||||
branch = util.cmd_output('git', 'symbolic-ref', 'HEAD')
|
||||
chunks = branch.strip().split('/')
|
||||
if chunks[2] == args.b:
|
||||
retval = -1
|
||||
return retval
|
||||
if is_on_branch(args.b):
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
30
tests/check_no_commit_to_branch_test.py
Normal file
30
tests/check_no_commit_to_branch_test.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.no_commit_to_branch import is_on_branch
|
||||
from pre_commit_hooks.no_commit_to_branch import main
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
||||
def test_other_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
||||
assert is_on_branch('master') == False
|
||||
|
||||
def test_master_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
assert is_on_branch('master') == True
|
||||
|
||||
def test_main_other_call(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'other')
|
||||
assert main(['-b','other']) == 1
|
||||
|
||||
def test_main_default_call(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
||||
assert main() == 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue