"""
unit tests for the script engine
"""

import pytest
import salt.config
import salt.engines.script as script
from salt.exceptions import CommandExecutionError
from tests.support.mock import patch


@pytest.fixture
def configure_loader_modules():
    opts = salt.config.DEFAULT_MASTER_OPTS
    return {script: {"__opts__": opts}}


def test__get_serializer():
    """
    Test known serializer is returned or exception is raised
    if unknown serializer
    """
    for serializers in ("json", "yaml", "msgpack"):
        assert script._get_serializer(serializers)

    with pytest.raises(CommandExecutionError):
        script._get_serializer("bad")


def test__read_stdout():
    """
    Test we can yield stdout
    """
    with patch("subprocess.Popen") as popen_mock:
        popen_mock.stdout.readline.return_value = "test"
        assert next(script._read_stdout(popen_mock)) == "test"

@@ -881,16 +881,346 @@
 ock)) == %22test%22%0A
+%0Adef test__read_stdout_terminates_properly():%0A    %22%22%22%0A    Test that _read_stdout terminates with the sentinel%0A    %22%22%22%0A    with patch(%22subprocess.Popen%22, autospec=True) as popen_mock:%0A        popen_mock.stdout.readline.return_value = b%22%22%0A        with pytest.raises(StopIteration):%0A            next(script._read_stdout(popen_mock))%0A

Test iteration stops at empty bytes