def test_stdout_and_stderr_transformers_bash(capfd, stdin): with stdin: assert ExecuteCommand( args=['/bin/bash', '-c', 'echo "stdout"; echo "stderr" >&2'], stdout_transformer=ReplaceTransformer('stdout', 'STDOUT_REPLACED'), stderr_transformer=ReplaceTransformer('stderr', 'STDERR_REPLACED') ).run() == 0 out, err = capfd.readouterr() assert out == 'STDOUT_REPLACED\n' + ansi.ANSI_RESET_ALL assert err == 'STDERR_REPLACED\n' + ansi.ANSI_RESET_ALL
def test_stdout_and_stderr_transformers(capfd, stdin): with stdin: assert ExecuteCommand( args=[sys.executable, '-c', dedent(r''' import sys sys.stdout.write('stdout\n') sys.stderr.write('stderr\n') ''')], stdout_transformer=ReplaceTransformer('stdout', 'STDOUT_REPLACED'), stderr_transformer=ReplaceTransformer('stderr', 'STDERR_REPLACED') ).run() == 0 out, err = capfd.readouterr() assert out == 'STDOUT_REPLACED\n' + ansi.ANSI_RESET_ALL assert err == 'STDERR_REPLACED\n' + ansi.ANSI_RESET_ALL
def test_stderr_transformer_bash(capfd, stdin): with stdin: assert ExecuteCommand( args=['/bin/bash', '-c', 'echo "message"; echo "message" >&2'], stderr_transformer=ReplaceTransformer('message', 'REPLACED') ).run() == 0 out, err = capfd.readouterr() assert out == 'message\n' + ansi.ANSI_RESET_ALL assert err == 'REPLACED\n' + ansi.ANSI_RESET_ALL
def test_config_by_reference(test): config_name = test[0] input_file = test[1] expected_file = input_file + '.out' (transformer, errors) = load_builtin_config(config_name) assert not errors transformer = ListTransformer([ transformer, ReplaceTransformer(ANSI_FOREGROUND_RED, '[FOREGROUND_RED]'), ReplaceTransformer(ANSI_FOREGROUND_GREEN, '[FOREGROUND_GREEN]'), ReplaceTransformer(ANSI_FOREGROUND_YELLOW, '[FOREGROUND_YELLOW]'), ReplaceTransformer(ANSI_FOREGROUND_BLUE, '[FOREGROUND_BLUE]'), ReplaceTransformer(ANSI_FOREGROUND_MAGENTA, '[FOREGROUND_MAGENTA]'), ReplaceTransformer(ANSI_FOREGROUND_CYAN, '[FOREGROUND_CYAN]'), ReplaceTransformer(ANSI_FOREGROUND_RESET, '[FOREGROUND_RESET]'), ReplaceTransformer(ANSI_BACKGROUND_RED, '[BACKGROUND_RED]'), ReplaceTransformer(ANSI_BACKGROUND_GREEN, '[BACKGROUND_GREEN]'), ReplaceTransformer(ANSI_BACKGROUND_YELLOW, '[BACKGROUND_YELLOW]'), ReplaceTransformer(ANSI_BACKGROUND_BLUE, '[BACKGROUND_BLUE]'), ReplaceTransformer(ANSI_BACKGROUND_MAGENTA, '[BACKGROUND_MAGENTA]'), ReplaceTransformer(ANSI_BACKGROUND_CYAN, '[BACKGROUND_CYAN]'), ReplaceTransformer(ANSI_BACKGROUND_RESET, '[BACKGROUND_RESET]'), ReplaceTransformer(ANSI_BOLD, '[BOLD]'), ReplaceTransformer(ANSI_FAINT, '[FAINT]'), ReplaceTransformer(ANSI_ITALIC, '[ITALIC]'), ReplaceTransformer(ANSI_UNDERLINE, '[UNDERLINE]'), ReplaceTransformer(ANSI_UNDERLINE_DOUBLE, '[UNDERLINE_DOUBLE]'), ReplaceTransformer(ANSI_BLINK, '[BLINK]'), ReplaceTransformer(ANSI_BLINK_RAPID, '[BLINK_RAPID]'), ReplaceTransformer(ANSI_NEGATIVE, '[NEGATIVE]'), ReplaceTransformer(ANSI_HIDE, '[HIDE]'), ReplaceTransformer(ANSI_RESET_INTENSITY, '[RESET_INTENSITY]'), ReplaceTransformer(ANSI_RESET_ITALIC, '[RESET_ITALIC]'), ReplaceTransformer(ANSI_RESET_UNDERLINE, '[RESET_UNDERLINE]'), ReplaceTransformer(ANSI_RESET_BLINK, '[RESET_BLINK]'), ReplaceTransformer(ANSI_RESET_NEGATIVE, '[RESET_NEGATIVE]'), ReplaceTransformer(ANSI_RESET_HIDE, '[RESET_HIDE]'), ReplaceTransformer(ANSI_RESET_ALL, '[RESET_ALL]') ]) with open(input_file) as f: actual_lines = [ transformer.transform(l) for l in f.read().splitlines() ] if GENERATE_REFERENCES: with open(expected_file, 'w') as f: for line in actual_lines: f.write('%s\n' % line) with open(expected_file) as f: expected_lines = f.read().splitlines() assert actual_lines == expected_lines
def test_replace_multiples_matches(): assert ReplaceTransformer('test', 'b').transform("testtesttest") == "bbb"
def test_replace_matching(): assert ReplaceTransformer('test', 'b').transform("test") == "b"
def test_replace_not_matching(): assert ReplaceTransformer('a', 'b').transform("test") == "test"
def test_transformer_replace_not_eq(): assert ReplaceTransformer("test", "REPLACEMENT") != ReplaceTransformer( "test", "REPLACEMENT2")
def test_transformer_replace_str(): assert ReplaceTransformer( "test", "REPLACEMENT").__str__() == 'replace "test" with "REPLACEMENT"'