Provided a mechanism for the test class to cleanup after itself once it's done.

This will remove the confusion experienced when previous test runs left some
files (both intermediate or by-product as a result of the test).

lldbtest.TestBase defines a classmethod tearDownClass(cls) which invokes the
platform-specific cleanup() function as defined by the plugin; after that, it
invokes a subclass-specific function classCleanup(cls) if defined; and, finally,
it restores the old working directory.

An example of classCleanup(cls) is in settings/TestSettings.py:

    @classmethod
    def classCleanup(cls):
        system(["/bin/sh", "-c", "rm output.txt"])

where it deletes the by-product "output.txt" as a result of running a.out.

llvm-svn: 114058
This commit is contained in:
Johnny Chen 2010-09-16 01:53:04 +00:00
parent abf4a3e4c6
commit 1a9f4dd5a0
6 changed files with 67 additions and 11 deletions

View File

@ -271,20 +271,47 @@ class TestBase(unittest2.TestCase):
# Can be overridden by the LLDB_TIME_WAIT environment variable.
timeWait = 1.0;
def setUp(self):
#import traceback
#traceback.print_stack()
# Keep track of the old current working directory.
oldcwd = None
@classmethod
def setUpClass(cls):
# Fail fast if 'mydir' attribute is not overridden.
if not self.mydir or len(self.mydir) == 0:
if not cls.mydir or len(cls.mydir) == 0:
raise Exception("Subclasses must override the 'mydir' attribute.")
# Save old working directory.
self.oldcwd = os.getcwd()
cls.oldcwd = os.getcwd()
# Change current working directory if ${LLDB_TEST} is defined.
# See also dotest.py which sets up ${LLDB_TEST}.
if ("LLDB_TEST" in os.environ):
os.chdir(os.path.join(os.environ["LLDB_TEST"], self.mydir));
if traceAlways:
print "Change dir to:", os.path.join(os.environ["LLDB_TEST"], cls.mydir)
os.chdir(os.path.join(os.environ["LLDB_TEST"], cls.mydir))
@classmethod
def tearDownClass(cls):
"""Do class-wide cleanup."""
# First, let's do the platform-specific cleanup.
module = __import__(sys.platform)
if not module.cleanup():
raise Exception("Don't know how to do cleanup")
# Subclass might have specific cleanup function defined.
if getattr(cls, "classCleanup", None):
if traceAlways:
print "Call class-specific cleanup function for class:", cls
cls.classCleanup()
# Restore old working directory.
if traceAlways:
print "Restore dir to:", cls.oldcwd
os.chdir(cls.oldcwd)
def setUp(self):
#import traceback
#traceback.print_stack()
if "LLDB_MAX_LAUNCH_COUNT" in os.environ:
self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"])
@ -330,9 +357,6 @@ class TestBase(unittest2.TestCase):
del self.dbg
# Restore old working directory.
os.chdir(self.oldcwd)
def runCmd(self, cmd, msg=None, check=True, trace=False, setCookie=True):
"""
Ask the command interpreter to handle the command and then check its

View File

@ -0,0 +1,5 @@
LEVEL = ../make
C_SOURCES := main.c
include $(LEVEL)/Makefile.rules

View File

@ -13,7 +13,9 @@ class PersistentVariablesTestCase(TestBase):
def test_persistent_variables(self):
"""Test that lldb persistent variables works correctly."""
self.runCmd("file ../array_types/a.out", CURRENT_EXECUTABLE_SET)
self.buildDefault()
self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
self.runCmd("breakpoint set --name main")

View File

@ -0,0 +1,13 @@
//===-- main.c --------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
int main (int argc, char const *argv[])
{
return 0;
}

View File

@ -50,3 +50,11 @@ def buildDwarf(compiler=None):
# True signifies that we can handle building dsym.
return True
def cleanup():
"""Do class-wide cleanup after the test."""
if os.path.isfile("Makefile"):
lldbtest.system(["/bin/sh", "-c", "make clean"])
# True signifies that we can handle building dsym.
return True

View File

@ -11,6 +11,10 @@ class SettingsCommandTestCase(TestBase):
mydir = "settings"
@classmethod
def classCleanup(cls):
system(["/bin/sh", "-c", "rm output.txt"])
def test_set_prompt(self):
"""Test that 'set prompt' actually changes the prompt."""
@ -62,7 +66,7 @@ class SettingsCommandTestCase(TestBase):
self.runCmd("run", RUN_SUCCEEDED)
# Read the output file produced by running the program.
output = open('/tmp/output.txt', 'r').read()
output = open('output.txt', 'r').read()
self.assertTrue(output.startswith("argv[1] matches") and
output.find("argv[2] matches") > 0 and