From 46149b06d894f20fc7eb0cd433aee188b369b17c Mon Sep 17 00:00:00 2001 From: Steven Knight Date: Mon, 13 Jan 2003 22:30:23 +0000 Subject: Test improvements suggested by Charles Crain. --- src/engine/SCons/Builder.py | 5 +- src/engine/SCons/BuilderTests.py | 24 +++++ src/engine/SCons/EnvironmentTests.py | 4 +- src/engine/SCons/Node/FSTests.py | 185 +++++++++++++++++++++++------------ 4 files changed, 152 insertions(+), 66 deletions(-) diff --git a/src/engine/SCons/Builder.py b/src/engine/SCons/Builder.py index 122ee49..aa79253 100644 --- a/src/engine/SCons/Builder.py +++ b/src/engine/SCons/Builder.py @@ -46,7 +46,7 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" import os.path -from SCons.Errors import UserError +from SCons.Errors import InternalError, UserError import SCons.Action import SCons.Node @@ -234,6 +234,9 @@ class BuilderBase: self.emitter = emitter + def __nonzero__(self): + raise InternalError, "Do not test for the Node.builder attribute directly; use Node.has_builder() instead" + def get_name(self, env): """Attempts to get the name of the Builder. diff --git a/src/engine/SCons/BuilderTests.py b/src/engine/SCons/BuilderTests.py index 5c0f0d6..02bd7cd 100644 --- a/src/engine/SCons/BuilderTests.py +++ b/src/engine/SCons/BuilderTests.py @@ -103,6 +103,30 @@ env = Environment() class BuilderTestCase(unittest.TestCase): + def test__nonzero__(self): + """Test a builder raising an exception when __nonzero__ is called + """ + builder = SCons.Builder.Builder(action="foo") + exc_caught = None + try: + builder.__nonzero__() + except SCons.Errors.InternalError: + exc_caught = 1 + assert exc_caught, "did not catch expected InternalError exception" + + class Node: + pass + + n = Node() + n.builder = builder + exc_caught = None + try: + if n.builder: + pass + except SCons.Errors.InternalError: + exc_caught = 1 + assert exc_caught, "did not catch expected InternalError exception" + def test__call__(self): """Test calling a builder to establish source dependencies """ diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 46aad46..6780a18 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -432,7 +432,7 @@ class EnvironmentTestCase(unittest.TestCase): env = Environment() t = env.Command(target='foo.out', source=['foo1.in', 'foo2.in'], action='buildfoo $target $source') - assert t.builder + assert not t.builder is None assert t.builder.action.__class__.__name__ == 'CommandAction' assert t.builder.action.cmd_list == ['buildfoo', '$target', '$source'] assert 'foo1.in' in map(lambda x: x.path, t.sources) @@ -444,7 +444,7 @@ class EnvironmentTestCase(unittest.TestCase): return 0 t = env.Command(target='foo.out', source=['foo1.in','foo2.in'], action=testFunc) - assert t.builder + assert not t.builder is None assert t.builder.action.__class__.__name__ == 'FunctionAction' t.build() assert 'foo1.in' in map(lambda x: x.path, t.sources) diff --git a/src/engine/SCons/Node/FSTests.py b/src/engine/SCons/Node/FSTests.py index 63cdf2c..b3fac3f 100644 --- a/src/engine/SCons/Node/FSTests.py +++ b/src/engine/SCons/Node/FSTests.py @@ -140,6 +140,10 @@ class BuildDirTestCase(unittest.TestCase): # A source file in the source directory test.write([ 'work', 'src', 'test.in' ], 'test.in') + # A source file in a subdir of the source directory + test.subdir([ 'work', 'src', 'new_dir' ]) + test.write([ 'work', 'src', 'new_dir', 'test9.out' ], 'test9.out\n') + # A source file in the repository test.write([ 'rep1', 'src', 'test2.in' ], 'test2.in') @@ -245,6 +249,7 @@ class BuildDirTestCase(unittest.TestCase): # We should not copy the file from the source dir, since this is # a derived file. assert test.read(['work', 'build', 'var2', 'test.out']) == 'test.old' + f7 = fs.File('build/var1/test2.out') f8 = fs.File('build/var2/test2.out') @@ -257,6 +262,32 @@ class BuildDirTestCase(unittest.TestCase): assert f8.rexists() assert f8.rfile().path == os.path.normpath(test.workpath('rep1/build/var2/test2.out')),\ f8.rfile().path + + # Verify the Mkdir and Link actions are called + f9 = fs.File('build/var2/new_dir/test9.out') + + save_Mkdir = SCons.Node.FS.Mkdir + dir_made = [] + def mkdir_func(target, source, env, dir_made=dir_made): + dir_made.append(target) + SCons.Node.FS.Mkdir = mkdir_func + + save_Link = SCons.Node.FS.Link + link_made = [] + def link_func(target, source, env, link_made=link_made): + link_made.append(target) + SCons.Node.FS.Link = link_func + + try: + f9.exists() + expect = os.path.join('build', 'var2', 'new_dir') + assert dir_made[0].path == expect, dir_made[0].path + expect = os.path.join('build', 'var2', 'new_dir', 'test9.out') + assert link_made[0].path == expect, link_made[0].path + assert f9.linked + finally: + SCons.Node.FS.Mkdir = save_Mkdir + SCons.Node.FS.Link = save_Link # Test to see if Link() works... test.subdir('src','build') @@ -280,13 +311,15 @@ class BuildDirTestCase(unittest.TestCase): exc_caught = 0 try: - fs = SCons.Node.FS.FS() - fs.BuildDir('build', 'build/src') - except SCons.Errors.UserError: - exc_caught = 1 - assert exc_caught, "Should have caught a UserError." - test.unlink( "src/foo" ) - test.unlink( "build/foo" ) + try: + fs = SCons.Node.FS.FS() + fs.BuildDir('build', 'build/src') + except SCons.Errors.UserError: + exc_caught = 1 + assert exc_caught, "Should have caught a UserError." + finally: + test.unlink( "src/foo" ) + test.unlink( "build/foo" ) # verify the link creation attempts in file_link() class LinkSimulator : @@ -365,26 +398,29 @@ class BuildDirTestCase(unittest.TestCase): os.symlink = simulator.symlink_fail shutil.copy2 = simulator.copy - test.write('src/foo', 'src/foo\n') - os.chmod(test.workpath('src/foo'), stat.S_IRUSR) - SCons.Node.FS.Link(fs.File(test.workpath('build/foo')), - fs.File(test.workpath('src/foo')), - None) - os.chmod(test.workpath('src/foo'), ~stat.S_IRUSR) - test.unlink( "src/foo" ) - test.unlink( "build/foo" ) - - # restore the real functions - if real_link: - os.link = real_link - else: - delattr(os, 'link') - if real_symlink: - os.symlink = real_symlink - else: - delattr(os, 'symlink') - shutil.copy2 = real_copy - + try: + test.write('src/foo', 'src/foo\n') + try: + os.chmod(test.workpath('src/foo'), stat.S_IRUSR) + SCons.Node.FS.Link(fs.File(test.workpath('build/foo')), + fs.File(test.workpath('src/foo')), + None) + os.chmod(test.workpath('src/foo'), ~stat.S_IRUSR) + finally: + test.unlink( "src/foo" ) + test.unlink( "build/foo" ) + + finally: + # restore the real functions + if real_link: + os.link = real_link + else: + delattr(os, 'link') + if real_symlink: + os.symlink = real_symlink + else: + delattr(os, 'symlink') + shutil.copy2 = real_copy class FSTestCase(unittest.TestCase): def runTest(self): @@ -813,11 +849,13 @@ class FSTestCase(unittest.TestCase): assert exc_caught, "Should have caught an AttributError" test.write("file", "file\n") - e = fs.Entry('file') - c = e.get_contents() - assert c == "file\n", c - assert e.__class__ == SCons.Node.FS.File - test.unlink("file") + try: + e = fs.Entry('file') + c = e.get_contents() + assert c == "file\n", c + assert e.__class__ == SCons.Node.FS.File + finally: + test.unlink("file") test.subdir("dir") e = fs.Entry('dir') @@ -826,17 +864,19 @@ class FSTestCase(unittest.TestCase): assert e.__class__ == SCons.Node.FS.Dir test.write("tstamp", "tstamp\n") - # Okay, *this* manipulation accomodates Windows FAT file systems - # that only have two-second granularity on their timestamps. - # We round down the current time to the nearest even integer - # value, subtract two to make sure the timestamp is not "now," - # and then convert it back to a float. - tstamp = float(int(time.time() / 2) * 2) - 2 - os.utime(test.workpath("tstamp"), (tstamp - 2.0, tstamp)) - f = fs.File("tstamp") - t = f.get_timestamp() - assert t == tstamp, "expected %f, got %f" % (tstamp, t) - test.unlink("tstamp") + try: + # Okay, *this* manipulation accomodates Windows FAT file systems + # that only have two-second granularity on their timestamps. + # We round down the current time to the nearest even integer + # value, subtract two to make sure the timestamp is not "now," + # and then convert it back to a float. + tstamp = float(int(time.time() / 2) * 2) - 2 + os.utime(test.workpath("tstamp"), (tstamp - 2.0, tstamp)) + f = fs.File("tstamp") + t = f.get_timestamp() + assert t == tstamp, "expected %f, got %f" % (tstamp, t) + finally: + test.unlink("tstamp") #XXX test get_prevsiginfo() @@ -850,13 +890,15 @@ class FSTestCase(unittest.TestCase): assert parents == [ d1 ], parents test.write("i_am_not_a_directory", "\n") - exc_caught = 0 try: - fs.Dir(test.workpath("i_am_not_a_directory")) - except TypeError: - exc_caught = 1 - assert exc_caught, "Should have caught a TypeError" - test.unlink("i_am_not_a_directory") + exc_caught = 0 + try: + fs.Dir(test.workpath("i_am_not_a_directory")) + except TypeError: + exc_caught = 1 + assert exc_caught, "Should have caught a TypeError" + finally: + test.unlink("i_am_not_a_directory") exc_caught = 0 try: @@ -1021,23 +1063,27 @@ class RepositoryTestCase(unittest.TestCase): assert f2.rexists() test.write(["rep2", "tstamp"], "tstamp\n") - # Okay, *this* manipulation accomodates Windows FAT file systems - # that only have two-second granularity on their timestamps. - # We round down the current time to the nearest even integer - # value, subtract two to make sure the timestamp is not "now," - # and then convert it back to a float. - tstamp = float(int(time.time() / 2) * 2) - 2 - os.utime(test.workpath("rep2", "tstamp"), (tstamp - 2.0, tstamp)) - f = fs.File("tstamp") - t = f.get_timestamp() - assert t == tstamp, "expected %f, got %f" % (tstamp, t) - test.unlink(["rep2", "tstamp"]) + try: + # Okay, *this* manipulation accomodates Windows FAT file systems + # that only have two-second granularity on their timestamps. + # We round down the current time to the nearest even integer + # value, subtract two to make sure the timestamp is not "now," + # and then convert it back to a float. + tstamp = float(int(time.time() / 2) * 2) - 2 + os.utime(test.workpath("rep2", "tstamp"), (tstamp - 2.0, tstamp)) + f = fs.File("tstamp") + t = f.get_timestamp() + assert t == tstamp, "expected %f, got %f" % (tstamp, t) + finally: + test.unlink(["rep2", "tstamp"]) # Make sure get_contents() returns the binary contents. test.write(["rep3", "contents"], "Con\x1aTents\n") - c = fs.File("contents").get_contents() - assert c == "Con\x1aTents\n", "got '%s'" % c - test.unlink(["rep3", "contents"]) + try: + c = fs.File("contents").get_contents() + assert c == "Con\x1aTents\n", "got '%s'" % c + finally: + test.unlink(["rep3", "contents"]) # XXX test calc_signature() @@ -1096,6 +1142,19 @@ class prepareTestCase(unittest.TestCase): exc_caught = 1 assert exc_caught, "Should have caught a StopError." + save_Mkdir = SCons.Node.FS.Mkdir + dir_made = [] + def mkdir_func(target, source, env, dir_made=dir_made): + dir_made.append(target) + SCons.Node.FS.Mkdir = mkdir_func + + file = fs.File(os.path.join("new_dir", "xyz")) + try: + file.prepare() + assert dir_made[0].path == "new_dir", dir_made[0].path + finally: + SCons.Node.FS.Mkdir = save_Mkdir + class get_actionsTestCase(unittest.TestCase): def runTest(self): """Test the Dir's get_action() method""" -- cgit v0.12