From 738149c9bfb9965d013d01ef99f9bb1c2819e7e8 Mon Sep 17 00:00:00 2001 From: Luca Falavigna Date: Tue, 15 Jun 2010 14:28:22 +0000 Subject: Imported Upstream version 2.0.0 --- src/engine/SCons/Node/FSTests.py | 214 ++++++++++++++++++--------------------- 1 file changed, 98 insertions(+), 116 deletions(-) (limited to 'src/engine/SCons/Node/FSTests.py') diff --git a/src/engine/SCons/Node/FSTests.py b/src/engine/SCons/Node/FSTests.py index c059925..ee6a2f5 100644 --- a/src/engine/SCons/Node/FSTests.py +++ b/src/engine/SCons/Node/FSTests.py @@ -20,12 +20,14 @@ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # +from __future__ import division -__revision__ = "src/engine/SCons/Node/FSTests.py 4720 2010/03/24 03:14:11 jars" +__revision__ = "src/engine/SCons/Node/FSTests.py 5023 2010/06/14 22:05:46 scons" + +import SCons.compat import os import os.path -import string import sys import time import unittest @@ -40,16 +42,9 @@ import SCons.Warnings built_it = None -# This will be built-in in 2.3. For now fake it. -try : - True , False -except NameError : - True = 1 ; False = 0 - - scanner_count = 0 -class Scanner: +class Scanner(object): def __init__(self, node=None): global scanner_count scanner_count = scanner_count + 1 @@ -66,7 +61,7 @@ class Scanner: def recurse_nodes(self, nodes): return nodes -class Environment: +class Environment(object): def __init__(self): self.scanner = Scanner() def Dictionary(self, *args): @@ -80,7 +75,7 @@ class Environment: def _update(self, dict): pass -class Action: +class Action(object): def __call__(self, targets, sources, env, **kw): global built_it if kw.get('execute', 1): @@ -97,7 +92,7 @@ class Action: def get_implicit_deps(self, target, source, env): return [] -class Builder: +class Builder(object): def __init__(self, factory, action=Action()): self.factory = factory self.env = Environment() @@ -341,7 +336,7 @@ class VariantDirTestCase(unittest.TestCase): f11 = fs.File('src/file11') t, m = f11.alter_targets() - bdt = map(lambda n: n.path, t) + bdt = [n.path for n in t] var1_file11 = os.path.normpath('build/var1/file11') var2_file11 = os.path.normpath('build/var2/file11') assert bdt == [var1_file11, var2_file11], bdt @@ -349,11 +344,11 @@ class VariantDirTestCase(unittest.TestCase): f12 = fs.File('src/file12') f12.builder = 1 bdt, m = f12.alter_targets() - assert bdt == [], map(lambda n: n.path, bdt) + assert bdt == [], [n.path for n in bdt] d13 = fs.Dir('src/new_dir') t, m = d13.alter_targets() - bdt = map(lambda n: n.path, t) + bdt = [n.path for n in t] var1_new_dir = os.path.normpath('build/var1/new_dir') var2_new_dir = os.path.normpath('build/var2/new_dir') assert bdt == [var1_new_dir, var2_new_dir], bdt @@ -364,7 +359,7 @@ class VariantDirTestCase(unittest.TestCase): save_Link = SCons.Node.FS.Link def Link_IOError(target, source, env): - raise IOError, (17, "Link_IOError") + raise IOError(17, "Link_IOError") SCons.Node.FS.Link = SCons.Action.Action(Link_IOError, None) test.write(['work', 'src', 'IOError'], "work/src/IOError\n") @@ -434,7 +429,7 @@ class VariantDirTestCase(unittest.TestCase): assert r == d1, "%s != %s" % (r, d1) # verify the link creation attempts in file_link() - class LinkSimulator : + class LinkSimulator (object): """A class to intercept os.[sym]link() calls and track them.""" def __init__( self, duplicate, link, symlink, copy ) : @@ -445,7 +440,7 @@ class VariantDirTestCase(unittest.TestCase): self.have['copy'] = copy self.links_to_be_called = [] - for link in string.split(self.duplicate, '-'): + for link in self.duplicate.split('-'): if self.have[link]: self.links_to_be_called.append(link) @@ -817,7 +812,7 @@ class FileBuildInfoTestCase(_tempdirTestCase): 'actionsig [action]', ] - expect = string.join(expect_lines, '\n') + expect = '\n'.join(expect_lines) format = bi1.format() assert format == expect, (repr(expect), repr(format)) @@ -898,7 +893,7 @@ class FSTestCase(_tempdirTestCase): except TypeError: pass else: - raise Exception, "did not catch expected TypeError" + raise Exception("did not catch expected TypeError") assert x1.Entry(x4) == x4 try: @@ -906,7 +901,7 @@ class FSTestCase(_tempdirTestCase): except TypeError: pass else: - raise Exception, "did not catch expected TypeError" + raise Exception("did not catch expected TypeError") x6 = x1.File(x6) assert isinstance(x6, SCons.Node.FS.File) @@ -921,12 +916,12 @@ class FSTestCase(_tempdirTestCase): drive, path = os.path.splitdrive(os.getcwd()) def _do_Dir_test(lpath, path_, abspath_, up_path_, sep, fileSys=fs, drive=drive): - dir = fileSys.Dir(string.replace(lpath, '/', sep)) + dir = fileSys.Dir(lpath.replace('/', sep)) if os.sep != '/': - path_ = string.replace(path_, '/', os.sep) - abspath_ = string.replace(abspath_, '/', os.sep) - up_path_ = string.replace(up_path_, '/', os.sep) + path_ = path_.replace('/', os.sep) + abspath_ = abspath_.replace('/', os.sep) + up_path_ = up_path_.replace('/', os.sep) def strip_slash(p, drive=drive): if p[-1] == os.sep and len(p) > 1: @@ -937,7 +932,7 @@ class FSTestCase(_tempdirTestCase): path = strip_slash(path_) abspath = strip_slash(abspath_) up_path = strip_slash(up_path_) - name = string.split(abspath, os.sep)[-1] + name = abspath.split(os.sep)[-1] assert dir.name == name, \ "dir.name %s != expected name %s" % \ @@ -978,7 +973,7 @@ class FSTestCase(_tempdirTestCase): Dir_test('#', './', sub_dir, sub) try: - f2 = fs.File(string.join(['f1', 'f2'], sep), directory = d1) + f2 = fs.File(sep.join(['f1', 'f2']), directory = d1) except TypeError, x: assert str(x) == ("Tried to lookup File '%s' as a Dir." % d1_f1), x @@ -986,7 +981,7 @@ class FSTestCase(_tempdirTestCase): raise try: - dir = fs.Dir(string.join(['d1', 'f1'], sep)) + dir = fs.Dir(sep.join(['d1', 'f1'])) except TypeError, x: assert str(x) == ("Tried to lookup File '%s' as a Dir." % d1_f1), x @@ -1051,7 +1046,7 @@ class FSTestCase(_tempdirTestCase): assert built_it def match(path, expect): - expect = string.replace(expect, '/', os.sep) + expect = expect.replace('/', os.sep) assert path == expect, "path %s != expected %s" % (path, expect) e1 = fs.Entry("d1") @@ -1152,7 +1147,7 @@ class FSTestCase(_tempdirTestCase): # Make sure we can scan this file even if the target isn't # a file that has a scanner (it might be an Alias, e.g.). - class DummyNode: + class DummyNode(object): pass deps = f12.get_found_includes(env, s, DummyNode()) @@ -1195,17 +1190,12 @@ class FSTestCase(_tempdirTestCase): f1 = fs.File(test.workpath("binary_file")) assert f1.get_contents() == "Foo\x1aBar", f1.get_contents() - try: - # TODO(1.5) - eval('test_string = u"Foo\x1aBar"') - except SyntaxError: - pass - else: - # This tests to make sure we can decode UTF-8 text files. - test.write("utf8_file", test_string.encode('utf-8')) - f1 = fs.File(test.workpath("utf8_file")) - assert eval('f1.get_text_contents() == u"Foo\x1aBar"'), \ - f1.get_text_contents() + # This tests to make sure we can decode UTF-8 text files. + test_string = u"Foo\x1aBar" + test.write("utf8_file", test_string.encode('utf-8')) + f1 = fs.File(test.workpath("utf8_file")) + assert eval('f1.get_text_contents() == u"Foo\x1aBar"'), \ + f1.get_text_contents() def nonexistent(method, s): try: @@ -1213,7 +1203,7 @@ class FSTestCase(_tempdirTestCase): except SCons.Errors.UserError: pass else: - raise Exception, "did not catch expected UserError" + raise Exception("did not catch expected UserError") nonexistent(fs.Entry, 'nonexistent') nonexistent(fs.Entry, 'nonexistent/foo') @@ -1318,7 +1308,7 @@ class FSTestCase(_tempdirTestCase): # 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 + tstamp = float(int(time.time() // 2) * 2) - 2.0 os.utime(test.workpath("tstamp"), (tstamp - 2.0, tstamp)) f = fs.File("tstamp") t = f.get_timestamp() @@ -1336,7 +1326,7 @@ class FSTestCase(_tempdirTestCase): f2 = test.workpath('tdir2', 'file2') test.write(f1, 'file1\n') test.write(f2, 'file2\n') - current_time = float(int(time.time() / 2) * 2) + current_time = float(int(time.time() // 2) * 2) t1 = current_time - 4.0 t2 = current_time - 2.0 os.utime(f1, (t1 - 2.0, t1)) @@ -1434,7 +1424,7 @@ class FSTestCase(_tempdirTestCase): test.subdir('sub', ['sub', 'dir']) def drive_workpath(drive, dirs, test=test): - x = apply(test.workpath, dirs) + x = test.workpath(*dirs) drive, path = os.path.splitdrive(x) return 'X:' + path @@ -1466,11 +1456,11 @@ class FSTestCase(_tempdirTestCase): seps = seps + ['/'] def _do_Dir_test(lpath, path_, up_path_, sep, fileSys=fs): - dir = fileSys.Dir(string.replace(lpath, '/', sep)) + dir = fileSys.Dir(lpath.replace('/', sep)) if os.sep != '/': - path_ = string.replace(path_, '/', os.sep) - up_path_ = string.replace(up_path_, '/', os.sep) + path_ = path_.replace('/', os.sep) + up_path_ = up_path_.replace('/', os.sep) def strip_slash(p): if p[-1] == os.sep and len(p) > 3: @@ -1478,7 +1468,7 @@ class FSTestCase(_tempdirTestCase): return p path = strip_slash(path_) up_path = strip_slash(up_path_) - name = string.split(path, os.sep)[-1] + name = path.split(os.sep)[-1] assert dir.name == name, \ "dir.name %s != expected name %s" % \ @@ -1577,8 +1567,8 @@ class FSTestCase(_tempdirTestCase): d1 = fs.Dir('d1') d2 = d1.Dir('d2') - dirs = string.split(os.path.normpath(d2.abspath), os.sep) - above_path = apply(os.path.join, ['..']*len(dirs) + ['above']) + dirs = os.path.normpath(d2.abspath).split(os.sep) + above_path = os.path.join(*['..']*len(dirs) + ['above']) above = d2.Dir(above_path) def test_rel_path(self): @@ -1667,16 +1657,11 @@ class FSTestCase(_tempdirTestCase): def test_proxy(self): """Test a Node.FS object wrapped in a proxy instance""" f1 = self.fs.File('fff') - class Proxy: - # Simplest possibly Proxy class that works for our test, - # this is stripped down from SCons.Util.Proxy. - def __init__(self, subject): - self.__subject = subject - def __getattr__(self, name): - return getattr(self.__subject, name) - p = Proxy(f1) + class MyProxy(SCons.Util.Proxy): + __str__ = SCons.Util.Delegate('__str__') + p = MyProxy(f1) f2 = self.fs.Entry(p) - assert f1 is f2, (f1, f2) + assert f1 is f2, (f1, str(f1), f2, str(f2)) @@ -1730,8 +1715,7 @@ class DirTestCase(_tempdirTestCase): fs.Dir(os.path.join('ddd', 'd1', 'f4')) fs.Dir(os.path.join('ddd', 'd1', 'f5')) dir.scan() - kids = map(lambda x: x.path, dir.children(None)) - kids.sort() + kids = sorted([x.path for x in dir.children(None)]) assert kids == [os.path.join('ddd', 'd1'), os.path.join('ddd', 'f1'), os.path.join('ddd', 'f2'), @@ -1756,8 +1740,7 @@ class DirTestCase(_tempdirTestCase): e = self.fs.Dir(os.path.join('d', 'empty')) s = self.fs.Dir(os.path.join('d', 'sub')) - #TODO(1.5) files = d.get_contents().split('\n') - files = string.split(d.get_contents(), '\n') + files = d.get_contents().split('\n') assert e.get_contents() == '', e.get_contents() assert e.get_text_contents() == '', e.get_text_contents() @@ -1776,14 +1759,12 @@ class DirTestCase(_tempdirTestCase): fs.File(os.path.join('ddd', 'f1')) dir.scan() - kids = map(lambda x: x.path, dir.children()) - kids.sort() + kids = sorted([x.path for x in dir.children()]) assert kids == [os.path.join('ddd', 'f1')], kids fs.File(os.path.join('ddd', 'f2')) dir.scan() - kids = map(lambda x: x.path, dir.children()) - kids.sort() + kids = sorted([x.path for x in dir.children()]) assert kids == [os.path.join('ddd', 'f1'), os.path.join('ddd', 'f2')], kids @@ -1818,8 +1799,8 @@ class DirTestCase(_tempdirTestCase): self.fs.VariantDir(sub2, src, duplicate=0) def check(result, expect): - result = map(str, result) - expect = map(os.path.normpath, expect) + result = list(map(str, result)) + expect = list(map(os.path.normpath, expect)) assert result == expect, result s = src.srcdir_list() @@ -1922,8 +1903,8 @@ class DirTestCase(_tempdirTestCase): exists_e.exists = return_true def check(result, expect): - result = map(str, result) - expect = map(os.path.normpath, expect) + result = list(map(str, result)) + expect = list(map(os.path.normpath, expect)) assert result == expect, result # First check from the source directory. @@ -2095,14 +2076,17 @@ class EntryTestCase(_tempdirTestCase): assert e4n.__class__ is SCons.Node.FS.File, e4n.__class__ assert not exists, "e4n exists?" - class MyCalc: + class MyCalc(object): def __init__(self, val): self.max_drift = 0 - class M: + class M(object): def __init__(self, val): self.val = val def collect(self, args): - return reduce(lambda x, y: x+y, args) + result = 0 + for a in args: + result += a + return result def signature(self, executor): return self.val + 222 self.module = M(val) @@ -2135,7 +2119,7 @@ class FileTestCase(_tempdirTestCase): d1 = self.fs.Dir('subdir/d1') d2 = self.fs.Dir('subdir/d2') dirs = fff.Dirs(['d1', 'd2']) - assert dirs == [d1, d2], map(str, dirs) + assert dirs == [d1, d2], list(map(str, dirs)) def test_exists(self): """Test the File.exists() method""" @@ -2248,8 +2232,7 @@ class GlobTestCase(_tempdirTestCase): strings_kwargs = copy.copy(kwargs) strings_kwargs['strings'] = True for input, string_expect, node_expect in cases: - r = apply(self.fs.Glob, (input,), strings_kwargs) - r.sort() + r = sorted(self.fs.Glob(input, **strings_kwargs)) assert r == string_expect, "Glob(%s, strings=True) expected %s, got %s" % (input, string_expect, r) # Now execute all of the cases without string=True and look for @@ -2259,26 +2242,25 @@ class GlobTestCase(_tempdirTestCase): # Verify those by running the list through str() before comparing # them with the expected list of strings. for input, string_expect, node_expect in cases: - r = apply(self.fs.Glob, (input,), kwargs) + r = self.fs.Glob(input, **kwargs) if node_expect: - r.sort(lambda a,b: cmp(a.path, b.path)) + r = sorted(r, key=lambda a: a.path) result = [] for n in node_expect: - if type(n) == type(''): + if isinstance(n, str): n = self.fs.Entry(n) result.append(n) fmt = lambda n: "%s %s" % (repr(n), repr(str(n))) else: - r = map(str, r) - r.sort() + r = sorted(map(str, r)) result = string_expect fmt = lambda n: n if r != result: import pprint print "Glob(%s) expected:" % repr(input) - pprint.pprint(map(fmt, result)) + pprint.pprint(list(map(fmt, result))) print "Glob(%s) got:" % repr(input) - pprint.pprint(map(fmt, r)) + pprint.pprint(list(map(fmt, r))) self.fail() def test_exact_match(self): @@ -2538,7 +2520,7 @@ class RepositoryTestCase(_tempdirTestCase): ] rep = self.fs.Dir('#').getRepositories() - r = map(lambda x, np=os.path.normpath: np(str(x)), rep) + r = [os.path.normpath(str(x)) for x in rep] assert r == expect, r def test_get_all_rdirs(self): @@ -2560,7 +2542,7 @@ class RepositoryTestCase(_tempdirTestCase): ] rep = self.fs.Dir('#').get_all_rdirs() - r = map(lambda x, np=os.path.normpath: np(str(x)), rep) + r = [os.path.normpath(str(x)) for x in rep] assert r == expect, r def test_rentry(self): @@ -2742,25 +2724,25 @@ class RepositoryTestCase(_tempdirTestCase): rep3_sub_d1 = fs.Dir(test.workpath('rep3', 'sub', 'd1')) r = fs.Top.Rfindalldirs((d1,)) - assert r == [d1], map(str, r) + assert r == [d1], list(map(str, r)) r = fs.Top.Rfindalldirs((d1, d2)) - assert r == [d1, d2], map(str, r) + assert r == [d1, d2], list(map(str, r)) r = fs.Top.Rfindalldirs(('d1',)) - assert r == [d1, rep1_d1, rep2_d1, rep3_d1], map(str, r) + assert r == [d1, rep1_d1, rep2_d1, rep3_d1], list(map(str, r)) r = fs.Top.Rfindalldirs(('#d1',)) - assert r == [d1, rep1_d1, rep2_d1, rep3_d1], map(str, r) + assert r == [d1, rep1_d1, rep2_d1, rep3_d1], list(map(str, r)) r = sub.Rfindalldirs(('d1',)) - assert r == [sub_d1, rep1_sub_d1, rep2_sub_d1, rep3_sub_d1], map(str, r) + assert r == [sub_d1, rep1_sub_d1, rep2_sub_d1, rep3_sub_d1], list(map(str, r)) r = sub.Rfindalldirs(('#d1',)) - assert r == [d1, rep1_d1, rep2_d1, rep3_d1], map(str, r) + assert r == [d1, rep1_d1, rep2_d1, rep3_d1], list(map(str, r)) r = fs.Top.Rfindalldirs(('d1', d2)) - assert r == [d1, rep1_d1, rep2_d1, rep3_d1, d2], map(str, r) + assert r == [d1, rep1_d1, rep2_d1, rep3_d1, d2], list(map(str, r)) def test_rexists(self): """Test the Entry.rexists() method""" @@ -2800,7 +2782,7 @@ class RepositoryTestCase(_tempdirTestCase): # 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 + tstamp = float(int(time.time() // 2) * 2) - 2.0 os.utime(test.workpath("rep2", "tstamp"), (tstamp - 2.0, tstamp)) f = fs.File("tstamp") t = f.get_timestamp() @@ -2831,8 +2813,8 @@ class RepositoryTestCase(_tempdirTestCase): try: eval('test_string = u"Con\x1aTents\n"') except SyntaxError: - import UserString - class FakeUnicodeString(UserString.UserString): + import collections + class FakeUnicodeString(collections.UserString): def encode(self, encoding): return str(self) test_string = FakeUnicodeString("Con\x1aTents\n") @@ -2892,10 +2874,10 @@ class find_fileTestCase(unittest.TestCase): nodes.append(SCons.Node.FS.find_file('pseudo', paths)) nodes.append(SCons.Node.FS.find_file('same', paths)) - file_names = map(str, nodes) - file_names = map(os.path.normpath, file_names) + file_names = list(map(str, nodes)) + file_names = list(map(os.path.normpath, file_names)) expect = ['./foo', './bar/baz', './pseudo', './bar/same'] - expect = map(os.path.normpath, expect) + expect = list(map(os.path.normpath, expect)) assert file_names == expect, file_names # Make sure we don't blow up if there's already a File in place @@ -2904,11 +2886,11 @@ class find_fileTestCase(unittest.TestCase): # 'bar/baz' as a Dir. SCons.Node.FS.find_file('baz/no_file_here', paths) - import StringIO + import io save_sys_stdout = sys.stdout try: - sio = StringIO.StringIO() + sio = io.StringIO() sys.stdout = sio SCons.Node.FS.find_file('foo2', paths, verbose="xyz") expect = " xyz: looking for 'foo2' in '.' ...\n" + \ @@ -2916,7 +2898,7 @@ class find_fileTestCase(unittest.TestCase): c = sio.getvalue() assert c == expect, c - sio = StringIO.StringIO() + sio = io.StringIO() sys.stdout = sio SCons.Node.FS.find_file('baz2', paths, verbose=1) expect = " find_file: looking for 'baz2' in '.' ...\n" + \ @@ -2925,7 +2907,7 @@ class find_fileTestCase(unittest.TestCase): c = sio.getvalue() assert c == expect, c - sio = StringIO.StringIO() + sio = io.StringIO() sys.stdout = sio SCons.Node.FS.find_file('on_disk', paths, verbose=1) expect = " find_file: looking for 'on_disk' in '.' ...\n" + \ @@ -2965,8 +2947,8 @@ class stored_infoTestCase(unittest.TestCase): bi = f.get_stored_info() assert hasattr(bi, 'ninfo') - class MySConsign: - class Null: + class MySConsign(object): + class Null(object): def __init__(self): self.xyzzy = 7 def get_entry(self, name): @@ -3433,14 +3415,14 @@ class SaveStringsTestCase(unittest.TestCase): fs1.VariantDir('d0', 'src', duplicate=0) fs1.VariantDir('d1', 'src', duplicate=1) - s = map(str, nodes) - expect = map(os.path.normpath, ['src/f', 'd1/f', 'd0/b', 'd1/b']) + s = list(map(str, nodes)) + expect = list(map(os.path.normpath, ['src/f', 'd1/f', 'd0/b', 'd1/b'])) assert s == expect, s modify(nodes) - s = map(str, nodes) - expect = map(os.path.normpath, ['src/f', 'src/f', 'd0/b', 'd1/b']) + s = list(map(str, nodes)) + expect = list(map(os.path.normpath, ['src/f', 'src/f', 'd0/b', 'd1/b'])) assert s == expect, s SCons.Node.FS.save_strings(1) @@ -3449,14 +3431,14 @@ class SaveStringsTestCase(unittest.TestCase): fs2.VariantDir('d0', 'src', duplicate=0) fs2.VariantDir('d1', 'src', duplicate=1) - s = map(str, nodes) - expect = map(os.path.normpath, ['src/f', 'd1/f', 'd0/b', 'd1/b']) + s = list(map(str, nodes)) + expect = list(map(os.path.normpath, ['src/f', 'd1/f', 'd0/b', 'd1/b'])) assert s == expect, s modify(nodes) - s = map(str, nodes) - expect = map(os.path.normpath, ['src/f', 'd1/f', 'd0/b', 'd1/b']) + s = list(map(str, nodes)) + expect = list(map(os.path.normpath, ['src/f', 'd1/f', 'd0/b', 'd1/b'])) assert s == expect, 'node str() not cached: %s'%s @@ -3509,7 +3491,7 @@ if __name__ == "__main__": ] for tclass in tclasses: names = unittest.getTestCaseNames(tclass, 'test_') - suite.addTests(map(tclass, names)) + suite.addTests(list(map(tclass, names))) if not unittest.TextTestRunner().run(suite).wasSuccessful(): sys.exit(1) -- cgit v1.2.3