# Path: fastavro/_write_common.py
# def _is_appendable(file_like):
#     if file_like.seekable() and file_like.tell() != 0:
#         if "<stdout>" == getattr(file_like, "name", ""):
#             # In OSX, sys.stdout is seekable and has a non-zero tell() but
#             # we wouldn't want to append to a stdout. In the python REPL,
#             # sys.stdout is named `<stdout>`
#             return False
#         if file_like.readable():
#             return True
#         else:
#             raise ValueError(
#                 "When appending to an avro file you must use the "
#                 + "'a+' mode, not just 'a'"
#             )
#     else:
#         return False


import sys
import pytest
from fastavro._write_common import _is_appendable
def test_appendable_raises_valuerror(tmpdir):
    """_is_appendable() raises ValueError when file is only 'a' mode."""
    test_file = str(tmpdir.join("test.avro"))

    with open(test_file, "a") as new_file:
        new_file.write("this phrase forwards cursor position beyond zero")
        with pytest.raises(ValueError, match=r"you must use the 'a\+' mode"):
            _is_appendable(new_file)


def test_appendable_true_nonzero(tmpdir):
    """_is_appendable() returns True when file_like.tell() is non-zero."""
    test_file = str(tmpdir.join("test.avro"))

    with open(test_file, "a+b") as new_file:
        new_file.write(b"this phrase forwards cursor position beyond zero")
        assert _is_appendable(new_file)


def test_appendable_false_zero():
    """_is_appendable() returns True when file_like.tell() returns 0."""

    class MockFileLike:
        def seekable(self):
            return True

        def tell(self):
            # mock a 0 position
            return 0


# Path: fastavro/_write_common.py
# def _is_appendable(file_like):
#     if file_like.seekable() and file_like.tell() != 0:
#         if "<stdout>" == getattr(file_like, "name", ""):
#             # In OSX, sys.stdout is seekable and has a non-zero tell() but
#             # we wouldn't want to append to a stdout. In the python REPL,
#             # sys.stdout is named `<stdout>`
#             return False
#         if file_like.readable():
#             return True
#         else:
#             raise ValueError(
#                 "When appending to an avro file you must use the "
#                 + "'a+' mode, not just 'a'"
#             )
#     else:
#         return False


# Path: tests/test_appendable.py
import sys
import pytest
from fastavro._write_common import _is_appendable

def test_appendable_raises_valuerror(tmpdir):
    """_is_appendable() raises ValueError when file is only 'a' mode."""
    test_file = str(tmpdir.join("test.avro"))

    with open(test_file, "a") as new_file:
        new_file.write("this phrase forwards cursor position beyond zero")
        with pytest.raises(ValueError, match=r"you must use the 'a\+' mode"):
            _is_appendable(new_file)


def test_appendable_true_nonzero(tmpdir):
    """_is_appendable() returns True when file_like.tell() is non-zero."""
    test_file = str(tmpdir.join("test.avro"))

    with open(test_file, "a+b") as new_file:
        new_file.write(b"this phrase forwards cursor position beyond zero")
        assert _is_appendable(new_file)


def test_appendable_false_zero():
    """_is_appendable() returns True when file_like.tell() returns 0."""

    class MockFileLike:
        def seekable(self):
            return True

        def tell(self):
            # mock a 0 position
            return 0


    assert not _is_appendable(MockFileLike())