From 895f277379ccb6272549468d18f27f4a5fc2ee2f Mon Sep 17 00:00:00 2001 From: Steven Knight Date: Sun, 8 Feb 2009 15:17:32 +0000 Subject: Update Test{Cmd,Common}.py to print the command arguments if TestCommon.start() gets an exception trying to run a program, using a new TestCmd.command_args() method. Doc string updates. Code cleanups. --- QMTest/TestCmd.py | 74 ++++++++++++++++++++++++++++++++-------------------- QMTest/TestCommon.py | 49 +++++++++++++++++++++++++++------- 2 files changed, 84 insertions(+), 39 deletions(-) diff --git a/QMTest/TestCmd.py b/QMTest/TestCmd.py index 4bf1270..48ba850 100644 --- a/QMTest/TestCmd.py +++ b/QMTest/TestCmd.py @@ -16,8 +16,7 @@ A TestCmd environment object is created via the usual invocation: import TestCmd test = TestCmd.TestCmd() -There are a bunch of keyword arguments that you can use at instantiation -time: +There are a bunch of keyword arguments available at instantiation: test = TestCmd.TestCmd(description = 'string', program = 'program_or_script_to_test', @@ -28,8 +27,7 @@ time: match = default_match_function, combine = Boolean) -There are a bunch of methods that let you do a bunch of different -things. Here is an overview of them: +There are a bunch of methods that let you do different things: test.verbose_set(1) @@ -65,11 +63,23 @@ things. Here is an overview of them: test.cleanup(condition) + test.command_args(program = 'program_or_script_to_run', + interpreter = 'script_interpreter', + arguments = 'arguments to pass to program') + test.run(program = 'program_or_script_to_run', interpreter = 'script_interpreter', arguments = 'arguments to pass to program', chdir = 'directory_to_chdir_to', stdin = 'input to feed to the program\n') + universal_newlines = True) + + p = test.start(program = 'program_or_script_to_run', + interpreter = 'script_interpreter', + arguments = 'arguments to pass to program', + universal_newlines = None) + + test.finish(self, p) test.pass_test() test.pass_test(condition) @@ -181,8 +191,8 @@ version. # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. __author__ = "Steven Knight " -__revision__ = "TestCmd.py 0.34.D001 2008/12/28 23:12:34 knight" -__version__ = "0.34" +__revision__ = "TestCmd.py 0.35.D001 2009/02/08 07:10:39 knight" +__version__ = "0.35" import errno import os @@ -851,6 +861,12 @@ class TestCmd: path = os.path.join(self.workdir, path) return path + def chmod(self, path, mode): + """Changes permissions on the specified file or directory + path name.""" + path = self.canonicalize(path) + os.chmod(path, mode) + def cleanup(self, condition = None): """Removes any temporary working directories for the specified TestCmd environment. If the environment variable PRESERVE was @@ -889,11 +905,28 @@ class TestCmd: except (AttributeError, ValueError): pass - def chmod(self, path, mode): - """Changes permissions on the specified file or directory - path name.""" - path = self.canonicalize(path) - os.chmod(path, mode) + def command_args(self, program = None, + interpreter = None, + arguments = None): + if program: + if type(program) == type('') and not os.path.isabs(program): + program = os.path.join(self._cwd, program) + else: + program = self.program + if not interpreter: + interpreter = self.interpreter + if not type(program) in [type([]), type(())]: + program = [program] + cmd = list(program) + if interpreter: + if not type(interpreter) in [type([]), type(())]: + interpreter = [interpreter] + cmd = list(interpreter) + cmd + if arguments: + if type(arguments) == type(''): + arguments = string.split(arguments) + cmd.extend(arguments) + return cmd def description_set(self, description): """Set the description of the functionality being tested. @@ -1015,24 +1048,7 @@ class TestCmd: The specified program will have the original directory prepended unless it is enclosed in a [list]. """ - if program: - if type(program) == type('') and not os.path.isabs(program): - program = os.path.join(self._cwd, program) - else: - program = self.program - if not interpreter: - interpreter = self.interpreter - if not type(program) in [type([]), type(())]: - program = [program] - cmd = list(program) - if interpreter: - if not type(interpreter) in [type([]), type(())]: - interpreter = [interpreter] - cmd = list(interpreter) + cmd - if arguments: - if type(arguments) == type(''): - arguments = string.split(arguments) - cmd.extend(arguments) + cmd = self.command_args(program, interpreter, arguments) cmd_string = string.join(map(self.escape, cmd), ' ') if self.verbose: sys.stderr.write(cmd_string + "\n") diff --git a/QMTest/TestCommon.py b/QMTest/TestCommon.py index 845dfd2..9b5702a 100644 --- a/QMTest/TestCommon.py +++ b/QMTest/TestCommon.py @@ -87,8 +87,8 @@ The TestCommon module also provides the following variables # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. __author__ = "Steven Knight " -__revision__ = "TestCommon.py 0.34.D001 2008/12/28 23:12:34 knight" -__version__ = "0.34" +__revision__ = "TestCommon.py 0.35.D001 2009/02/08 07:10:39 knight" +__version__ = "0.35" import copy import os @@ -310,6 +310,16 @@ class TestCommon(TestCmd): self.fail_test(not contains) def must_contain_all_lines(self, output, lines, title=None, find=None): + """Ensures that the specified output string (first argument) + contains all of the specified lines (second argument). + + An optional third argument can be used to describe the type + of output being searched, and only shows up in failure output. + + An optional fourth argument can be used to supply a different + function, of the form "find(line, output), to use when searching + for lines in the output. + """ if find is None: find = lambda o, l: string.find(o, l) != -1 missing = [] @@ -323,12 +333,21 @@ class TestCommon(TestCmd): sys.stdout.write("Missing expected lines from %s:\n" % title) for line in missing: sys.stdout.write(' ' + repr(line) + '\n') - separator = title + ' ' + '=' * (78 - len(title) - 1) - sys.stdout.write(separator + '\n') + sys.stdout.write(self.banner(title + ' ')) sys.stdout.write(output) self.fail_test() def must_contain_any_line(self, output, lines, title=None, find=None): + """Ensures that the specified output string (first argument) + contains at least one of the specified lines (second argument). + + An optional third argument can be used to describe the type + of output being searched, and only shows up in failure output. + + An optional fourth argument can be used to supply a different + function, of the form "find(line, output), to use when searching + for lines in the output. + """ if find is None: find = lambda o, l: string.find(o, l) != -1 for line in lines: @@ -340,8 +359,7 @@ class TestCommon(TestCmd): sys.stdout.write("Missing any expected line from %s:\n" % title) for line in lines: sys.stdout.write(' ' + repr(line) + '\n') - separator = title + ' ' + '=' * (78 - len(title) - 1) - sys.stdout.write(separator + '\n') + sys.stdout.write(self.banner(title + ' ')) sys.stdout.write(output) self.fail_test() @@ -378,6 +396,16 @@ class TestCommon(TestCmd): raise def must_not_contain_any_line(self, output, lines, title=None, find=None): + """Ensures that the specified output string (first argument) + does not contain any of the specified lines (second argument). + + An optional third argument can be used to describe the type + of output being searched, and only shows up in failure output. + + An optional fourth argument can be used to supply a different + function, of the form "find(line, output), to use when searching + for lines in the output. + """ if find is None: find = lambda o, l: string.find(o, l) != -1 unexpected = [] @@ -391,13 +419,12 @@ class TestCommon(TestCmd): sys.stdout.write("Unexpected lines in %s:\n" % title) for line in unexpected: sys.stdout.write(' ' + repr(line) + '\n') - separator = title + ' ' + '=' * (78 - len(title) - 1) - sys.stdout.write(separator + '\n') + sys.stdout.write(self.banner(title + ' ')) sys.stdout.write(output) self.fail_test() - def must_not_contain_lines(self, lines, output, title=None, find=None): - return self.must_not_contain_any_line(output, lines, title, find) + def must_not_contain_lines(self, lines, output, title=None): + return self.must_not_contain_any_line(output, lines, title) def must_not_exist(self, *files): """Ensures that the specified file(s) must not exist. @@ -494,6 +521,8 @@ class TestCommon(TestCmd): print self.stderr() except IndexError: pass + cmd_args = self.command_args(program, interpreter, arguments) + sys.stderr.write('Exception trying to execute: %s\n' % cmd_args) raise e def finish(self, popen, stdout = None, stderr = '', status = 0, **kw): -- cgit v0.12