from contextlib import contextmanager

from invoke import MockContext
from invocations.pytest import test, coverage
from mock import Mock
from pytest import skip


@contextmanager
def _expect(flags=None, extra_flags=None, kwargs=None):
    if kwargs is None:
        kwargs = dict(pty=True)
    flags = flags or "--verbose --color=yes --capture=sys"
    if extra_flags is not None:
        flags = flags + " " + extra_flags
    c = MockContext(run=True)
    yield c
    c.run.assert_called_once_with("pytest {}".format(flags), **kwargs)


class test_:
    def defaults_to_verbose_color_and_syscapture_with_pty_True(self):
        # Relies on default flags within expect helper
        with _expect() as c:
            test(c)

    def can_turn_off_or_change_defaults(self):
        with _expect(flags="--capture=no", kwargs=dict(pty=False)) as c:
            test(c, verbose=False, color=False, pty=False, capture="no")

    def can_passthru_k_x_and_arbitrary_opts(self):
        with _expect(extra_flags="--whatever -man -k 'lmao' -x") as c:
            test(c, k="lmao", x=True, opts="--whatever -man")

    def can_disable_warnings(self):
        with _expect(extra_flags="--disable-warnings") as c:
            test(c, warnings=False)


class coverage_:
    FLAGS = "--cov --no-cov-on-fail --cov-report={}"

    def default_args(self):
        with _expect(extra_flags=self.FLAGS.format("term")) as c:
            coverage(c)

    def report_type(self):
        with _expect(extra_flags=self.FLAGS.format("xml")) as c:
            coverage(c, report="xml")

    def opts(self):
        with _expect(extra_flags=self.FLAGS.format("term") + " --meh") as c:
            coverage(c, opts="--meh")

    def test_function(self):
        c = MockContext()
        faketest = Mock()
        coverage(c, tester=faketest)
        faketest.assert_called_once_with(c, opts=self.FLAGS.format("term"))

    def open_html_report(self):
        c = MockContext(run=True, repeat=True)
        coverage(c, report="html")
        print(c.run.mock_calls)
        c.run.assert_any_call("open htmlcov/index.html")

    class codecov_support:
        def defaults_False(self):
            skip()

        def runs_xml_and_codecov_when_True(self):
            skip()

@@ -130,16 +130,22 @@
 ort Mock
+, call
 %0Afrom py
@@ -2173,81 +2173,342 @@
     
-skip()%0A%0A        def runs_xml_and_codecov_when_True(self):%0A            skip(
+c = MockContext(run=True, repeat=True)%0A            coverage(c)%0A            assert call(%22codecov%22) not in c.run.mock_calls%0A%0A        def runs_xml_and_codecov_when_True(self):%0A            c = MockContext(run=True, repeat=True)%0A            coverage(c, codecov=True)%0A            c.run.assert_has_calls(%5Bcall(%22coverage xml%22), call(%22codecov%22)%5D
 )%0A

Test codecov