# Path: picrust/util.py
# def get_picrust_project_dir():
#     """ Returns the top-level PICRUST directory
#     """
#     # Get the full path of util.py
#     current_file_path = abspath(__file__)
#     # Get the directory containing util.py
#     current_dir_path = dirname(current_file_path)
#     # Return the directory containing the directory containing util.py
#     return dirname(current_dir_path)
# 
# def system_call(cmd, shell=True):
#     """Call cmd and return (stdout, stderr, return_value).
# 
#     cmd can be either a string containing the command to be run, or a sequence
#     of strings that are the tokens of the command.
# 
#     Please see Python's subprocess.Popen for a description of the shell
#     parameter and how cmd is interpreted differently based on its value.
# 
#     This code was copied from QIIME's qiime_system_call() (util.py) function on June 3rd, 2013.
#     """
#     proc = Popen(cmd, shell=shell, universal_newlines=True, stdout=PIPE,
#                  stderr=PIPE)
#     # communicate pulls all stdout/stderr from the PIPEs to
#     # avoid blocking -- don't remove this line!
#     stdout, stderr = proc.communicate()
#     return_value = proc.returncode
#     return stdout, stderr, return_value


from os import walk, environ
from sys import exit
from os.path import join, abspath, dirname, exists, split
from glob import glob
from picrust.util import get_picrust_project_dir, system_call
from cogent.util.option_parsing import parse_command_line_parameters, make_option
            from qiime.test import run_script_usage_tests
import re
import cogent.util.unit_test as unittest
       option_parser.error("You're suppressing both test types. Nothing to run.")

    test_dir = abspath(dirname(__file__))

    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 PICRUSt's unit tests, and keep track of any files which
    # fail unit tests.
    if not opts.suppress_unit_tests:
        unittest_names = []
        if not opts.unittest_glob:
            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))
        else:
            for fp in glob(opts.unittest_glob):
                fn = split(fp)[1]
                if fn.startswith('test_') and fn.endswith('.py'):
                    unittest_names.append(abspath(fp))

        unittest_names.sort()

        for unittest_name in unittest_names:
            print "Testing %s:\n" % unittest_name
            command = '%s %s -v' % (python_name, unittest_name)

# Path: picrust/util.py
# def get_picrust_project_dir():
#     """ Returns the top-level PICRUST directory
#     """
#     # Get the full path of util.py
#     current_file_path = abspath(__file__)
#     # Get the directory containing util.py
#     current_dir_path = dirname(current_file_path)
#     # Return the directory containing the directory containing util.py
#     return dirname(current_dir_path)
# 
# def system_call(cmd, shell=True):
#     """Call cmd and return (stdout, stderr, return_value).
# 
#     cmd can be either a string containing the command to be run, or a sequence
#     of strings that are the tokens of the command.
# 
#     Please see Python's subprocess.Popen for a description of the shell
#     parameter and how cmd is interpreted differently based on its value.
# 
#     This code was copied from QIIME's qiime_system_call() (util.py) function on June 3rd, 2013.
#     """
#     proc = Popen(cmd, shell=shell, universal_newlines=True, stdout=PIPE,
#                  stderr=PIPE)
#     # communicate pulls all stdout/stderr from the PIPEs to
#     # avoid blocking -- don't remove this line!
#     stdout, stderr = proc.communicate()
#     return_value = proc.returncode
#     return stdout, stderr, return_value


# Path: tests/all_tests.py
from os import walk, environ
from sys import exit
from os.path import join, abspath, dirname, exists, split
from glob import glob
from picrust.util import get_picrust_project_dir, system_call
from cogent.util.option_parsing import parse_command_line_parameters, make_option
            from qiime.test import run_script_usage_tests
import re
import cogent.util.unit_test as unittest

       option_parser.error("You're suppressing both test types. Nothing to run.")

    test_dir = abspath(dirname(__file__))

    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 PICRUSt's unit tests, and keep track of any files which
    # fail unit tests.
    if not opts.suppress_unit_tests:
        unittest_names = []
        if not opts.unittest_glob:
            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))
        else:
            for fp in glob(opts.unittest_glob):
                fn = split(fp)[1]
                if fn.startswith('test_') and fn.endswith('.py'):
                    unittest_names.append(abspath(fp))

        unittest_names.sort()

        for unittest_name in unittest_names:
            print "Testing %s:\n" % unittest_name
            command = '%s %s -v' % (python_name, unittest_name)

            stdout, stderr, return_value = system_call(command)