Skip to content

Commit

Permalink
ported the core functionality to qiime.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcaporaso committed Apr 27, 2012
1 parent a616df6 commit 96816cc
Showing 1 changed file with 9 additions and 97 deletions.
106 changes: 9 additions & 97 deletions script_usage_tests.py
Expand Up @@ -12,25 +12,20 @@
__status__ = "Development"


from os.path import isdir, split, join, abspath
from os import chdir, getcwd
from shutil import copytree, rmtree
from glob import glob
from site import addsitedir
from sys import path
from cogent.util.misc import remove_files

from qiime.util import (parse_command_line_parameters,
make_option,
load_qiime_config,
qiime_system_call)
make_option, load_qiime_config)
from qiime.test import run_script_usage_tests


qiime_config = load_qiime_config()

script_info = {}
script_info['brief_description'] = ""
script_info['script_description'] = ""
script_info['script_usage'] = [("","","")]
script_info['script_usage'] = []
script_info['script_usage'].append(("Run a subset of the interface tests in verbose mode","Run interface tests for the add_taxa.py and make_otu_table.py scripts. This illustrates how to run from the qiime_test_dir directory.","%prog -i $PWD/ -l $HOME/qime_script_tests.log -t add_taxa,make_otu_table -v"))
script_info['script_usage'].append(("Run all of the interface tests","Run all script interface tests. This illustrates how to run from the qiime_test_dir directory.","%prog -i $PWD/ -l $HOME/all_qime_script_tests.log"))
script_info['output_description']= ""
script_info['required_options'] = [
make_option('-i','--qiime_test_data_dir',type="existing_dirpath",
Expand All @@ -50,99 +45,16 @@
]
script_info['version'] = __version__

def run_script_usage_tests(qiime_test_data_dir,
qiime_scripts_dir,
working_dir,
verbose=False,
tests=None,
failure_log_fp=None):
if tests == None:
tests = [split(d)[1] for d in glob('%s/*' % qiime_test_data_dir) if isdir(d)]
else:
tests = tests.split(',')

if verbose:
print 'Tests to run:\n %s' % ' '.join(tests)

addsitedir(qiime_scripts_dir)

failed_tests = []
warnings = []
total_tests = 0
for test in tests:

# import the usage examples - this is possible because we added
# qiime_scripts_dir to the PYTHONPATH above
script_fn = '%s/%s.py' % (qiime_scripts_dir,test)
script = __import__(test)
usage_examples = script.script_info['script_usage']

if verbose:
print 'Testing %d usage examples from: %s.py' % (len(usage_examples),script_fn)

# init the test environment
test_input_dir = '%s/%s' % (qiime_test_data_dir,test)
test_working_dir = '%s/%s' % (working_dir,test)
copytree(test_input_dir,test_working_dir)
chdir(test_working_dir)

# remove pre-exisitng output files if any
try:
script_usage_output_to_remove = script.script_info['script_usage_output_to_remove']
except KeyError:
script_usage_output_to_remove = []
for e in script_usage_output_to_remove:
rmtree(e.replace('$PWD',getcwd()),ignore_errors=True)

if verbose:
print ' Running tests in: %s' % getcwd()
print ' Tests:'

for usage_example in usage_examples:
if '%prog' not in usage_example[2]:
warnings.append('%s usage examples do not all use %%prog to represent the command name. You may not be running the version of the command that you think you are!' % test)
cmd = usage_example[2].replace('%prog',script_fn)
if verbose:
print ' %s' % cmd
stdout, stderr, return_value = qiime_system_call(cmd)
total_tests += 1
if return_value != 0:
failed_tests.append((cmd, stdout, stderr, return_value))

if verbose:
print ''

if failure_log_fp:
failure_log_f = open(failure_log_fp,'w')
if len(failed_tests) == 0:
failure_log_f.write('All tests passed.')
else:
i = 0
for cmd, stdout, stderr, return_value in failed_tests:
failure_log_f.write('**Failed test %d:\n%s\n\nReturn value: %d\n\nStdout:\n%s\n\nStderr:\n%s\n\n' % (i,cmd,return_value, stdout, stderr))
failure_log_f.close()


if warnings:
print 'Warnings:'
for warning in warnings:
print ' ' + warning
print ''

print 'Ran %d commands to test %d scripts. %d of these commands failed. Failures are summarized in %s.' % (total_tests,len(tests),len(failed_tests),failure_log_fp)

rmtree(working_dir)

def main():
option_parser, opts, args =\
parse_command_line_parameters(**script_info)

qiime_test_data_dir = abspath(opts.qiime_test_data_dir)
qiime_test_data_dir = opts.qiime_test_data_dir
qiime_scripts_dir = opts.qiime_scripts_dir
working_dir = join(opts.working_dir,'script_usage_tests')
working_dir = opts.working_dir
verbose = opts.verbose
tests = opts.tests
failure_log_fp = abspath(opts.failure_log_fp)
failure_log_fp = opts.failure_log_fp

run_script_usage_tests(qiime_test_data_dir,
qiime_scripts_dir,
Expand Down

0 comments on commit 96816cc

Please sign in to comment.