"""
Make sure that the old syntax where Server32 required a `quiet` argument is still valid.
"""
import os
import subprocess

try:
    import pytest
except ImportError:  # the 32-bit server does not need pytest installed
    class Mark(object):
        @staticmethod
        def skipif(condition, reason=None):
            def func(function):
                return function
            return func

    class pytest(object):
        mark = Mark

from msl.loadlib import Server32, Client64, SERVER_FILENAME, IS_MAC
from msl.examples.loadlib import EXAMPLES_DIR


class Quiet32(Server32):

    def __init__(self, host, port, quiet, **kwargs):
        super(Quiet32, self).__init__(
            os.path.join(kwargs['ex_dir'], 'cpp_lib32'),
            'cdll', host, port, quiet
        )
        self._kwargs = kwargs

    def add(self, a, b):
        return self.lib.add(a, b)

    def kwargs(self):
        return self._kwargs


class Quiet64(Client64):

    def __init__(self, **kwargs):
        super(Quiet64, self).__init__(__file__, **kwargs)

    def add(self, a, b):
        # a and b must be integers
        return self.request32('add', a, b)

    def kwargs(self):
        return self.request32('kwargs')


@pytest.mark.skipif(IS_MAC, reason='the 32-bit server for macOS does not exist')
def test_quiet_argument():

    q = Quiet64(one=1, array=[1., -2.2, 3e9], ex_dir=EXAMPLES_DIR)

    assert q.add(-5, 2) == -3
    assert q.kwargs() == {'one': '1', 'array': '[1.0, -2.2, 3000000000.0]', 'ex_dir': EXAMPLES_DIR}


@pytest.mark.skipif(IS_MAC, reason='the 32-bit server for macOS does not exist')
def test_cli_quiet_flag():
    cmd = [os.path.join(os.path.dirname(__file__), os.pardir, 'msl', 'loadlib', SERVER_FILENAME), '--quiet']
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()

    assert out.rstrip() == b'DeprecationWarning: the --quiet flag is ignored and will be removed in a future release'

    lines = err.splitlines()
    assert lines[0] == b'You must specify a Python module to run on the 32-bit server.'
    assert lines[1] == b'For example: ' + SERVER_FILENAME.encode() + b' -m my_module.py'
    assert lines[2] == b'Cannot start the 32-bit server.'

@@ -1,101 +1,4 @@
-%22%22%22%0AMake sure that the old syntax where Server32 required a %60quiet%60 argument is still valid.%0A%22%22%22%0A
 impo
@@ -1220,16 +1220,110 @@
 ment():%0A
+    # Make sure that the old syntax where Server32 required a %60quiet%60 argument is still valid.
 %0A    q =
@@ -1382,17 +1382,16 @@
 ES_DIR)%0A
-%0A
     asse

move comment to test function