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) 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_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)
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, f"input_{idx}.py") 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)
def test_nofix_fixers(tmp_path): stdout = io.StringIO() stderr = io.StringIO() with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr( stderr): returncode = modernize_main(["--nofix=ham", str(tmp_path)]) assert returncode == 2 assert stderr.getvalue() == "Error: fix 'ham' was not found\n" assert stdout.getvalue() == ""
def test_list_fixers(): stdout = io.StringIO() with contextlib.redirect_stdout(stdout): returncode = modernize_main(["-l"]) assert returncode == 0 assert "xrange_six" in stdout.getvalue()