<commit_before>import locale
import sys
from invoke.vendor.six import StringIO

from spec import Spec, trap, eq_, skip, ok_
from mock import patch, Mock

from invoke import Runner, Local, Context, Config

from _utils import mock_subprocess, mock_pty


class Dummy(Runner):
    """
    Dummy runner subclass that does minimum work required to execute run().
    """
    def start(self, command):
        pass

    def stdout_reader(self):
        return lambda n: ""

    def stderr_reader(self):
        return lambda n: ""

    def default_encoding(self):
        return "US-ASCII"

    def wait(self):
        pass

    def returncode(self):
        return 0


def _expect_encoding(codecs, encoding):
    assert codecs.iterdecode.called
    for call in codecs.iterdecode.call_args_list:
        eq_(call[0][1], encoding)


class Runner_(Spec):
    def _run(self, *args, **kwargs):
        settings = kwargs.pop('settings', {})
        context = Context(config=Config(overrides=settings))
        return Dummy(context).run(*args, **kwargs)

    class echoing:
        @trap
        def off_by_default(self):
            self._run("my command")
            eq_(sys.stdout.getvalue(), "")

        @trap
        def enabled_via_kwarg(self):
            self._run("my command", echo=True)
            ok_("my command" in sys.stdout.getvalue())

        @trap
        def enabled_via_config(self):
            self._run("yup", settings={'run': {'echo': True}})
            ok_("yup" in sys.stdout.getvalue())

        @trap
        def kwarg_beats_config(self):
            self._run("yup", echo=True, settings={'run': {'echo': False}})
            ok_("yup" in sys.stdout.getvalue())<commit_msg>Start migrating return-value tests from run.py<commit_after>import locale
import sys
from invoke.vendor.six import StringIO

from spec import Spec, trap, eq_, skip, ok_
from mock import patch, Mock

from invoke import Runner, Local, Context, Config

from _utils import mock_subprocess, mock_pty


class Dummy(Runner):
    """
    Dummy runner subclass that does minimum work required to execute run().
    """
    def start(self, command):
        pass

    def stdout_reader(self):
        return lambda n: ""

    def stderr_reader(self):
        return lambda n: ""

    def default_encoding(self):
        return "US-ASCII"

    def wait(self):
        pass

    def returncode(self):
        return 0


def _expect_encoding(codecs, encoding):
    assert codecs.iterdecode.called
    for call in codecs.iterdecode.call_args_list:
        eq_(call[0][1], encoding)


class Runner_(Spec):
    def _run(self, *args, **kwargs):
        settings = kwargs.pop('settings', {})
        context = Context(config=Config(overrides=settings))
        return Dummy(context).run(*args, **kwargs)

    class return_value:
        def return_code_in_result(self):
            """
            Result has .return_code (and .exited) containing exit code int
            """
            runner = Dummy(Context())
            runner.returncode = Mock(return_value=17)
            r = runner.run("nope", warn=True)
            eq_(r.return_code, 17)
            eq_(r.exited, 17)

        @trap
        def stdout_attribute_contains_stdout(self):
            runner = Dummy(Context())
            out_file = StringIO("foo")
            def reader(count):
                return out_file.read(count)
            runner.stdout_reader = lambda: reader
            eq_(runner.run("nope").stdout, "foo")
            eq_(sys.stdout.getvalue(), "foo")

        #def stderr_attribute_contains_stderr(self):
        #    eq_(run(self.err, hide='both').stderr, 'bar\n')

        #def ok_attr_indicates_success(self):
        #    eq_(_run().ok, True)
        #    eq_(_run(returns={'exited': 1}, warn=True).ok, False)

        #def failed_attr_indicates_failure(self):
        #    eq_(_run().failed, False)
        #    eq_(_run(returns={'exited': 1}, warn=True).failed, True)

        #def has_exception_attr(self):
        #    eq_(_run().exception, None)


    class echoing:
        @trap
        def off_by_default(self):
            self._run("my command")
            eq_(sys.stdout.getvalue(), "")

        @trap
        def enabled_via_kwarg(self):
            self._run("my command", echo=True)
            ok_("my command" in sys.stdout.getvalue())

        @trap
        def enabled_via_config(self):
            self._run("yup", settings={'run': {'echo': True}})
            ok_("yup" in sys.stdout.getvalue())

        @trap
        def kwarg_beats_config(self):
            self._run("yup", echo=True, settings={'run': {'echo': False}})
            ok_("yup" in sys.stdout.getvalue())
{'fraction_non_alphanumeric': 0.1232583065380493, 'fraction_numerical': 0.0030010718113612006, 'mean_word_length': 2.4008746355685133, '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': '1629086', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1388, 'n_words': 339}