2016-09-19 21:41:01 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-09-29 19:38:23 +00:00
|
|
|
from __future__ import print_function
|
2016-09-19 21:41:01 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
test_git_lint
|
|
|
|
----------------------------------
|
|
|
|
|
|
|
|
Tests for `git_lint` module.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import pytest
|
2016-09-29 19:38:23 +00:00
|
|
|
import copy
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import pprint
|
2016-09-29 22:14:46 +00:00
|
|
|
import tempfile
|
2016-09-19 21:41:01 +00:00
|
|
|
from git_lint import git_lint
|
|
|
|
|
2016-09-29 19:38:23 +00:00
|
|
|
environment = copy.copy(os.environ)
|
|
|
|
environment.update({
|
|
|
|
'LANG': 'C',
|
|
|
|
'LC_ALL': 'C',
|
|
|
|
'PAGER': 'cat',
|
|
|
|
'TZ': 'UTC',
|
|
|
|
'EDITOR': ':',
|
|
|
|
'GIT_AUTHOR_EMAIL': 'author@example.com',
|
|
|
|
'GIT_AUTHOR_NAME': '"A U Thor"',
|
|
|
|
'GIT_COMMITTER_EMAIL': 'committer@example.com',
|
|
|
|
'GIT_COMMITTER_NAME': '"C O Mitter"',
|
|
|
|
'GIT_MERGE_VERBOSITY': '5',
|
|
|
|
'GIT_MERGE_AUTOEDIT': 'no'
|
|
|
|
})
|
|
|
|
|
|
|
|
for key in ['XDF_CONFIG_HOME', 'GITPERLLIB', 'CDPATH',
|
|
|
|
'GREP_OPTIONS', 'UNZIP']:
|
|
|
|
environment.pop(key, None)
|
|
|
|
|
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
git_lint_src = """
|
|
|
|
[pep8]
|
|
|
|
comment = PEP8 with some white space and line length checking turned off
|
|
|
|
output = Running pep8...
|
|
|
|
command = pep8 -r --ignore=E501,W293,W391
|
|
|
|
match = .py
|
|
|
|
print = False
|
|
|
|
condition = error
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Basic TOX settings aren't good enough: we need to have something more or less guaranteed
|
|
|
|
# to not have a '.git' directory somewhere lurking in a parent folder.
|
|
|
|
|
|
|
|
class gittemp:
|
|
|
|
def __enter__(self):
|
|
|
|
self.cwd = os.getcwd()
|
|
|
|
self.path = tempfile.mkdtemp()
|
|
|
|
return self.path
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
os.chdir(self.cwd)
|
|
|
|
shutil.rmtree(self.path)
|
|
|
|
|
|
|
|
|
|
|
|
def test_01_not_a_repository():
|
|
|
|
with gittemp() as path:
|
|
|
|
os.chdir(path)
|
|
|
|
p = subprocess.Popen('git lint', shell=True, env=environment,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = p.communicate()
|
|
|
|
assert stderr.startswith('A git repository was not found')
|
|
|
|
|
2016-09-19 21:41:01 +00:00
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
def test_02_empty_repository():
|
|
|
|
with gittemp() as path:
|
|
|
|
os.chdir(path)
|
|
|
|
subprocess.check_call('git init', shell=True, env=environment)
|
|
|
|
p = subprocess.Popen('git lint -l', shell=True, env=environment,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = p.communicate()
|
|
|
|
assert stderr.startswith('No configuration file found,')
|
2016-09-29 19:38:23 +00:00
|
|
|
|
2016-09-19 21:41:01 +00:00
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
def test_03_simple_repository():
|
|
|
|
with gittemp() as path:
|
|
|
|
os.chdir(path)
|
|
|
|
with open(".git-lint", "w") as f:
|
|
|
|
f.write(git_lint_src)
|
|
|
|
subprocess.check_call('git init', shell=True, env=environment)
|
|
|
|
subprocess.check_call('git add .', shell=True, env=environment)
|
|
|
|
subprocess.check_call('git commit -m "Test."', shell=True, env=environment)
|
2016-09-29 19:38:23 +00:00
|
|
|
ret = subprocess.check_output('git lint -v', shell=True, env=environment)
|
2016-09-29 22:14:46 +00:00
|
|
|
assert ret.index('Copyright') > 0
|
2016-09-19 21:41:01 +00:00
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
def test_04_linters_present():
|
|
|
|
with gittemp() as path:
|
|
|
|
os.chdir(path)
|
|
|
|
with open(".git-lint", "w") as f:
|
|
|
|
f.write(git_lint_src)
|
|
|
|
subprocess.check_call('git init', shell=True, env=environment)
|
|
|
|
subprocess.check_call('git add .', shell=True, env=environment)
|
|
|
|
subprocess.check_call('git commit -m "Test."', shell=True, env=environment)
|
|
|
|
ret = subprocess.check_output('git lint -l', shell=True, env=environment)
|
|
|
|
assert len(ret.split("\n")) == 3
|
|
|
|
assert ret.index('pep8') > 0
|
2016-09-19 21:41:01 +00:00
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
|