"""
sphinx-autobuild tests.
"""

import mock
import pytest
import livereload
from watchdog import observers

from sphinx_autobuild import main


@pytest.fixture(autouse=True)
def patched_args(sys_args, monkeypatch):
    """Patch sys args."""
    monkeypatch.setattr('sys.argv', sys_args)


@pytest.mark.parametrize('sys_args', (
    ['sphinx-autobuild', '/source', '/output'],
))
@mock.patch.object(observers.Observer, 'schedule')
@mock.patch.object(livereload.Server, 'serve')
@mock.patch('os.makedirs')
def test_autobuild(mock_makedirs, mock_serve, mock_schedule):
    """
    Test autobuild entry point.
    """
    main()
    mock_makedirs.assert_called_once_with('/output')
    mock_serve.assert_called_once_with(
        host='127.0.0.1', root='/output', port=8000)

"""
sphinx-autobuild tests.
"""

import mock
import pytest
import livereload
from mock import call
from watchdog import observers

import sphinx_autobuild
from sphinx_autobuild import main


@pytest.fixture(autouse=True)
def patched_args(sys_args, monkeypatch):
    """Patch sys args."""
    monkeypatch.setattr('sys.argv', sys_args)


@pytest.mark.parametrize('sys_args', (
    ['sphinx-autobuild', '/source', '/output'],
))
@mock.patch.object(observers.Observer, 'schedule')
@mock.patch.object(livereload.Server, 'serve')
@mock.patch('os.makedirs')
def test_autobuild(mock_makedirs, mock_serve, mock_schedule):
    """
    Test autobuild entry point.
    """
    main()
    mock_makedirs.assert_called_once_with('/output')
    mock_serve.assert_called_once_with(
        host='127.0.0.1', root='/output', port=8000)


@pytest.mark.parametrize('sys_args', (
    ['sphinx-autobuild', '/source', '/output',
     '--port', '8888',
     '--host', 'example.org',
     '--ignore', '/ignored',
     '--watch', '/external'],
))
@mock.patch.object(observers.Observer, 'schedule')
@mock.patch.object(livereload.Server, 'serve')
@mock.patch('sphinx_autobuild.SphinxBuilder')
@mock.patch.object(livereload.Server, 'watch')
@mock.patch('os.makedirs')
def test_autobuild_with_options(mock_makedirs,
                                mock_watch, mock_builder,
                                mock_serve, mock_schedule):
    """
    Test autobuild entry point with host, ignore, and watch options
    """
    main()
    mock_makedirs.assert_called_once_with('/output')

    # --port, --host
    mock_serve.assert_called_once_with(
        host='example.org', root='/output', port=8888)

    # --ignore
    mock_builder.assert_called_once_with(
        '/output', ['/source', '/output'], ['/ignored'])

    list_cmp_unordered = lambda l_0, l_1: all(elt_0 in l_1 for elt_0 in l_0)

    # --watch
    # ignore order of calls with `list_cmp_unordered`
    assert list_cmp_unordered(mock_watch.call_args_list,
                              [call('/source', mock_builder.return_value),
                               call('/external', mock_builder.return_value),
                               call('/output')])

Add test_autobuild_with_options