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

"""Test for test runner handler"""

import pytest
from mock import Mock
import purkinje_pytest.handler as sut


@pytest.fixture
def py_event():
    """An event concerning a Python file"""
    return Mock(dest_path='/a/b/c/myfile.py')


@pytest.fixture
def non_py_event():
    """An event not concerning a Python file
       (to be ignored)
    """
    return Mock(dest_path='/a/b/c/myfile.txt')


@pytest.fixture
def handler(monkeypatch):
    result = sut.Handler()
    monkeypatch.setattr(result,
                        '_trigger',
                        Mock(side_effect=result._trigger))
    monkeypatch.setattr(result,
                        'run_tests',
                        Mock())
    return result


def test_created_relevant_event(handler, py_event):
    handler.on_created(py_event)
    assert handler._trigger.called
    assert handler.run_tests.called


def test_created_irrelevant_event(handler,
                                  non_py_event):
    handler.on_created(non_py_event)
    assert handler._trigger.called
    assert not handler.run_tests.called


def test_deleted(handler, py_event):
    handler.on_deleted(py_event)
    assert handler._trigger.called


def test_modified(handler, py_event):
    handler.on_modified(py_event)
    assert handler._trigger.called


def test_moved(handler, py_event):
    handler.on_moved(py_event)
    assert handler._trigger.called


def test_run_tests(handler, monkeypatch):
    assert not handler._tests_running
    monkeypatch.setattr(sut.os, 'system', Mock())
    handler.run_tests()
    assert not handler._tests_running


def test_run_tests_with_error(handler, monkeypatch):
    assert not handler._tests_running

    def do_raise():
        raise Exception('Dummy exception')

    sys_mock = Mock(side_effect=do_raise)
    monkeypatch.setattr(sut.os, 'system', sys_mock)
    handler.run_tests()
    assert not handler._tests_running<commit_msg>use unpatched handler instance in tests of run_tests<commit_after># -*- coding: utf-8 -*-

"""Test for test runner handler"""

import pytest
from mock import Mock
import purkinje_pytest.handler as sut


@pytest.fixture
def py_event():
    """An event concerning a Python file"""
    return Mock(dest_path='/a/b/c/myfile.py')


@pytest.fixture
def non_py_event():
    """An event not concerning a Python file
       (to be ignored)
    """
    return Mock(dest_path='/a/b/c/myfile.txt')


@pytest.fixture
def unpatched_handler():
    return sut.Handler()  # need unpatched run_tests


@pytest.fixture
def handler(monkeypatch):
    result = sut.Handler()
    monkeypatch.setattr(result,
                        '_trigger',
                        Mock(side_effect=result._trigger))
    monkeypatch.setattr(result,
                        'run_tests',
                        Mock())
    return result


def test_created_relevant_event(handler, py_event):
    handler.on_created(py_event)
    assert handler._trigger.called
    assert handler.run_tests.called


def test_created_irrelevant_event(handler,
                                  non_py_event):
    handler.on_created(non_py_event)
    assert handler._trigger.called
    assert not handler.run_tests.called


def test_deleted(handler, py_event):
    handler.on_deleted(py_event)
    assert handler._trigger.called


def test_modified(handler, py_event):
    handler.on_modified(py_event)
    assert handler._trigger.called


def test_moved(handler, py_event):
    handler.on_moved(py_event)
    assert handler._trigger.called


def test_run_tests(unpatched_handler, monkeypatch):
    assert not unpatched_handler._tests_running

    def check_state(_):
        assert unpatched_handler._tests_running

    monkeypatch.setattr(sut.os, 'system', Mock(side_effect=check_state))
    unpatched_handler.run_tests()
    assert sut.os.system.called
    assert not unpatched_handler._tests_running


def test_run_tests_with_error(unpatched_handler, monkeypatch):
    assert not unpatched_handler._tests_running

    def do_raise(_):
        raise Exception('Dummy exception')

    sys_mock = Mock(side_effect=do_raise)
    monkeypatch.setattr(sut.os, 'system', sys_mock)
    with pytest.raises(Exception):
        unpatched_handler.run_tests()
    assert not unpatched_handler._tests_running
{'fraction_non_alphanumeric': 0.09249767008387698, 'fraction_numerical': 0.00046598322460391424, 'mean_word_length': 3.2379072063178675, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6053503', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1315, 'n_words': 272}