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 22:47:36 +00:00
|
|
|
# Set up the environment to closely match the one cgit uses for its own unit testing.
|
|
|
|
|
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'
|
|
|
|
})
|
|
|
|
|
2016-09-29 22:47:36 +00:00
|
|
|
for key in ['XDF_CONFIG_HOME', 'GITPERLLIB', 'CDPATH',
|
2016-09-29 19:38:23 +00:00
|
|
|
'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
|
|
|
|
"""
|
|
|
|
|
2016-09-29 22:47:36 +00:00
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
# 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.
|
|
|
|
|
2016-09-29 22:27:33 +00:00
|
|
|
def shell(cmd, environment=environment):
|
|
|
|
return subprocess.check_call(cmd, shell=True, env=environment)
|
|
|
|
|
2016-09-29 22:47:36 +00:00
|
|
|
|
2016-09-29 22:27:33 +00:00
|
|
|
def outshell(cmd, environment=environment):
|
|
|
|
return subprocess.check_output(cmd, shell=True, env=environment)
|
|
|
|
|
2016-09-29 22:47:36 +00:00
|
|
|
|
|
|
|
def fullshell(cmd, environment=environment):
|
2016-09-29 22:27:33 +00:00
|
|
|
process = subprocess.Popen(cmd, shell=True, env=environment,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = process.communicate()
|
|
|
|
return (stdout, stderr, process.returncode)
|
|
|
|
|
2016-09-29 22:47:36 +00:00
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
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)
|
2016-09-29 22:27:33 +00:00
|
|
|
(stdout, stderr, rc) = fullshell('git lint')
|
2016-09-29 22:14:46 +00:00
|
|
|
assert stderr.startswith('A git repository was not found')
|
|
|
|
|
2016-09-19 21:41:01 +00:00
|
|
|
|
2016-09-29 22:47:36 +00:00
|
|
|
# The important aspect here is that it SHOULD NOT CRASH even though
|
|
|
|
# there is no HEAD repository and no configuration file. It should
|
|
|
|
# report the lack of a configuration file without blinking.
|
|
|
|
|
2016-09-29 22:14:46 +00:00
|
|
|
def test_02_empty_repository():
|
|
|
|
with gittemp() as path:
|
|
|
|
os.chdir(path)
|
2016-09-29 22:27:33 +00:00
|
|
|
shell('git init')
|
2016-09-29 22:47:36 +00:00
|
|
|
(stdout, stderr, rc) = fullshell('git lint')
|
2016-09-29 22:14:46 +00:00
|
|
|
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)
|
2016-09-29 22:27:33 +00:00
|
|
|
shell('git init && git add . && git commit -m "Test"')
|
|
|
|
ret = outshell('git lint -v')
|
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:47:36 +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)
|
2016-09-29 22:27:33 +00:00
|
|
|
shell('git init && git add . && git commit -m "Test"')
|
|
|
|
ret = outshell('git lint -l')
|
2016-09-29 22:14:46 +00:00
|
|
|
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
|
|
|
|