# Path: kibitzr/fetcher/script.py
# def fetch_by_script(conf):
#     code = conf['script']
#     try:
#         python_code = code['python']
#     except (KeyError, TypeError):
#         # Not a python script
#         pass
#     else:
#         return fetch_by_python(python_code, conf)
#     try:
#         # Explicit notation:
#         bash_script = code['bash']
#     except (KeyError, TypeError):
#         bash_script = code
#     # Pass something to stdin to disable sanity check:
#     return execute_bash(
#         bash_script,
#         conf.get('name', 'dummy'),
#     )
# 
# Path: tests/helpers.py
# @contextlib.contextmanager
# def stash_mock():
#     with tempfile.NamedTemporaryFile() as fp:
#         fp.close()
#         with mock.patch.object(Stash, 'FILENAME', fp.name):
#             yield Stash()


import pytest
from kibitzr.fetcher.script import fetch_by_script
from ...helpers import stash_mock
def test_fetch_by_python_script():
    ok, content = run_script({'python': 'ok, content = True, "hello"'})
    assert ok is True
    assert content == "hello"


def test_python_script_must_define_content():
    with pytest.raises(AssertionError):
        run_script({'python': 'dummy'})


def test_python_script_exception_is_reported():
    ok, content = run_script({'python': 'content = 1 / 0'})
    assert ok is False
    assert content.splitlines()[-1].startswith("ZeroDivisionError")


def test_ok_is_optional_in_python_script():
    ok, content = run_script({'python': 'content = "dummy"'})
    assert ok is True
    assert content == "dummy"


def test_bash_error_is_returned():
    ok, content = run_script('no command not found')
    assert ok is False
    assert content.split(':', 1)[1].strip() == "line 1: no: command not found"


def test_python_script_has_stash():

# Path: kibitzr/fetcher/script.py
# def fetch_by_script(conf):
#     code = conf['script']
#     try:
#         python_code = code['python']
#     except (KeyError, TypeError):
#         # Not a python script
#         pass
#     else:
#         return fetch_by_python(python_code, conf)
#     try:
#         # Explicit notation:
#         bash_script = code['bash']
#     except (KeyError, TypeError):
#         bash_script = code
#     # Pass something to stdin to disable sanity check:
#     return execute_bash(
#         bash_script,
#         conf.get('name', 'dummy'),
#     )
# 
# Path: tests/helpers.py
# @contextlib.contextmanager
# def stash_mock():
#     with tempfile.NamedTemporaryFile() as fp:
#         fp.close()
#         with mock.patch.object(Stash, 'FILENAME', fp.name):
#             yield Stash()


# Path: tests/unit/fetchers/test_script.py
import pytest
from kibitzr.fetcher.script import fetch_by_script
from ...helpers import stash_mock

def test_fetch_by_python_script():
    ok, content = run_script({'python': 'ok, content = True, "hello"'})
    assert ok is True
    assert content == "hello"


def test_python_script_must_define_content():
    with pytest.raises(AssertionError):
        run_script({'python': 'dummy'})


def test_python_script_exception_is_reported():
    ok, content = run_script({'python': 'content = 1 / 0'})
    assert ok is False
    assert content.splitlines()[-1].startswith("ZeroDivisionError")


def test_ok_is_optional_in_python_script():
    ok, content = run_script({'python': 'content = "dummy"'})
    assert ok is True
    assert content == "dummy"


def test_bash_error_is_returned():
    ok, content = run_script('no command not found')
    assert ok is False
    assert content.split(':', 1)[1].strip() == "line 1: no: command not found"


def test_python_script_has_stash():

    with stash_mock() as stash: