add Tests and cleanup testing issues

This commit is contained in:
Evan J Felix 2017-03-17 13:10:51 -07:00
parent 564a82ca89
commit c78ebd0d85
4 changed files with 49 additions and 6 deletions

View file

@ -147,3 +147,9 @@
entry: trailing-whitespace-fixer entry: trailing-whitespace-fixer
language: python 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)$ 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

View file

@ -56,6 +56,8 @@ Add this to your `.pre-commit-config.yaml`
- `forbid-new-submodules` - Prevent addition of new git submodules. - `forbid-new-submodules` - Prevent addition of new git submodules.
- `name-tests-test` - Assert that files in tests/ end in `_test.py`. - `name-tests-test` - Assert that files in tests/ end in `_test.py`.
- Use `args: ['--django']` to match `test*.py` instead. - 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. - `pyflakes` - Run pyflakes on your python files.
- `pretty-format-json` - Checks that all your JSON files are pretty. "Pretty" - `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 here means that keys are sorted and indented. You can configure this with

View file

@ -5,6 +5,13 @@ import sys
import util 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): def main(argv=None):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -12,12 +19,10 @@ def main(argv=None):
parser.add_argument('filenames', nargs='*', help='filenames to check.') parser.add_argument('filenames', nargs='*', help='filenames to check.')
args = parser.parse_args(argv) args = parser.parse_args(argv)
retval = 0 if is_on_branch(args.b):
branch = util.cmd_output('git', 'symbolic-ref', 'HEAD') return 1
chunks = branch.strip().split('/') else:
if chunks[2] == args.b: return 0
retval = -1
return retval
if __name__ == '__main__': if __name__ == '__main__':

View 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