<commit_before>import os
import json
from StringIO import StringIO

import six

if six.PY2:
    from mock import patch
else:
    from unittest.mock import patch

from django.conf import settings
from django.test import TestCase
from django.core.management import call_command, CommandError


class CommandsTests(TestCase):

    def test_eval_command_with_only_pypy_interact_option(self):
        namespace = json.dumps({})
        out = StringIO()

        with self.assertRaises(CommandError):
            call_command('eval', '1+1', namespace, pypy_interact="pypy_interact.py", stdout=out)

    @patch('subprocess.check_output')
    def test_eval_command_with_all_options_to_run_in_pypy(self, check_output):
        namespace = json.dumps({})
        out = StringIO()

        call_command('eval', '1+1', namespace,
                     pypy_interact="pypy_interact.py",
                     tmp_folder="TryPython/",
                     pypy_c="/usr/bin/pypy-c",
                     stdout=out)

        eval_script_path = os.path.join(settings.BASE_DIR, 'eval.py')
        check_output.assert_called_with(
            ['/usr/bin/python',
             'pypy_interact.py',
             '--tmp=TryPython/',
             '/usr/bin/pypy-c',
             eval_script_path,
             '1+1',
             '{}']
        )<commit_msg>more tests<commit_after>import sys
import os
import json
from StringIO import StringIO
from subprocess import CalledProcessError

import six

if six.PY2:
    from mock import patch
else:
    from unittest.mock import patch

from django.conf import settings
from django.test import TestCase
from django.core.management import call_command, CommandError


class CommandsTests(TestCase):

    def setUp(self):
        super(self.__class__, self).setUp()
        self.namespace = json.dumps({})
        self.out = StringIO()
        self.fake_stderr = StringIO()
        self.original_stderr = sys.stderr
        sys.stderr = self.fake_stderr

    def tearDown(self):
        super(self.__class__, self).tearDown()
        sys.stderr = self.original_stderr

    def test_eval_command_with_only_pypy_interact_option(self):
        with self.assertRaises(CommandError):
            call_command('eval', '1+1', self.namespace,
                         pypy_interact="pypy_interact.py", stdout=self.out)

    @patch('subprocess.check_output')
    def test_subprocess_raises_called_error(self, check_output):
        check_output.side_effect = CalledProcessError(2, "<console>")

        call_command('eval', '1+1', self.namespace)
        self.assertEqual(self.fake_stderr.getvalue(), "Command '<console>' returned non-zero exit status 2")

    @patch('subprocess.check_output')
    def test_eval_command_with_all_options_to_run_in_pypy(self, check_output):

        call_command('eval', '1+1', self.namespace,
                     pypy_interact="pypy_interact.py",
                     tmp_folder="TryPython/",
                     pypy_c="/usr/bin/pypy-c",
                     stdout=self.out)

        eval_script_path = os.path.join(settings.BASE_DIR, 'eval.py')
        check_output.assert_called_with(
            ['/usr/bin/python',
             'pypy_interact.py',
             '--tmp=TryPython/',
             '/usr/bin/pypy-c',
             eval_script_path,
             '1+1',
             '{}']
        )
{'fraction_non_alphanumeric': 0.10771992818671454, 'fraction_numerical': 0.005385996409335727, 'mean_word_length': 2.460662525879917, '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': '4609648', 'n_tokens_mistral': 1047, 'n_tokens_neox': 1013, 'n_words': 185}