Commits

Brodie Rao  committed abf30d4

Cleaning up queue and adding custom-indent patch

  • Participants
  • Parent commits ec6eeb9

Comments (0)

Files changed (4)

File custom-indent

+# HG changeset patch
+# User Brodie Rao <brodie@bitheap.org>
+# Date 1290220453 -39600
+# Node ID 51a7e99b70800874dafac03ca80917683ea1b832
+# Parent af86513f6ca57d9a2024131bad75ab5d152b85b4
+Add --indent=n to support custom levels of indentation
+
+diff --git a/cram.py b/cram.py
+--- a/cram.py
++++ b/cram.py
+@@ -134,7 +134,7 @@ def escape(s):
+     """Like the string-escape codec, but doesn't escape quotes"""
+     return escapesub(lambda m: escapemap[m.group(0)], s[:-1]) + ' (esc)\n'
+ 
+-def test(path):
++def test(path, indent=2):
+     """Run test at path and return input, output, and diff.
+ 
+     This returns a 3-tuple containing the following:
+@@ -146,6 +146,10 @@ def test(path):
+     If a test exits with return code 80, the actual output is set to
+     None and diff is set to [].
+     """
++    indent = ' ' * indent
++    cmdline = '%s$ ' % indent
++    conline = '%s> ' % indent
++
+     f = open(path)
+     abspath = os.path.abspath(path)
+     env = os.environ.copy()
+@@ -161,16 +165,16 @@ def test(path):
+     i = pos = prepos = -1
+     for i, line in enumerate(f):
+         refout.append(line)
+-        if line.startswith('  $ '):
++        if line.startswith(cmdline):
+             after.setdefault(pos, []).append(line)
+             prepos = pos
+             pos = i
+             p.stdin.write('echo "\n%s %s $?"\n' % (salt, i))
+-            p.stdin.write(line[4:])
+-        elif line.startswith('  > '):
++            p.stdin.write(line[len(cmdline):])
++        elif line.startswith(conline):
+             after.setdefault(prepos, []).append(line)
+-            p.stdin.write(line[4:])
+-        elif not line.startswith('  '):
++            p.stdin.write(line[len(conline):])
++        elif not line.startswith(indent):
+             after.setdefault(pos, []).append(line)
+     p.stdin.write('echo "\n%s %s $?"\n' % (salt, i + 1))
+ 
+@@ -191,17 +195,17 @@ def test(path):
+         line += '\n'
+         if line.startswith(salt):
+             presalt = postout.pop()
+-            if presalt != '  \n':
++            if presalt != '%s\n' % indent:
+                 postout.append(presalt[:-1] + ' (no-eol)\n')
+             ret = int(line.split()[2])
+             if ret != 0:
+-                postout.append('  [%s]\n' % ret)
++                postout.append('%s[%s]\n' % (indent, ret))
+             postout += after.pop(pos, [])
+             pos = int(line.split()[1])
+         else:
+             if needescape(line):
+                 line = escape(line)
+-            postout.append('  ' + line)
++            postout.append(indent + line)
+     postout += after.pop(pos, [])
+ 
+     diff = unified_diff(refout, postout, abspath, abspath + '.err')
+@@ -257,7 +261,8 @@ def patch(cmd, diff):
+     p.wait()
+     return p.returncode == 0
+ 
+-def run(paths, tmpdir, quiet=False, verbose=False, patchcmd=None, answer=None):
++def run(paths, tmpdir, quiet=False, verbose=False, patchcmd=None, answer=None,
++        indent=2):
+     """Run tests in paths in tmpdir.
+ 
+     If quiet is True, diffs aren't printed. If verbose is True,
+@@ -286,7 +291,7 @@ def run(paths, tmpdir, quiet=False, verb
+             os.mkdir(testdir)
+             try:
+                 os.chdir(testdir)
+-                refout, postout, diff = test(abspath)
++                refout, postout, diff = test(abspath, indent)
+             finally:
+                 os.chdir(cwd)
+ 
+@@ -356,6 +361,8 @@ def main(args):
+                  help='answer no to all questions')
+     p.add_option('--keep-tmpdir', action='store_true',
+                  help='keep temporary directories')
++    p.add_option('--indent', action='store', default=2, metavar='NUM',
++                 type='int', help='number of spaces to use for indentation')
+     p.add_option('-E', action='store_false', dest='sterilize', default=True,
+                  help="don't reset common environment variables")
+     opts, paths = p.parse_args(args)
+@@ -415,7 +422,8 @@ warranty; not even for MERCHANTABILITY o
+         answer = None
+ 
+     try:
+-        return run(paths, tmpdir, opts.quiet, opts.verbose, patchcmd, answer)
++        return run(paths, tmpdir, opts.quiet, opts.verbose, patchcmd, answer,
++                   opts.indent)
+     finally:
+         if opts.keep_tmpdir:
+             log('# Kept temporary directory: %s\n' % tmpdir)
+diff --git a/tests/cram.t b/tests/cram.t
+--- a/tests/cram.t
++++ b/tests/cram.t
+@@ -24,6 +24,7 @@ Usage:
+     -y, --yes          answer yes to all questions
+     -n, --no           answer no to all questions
+     --keep-tmpdir      keep temporary directories
++    --indent=NUM       number of spaces to use for indentation
+     -E                 don't reset common environment variables
+   $ cram -V
+   Cram CLI testing framework (version 0.4)
+@@ -429,3 +430,20 @@ Test --keep-tmpdir:
+ 
+ Note: We can't set the locale to foo because some shells will issue
+ warnings for invalid locales.
++
++Custom indentation:
++
++  $ cat > indent.t <<EOF
++  > Indented by 4 spaces:
++  > 
++  >     $ echo foo
++  >     foo
++  > 
++  > Not part of the test:
++  > 
++  >   $ echo foo
++  >   bar
++  > EOF
++  $ cram --indent=4 indent.t
++  .
++  # Ran 1 tests, 0 skipped, 0 failed.
+# HG changeset patch
+# Parent 3baf83bdc5d21ef2d3b5f6461dd3c52bf2b81318
-spaces
-cleanup
-whitespace #+whitespace
+custom-indent
+n-match
+spaces #+spaces
+cleanup #+spaces
 runone #+api
 nicer-api #+api
 stdin #+api

File whitespace

-# HG changeset patch
-# Parent 0171f5b9e4c7c4e0c6849b2b35cc01bdd8a45236
-diff --git a/cram.py b/cram.py
---- a/cram.py
-+++ b/cram.py
-@@ -137,24 +137,27 @@ def test(path):
-                          universal_newlines=True, env=env,
-                          close_fds=os.name == 'posix')
-     salt = 'CRAM%s' % time.time()
-+    presalt = 'PRE' + salt
- 
-     after = {}
-     refout, postout = [], []
-     i = pos = prepos = -1
-     for i, line in enumerate(f):
-         refout.append(line)
--        if line.startswith('  $ '):
-+        sl = line.lstrip()
-+        indent = line[:-len(sl)]
-+        if not indent:
-+            after.setdefault(pos, []).append(line)
-+        elif sl.startswith('$ '):
-             after.setdefault(pos, []).append(line)
-             prepos = pos
-             pos = i
--            p.stdin.write('echo "\n%s %s $?"\n' % (salt, i))
--            p.stdin.write(line[4:])
--        elif line.startswith('  > '):
-+            p.stdin.write('echo "PRE%s\n%s %s $?"\n' % (salt, salt, i))
-+            p.stdin.write(sl[2:])
-+        elif sl.startswith('> '):
-             after.setdefault(prepos, []).append(line)
--            p.stdin.write(line[4:])
--        elif not line.startswith('  '):
--            after.setdefault(pos, []).append(line)
--    p.stdin.write('echo "\n%s %s $?"\n' % (salt, i + 1))
-+            p.stdin.write(sl[2:])
-+    p.stdin.write('echo "PRE%s\n%s %s $?"\n' % (salt, salt, i + 1))
- 
-     output = p.communicate()[0]
-     if p.returncode == 80:
-@@ -164,9 +167,11 @@ def test(path):
-     ret = 0
-     for i, line in enumerate(output.splitlines(True)):
-         if line.startswith(salt):
--            presalt = postout.pop()
--            if presalt != '  \n':
--                postout.append(presalt[:-1] + '%\n')
-+            preline = postout.pop()
-+            print repr(preline), repr(presalt)
-+            if preline[:-1].endswith(presalt):
-+                print repr(preline[:-len(presalt) - 1])
-+                postout.append(preline[1:-len(presalt) - 1] + '%\n')
-             ret = int(line.split()[2])
-             if ret != 0:
-                 postout.append('  [%s]\n' % ret)