Created
August 14, 2013 15:48
-
-
Save andresriancho/6232343 to your computer and use it in GitHub Desktop.
Test runner for circleci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
import os | |
import subprocess | |
TEST_RUNNER = 'nosetests' | |
TEST_ARGS = ['-s', '-v'] | |
CRAWL_EXCLUSIONS = ('./build', './.git', './venv',) | |
def find_tests(ci_args): | |
''' | |
:return: A list of filenames which match test_....py where "..." is one | |
of the ci_args. | |
''' | |
base_dir = '.' | |
test_list = [] | |
filtered_tests = [] | |
os.path.walk(base_dir, _process_directory, test_list) | |
for test_name in test_list: | |
for ci_arg in ci_args: | |
if 'test_%s.py' % ci_arg in test_name: | |
filtered_tests.append(test_name) | |
return filtered_tests | |
def _process_directory(test_list, dirname, filenames): | |
for excluded_path in CRAWL_EXCLUSIONS: | |
if dirname.startswith(excluded_path): | |
return | |
for filename in filenames: | |
if not filename.endswith('.py'): | |
continue | |
if not filename.startswith('test_'): | |
continue | |
test_list.append(os.path.join(dirname, filename)) | |
def default_run(ci_args): | |
''' | |
:param ci_args: The arguments you pass in [ci ...] , for example if in the | |
commit log you enter [ci foo] ci_args will have a value of | |
['foo'] | |
:return: The return-code to send to the OS | |
If you follow the usual test filename naming format of 'test_*.py', | |
this default runner will find all files named 'test_foo.py' and run them. | |
''' | |
cmd = [TEST_RUNNER,] | |
cmd.extend(TEST_ARGS) | |
cmd.extend(find_tests(ci_args)) | |
cmd = ' '.join(cmd) | |
print '=' * 60 | |
print 'ci_args: %s' % ci_args | |
print 'tests: %s' % find_tests(ci_args) | |
print 'command: %s' % cmd | |
print '=' * 60 | |
proc = subprocess.Popen(cmd, shell=True) | |
proc.wait() | |
return proc.returncode | |
def parse_gitlog(): | |
''' | |
:return: A list with the arguments passed in [ci ...], for example if you | |
enter something like [ci abc def] you'll get an output of | |
['abc', 'def'] | |
''' | |
log = subprocess.check_output('git log -1', shell=True) | |
mo = re.search('\[ci (.*?)\]', log) | |
if not mo: | |
return [] | |
return mo.group(1).split(' ') | |
if __name__ == '__main__': | |
''' | |
This is a custom test runner for circleci which parses any [ci ...] tags | |
you define, and runs specific commands based on it. | |
''' | |
ci_args = parse_gitlog() | |
return_code = default_run(ci_args) | |
sys.exit(return_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment