Example #1
0
def main():
    indigo = Indigo()
    output_dir_base = os.path.join(base_root, "ref")
    output_dir = os.path.join(base_root, "out")
    pattern_list = []
    exclude_pattern_list = []
    nunit_report_name = ""
    junit_report_name = ""

    if 'LABEL' in os.environ:
        binenv = os.environ['LABEL'].upper()
        python_exec = os.environ[binenv]
    else:
        python_exec = sys.executable

    for i in range(1, len(sys.argv), 2):
        if sys.argv[i] == '-p':
            pattern_list = sys.argv[i + 1].split(',')
        elif sys.argv[i] == '-e':
            exclude_pattern_list = sys.argv[i + 1].split(',')
        elif sys.argv[i] == '-o':
            output_dir = sys.argv[i + 1]
        elif sys.argv[i] == '-b':
            output_dir_base = sys.argv[i + 1]
        elif sys.argv[i] == '-n':
            nunit_report_name = sys.argv[i + 1]
        elif sys.argv[i] == '-j':
            junit_report_name = sys.argv[i + 1]
        elif sys.argv[i] == '-exec':
            python_exec = sys.argv[i + 1]
        else:
            print("Unexpected options: %s" % (sys.argv[i]))
            exit()

    if not dir_exists(output_dir):
        makedirs(output_dir)

    sys.stdout = Logger(output_dir + "/results.txt")

    if python_exec.startswith('"') and python_exec.endswith('"'):
        python_exec = python_exec[1:-1]

    print("Indigo version: " + indigo.version())
    print("Indigo library path: " + dll_full_path())
    print("Date & time: " + datetime.datetime.now().strftime("%d.%m.%Y %H:%M"))
    if sys.platform == 'cli':
        import System.Environment
    #print("Platform: {}".format(platform.platform() if sys.platform != 'cli' else System.Environment.OSVersion.ToString()))
    #print("Processor: {}".format(platform.processor() if sys.platform != 'cli' else 'x86_64' if System.Environment.Is64BitProcess else 'x86'))
    print("Python: " + sys.version.replace('\n', '\t'))
    print("Executable: " + python_exec)
    import socket
    print("Host name: " + socket.gethostname())  # platform.node())
    print("")

    # Collect tests and sort them
    tests_dir = os.path.join(base_root, 'tests')
    tests = sorted(get_tests(tests_dir))
    # Calcuate maximum lenthd of the test names
    max_name_len = max((len(item[0]) + len(item[1]) + 1 for item in tests))
    # add small gap
    max_name_len += 3

    test_results = []

    tests_status = 0

    total_time = time.time()
    # Execute tests in sorted order
    for root, filename in tests:
        test_dir = os.path.join(output_dir, root)
        if not dir_exists(test_dir):
            os.makedirs(test_dir)
        test_name = os.path.join(root, filename).replace('\\', '/')

        if test_name != "":
            # Check test name by input pattern
            if len(pattern_list):
                skip_test = True
                for pattern in pattern_list:
                    if re.search(pattern, test_name):
                        skip_test = False
                if skip_test:
                    continue
            # exclude some files from test by pattern
            if len(exclude_pattern_list):
                skip_test = False
                for exclude_pattern in exclude_pattern_list:
                    if re.search(exclude_pattern, test_name):
                        skip_test = True
                if skip_test:
                    continue

        sys.stdout.write("%s" % test_name)
        sys.stdout.flush()

        spacer_len = max_name_len - len(test_name)

        test_root = os.path.join(tests_dir, root)

        old_dir = os.path.abspath(os.curdir)
        os.chdir(test_root)
        out_filename = os.path.join(test_dir, filename + '.out')
        err_filename = os.path.join(test_dir, filename + '.err')
        with open_file_utf8(out_filename) as sys.stdout, open_file_utf8(
                err_filename) as sys.stderr:
            t0 = time.time()
            try:
                runpy.run_path(filename, run_name='__main__')
            except:
                sys.stderr.write(traceback.format_exc())
            tspent = time.time() - t0
        os.chdir(old_dir)
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

        output_file = os.path.join(test_dir, filename + '.out')
        error_file = os.path.join(test_dir, filename + '.err')
        failed_stderr = False
        if not file_size(error_file):
            os.remove(error_file)
        else:
            failed_stderr = True
        base_dir = os.path.join(output_dir_base, root)
        base_output_file = os.path.join(base_dir, filename + ".out")

        base_exists = False
        ndiffcnt = 0
        diff_file = None
        if file_exists(base_output_file):
            diff_file = os.path.join(test_dir, filename + ".diff")
            # copy reference file
            system_name = getPlatform()
            if system_name and file_exists(
                    os.path.join(base_dir, system_name, filename + '.out')):
                base_output_file = os.path.join(base_dir, system_name,
                                                filename + '.out')
            new_ref_file = os.path.join(test_dir, filename + ".std")
            if not os.path.normpath(
                    os.path.abspath(base_output_file)) == os.path.normpath(
                        os.path.abspath(new_ref_file)):
                if not sys.platform == 'cli':
                    shutil.copy(base_output_file, new_ref_file)
                else:
                    import clr
                    clr.AddReference("System.IO.FileSystem")
                    from System.IO import File
                    File.Copy(base_output_file, new_ref_file, True)

            ndiffcnt = write_difference(new_ref_file, output_file, diff_file)
            # remove empty diff file
            if not ndiffcnt:
                os.remove(diff_file)
            base_exists = True

        spacer = '.'
        msg = ''
        if failed_stderr:
            test_status = "[ERROR]"
            tests_status |= 2
        elif not base_exists:
            test_status = "[NEW]"
        elif not ndiffcnt:
            test_status = "[PASSED]"
            spacer = ' '
            spacer_len += 2
        else:
            test_status = "[FAILED]"
            if root != 'todo':
                tests_status |= 1

        print("{}{}    {:.2f} sec".format(spacer * spacer_len, test_status,
                                          tspent))
        if diff_file and file_exists(diff_file):
            f = open(diff_file, 'rt')
            msg = 'Diff:\n' + f.read()
            f.close()
        if error_file and file_exists(error_file):
            f = open(error_file, 'rt')
            msg = 'Error:\n' + f.read()
            f.close()

        test_results.append((root, filename, test_status, msg, tspent))

    total_time = time.time() - total_time
    print("\nTotal time: {:.2f} sec".format(total_time))

    if nunit_report_name != "":
        from generate_nunit_report import generateNUnitReport
        generateNUnitReport(test_results, nunit_report_name)

    if junit_report_name != "":
        from generate_junit_report import generate_junit_report
        generate_junit_report(test_results, junit_report_name)

    return tests_status
