<commit_before># -*- coding: utf-8 -*-


import textwrap

import pytest


@pytest.mark.parametrize("marker_type", ["setup", "teardown"])
def test_simple(testdir, inventory, minimal_playbook, marker_type):
    """
    Make sure that``ansible_playbook`` fixture is recognized and pytest itself
    is not broken by running very simple playbook which has no side effects.
    """
    # create a temporary pytest test module
    testdir.makepyfile(textwrap.dedent("""\
        import pytest

        @pytest.mark.ansible_playbook_{0}('{1}')
        def test_foo(ansible_playbook):
            assert 1 == 1
        """.format(marker_type, minimal_playbook.basename)))
    # run pytest with the following cmd args
    result = testdir.runpytest(
        '--ansible-playbook-directory={0}'.format(minimal_playbook.dirname),
        '--ansible-playbook-inventory={0}'.format(inventory.basename),
        '-v',
        )
    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(['*::test_foo PASSED*'])
    # make sure that that we get a '0' exit code for the testsuite
    assert result.ret == 0


@pytest.mark.parametrize("marker_type", ["setup", "teardown"])
def test_checkfile(
        testdir, inventory, testfile_playbook_generator, marker_type):
    """
    Make sure that ``ansible_playbook`` fixture is actually executes
    given playbook.
    """
    playbook, test_file_path, test_file_content = \
        testfile_playbook_generator.get()
    # create a temporary pytest test module
    testdir.makepyfile(textwrap.dedent("""\
        import pytest
<commit_msg>add negative unit test for markers on fixture<commit_after># -*- coding: utf-8 -*-


import textwrap

import pytest


@pytest.mark.parametrize("marker_type", ["setup", "teardown"])
def test_simple(testdir, inventory, minimal_playbook, marker_type):
    """
    Make sure that``ansible_playbook`` fixture is recognized and pytest itself
    is not broken by running very simple playbook which has no side effects.
    """
    # create a temporary pytest test module
    testdir.makepyfile(textwrap.dedent("""\
        import pytest

        @pytest.mark.ansible_playbook_{0}('{1}')
        def test_foo(ansible_playbook):
            assert 1 == 1
        """.format(marker_type, minimal_playbook.basename)))
    # run pytest with the following cmd args
    result = testdir.runpytest(
        '--ansible-playbook-directory={0}'.format(minimal_playbook.dirname),
        '--ansible-playbook-inventory={0}'.format(inventory.basename),
        '-v',
        )
    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(['*::test_foo PASSED*'])
    # make sure that that we get a '0' exit code for the testsuite
    assert result.ret == 0


@pytest.mark.parametrize("marker_type", ["setup", "teardown"])
def test_simple_usedbyfixture(
        testdir,
        inventory,
        minimal_playbook,
        marker_type):
    """
    Check what happens when ``ansible_playbook`` fixture is used with a fixtrue
    by running very simple playbook which has no side effects.

    This is expected to end with ERROR as mark has no effect in fixture
    functions, see also:

     * https://gitlab.com/mbukatov/pytest-ansible-playbook/issues/4
     * https://github.com/pytest-dev/pytest/issues/3664

    I keep it here as a reference and to be able to notice any changes related
    to this use case in pytest.
    """
    # create a temporary pytest test module
    testdir.makepyfile(textwrap.dedent("""\
        import pytest

        @pytest.mark.ansible_playbook_{0}('{1}')
        @pytest.fixture
        def fixture_foo(ansible_playbook):
            return 1

        def test_foo(fixture_foo):
            assert 1 == fixture_foo
        """.format(marker_type, minimal_playbook.basename)))
    # run pytest with the following cmd args
    result = testdir.runpytest(
        '--ansible-playbook-directory={0}'.format(minimal_playbook.dirname),
        '--ansible-playbook-inventory={0}'.format(inventory.basename),
        '-v',
        )
    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(['*::test_foo ERROR*'])
    # make sure that that we get a '1' exit code for the testsuite
    assert result.ret == 1


@pytest.mark.parametrize("marker_type", ["setup", "teardown"])
def test_checkfile(
        testdir, inventory, testfile_playbook_generator, marker_type):
    """
    Make sure that ``ansible_playbook`` fixture is actually executes
    given playbook.
    """
    playbook, test_file_path, test_file_content = \
        testfile_playbook_generator.get()
    # create a temporary pytest test module
    testdir.makepyfile(textwrap.dedent("""\
        import pytest

{'fraction_non_alphanumeric': 0.10986214209968187, 'fraction_numerical': 0.006574761399787911, 'mean_word_length': 3.155066079295154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5202214', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1454, 'n_words': 377}