# Path: tests/utils.py
# def in_temporary_directory(f):
#     @functools.wraps(f)
#     def decorated(*args, **kwds):
#         with temporary_directory() as directory:
#             from inspect import getargspec
#             # If it takes directory of kwargs and kwds does already have
#             # directory, inject it
#             if 'directory' not in kwds and 'directory' in getargspec(f)[0]:
#                 kwds['directory'] = directory
#             return f(*args, **kwds)
#     decorated.__name__ = f.__name__
#     return decorated
# 
# Path: tests/utils.py
# def assert_cmd_success(cmd, **kwargs):
#     """
#     Asserts that running a command returns zero.
# 
#     returns: stdout
#     """
#     print(">>>", cmd, kwargs)
#     (r, out, err) = run(cmd, **kwargs)
#     print("<<<", str(out))
#     assert r == 0, "cmd failed with result %s:\n %s " % (r, str(cmd))
#     return out
# 
# Path: tests/utils.py
# def assert_cmd_failure(cmd, **kwargs):
#     """
#     Asserts that running a command returns non-zero.
# 
#     returns: stdout
#     """
#     print(">>>", cmd, kwargs)
#     (r, out, err) = run(cmd, withexitstatus=True, **kwargs)
#     print("<<<", str(out))
#     assert 0 != r, "cmd succeeded, but it should fail: %s result=%u\noutput=\n%s" % (cmd, r, out)
#     return out
# 
# Path: tests/utils.py
# def catkin_success(args, env={}):
#     orig_environ = dict(os.environ)
#     try:
#         os.environ.update(env)
#         catkin_main(args)
#     except SystemExit as exc:
#         ret = exc.code
#         if ret != 0:
#             import traceback
#             traceback.print_exc()
#     finally:
#         os.environ = orig_environ
#     return ret == 0
# 
# Path: tests/utils.py
# def catkin_failure(args, env={}):
#     orig_environ = dict(os.environ)
#     try:
#         os.environ.update(env)
#         catkin_main(args)
#     except SystemExit as exc:
#         ret = exc.code
#     finally:
#         os.environ = orig_environ
#     return ret != 0
# 
# Path: tests/utils.py
# class redirected_stdio(object):
# 
#     def __enter__(self):
#         self.original_stdout = sys.stdout
#         self.original_stderr = sys.stderr
#         self.out = StringIO()
#         self.err = StringIO()
#         sys.stdout = self.out
#         sys.stderr = self.err
#         return self.out, self.err
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         sys.stdout = self.original_stdout
#         sys.stderr = self.original_stderr
# 
#         print(self.out.getvalue())


import os
import shutil
from ....utils import in_temporary_directory
from ....utils import assert_cmd_success
from ....utils import assert_cmd_failure
from ....utils import catkin_success
from ....utils import catkin_failure
from ....utils import redirected_stdio
    shutil.copytree(os.path.join(RESOURCES_DIR, 'cmake_pkgs', 'test_err_pkg'), source_space)

    with redirected_stdio() as (out, err):
        assert catkin_success(['build', 'test_err_pkg', '--no-notify', '--no-status'])
        assert catkin_failure(['test', 'test_err_pkg', '--no-notify', '--no-status'])


@in_temporary_directory
def test_skip_missing_test():
    """Test to skip packages without tests"""
    cwd = os.getcwd()
    source_space = os.path.join(cwd, 'src')
    shutil.copytree(os.path.join(RESOURCES_DIR, 'cmake_pkgs', 'cmake_pkg'), source_space)

    with redirected_stdio() as (out, err):
        assert catkin_success(['build', 'cmake_pkg', '--no-notify', '--no-status'])
        assert catkin_success(['test', '--no-notify', '--no-status'])


@in_temporary_directory
def test_other_target():
    """Test with a manually specified target"""
    cwd = os.getcwd()
    source_space = os.path.join(cwd, 'src')
    shutil.copytree(os.path.join(RESOURCES_DIR, 'catkin_pkgs', 'python_tests_targets'), source_space)

    with redirected_stdio() as (out, err):
        assert catkin_success(['build', 'python_tests_targets', '--no-notify', '--no-status'])
        assert catkin_success(['test', '--test-target', 'run_tests_python_tests_targets_nosetests_test_good.py',
                               '--no-notify', '--no-status'])

