Rev 4675: Start separating the script runner from the test case. in file:///home/vila/src/bzr/experimental/shell-like-tests/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Thu Sep 3 16:24:08 BST 2009
At file:///home/vila/src/bzr/experimental/shell-like-tests/
------------------------------------------------------------
revno: 4675
revision-id: v.ladeuil+lp at free.fr-20090903152408-6z86c4vcol338i5h
parent: v.ladeuil+lp at free.fr-20090903132836-o9pt488nog9d5fmr
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: shell-like-tests
timestamp: Thu 2009-09-03 17:24:08 +0200
message:
Start separating the script runner from the test case.
* bzrlib/tests/test_script.py:
Miniaml modifications to make the tests pass again.
* bzrlib/tests/script.py:
More doc.
(ScriptRunner): Extracted from TestCaseWithScript to better
highlight the TestCase requirements.
(TestCaseWithTransportAndScript): We requires a disk based test
case so far.
-------------- next part --------------
=== modified file 'bzrlib/tests/script.py'
--- a/bzrlib/tests/script.py 2009-09-03 13:28:36 +0000
+++ b/bzrlib/tests/script.py 2009-09-03 15:24:08 +0000
@@ -31,22 +31,35 @@
- '2>' for errors,
The execution stops as soon as an expected output or an expected error is not
-matched. When no output is specified, any ouput from the command is accepted
-and let the execution continue.
-
-The matching is done on a full string comparison.
-
-Example:
+matched.
+
+When no output is specified, any ouput from the command is accepted
+and let the execution continue.
+
+If an error occurs and no expected error is specified, the execution stops.
+
+The matching is done on a full string comparison basis.
+
+Examples:
+
+The following will succeeds only if 'bzr add' outputs 'adding file'.
bzr add file
>adding file
-The above will succeeds only if 'bzr add' outputs 'adding file'.
-
If you want the command to succeed for any output, just use:
bzr add file
+The following will stop with an error:
+
+ bzr not-a-command
+
+If you want it to succeed, use:
+
+ bzr not-a-command
+ 2> bzr: ERROR: unknown command "not-a-command"
+
"""
from cStringIO import StringIO
@@ -179,11 +192,10 @@
return in_name, out_name, out_mode, remaining
-class TestCaseWithScript(tests.TestCaseWithTransport):
+class ScriptRunner(object):
- def setUp(self):
- super(TestCaseWithScript, self).setUp()
- self._vars = {}
+ def __init__(self, test_case):
+ self.test_case = test_case
def run_script(self, text):
for cmd, input, output, error in _script_to_commands(text):
@@ -193,7 +205,7 @@
if expected is None:
# Specifying None means: any output is accepted
return
- self.assertEquals(expected, actual)
+ self.test_case.assertEquals(expected, actual)
def run_command(self, cmd, input, output, error):
mname = 'do_' + cmd[0]
@@ -232,8 +244,8 @@
return output
def do_bzr(self, input, args):
- out, err = self._run_bzr_core(args, retcode=None, encoding=None,
- stdin=input, working_dir=None)
+ out, err = self.test_case._run_bzr_core(
+ args, retcode=None, encoding=None, stdin=input, working_dir=None)
return out, err
def do_cat(self, input, args):
@@ -267,8 +279,9 @@
return output, None
def _ensure_in_jail(self, path):
- if not osutils.is_inside(self.test_dir, osutils.normalizepath(path)):
- raise ValueError('%s is not inside %s' % (path, self.test_dir))
+ jail_root = self.test_case.get_jail_root()
+ if not osutils.is_inside(jail_root, osutils.normalizepath(path)):
+ raise ValueError('%s is not inside %s' % (path, jail_root))
def do_cd(self, input, args):
if len(args) > 1:
@@ -277,7 +290,7 @@
d = args[0]
self._ensure_in_jail(d)
else:
- d = self.test_dir
+ d = self.test_case.get_jail_root()
os.chdir(d)
return None, None
@@ -289,3 +302,22 @@
os.mkdir(d)
return None, None
+
+class TestCaseWithTransportAndScript(tests.TestCaseWithTransport):
+
+ def setUp(self):
+ super(TestCaseWithTransportAndScript, self).setUp()
+ self.script_runner = ScriptRunner(self)
+ # Break the circular dependency
+ def break_dependency():
+ self.script_runner = None
+ self.addCleanup(break_dependency)
+
+ def get_jail_root(self):
+ return self.test_dir
+
+ def run_script(self, script):
+ return self.script_runner.run_script(script)
+
+ def run_command(self, cmd, input, output, error):
+ return self.script_runner.run_command(cmd, input, output, error)
=== modified file 'bzrlib/tests/test_script.py'
--- a/bzrlib/tests/test_script.py 2009-09-01 15:45:52 +0000
+++ b/bzrlib/tests/test_script.py 2009-09-03 15:24:08 +0000
@@ -83,7 +83,12 @@
script._script_to_commands(story))
-class TestScriptExecution(script.TestCaseWithScript):
+class TestScriptBase(script.TestCaseWithTransportAndScript):
+
+ pass
+
+
+class TestScriptExecution(TestScriptBase):
def test_unknown_command(self):
self.assertRaises(SyntaxError, self.run_script, 'foo')
@@ -97,7 +102,7 @@
self.assertRaises(AssertionError, self.run_script, story)
-class TestCat(script.TestCaseWithScript):
+class TestCat(TestScriptBase):
def test_cat_usage(self):
self.assertRaises(SyntaxError, self.run_script, 'cat foo bar baz')
@@ -130,7 +135,7 @@
self.assertFileEqual('content\n', 'file2')
-class TestMkdir(script.TestCaseWithScript):
+class TestMkdir(TestScriptBase):
def test_mkdir_usage(self):
self.assertRaises(SyntaxError, self.run_script, 'mkdir')
@@ -151,7 +156,7 @@
self.failUnlessExists('dir2')
-class TestCd(script.TestCaseWithScript):
+class TestCd(TestScriptBase):
def test_cd_usage(self):
self.assertRaises(SyntaxError, self.run_script, 'cd foo bar')
@@ -173,14 +178,14 @@
self.assertEquals(self.test_dir, osutils.getcwd())
-class TestBzr(script.TestCaseWithScript):
+class TestBzr(TestScriptBase):
def test_bzr_smoke(self):
self.run_script('bzr init branch')
self.failUnlessExists('branch')
-class TestEcho(script.TestCaseWithScript):
+class TestEcho(TestScriptBase):
def test_echo_usage(self):
story = """
More information about the bazaar-commits
mailing list