Commits

Brodie Rao committed 3cd99da

Adding nicer-api patch

  • Participants
  • Parent commits beb503c

Comments (0)

Files changed (2)

+# HG changeset patch
+# Parent 620f57a4d8bc986c84f948561abffc27c9e22283
+diff --git a/cram.py b/cram.py
+--- a/cram.py
++++ b/cram.py
+@@ -133,7 +133,7 @@ def unified_diff(a, b, fromfile='', tofi
+                 for line in b[j1:j2]:
+                     yield '+' + line
+ 
+-def test(path):
++def test(testdata):
+     """Run test at path and return input, output, and diff.
+ 
+     This returns a 3-tuple containing the following:
+@@ -141,8 +141,17 @@ def test(path):
+         (list of lines in test, same list with actual output, diff)
+ 
+     diff is a generator that yields the diff between the two lists.
++
++    testdata can be either an object that implements splitlines(),
++    like str, or an iterable. If it's a file object, its path is
++    printed in the diff.
+     """
+-    f = open(path)
++    dpath = 'test'
++    if hasattr(testdata, 'splitlines'):
++        testdata = testdata.splitlines(True)
++    if hasattr(testdata, 'name'):
++        dpath = os.path.abspath(testdata.name)
++
+     p = subprocess.Popen(['/bin/sh', '-'], bufsize=-1, stdin=subprocess.PIPE,
+                          stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+                          universal_newlines=True,
+@@ -153,7 +162,7 @@ def test(path):
+     refout, postout = [], []
+ 
+     i = pos = prepos = -1
+-    for i, line in enumerate(f):
++    for i, line in enumerate(testdata):
+         refout.append(line)
+         if line.startswith('  $ '):
+             after.setdefault(pos, []).append(line)
+@@ -184,12 +193,15 @@ def test(path):
+             postout.append('  ' + line)
+     postout += after.pop(pos, [])
+ 
+-    dpath = os.path.abspath(path)
+     diff = unified_diff(refout, postout, dpath, dpath + '.err')
+     for firstline in diff:
+         return refout, postout, itertools.chain([firstline], diff)
+     return refout, postout, []
+ 
++def teststring(s):
++    """A shortcut for ''.join(cram.test(s)[2])"""
++    return ''.join(test(s)[2])
++
+ def _prompt(question, answers, auto=None):
+     """Write a prompt to stdout and ask for answer in stdin.
+ 
+@@ -263,7 +275,11 @@ def run(paths, quiet=False, verbose=Fals
+             try:
+                 if basetmp:
+                     os.chdir(tmpdir)
+-                refout, postout, diff = test(abspath)
++                f = open(abspath)
++                try:
++                    refout, postout, diff = test(f)
++                finally:
++                    f.close()
+             finally:
+                 if basetmp:
+                     os.chdir(cwd)
+nicer-api
 jobs