# Path: tests/utils.py
# def in_temporary_directory(f):
#     @functools.wraps(f)
#     def decorated(*args, **kwds):
#         with temporary_directory() as directory:
#             from inspect import getargspec
#             # If it takes directory of kwargs and kwds does already have
#             # directory, inject it
#             if 'directory' not in kwds and 'directory' in getargspec(f)[0]:
#                 kwds['directory'] = directory
#             return f(*args, **kwds)
#     decorated.__name__ = f.__name__
#     return decorated
# 
# Path: tests/utils.py
# def assert_cmd_success(cmd, **kwargs):
#     """
#     Asserts that running a command returns zero.
# 
#     returns: stdout
#     """
#     print(">>>", cmd, kwargs)
#     (r, out, err) = run(cmd, **kwargs)
#     print("<<<", str(out))
#     assert r == 0, "cmd failed with result %s:\n %s " % (r, str(cmd))
#     return out
# 
# Path: tests/utils.py
# def assert_cmd_failure(cmd, **kwargs):
#     """
#     Asserts that running a command returns non-zero.
# 
#     returns: stdout
#     """
#     print(">>>", cmd, kwargs)
#     (r, out, err) = run(cmd, withexitstatus=True, **kwargs)
#     print("<<<", str(out))
#     assert 0 != r, "cmd succeeded, but it should fail: %s result=%u\noutput=\n%s" % (cmd, r, out)
#     return out
# 
# Path: tests/utils.py
# def catkin_success(args, env={}):
#     orig_environ = dict(os.environ)
#     try:
#         os.environ.update(env)
#         catkin_main(args)
#     except SystemExit as exc:
#         ret = exc.code
#         if ret != 0:
#             import traceback
#             traceback.print_exc()
#     finally:
#         os.environ = orig_environ
#     return ret == 0
# 
# Path: tests/utils.py
# def catkin_failure(args, env={}):
#     orig_environ = dict(os.environ)
#     try:
#         os.environ.update(env)
#         catkin_main(args)
#     except SystemExit as exc:
#         ret = exc.code
#     finally:
#         os.environ = orig_environ
#     return ret != 0
# 
# Path: tests/utils.py
# class redirected_stdio(object):
# 
#     def __enter__(self):
#         self.original_stdout = sys.stdout
#         self.original_stderr = sys.stderr
#         self.out = StringIO()
#         self.err = StringIO()
#         sys.stdout = self.out
#         sys.stderr = self.err
#         return self.out, self.err
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         sys.stdout = self.original_stdout
#         sys.stderr = self.original_stderr
# 
#         print(self.out.getvalue())


# Path: tests/system/verbs/catkin_test/test_unit_tests.py
import os
import shutil
from ....utils import in_temporary_directory
from ....utils import assert_cmd_success
from ....utils import assert_cmd_failure
from ....utils import catkin_success
from ....utils import catkin_failure
from ....utils import redirected_stdio

    shutil.copytree(os.path.join(RESOURCES_DIR, 'cmake_pkgs', 'test_err_pkg'), source_space)

    with redirected_stdio() as (out, err):
        assert catkin_success(['build', 'test_err_pkg', '--no-notify', '--no-status'])
        assert catkin_failure(['test', 'test_err_pkg', '--no-notify', '--no-status'])


@in_temporary_directory
def test_skip_missing_test():
    """Test to skip packages without tests"""
    cwd = os.getcwd()
    source_space = os.path.join(cwd, 'src')
    shutil.copytree(os.path.join(RESOURCES_DIR, 'cmake_pkgs', 'cmake_pkg'), source_space)

    with redirected_stdio() as (out, err):
        assert catkin_success(['build', 'cmake_pkg', '--no-notify', '--no-status'])
        assert catkin_success(['test', '--no-notify', '--no-status'])


@in_temporary_directory
def test_other_target():
    """Test with a manually specified target"""
    cwd = os.getcwd()
    source_space = os.path.join(cwd, 'src')
    shutil.copytree(os.path.join(RESOURCES_DIR, 'catkin_pkgs', 'python_tests_targets'), source_space)

    with redirected_stdio() as (out, err):
        assert catkin_success(['build', 'python_tests_targets', '--no-notify', '--no-status'])
        assert catkin_success(['test', '--test-target', 'run_tests_python_tests_targets_nosetests_test_good.py',
                               '--no-notify', '--no-status'])

        assert catkin_failure(['test', '--test-target', 'run_tests_python_tests_targets_nosetests_test_bad.py',