"""Shared set-up: every test starts with no ASKCLI_* variables, in an empty working directory
(so a stray askcli.toml cannot leak in), and ends with the askcli logger put back as it was."""

import logging
import os

import pytest


@pytest.fixture(autouse=True)
def clean_slate(monkeypatch, tmp_path):
    for key in list(os.environ):
        if key.startswith("ASKCLI_"):
            monkeypatch.delenv(key)
    monkeypatch.chdir(tmp_path)
    yield
    logger = logging.getLogger("askcli")
    for handler in list(logger.handlers):
        logger.removeHandler(handler)
    logger.setLevel(logging.NOTSET)
    logger.propagate = True


def write_toml(tmp_path, text):
    path = tmp_path / "askcli.toml"
    path.write_text(text)
    return path
