Archive-BagIt

 view release on metacpan or  search on metacpan

bagit_conformance_suite/test-harness  view on Meta::CPAN

#!/usr/bin/env python
# encoding: utf-8
"""
Run compliance tests for a BagIt command-line tool

This requires a tool which behaves like a standard Unix tool and returns 0 for
success and 1 for failures. This tool will check stderr and report its contents
for the warning test bags.

Example usage:

    %(prog)s -- bagit.py --validate --quiet

"""

from __future__ import absolute_import, division, print_function, unicode_literals

import argparse
import logging
import os
import platform
import subprocess
import sys

BASE_DIR = os.path.dirname(__file__)

SYSTEM = platform.system().lower()

DEFAULT_ENCODING = sys.stdout.encoding
if DEFAULT_ENCODING != 'UTF-8':
    print('Default encoding is %s: use with caution, UTF-8 is recommended', file=sys.stderr)


def run_version_suite(test_program_argv, version_dir):
    spec_version = os.path.basename(version_dir)

    for test_category in os.listdir(version_dir):
        test_subsuite_directory = os.path.join(version_dir, test_category)
        if not os.path.isdir(test_subsuite_directory):
            continue

        if test_category.endswith('-only') and not test_category.startswith(SYSTEM):
            logging.info('Skipping %s tests as we are running on %s', test_category, SYSTEM)
            continue

        logging.info('Running version %s %s tests', spec_version, test_category)

        for test_bag in os.listdir(test_subsuite_directory):
            if not os.path.isdir(os.path.join(test_subsuite_directory, test_bag)):
                continue

            proc = subprocess.Popen(test_program_argv + [test_bag],
                                    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                    cwd=test_subsuite_directory)
            rc = proc.wait()
            stdout, stderr = proc.communicate()

            # We'll select the log level based on whether we expected an error:
            log_f = logging.info
            dump_stderr = False
            if test_category == 'valid':
                if rc != 0:
                    log_f = logging.error
                    dump_stderr = True
            elif test_category == 'invalid':
                if rc == 0:
                    log_f = logging.error
            elif test_category == 'warning':
                if rc == 0 and not stderr:
                    log_f = logging.warning

            log_f('Expected %s test %s: rc=%d, stdout=%d bytes, stderr=%d bytes',
                  test_category, test_bag, rc, len(stdout), len(stderr))

            log_captured_output(logging.info, 'stdout', stdout)
            log_captured_output(log_f if dump_stderr else logging.info, 'stderr', stderr)



( run in 0.781 second using v1.01-cache-2.11-cpan-d7f47b0818f )