File: all_tests.py

package info (click to toggle)
pynast 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 776 kB
  • ctags: 308
  • sloc: python: 2,290; makefile: 92; sh: 10
file content (100 lines) | stat: -rwxr-xr-x 3,916 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python

#-----------------------------------------------------------------------------
# Copyright (c) 2013, The PyNAST Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

from os import walk, environ
from subprocess import Popen, PIPE, STDOUT
from os.path import join, abspath, dirname, split
from glob import glob
import re

def main():
    pynast_dir = abspath(join(dirname(__file__),'..'))
    test_dir = join(pynast_dir,'tests')
    scripts_dir = join(pynast_dir,'scripts')

    unittest_good_pattern = re.compile('OK\s*$')
    application_not_found_pattern = re.compile('ApplicationNotFoundError')
    python_name = 'python'
    bad_tests = []
    missing_application_tests = []

    # Run through all of PyNAST's unit tests, and keep track of any files which
    # fail unit tests.

    unittest_names = []

    for root, dirs, files in walk(test_dir):
        for name in files:
            if name.startswith('test_') and name.endswith('.py'):
                unittest_names.append(join(root,name))

    unittest_names.sort()

    for unittest_name in unittest_names:
        print "Testing %s:\n" % unittest_name
        command = '%s %s -v' % (python_name, unittest_name)
        result = Popen(command,shell=True,universal_newlines=True,\
                       stdout=PIPE,stderr=STDOUT).stdout.read()
        print result
        if not unittest_good_pattern.search(result):
            if application_not_found_pattern.search(result):
                missing_application_tests.append(unittest_name)
            else:
                bad_tests.append(unittest_name)


    # Run through all of PyNAST's scripts, and pass -h to each one. If the
    # resulting stdout does not being with the Usage text, that is an 
    # indicator of something being wrong with the script. Issues that would
    # cause that are bad import statements in the script, SyntaxErrors, or 
    # other failures prior to running parse_command_line_parameters.

    script_names = []
    script_names = glob('%s/*' % scripts_dir)
    script_names.sort()
    bad_scripts = []

    for script_name in script_names:
        script_good_pattern = re.compile('^Usage: %s' % split(script_name)[1])
        print "Testing %s." % script_name
        command = '%s %s -h' % (python_name, script_name)
        result = Popen(command,shell=True,universal_newlines=True,\
                       stdout=PIPE,stderr=STDOUT).stdout.read()
        # remove warning cause by missing python-mpi4py
        result = re.sub(r'(?m)^/usr/lib.*Not using MPI as mpi4py not found\n?', '', result)
        result = re.sub(r'(?m)^\s*from cogent.util.parallel import MPI\n?', '', result)
        if not script_good_pattern.search(result):
            bad_scripts.append(script_name)

    if bad_tests:
        print "\nFailed the following unit tests.\n%s" % '\n'.join(bad_tests)
        
    if missing_application_tests:
        print "\nFailed the following unit tests, in part or whole due "+\
        "to missing external applications.\nDepending on the QIIME features "+\
        "you plan to use, this may not be critical.\n%s"\
         % '\n'.join(missing_application_tests)
         
    if bad_scripts:
        print "\nFailed the following script tests.\n%s" % '\n'.join(bad_scripts)

    # If any of the unit tests or script tests failed, or if we have any
    # missing application errors, use return code 1 (as python's
    # unittest module does) to indicate one or more failures with the test
    # suite.
    return_code = 1
    if not (bad_tests or missing_application_tests or bad_scripts):
        print "\nAll tests passed successfully."
        return_code = 0
    return return_code


if __name__ == "__main__":
    exit(main())