def _check(this_input_content, which_check, check_return_code=True):
            return_code = modernize_main(extra_flags + ["-w", test_input_name])

            if check_return_code and expected_return_code is not None:
                if expected_return_code != return_code:
                    raise AssertionError("Actual return code: %s\nExpected return code: %s" %
                                         (return_code, expected_return_code))

            # Second pass to deal with cumulative effects that affect 'import'
            return_code = modernize_main(extra_flags + ["-w", test_input_name])

            if check_return_code and expected_return_code is not None:
                if expected_return_code != return_code:
                    raise AssertionError("Actual return code: %s\nExpected return code: %s" %
                                         (return_code, expected_return_code))

            output_content = ""
            with open(test_input_name, "rt") as output_file:
                for line in output_file:
                    if line:
                        output_content += line

            if output_content != expected_content:
                raise AssertionError("%s\nInput:\n%sOutput:\n%s\nExpecting:\n%s" %
                                     (which_check, this_input_content, output_content, expected_content))
Exemple #2
0
def _check_on_input(file_content, extra_flags = []):
    try:
        tmpdirname = tempfile.mkdtemp()
        test_input_name = os.path.join(tmpdirname, "input.py")
        with open(test_input_name, "wt") as input:
            input.write(file_content)
        modernize_main(extra_flags + ["-w", test_input_name])
        _check_for_multiple_futures(test_input_name, file_content)
    finally:
        shutil.rmtree(tmpdirname)
Exemple #3
0
        def _check(this_input_content, which_check):
            modernize_main(extra_flags + ["-w", test_input_name])

            output_content = ""
            with open(test_input_name, "rt") as output_file:
                for line in output_file:
                    if line:
                        output_content += line

            if output_content != expected_content:
                raise AssertionError("%s\nInput:\n%sOutput:\n%s\nExpecting:\n%s" %
                                     (which_check, this_input_content, output_content, expected_content))
Exemple #4
0
def test_two_files_on_single_run():
    # Mostly to test whether second file gets its "from future ..."
    try:
        tmpdirname = tempfile.mkdtemp()
        input_names = [ os.path.join(tmpdirname, "input_{0}.py".format(idx))
                        for idx in range(0,3) ]
        for input_name in input_names:
            with open(input_name, "wt") as input:
                input.write(TWO_PRINTS_CONTENT)
        modernize_main(["-w"] + input_names)
        for input_name in input_names:
            futs = _check_for_multiple_futures(input_name, TWO_PRINTS_CONTENT)
            if not futs:
                raise Exception("File {0} got no from __future__ (but it should)")
    finally:
        shutil.rmtree(tmpdirname)
Exemple #5
0
def test_list_fixers():
    sio = StringIO()
    real_stdout = sys.stdout
    sys.stdout = sio
    try:
        exitcode = modernize_main(["-l"])
    finally:
        sys.stdout = real_stdout
    assert exitcode == 0, exitcode
    assert "xrange_six" in sio.getvalue()
Exemple #6
0
def test_list_fixers():
    sio = StringIO()
    real_stdout = sys.stdout
    sys.stdout = sio
    try:
        exitcode = modernize_main(['-l'])
    finally:
        sys.stdout = real_stdout
    assert exitcode == 0, exitcode
    assert 'xrange_six' in sio.getvalue()
Exemple #7
0
        def _check(this_input_content, which_check, check_return_code=True):
            return_code = modernize_main(extra_flags + ["-w", test_input_name])

            if check_return_code and expected_return_code is not None:
                if expected_return_code != return_code:
                    raise AssertionError(
                        "Actual return code: %s\nExpected return code: %s" %
                        (return_code, expected_return_code))

            output_content = ""
            with open(test_input_name, "rt") as output_file:
                for line in output_file:
                    if line:
                        output_content += line

            if output_content != expected_content:
                raise AssertionError(
                    "%s\nInput:\n%sOutput:\n%s\nExpecting:\n%s" %
                    (which_check, this_input_content, output_content,
                     expected_content))
def check_modernizations(args, filename):
    """ Modernize (and 2to3) don't provide a uniform exit code to work with.
        Parse all possible outputs to determine the true final state.
    """
    if VERBOSE:
        print('=' * 78)
    full_args = args[:]
    full_args.append(filename)
    if VERBOSE:
        print('process  :', filename)
        print('arguments:', full_args)
    if USE_TEAMCITY:
        TC.testStarted(filename)

    # stdout/stderr diversion block
    stdout_orig = sys.stdout
    stdout_buffer = StringIO()
    sys.stdout = stdout_buffer
    stderr_orig = sys.stderr
    stderr_buffer = StringIO()
    sys.stderr = stderr_buffer
    exitcode = -1
    try:
        exitcode = modernize_main(full_args)
    except SystemExit as e:
        print("'Modernize' exited abnormally: {}".format(e))
        exitcode = -1
    finally:
        sys.stdout.flush()
        sys.stdout = stdout_orig
        sys.stderr.flush()
        sys.stderr = stderr_orig
    # stdout/stderr diversion block ends

    sout = stdout_buffer.getvalue()
    serr = stderr_buffer.getvalue()
    slog = LOG_CAPTURE_STRING.getvalue()
    LOG_CAPTURE_STRING.flush()
    LOG_CAPTURE_STRING.seek(0)
    LOG_CAPTURE_STRING.truncate(0)

    if VERBOSE:
        print('Exitcode (initial):', exitcode)
    if VERBOSE or exitcode == -1:
        for line in sout.split('\n'):
            print('STDOUT___:', line)
        for line in serr.split('\n'):
            print('STDERR___:', line)
        for line in slog.split('\n'):
            print('STDlog___:', line)
    mods = []
    for line in sout.split('\n'):
        if mods or line.endswith('\t(original)'):
            mods.append(line)
    details = '\n'.join(mods)
    if exitcode == 2:
        if slog.find('RefactoringTool: No changes to ') != -1:
            exitcode = 0
            if VERBOSE:
                print('File NOT changed per RefactoringTool')
    if details:
        # Final arbiter: if there are mods in the output, something definitely happened!
        exitcode = 2
        if VERBOSE:
            print('File changed per actual output')
    if VERBOSE:
        print('Exitcode (final):', exitcode)

    cmd_line = 'modernize {}'.format(' '.join(args[:]))
    if exitcode == 0:
        print('no change: {}'.format(filename))
    elif exitcode == 2:
        print('needs fix: {}'.format(filename))
        if USE_TEAMCITY:
            TC.testFailed(filename, message='Migration needed', details='\nSuggested changes from `{}`:\n\n'.format(cmd_line) + details)
        else:
            print('\nSuggested changes from `{}`:\n\n'.format(cmd_line) + details)
    else:
        print('UNK_ERROR: {}'.format(filename))
        if USE_TEAMCITY:
            TC.testFailed(filename, message='Unknown error', details='\nUnexpected output from `{}`:\n\n'.format(cmd_line) + details)
        else:
            print('\nUnexpected output from `{}`:\n\n'.format(cmd_line) + details)

    if USE_TEAMCITY:
        TC.testFinished(filename)

    return (sout, serr, exitcode)