import py, os, subprocess

def pytest_addoption(parser):
    group = parser.getgroup("RSqueak test options")
    group.addoption(
        "--quick", "-Q",
        dest="execute-quick-tests",
        action="store_false",
        default=True,
        help="Only execute quick tests (no image loading or long execution)"
    )
    group.addoption(
        "--slow", "-S",
        dest="execute-slow-tests",
        action="store_true",
        default=False,
        help="Execute all tests (including very slow tests)"
    )
    group.addoption(
        "--jit",
        dest="rsqueak-binary",
        action="store",
        default=None,
        help="Path to a compiled rsqueak binary. Enables jit tests."
    )

    group.addoption(
        "--jitargs",
        dest="jitargs",
        action="store",
        default=None,
        help="Arguments passed through to rsqueak."
    )

class _Executor_(object):
    def __init__(self, executable, *args):
        self.executable = executable
        self.args = list(args)
    def all_arguments(self, args):
        return map(str, [self.executable] + self.args + args)
    def system(self, *args):
        return os.system(" ".join(self.all_arguments(list(args))))
    def popen(self, *args, **kwargs):
        return subprocess.Popen(self.all_arguments(list(args)), **kwargs)

# The 'jit' parameter is used in tests under jittest/
def pytest_funcarg__spy(request):
    val = request.config.getvalue("rsqueak-binary")
    if not val:
        py.test.skip("Provide --jit parameter to execute jit tests")
    jitarg = request.config.getvalue("jitargs")
    if not jitarg:
        return _Executor_(py.path.local(val))
    else:
        return _Executor_(py.path.local(val), jitarg)

from .jittest.base import Trace
def pytest_assertrepr_compare(op, left, right):
    if isinstance(left, Trace) and isinstance(right, Trace) and op == "==":
        ltrace = [lop.name for lop in left.trace]
        rtrace = [rop.name for rop in right.trace]
        import difflib
        return (['Comparing Traces failed:'] +
                list(difflib.unified_diff(ltrace, rtrace)) +
                ["-------LEFT------"] +
                [str(op) for op in left.trace] +
                ["-------RIGHT-----"] +
                [str(op) for op in right.trace])


# Disable image loading output during testing
def fake_update(self, new_steps=-1):
    pass
from rsqueakvm.util.progress import Progress
Progress.update = fake_update

@@ -2014,22 +2014,24 @@
         
-return
+answer =
  (%5B'Comp
@@ -2071,16 +2071,18 @@
         
+  
 list(dif
@@ -2126,32 +2126,34 @@
                 
+  
 %5B%22-------LEFT---
@@ -2168,32 +2168,34 @@
                 
+ 
+ 
 %5Bstr(op) for op 
@@ -2219,32 +2219,34 @@
                 
+  
 %5B%22-------RIGHT--
@@ -2245,32 +2245,34 @@
 -RIGHT-----%22%5D +%0A
+  
                 
@@ -2304,16 +2304,1276 @@
 trace%5D)%0A
+    if os.environ.get(%22UPDATE_JITTESTS%22, None) == %221%22:%0A        print %22%5Cn%22.join(answer)%0A        import sys%0A        sys.stdout.write(%22Should we accept the new version (y/N)? %22)%0A        if sys.stdin.readline().strip().upper() == %22Y%22:%0A            import traceback, re%0A            stk = traceback.extract_stack()%0A            stk.reverse()%0A            for filename, lineno, funcname, text in stk:%0A                if re.search(%22jittest/test_%22, filename):%0A                    f = open(filename)%0A                    contents = f.readlines()%0A                    newline = %22%5Cr%5Cn%22 if contents%5B0%5D.endswith(%22%5Cr%5Cn%22) else %22%5Cn%22%0A                    while contents%5Blineno%5D.strip() != str(left.trace%5B0%5D).strip():%0A                        lineno += 1%0A                    indent = (len(contents%5Blineno%5D) - len(contents%5Blineno%5D.lstrip())) * %22 %22%0A                    contents%5Blineno:lineno + len(left.trace)%5D = %5B(indent + str(op) + newline) for op in right.trace%5D%0A                    f.close()%0A                    f = open(filename, %22w%22)%0A                    f.truncate(0)%0A                    f.writelines(contents)%0A                    f.close()%0A                    break%0A        sys.exit(os.system(%22%25s %5C%22%25s%5C%22%22 %25 (sys.executable, %22%5C%22 %5C%22%22.join(sys.argv))))%0A    else:%0A        return answer
 %0A%0A# Disa

add a simple helper to update failing jittests