Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions cram/_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Utilities for running individual tests"""

import itertools
import tempfile
import contextlib
import os
import re
import time
Expand All @@ -20,6 +22,14 @@ def _escape(s):
return (_escapesub(lambda m: _escapemap[m.group(0)], s[:-1]) +
b' (esc)\n')

@contextlib.contextmanager
def _maketestfile(script):
"""Write test to a temporary file and yield the path"""
with tempfile.NamedTemporaryFile() as f:
f.write(b''.join(script))
f.flush()
yield f.name

def test(lines, shell='/bin/sh', indent=2, testname=None, env=None,
cleanenv=True, debug=False):
r"""Run test lines and return input, output, and diff.
Expand Down Expand Up @@ -103,22 +113,23 @@ def test(lines, shell='/bin/sh', indent=2, testname=None, env=None,
env['TESTSHELL'] = shell[0]

if debug:
stdin = []
script = []
for line in lines:
if not line.endswith(b'\n'):
line += b'\n'
if line.startswith(cmdline):
stdin.append(line[len(cmdline):])
script.append(line[len(cmdline):])
elif line.startswith(conline):
stdin.append(line[len(conline):])
script.append(line[len(conline):])

execute(shell + ['-'], stdin=b''.join(stdin), env=env)
with _maketestfile(script) as f:
execute(shell + [f], env=env)
return ([], [], [])

after = {}
refout, postout = [], []
i = pos = prepos = -1
stdin = []
script = []
for i, line in enumerate(lines):
if not line.endswith(b'\n'):
line += b'\n'
Expand All @@ -127,17 +138,18 @@ def test(lines, shell='/bin/sh', indent=2, testname=None, env=None,
after.setdefault(pos, []).append(line)
prepos = pos
pos = i
stdin.append(b'echo %s %d $?\n' % (salt, i))
stdin.append(line[len(cmdline):])
script.append(b'echo %s %d $?\n' % (salt, i))
script.append(line[len(cmdline):])
elif line.startswith(conline):
after.setdefault(prepos, []).append(line)
stdin.append(line[len(conline):])
script.append(line[len(conline):])
elif not line.startswith(indent):
after.setdefault(pos, []).append(line)
stdin.append(b'echo %s %d $?\n' % (salt, i + 1))
script.append(b'echo %s %d $?\n' % (salt, i + 1))

output, retcode = execute(shell + ['-'], stdin=b''.join(stdin),
stdout=PIPE, stderr=STDOUT, env=env)
with _maketestfile(script) as f:
output, retcode = execute(shell + [f],
stdout=PIPE, stderr=STDOUT, env=env)
if retcode == 80:
return (refout, None, [])

Expand Down
1 change: 1 addition & 0 deletions examples/env.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Check environment variables:
fail.t
missingeol.t
skip.t
stdin.t
test.t
$ echo "$TESTFILE"
env.t
Expand Down
9 changes: 9 additions & 0 deletions examples/stdin.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Test that consumes stdin. Cram should not fail on this:

$ echo 123
123
$ cat > /dev/null
$ echo 456
456
$ echo 789
789
4 changes: 3 additions & 1 deletion tests/debug.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ Debug mode:

Debug mode with extra shell arguments:

$ cram --shell-opts='-s' -d debug.t
$ cram --shell-opts='-v' -d debug.t
echo hi
hi
echo bye
bye

Test debug mode with set -x:
Expand Down
11 changes: 6 additions & 5 deletions tests/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Set up cram alias and example tests:
Run cram examples:

$ cram -q examples examples/fail.t
.s.!.s.
# Ran 7 tests, 2 skipped, 1 failed.
.s.!.s..
# Ran 8 tests, 2 skipped, 1 failed.
[1]
$ md5 examples/fail.t examples/fail.t.err
.*\b0f598c2b7b8ca5bcb8880e492ff6b452\b.* (re)
Expand All @@ -16,8 +16,8 @@ Run cram examples:
Run examples with bash:

$ cram --shell=/bin/bash -q examples examples/fail.t
.s.!.s.
# Ran 7 tests, 2 skipped, 1 failed.
.s.!.s..
# Ran 8 tests, 2 skipped, 1 failed.
[1]
$ md5 examples/fail.t examples/fail.t.err
.*\b0f598c2b7b8ca5bcb8880e492ff6b452\b.* (re)
Expand All @@ -33,8 +33,9 @@ Verbose mode:
examples/fail.t: failed
examples/missingeol.t: passed
examples/skip.t: skipped
examples/stdin.t: passed
examples/test.t: passed
# Ran 7 tests, 2 skipped, 1 failed.
# Ran 8 tests, 2 skipped, 1 failed.
[1]
$ md5 examples/fail.t examples/fail.t.err
.*\b0f598c2b7b8ca5bcb8880e492ff6b452\b.* (re)
Expand Down
8 changes: 6 additions & 2 deletions tests/xunit.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ xUnit XML output:
examples/fail.t: failed
examples/missingeol.t: passed
examples/skip.t: skipped
examples/stdin.t: passed
examples/test.t: passed
# Ran 7 tests, 2 skipped, 1 failed.
# Ran 8 tests, 2 skipped, 1 failed.
[1]
$ cat cram.xml
<?xml version="1.0" encoding="utf-8"?>
<testsuite name="cram"
tests="7"
tests="8"
failures="1"
skipped="2"
timestamp="\d+-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}" (re)
Expand Down Expand Up @@ -81,6 +82,9 @@ xUnit XML output:
time="\d+\.\d{6}"> (re)
<skipped/>
</testcase>
<testcase classname="examples/stdin.t"
name="stdin.t"
time="\d+\.\d{6}"/> (re)
<testcase classname="examples/test.t"
name="test.t"
time="\d+\.\d{6}"/> (re)
Expand Down