<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals

import io
import mock
import os.path
import pytest
import sys

from pypi_practices import five
from pypi_practices.errors import FileValidationError
from pypi_practices.make_entry import make_entry
from testing.util import REMatcher


@pytest.yield_fixture
def fake_entry():
    class fake_entry_state(object):
        cwd_arg = None
        config_arg = None
        entry = None

    def check_fn(cwd, fix, config):
        fake_entry_state.cwd_arg = cwd
        fake_entry_state.fix_arg = fix
        fake_entry_state.config_arg = config
        return 0

    fake_entry_state.entry = staticmethod(make_entry(check_fn))
    yield fake_entry_state


def test_converts_args_to_text(fake_entry):
    # Native str (py2 vs py3)
    args = [str('--cwd'), str('path')]
    fake_entry.entry(args)
    assert type(fake_entry.cwd_arg) is five.text
    assert fake_entry.cwd_arg == 'path'


def test_cwd_defaults_to_dot(fake_entry):
    fake_entry.entry([])
    assert fake_entry.cwd_arg == '.'


def test_fix_calls_fix(fake_entry):
    fake_entry.entry(['--fix'])
    assert fake_entry.fix_arg is True


def test_ignores_extra_filename_args(fake_entry):
    fake_entry.entry(['README.md', 'tox.ini'])
    assert fake_entry.cwd_arg == '.'


@pytest.mark.parametrize('args', ([], ['--fix']))
def test_returns_0_for_ok(fake_entry, args):
    ret = fake_entry.entry(args)
    assert ret == 0


def test_no_args_passed_uses_sys_argv(fake_entry):
    with mock.patch.object(sys, 'argv', ['hook-exe', '--cwd', 'foo_cwd']):
        fake_entry.entry()
        assert fake_entry.cwd_arg == 'foo_cwd'


@pytest.yield_fixture
def print_mock():
    with mock.patch.object(five.builtins, 'print') as print_mock:
        yield print_mock


def test_ok_prints_nothing(fake_entry, print_mock):
    fake_entry.entry([])
    assert print_mock.call_count == 0


def test_raises_validation_error(print_mock):
    def raising_check(*_):
        raise FileValidationError(<commit_msg>Replace deprecated yield_fixture with fixture<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals

import io
import mock
import os.path
import pytest
import sys

from pypi_practices import five
from pypi_practices.errors import FileValidationError
from pypi_practices.make_entry import make_entry
from testing.util import REMatcher


@pytest.fixture
def fake_entry():
    class fake_entry_state(object):
        cwd_arg = None
        config_arg = None
        entry = None

    def check_fn(cwd, fix, config):
        fake_entry_state.cwd_arg = cwd
        fake_entry_state.fix_arg = fix
        fake_entry_state.config_arg = config
        return 0

    fake_entry_state.entry = staticmethod(make_entry(check_fn))
    yield fake_entry_state


def test_converts_args_to_text(fake_entry):
    # Native str (py2 vs py3)
    args = [str('--cwd'), str('path')]
    fake_entry.entry(args)
    assert type(fake_entry.cwd_arg) is five.text
    assert fake_entry.cwd_arg == 'path'


def test_cwd_defaults_to_dot(fake_entry):
    fake_entry.entry([])
    assert fake_entry.cwd_arg == '.'


def test_fix_calls_fix(fake_entry):
    fake_entry.entry(['--fix'])
    assert fake_entry.fix_arg is True


def test_ignores_extra_filename_args(fake_entry):
    fake_entry.entry(['README.md', 'tox.ini'])
    assert fake_entry.cwd_arg == '.'


@pytest.mark.parametrize('args', ([], ['--fix']))
def test_returns_0_for_ok(fake_entry, args):
    ret = fake_entry.entry(args)
    assert ret == 0


def test_no_args_passed_uses_sys_argv(fake_entry):
    with mock.patch.object(sys, 'argv', ['hook-exe', '--cwd', 'foo_cwd']):
        fake_entry.entry()
        assert fake_entry.cwd_arg == 'foo_cwd'


@pytest.fixture
def print_mock():
    with mock.patch.object(five.builtins, 'print') as print_mock:
        yield print_mock


def test_ok_prints_nothing(fake_entry, print_mock):
    fake_entry.entry([])
    assert print_mock.call_count == 0


def test_raises_validation_error(print_mock):
    def raising_check(*_):
        raise FileValidationError(
{'fraction_non_alphanumeric': 0.1024769305488101, 'fraction_numerical': 0.0029140359397765905, 'mean_word_length': 4.053987730061349, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4859442', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1410, 'n_words': 287}