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
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Here's a snippet from `Cram's own test suite`_::
-y, --yes answer yes to all questions
-n, --no answer no to all questions
-E, --preserve-env don't reset common environment variables
-e, --no-err-files don't write .err files on test failures
--keep-tmpdir keep temporary directories
--shell=PATH shell to use for running tests (default: /bin/sh)
--shell-opts=OPTS arguments to invoke shell with
Expand Down Expand Up @@ -132,9 +133,10 @@ For example, if we run Cram on `its own example tests`_::
s.
# Ran 6 tests, 2 skipped, 1 failed.

Cram will also write the test with its actual output to
``examples/fail.t.err``, allowing you to use other diff tools. This
file is automatically removed the next time the test passes.
Unless run with ``-e`` or ``--no-err-files``, Cram will also write the
test with its actual output to ``examples/fail.t.err``, allowing you to
use other diff tools. This file is automatically removed the next time
the test passes.

When you're first writing a test, you might just write the commands
and run the test to see what happens. If you run Cram with ``-i`` or
Expand Down
19 changes: 11 additions & 8 deletions cram/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def _patch(cmd, diff):
out, retcode = execute([cmd, '-p0'], stdin=b''.join(diff))
return retcode == 0

def runcli(tests, quiet=False, verbose=False, patchcmd=None, answer=None):
def runcli(tests, quiet=False, verbose=False, patchcmd=None, answer=None,
noerrfiles=False):
"""Run tests with command line interface input/output.

tests should be a sequence of 2-tuples containing the following:
Expand Down Expand Up @@ -101,12 +102,13 @@ def testwrapper():
if not quiet:
_log('\n', None, verbose)

errfile = open(errpath, 'wb')
try:
for line in postout:
errfile.write(line)
finally:
errfile.close()
if not noerrfiles:
errfile = open(errpath, 'wb')
try:
for line in postout:
errfile.write(line)
finally:
errfile.close()

if not quiet:
origdiff = diff
Expand All @@ -119,7 +121,8 @@ def testwrapper():
_prompt('Accept this change?', 'yN', answer) == 'y'):
if _patch(patchcmd, diff):
_log(None, path + b': merged output\n', verbose)
os.remove(errpath)
if not noerrfiles:
os.remove(errpath)
else:
_log(path + b': merge failed\n')

Expand Down
8 changes: 6 additions & 2 deletions cram/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def _parseopts(args):
help='answer no to all questions')
p.add_option('-E', '--preserve-env', action='store_true',
help="don't reset common environment variables")
p.add_option('-e', '--no-err-files', action='store_true',
help="don't write .err files on test failures")
p.add_option('--keep-tmpdir', action='store_true',
help='keep temporary directories')
p.add_option('--shell', action='store', default='/bin/sh', metavar='PATH',
Expand Down Expand Up @@ -183,10 +185,12 @@ def main(args):
os.mkdir(proctmp)
try:
tests = runtests(paths, tmpdirb, shell, indent=opts.indent,
cleanenv=not opts.preserve_env, debug=opts.debug)
cleanenv=not opts.preserve_env, debug=opts.debug,
noerrfiles=opts.no_err_files)
if not opts.debug:
tests = runcli(tests, quiet=opts.quiet, verbose=opts.verbose,
patchcmd=patchcmd, answer=answer)
patchcmd=patchcmd, answer=answer,
noerrfiles=opts.no_err_files)
if opts.xunit_file is not None:
tests = runxunit(tests, opts.xunit_file)

Expand Down
5 changes: 3 additions & 2 deletions cram/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def _findtests(paths):
else:
yield os.path.normpath(p)

def runtests(paths, tmpdir, shell, indent=2, cleanenv=True, debug=False):
def runtests(paths, tmpdir, shell, indent=2, cleanenv=True, debug=False,
noerrfiles=False):
"""Run tests and yield results.

This yields a sequence of 2-tuples containing the following:
Expand Down Expand Up @@ -69,7 +70,7 @@ def test():
os.chdir(testdir)
return testfile(abspath, shell, indent=indent,
cleanenv=cleanenv, debug=debug,
testname=path)
testname=path, noerrfile=noerrfiles)
finally:
os.chdir(cwd)

Expand Down
6 changes: 3 additions & 3 deletions cram/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _escape(s):
b' (esc)\n')

def test(lines, shell='/bin/sh', indent=2, testname=None, env=None,
cleanenv=True, debug=False):
cleanenv=True, debug=False, noerrfile=False):
r"""Run test lines and return input, output, and diff.

This returns a 3-tuple containing the following:
Expand Down Expand Up @@ -177,7 +177,7 @@ def test(lines, shell='/bin/sh', indent=2, testname=None, env=None,
return refout, postout, []

def testfile(path, shell='/bin/sh', indent=2, env=None, cleanenv=True,
debug=False, testname=None):
debug=False, testname=None, noerrfile=False):
"""Run test at path and return input, output, and diff.

This returns a 3-tuple containing the following:
Expand Down Expand Up @@ -218,6 +218,6 @@ def testfile(path, shell='/bin/sh', indent=2, env=None, cleanenv=True,
if testname is None: # pragma: nocover
testname = os.path.basename(abspath)
return test(f, shell, indent=indent, testname=testname, env=env,
cleanenv=cleanenv, debug=debug)
cleanenv=cleanenv, debug=debug, noerrfile=noerrfile)
finally:
f.close()
22 changes: 22 additions & 0 deletions tests/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,25 @@ Test running tests with the same filename in different directories:

# Ran 2 tests, 0 skipped, 2 failed.
[1]

Test failing a test in a read-only directory with the --no-err-files option:

$ mkdir subdir
$ cat > subdir/test.t <<EOF
> $ echo 1
> EOF
$ chmod a-w subdir
$ cram subdir >/dev/null 2>&1
[1]
$ cram --no-err-files subdir
!
--- subdir/test.t
+++ subdir/test.t.err
@@ -1,1 +1,2 @@
$ echo 1
+ 1

# Ran 1 tests, 0 skipped, 1 failed.
[1]

$ chmod a+w subdir
1 change: 1 addition & 0 deletions tests/usage.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Usage:
-y, --yes answer yes to all questions
-n, --no answer no to all questions
-E, --preserve-env don't reset common environment variables
-e, --no-err-files don't write .err files on test failures
--keep-tmpdir keep temporary directories
--shell=PATH shell to use for running tests (default: /bin/sh)
--shell-opts=OPTS arguments to invoke shell with
Expand Down