Exemple #1
0
def test_user_requested_raise(fake_unicode_decode_error):
    """If the user doesn't answer "yes", raise the error during write."""

    mock_input = patch.object(
        formatting,
        "_prompt_for_input_on_error",
        return_value="no",
    )

    if sys.version_info > (3,):
        m_print = Mock(
            "builtins.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(builtins, "print", new=m_print)
    else:
        m_print = Mock(
            "__builtin__.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(__builtin__, str("print"), new=m_print)

    with pytest.raises(SystemExit) as err:
        with print_patch:
            with mock_input:
                formatting.write("super important data", {})

    assert err.exconly() == (
        "SystemExit: Could not write results. Cancelled on user request."
    )
Exemple #2
0
def test_errors_writing_to_stdout(fake_unicode_decode_error):
    """We should prompt the user if there's an error printing."""

    fallback_file = tempfile.mktemp()

    mock_input = patch.object(
        formatting,
        "_prompt_for_input_on_error",
        side_effect=["yes", fallback_file]
    )

    if sys.version_info > (3,):
        m_print = Mock(
            "builtins.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(builtins, "print", new=m_print)
    else:
        m_print = Mock(
            "__builtin__.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(__builtin__, str("print"), new=m_print)

    with print_patch:
        with mock_input:
            formatting.write("super important data", {})

    assert os.path.exists(fallback_file)

    with open(fallback_file, "r") as openfile:
        assert "super important data" in openfile.read()
Exemple #3
0
def test_user_requested_raise(fake_unicode_decode_error):
    """If the user doesn't answer "yes", raise the error during write."""

    mock_input = patch.object(
        formatting,
        "_prompt_for_input_on_error",
        return_value="no",
    )

    if sys.version_info > (3, ):
        m_print = Mock(
            "builtins.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(builtins, "print", new=m_print)
    else:
        m_print = Mock(
            "__builtin__.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(__builtin__, str("print"), new=m_print)

    with pytest.raises(SystemExit) as err:
        with print_patch:
            with mock_input:
                formatting.write("super important data", {})

    assert err.exconly() == (
        "SystemExit: Could not write results. Cancelled on user request.")
Exemple #4
0
def test_errors_writing_to_stdout(fake_unicode_decode_error):
    """We should prompt the user if there's an error printing."""

    fallback_file = tempfile.mktemp()

    mock_input = patch.object(formatting,
                              "_prompt_for_input_on_error",
                              side_effect=["yes", fallback_file])

    if sys.version_info > (3, ):
        m_print = Mock(
            "builtins.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(builtins, "print", new=m_print)
    else:
        m_print = Mock(
            "__builtin__.print",
            side_effect=fake_unicode_decode_error,
        )
        print_patch = patch.object(__builtin__, str("print"), new=m_print)

    with print_patch:
        with mock_input:
            formatting.write("super important data", {})

    assert os.path.exists(fallback_file)

    with open(fallback_file, "r") as openfile:
        assert "super important data" in openfile.read()
Exemple #5
0
def test_writing_to_file():
    """Ensure we can write out to a file."""

    string = "some string with words and stuff in it"
    options = {"output_file": tempfile.mktemp()}
    formatting.write(string, options)
    with open(options["output_file"], "r") as openoutput:
        assert string in openoutput.read()
Exemple #6
0
def test_writing_to_file():
    """Ensure we can write out to a file."""

    string = "some string with words and stuff in it"
    options = {"output_file": tempfile.mktemp()}
    formatting.write(string, options)
    with open(options["output_file"], "r") as openoutput:
        assert string in openoutput.read()
Exemple #7
0
def test_write_encoding_errors(fake_unicode_decode_error):
    """All encodings should be tried, then finally a fallback to prompt."""

    open_patch = patch.object(
        formatting.io,
        "open",
        side_effect=fake_unicode_decode_error,
    )
    with open_patch as p_open:
        with patch.object(formatting, "_retry_write") as patched_retry:
            formatting.write("some stuff", {"output_file": "yep"}, "\o/")

    patched_retry.assert_called_once_with(
        "some stuff",
        {"output_file": "yep"},
        "\o/",
    )

    for call_, enc in zip(p_open.call_args_list, formatting.DEFAULT_ENCODINGS):
        assert call_ == call("yep", "a", encoding=enc)
Exemple #8
0
def test_write_encoding_errors(fake_unicode_decode_error):
    """All encodings should be tried, then finally a fallback to prompt."""

    open_patch = patch.object(
        formatting.io,
        "open",
        side_effect=fake_unicode_decode_error,
    )
    with open_patch as p_open:
        with patch.object(formatting, "_retry_write") as patched_retry:
            formatting.write("some stuff", {"output_file": "yep"}, "\o/")

    patched_retry.assert_called_once_with(
        "some stuff",
        {"output_file": "yep"},
        "\o/",
    )

    for call_, enc in zip(p_open.call_args_list, formatting.DEFAULT_ENCODINGS):
        assert call_ == call("yep", "a", encoding=enc)