Пример #1
0
    def test_writing_to_io(self, example_contents):
        buffer = io.StringIO()
        with ensure_open(buffer) as f:
            f.write(example_contents)

        buffer.seek(0)
        read_contents = buffer.read()

        assert read_contents == example_contents
Пример #2
0
    def test_writing_to_path(self, tmp_path: str, path_mapper, example_contents):
        path = path_mapper(tmp_path)
        with ensure_open(path, "w") as f:
            f.write(example_contents)

        with open(tmp_path, encoding="utf-8") as f:
            read_contents = f.read()

        assert read_contents == example_contents
Пример #3
0
    def test_writing_to_open_file(self, tmp_path: str, example_contents):
        with open(tmp_path, "w", encoding="utf-8") as open_file:
            with ensure_open(open_file, "w") as f:
                f.write(example_contents)

        with open(tmp_path, encoding="utf-8") as f:
            read_contents = f.read()

        assert read_contents == example_contents
Пример #4
0
    def test_reading_from_io(self, example_contents):
        buffer = io.StringIO()
        buffer.write(example_contents)
        buffer.seek(0)

        with ensure_open(buffer) as f:
            read_contents = f.read()

        assert read_contents == example_contents
Пример #5
0
def test_ensure_open_with_write_flag_and_read_only_file_raises_error(tmp_path: str):
    with open(tmp_path) as open_file:
        with pytest.raises(ValueError):
            with ensure_open(open_file, "w"):
                pass