"""test_replay."""
import os

import pytest

from cookiecutter import exceptions, main, replay


@pytest.mark.parametrize("replay_file_name", ['bar', 'bar.json'])
def test_get_replay_file_name(replay_file_name):
    """Make sure that replay.get_file_name generates a valid json file path."""
    exp_replay_file_path = os.path.join('foo', 'bar.json')
    replay_file_path = replay.get_file_name('foo', replay_file_name)
    assert replay_file_path == exp_replay_file_path


@pytest.mark.parametrize(
    'invalid_kwargs',
    (
        {'no_input': True},
        {'extra_context': {}},
        {'no_input': True, 'extra_context': {}},
    ),
)
def test_raise_on_invalid_mode(invalid_kwargs):
    """Test `cookiecutter` raise exception on unacceptable `replay` request."""
    with pytest.raises(exceptions.InvalidModeException):
        main.cookiecutter('foo', replay=True, **invalid_kwargs)


def test_main_does_not_invoke_dump_but_load(mocker):
    """Test `cookiecutter` calling correct functions on `replay`."""
    mock_prompt = mocker.patch('cookiecutter.main.prompt_for_config')
    mock_gen_context = mocker.patch('cookiecutter.main.generate_context')
    mock_gen_files = mocker.patch('cookiecutter.main.generate_files')
    mock_replay_dump = mocker.patch('cookiecutter.main.dump')
    mock_replay_load = mocker.patch('cookiecutter.main.load')

    main.cookiecutter('tests/fake-repo-tmpl/', replay=True)

    assert not mock_prompt.called
    assert mock_gen_context.called
    assert mock_replay_dump.called
    assert mock_replay_load.called
    assert mock_gen_files.called


def test_main_does_not_invoke_load_but_dump(mocker):
    """Test `cookiecutter` calling correct functions on non-replay launch."""
    mock_prompt = mocker.patch('cookiecutter.main.prompt_for_config')
    mock_gen_context = mocker.patch('cookiecutter.main.generate_context')
    mock_gen_files = mocker.patch('cookiecutter.main.generate_files')
    mock_replay_dump = mocker.patch('cookiecutter.main.dump')
    mock_replay_load = mocker.patch('cookiecutter.main.load')

    main.cookiecutter('tests/fake-repo-tmpl/', replay=False)

    assert mock_prompt.called
    assert mock_gen_context.called
    assert mock_replay_dump.called
    assert not mock_replay_load.called
    assert mock_gen_files.called