Example #2
0
def runTest(root, filename, output_dir, max_name_len, tests_dir, indigo,
            output_dir_base, test_results, writeResults, resultQueue):
    try:
        originalTestName = filename[:-3]
        modifiedTestName = '%s_modified' % (originalTestName)
        test_dir = os.path.join(output_dir, root)
        if not os.path.exists(test_dir):
            import errno
            try:
                os.makedirs(test_dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
        test_name = os.path.join(root, filename).replace('\\', '/')
        spacer_len = max_name_len - len(test_name)
        originalFp, originalPathName, originalDescription = imp.find_module(
            originalTestName, [os.path.join(tests_dir, root)])
        originalTestText = originalFp.read()
        originalFp.close()
        modifiedTestText = manageText(originalTestText)
        modifiedPathName = '%s_modified.py' % (originalPathName[:-3])
        with open(modifiedPathName, 'wt') as modifiedFp:
            modifiedFp.write(modifiedTestText)
        stdout = ''
        stderr = ''
        indigoOutput = None
        modifiedFp, modifiedPathName, modifiedDescription = imp.find_module(
            modifiedTestName, [os.path.join(tests_dir, root)])
        beginTime = time.time()
        module = imp.load_module(modifiedTestName, modifiedFp,
                                 modifiedPathName, modifiedDescription)
        try:
            indigoOutput = module.runIndigoTest()
        except BaseException:
            stderr = traceback.format_exc()
        finally:
            totalTime = time.time() - beginTime
            os.remove(modifiedPathName)
            modifiedFp.close()
        stdout = sys.stdout.getValueByTestName(test_name)
        if indigo.version().endswith('-coverage') and indigoOutput:
            for item in indigoOutput._indigoCoverageDict:
                indigo._indigoCoverageDict[
                    item] += indigoOutput._indigoCoverageDict[item]
            for item in indigoOutput._indigoObjectCoverageDict:
                indigo._indigoObjectCoverageDict[
                    item] += indigoOutput._indigoObjectCoverageDict[item]
            for type in indigoOutput._indigoObjectCoverageByTypeDict:
                if type not in indigo._indigoObjectCoverageByTypeDict:
                    indigo._indigoObjectCoverageByTypeDict[type] = {}
                for key, value in indigoOutput._indigoObjectCoverageByTypeDict[
                        type].items():
                    if key not in indigo._indigoObjectCoverageByTypeDict[type]:
                        indigo._indigoObjectCoverageByTypeDict[type][key] = 0
                    indigo._indigoObjectCoverageByTypeDict[type][
                        key] += indigoOutput._indigoObjectCoverageByTypeDict[
                            type][key]

        output_file = os.path.join(test_dir, filename + ".out")
        if sys.version_info[0] < 3:
            with open(output_file, 'wt') as output_file_handle:
                output_file_handle.write(stdout)
                if len(stderr) > 0:
                    output_file_handle.write("*** STDERR OUTPUT ***\n")
                    output_file_handle.write(stderr)
        else:
            with open(output_file, 'wt',
                      encoding='utf-8') as output_file_handle:
                output_file_handle.write(stdout)
                if len(stderr) > 0:
                    output_file_handle.write("*** STDERR OUTPUT ***\n")
                    output_file_handle.write(stderr)

        base_dir = os.path.join(output_dir_base, root)
        base_output_file = os.path.join(base_dir, filename + ".out")
        base_exists = False
        ndiffcnt = 0
        diff_file = None
        if os.path.exists(base_output_file):
            diff_file = os.path.join(test_dir, filename + ".diff")
            # copy reference file
            system_name = getPlatform()
            if system_name and os.path.exists(
                    os.path.join(base_dir, system_name, filename + '.out')):
                base_output_file = os.path.join(base_dir, system_name,
                                                filename + '.out')
            new_ref_file = os.path.join(test_dir, filename + ".std")
            if not shutil._samefile(base_output_file, new_ref_file):
                shutil.copy(base_output_file, new_ref_file)

            ndiffcnt = write_difference(output_file, new_ref_file, diff_file)
            if not ndiffcnt:
                os.remove(diff_file)
            base_exists = True

        spacer = '.'
        if stderr != "":
            msg = "[FAILED: stderr]"
            spacer_2_len = 0
        elif not base_exists:
            msg = "[NEW]"
            spacer_2_len = 11
        elif not ndiffcnt:
            msg = "[PASSED]"
            spacer = ' '
            spacer_2_len = 8
        else:
            spacer_2_len = 8
            msg = "[FAILED]"
        writeResults("%s%s%s\t%s%.2f sec\n" %
                     (test_name, spacer * spacer_len, msg, ' ' * spacer_2_len,
                      totalTime))
        if diff_file and os.path.exists(diff_file):
            with open(diff_file, 'rt') as f:
                msg += '\n\nDiff:\n' + f.read()
        resultQueue.put((root, filename, msg, totalTime))
    except BaseException:
        traceback.print_exc(file=sys.__stderr__)