<commit_before># pylint: disable=line-too-long, global-statement, unused-argument
# pylint: disable=invalid-name, too-many-locals, too-many-public-methods
# pylint: disable=too-few-public-methods
from __future__ import print_function, absolute_import, division

from argparse import Namespace
import os
import signal

from testfixtures import TempDirectory

import jacquard.jacquard as jacquard
import jacquard.utils.logger as logger
import jacquard.utils.utils as utils
import test.utils.mock_module as mock_module
import test.utils.test_case as test_case


MOCK_MOVE_TEMP_CONTENTS_CALLED = False
MOCK_PREFLIGHT_CALLED = False

def mock_preflight(output, desired_output_files, command):
    global MOCK_PREFLIGHT_CALLED
    MOCK_PREFLIGHT_CALLED = True

def mock_move_tmp_contents_to_original(args):
    global MOCK_MOVE_TEMP_CONTENTS_CALLED
    MOCK_MOVE_TEMP_CONTENTS_CALLED = True

def _change_mock_methods():
    jacquard._move_tmp_contents_to_original = mock_move_tmp_contents_to_original
    jacquard._preflight = mock_preflight


class MockSignalDispatcher(object):
    def __init__(self):
        self.calls = []

    def signal(self, signal_num, handler):
        self.calls.append((signal_num, handler))

class JacquardArgumentParserTestCase(test_case.JacquardBaseTestCase):
    def test_error_raisesMessage(self):
        message = "foo"
        parser = jacquard._JacquardArgumentParser()
        self.assertRaisesRegexp(utils.UsageError, "^foo$", parser.error, message)

    def test_error_raisesTransformedMessage(self):
        message = "argument subparser_name: invalid choice: 'foo' (choose from 'bar', 'baz')"
        parser = jacquard._JacquardArgumentParser()
        self.assertRaisesRegexp(utils.UsageError,
                                "^'foo' is not a Jacquard command; choose from 'bar', 'baz'.$",
                                parser.error,
                                message)

class JacquardTestCase(test_case.JacquardBaseTestCase):
    def test_version(self):
        self.assertEquals("0.42", jacquard.__version__)

    def test_get_execution_context(self):
        command = "foo input_dir output_dir"
        actual = jacquard._get_execution_context(command)
        expected = r'##jacquard=<timestamp=".*",command="foo input_dir output_dir",cwd=".*",source="Jacquard",version=".*">'
        self.assertRegexpMatches(actual[0], expected)

    def test_set_interrupt_handler(self):
        mock_signal_dispatcher = MockSignalDispatcher()

        jacquard._set_interrupt_handler(mock_signal_dispatcher.signal)

        self.assertEquals(2, len(mock_signal_dispatcher.calls))
        signal_type1, handler1 = mock_signal_dispatcher.calls[0]
        signal_type2, handler2 = mock_signal_dispatcher.calls[1]
        self.assertEquals(set([signal.SIGINT, signal.SIGTERM]),
                          set([signal_type1, signal_type2]))
        self.assertEquals(handler1, handler2)
        with self.assertRaises(SystemExit) as exit_code:
            handler1(None, None)
        self.assertEquals(exit_code.exception.code, 1)
        self.assertRegexpMatches(self.output.getvalue(), "WARNING: Jacquard was interrupted")

    def test_gracefulErrorMessageWhenUnanticipatedProblem(self):
        with TempDirectory() as input_dir, TempDirectory() as output_dir:
            mock_module.my_exception_string = "I'm feeling angry"<commit_msg>ROLL-11 (cgates/jebene): Fix version test<commit_after># pylint: disable=line-too-long, global-statement, unused-argument
# pylint: disable=invalid-name, too-many-locals, too-many-public-methods
# pylint: disable=too-few-public-methods
from __future__ import print_function, absolute_import, division

from argparse import Namespace
import os
import signal

from testfixtures import TempDirectory

import jacquard.jacquard as jacquard
import jacquard.utils.logger as logger
import jacquard.utils.utils as utils
import test.utils.mock_module as mock_module
import test.utils.test_case as test_case


MOCK_MOVE_TEMP_CONTENTS_CALLED = False
MOCK_PREFLIGHT_CALLED = False

def mock_preflight(output, desired_output_files, command):
    global MOCK_PREFLIGHT_CALLED
    MOCK_PREFLIGHT_CALLED = True

def mock_move_tmp_contents_to_original(args):
    global MOCK_MOVE_TEMP_CONTENTS_CALLED
    MOCK_MOVE_TEMP_CONTENTS_CALLED = True

def _change_mock_methods():
    jacquard._move_tmp_contents_to_original = mock_move_tmp_contents_to_original
    jacquard._preflight = mock_preflight


class MockSignalDispatcher(object):
    def __init__(self):
        self.calls = []

    def signal(self, signal_num, handler):
        self.calls.append((signal_num, handler))

class JacquardArgumentParserTestCase(test_case.JacquardBaseTestCase):
    def test_error_raisesMessage(self):
        message = "foo"
        parser = jacquard._JacquardArgumentParser()
        self.assertRaisesRegexp(utils.UsageError, "^foo$", parser.error, message)

    def test_error_raisesTransformedMessage(self):
        message = "argument subparser_name: invalid choice: 'foo' (choose from 'bar', 'baz')"
        parser = jacquard._JacquardArgumentParser()
        self.assertRaisesRegexp(utils.UsageError,
                                "^'foo' is not a Jacquard command; choose from 'bar', 'baz'.$",
                                parser.error,
                                message)

class JacquardTestCase(test_case.JacquardBaseTestCase):
    def test_version(self):
        self.assertEquals("0.43", jacquard.__version__)

    def test_get_execution_context(self):
        command = "foo input_dir output_dir"
        actual = jacquard._get_execution_context(command)
        expected = r'##jacquard=<timestamp=".*",command="foo input_dir output_dir",cwd=".*",source="Jacquard",version=".*">'
        self.assertRegexpMatches(actual[0], expected)

    def test_set_interrupt_handler(self):
        mock_signal_dispatcher = MockSignalDispatcher()

        jacquard._set_interrupt_handler(mock_signal_dispatcher.signal)

        self.assertEquals(2, len(mock_signal_dispatcher.calls))
        signal_type1, handler1 = mock_signal_dispatcher.calls[0]
        signal_type2, handler2 = mock_signal_dispatcher.calls[1]
        self.assertEquals(set([signal.SIGINT, signal.SIGTERM]),
                          set([signal_type1, signal_type2]))
        self.assertEquals(handler1, handler2)
        with self.assertRaises(SystemExit) as exit_code:
            handler1(None, None)
        self.assertEquals(exit_code.exception.code, 1)
        self.assertRegexpMatches(self.output.getvalue(), "WARNING: Jacquard was interrupted")

    def test_gracefulErrorMessageWhenUnanticipatedProblem(self):
        with TempDirectory() as input_dir, TempDirectory() as output_dir:
            mock_module.my_exception_string = "I'm feeling angry"
{'fraction_non_alphanumeric': 0.08869539551357733, 'fraction_numerical': 0.005312868949232586, 'mean_word_length': 3.9215686274509802, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3061425', 'n_tokens_mistral': 2102, 'n_tokens_neox': 2044, 'n_words': 379}