# Path: pgcli/config.py
# def ensure_dir_exists(path):
#     parent_dir = expanduser(dirname(path))
#     os.makedirs(parent_dir, exist_ok=True)
# 
# def skip_initial_comment(f_stream: TextIO) -> int:
#     """
#     Initial comment in ~/.pg_service.conf is not always marked with '#'
#     which crashes the parser. This function takes a file object and
#     "rewinds" it to the beginning of the first section,
#     from where on it can be parsed safely
# 
#     :return: number of skipped lines
#     """
#     section_regex = r"\s*\["
#     pos = f_stream.tell()
#     lines_skipped = 0
#     while True:
#         line = f_stream.readline()
#         if line == "":
#             break
#         if re.match(section_regex, line) is not None:
#             f_stream.seek(pos)
#             break
#         else:
#             pos += len(line)
#             lines_skipped += 1
#     return lines_skipped


import io
import os
import stat
import pytest
from pgcli.config import ensure_dir_exists, skip_initial_comment




def test_ensure_file_parent(tmpdir):
    subdir = tmpdir.join("subdir")
    rcfile = subdir.join("rcfile")
    ensure_dir_exists(str(rcfile))


def test_ensure_existing_dir(tmpdir):
    rcfile = str(tmpdir.mkdir("subdir").join("rcfile"))

    # should just not raise
    ensure_dir_exists(rcfile)


def test_ensure_other_create_error(tmpdir):
    subdir = tmpdir.join('subdir"')
    rcfile = subdir.join("rcfile")

    # trigger an  oserror that isn't "directory already exists"
    os.chmod(str(tmpdir), stat.S_IREAD)

    with pytest.raises(OSError):

# Path: pgcli/config.py
# def ensure_dir_exists(path):
#     parent_dir = expanduser(dirname(path))
#     os.makedirs(parent_dir, exist_ok=True)
# 
# def skip_initial_comment(f_stream: TextIO) -> int:
#     """
#     Initial comment in ~/.pg_service.conf is not always marked with '#'
#     which crashes the parser. This function takes a file object and
#     "rewinds" it to the beginning of the first section,
#     from where on it can be parsed safely
# 
#     :return: number of skipped lines
#     """
#     section_regex = r"\s*\["
#     pos = f_stream.tell()
#     lines_skipped = 0
#     while True:
#         line = f_stream.readline()
#         if line == "":
#             break
#         if re.match(section_regex, line) is not None:
#             f_stream.seek(pos)
#             break
#         else:
#             pos += len(line)
#             lines_skipped += 1
#     return lines_skipped


# Path: tests/test_config.py
import io
import os
import stat
import pytest
from pgcli.config import ensure_dir_exists, skip_initial_comment





def test_ensure_file_parent(tmpdir):
    subdir = tmpdir.join("subdir")
    rcfile = subdir.join("rcfile")
    ensure_dir_exists(str(rcfile))


def test_ensure_existing_dir(tmpdir):
    rcfile = str(tmpdir.mkdir("subdir").join("rcfile"))

    # should just not raise
    ensure_dir_exists(rcfile)


def test_ensure_other_create_error(tmpdir):
    subdir = tmpdir.join('subdir"')
    rcfile = subdir.join("rcfile")

    # trigger an  oserror that isn't "directory already exists"
    os.chmod(str(tmpdir), stat.S_IREAD)

    with pytest.raises(OSError):

        ensure_dir_exists(str(rcfile))