2 # Copyright 2014 The Swarming Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 that
4 # can be found in the LICENSE file.
14 # net_utils adjusts sys.path.
17 import isolated_format
23 class TestCase(net_utils.TestCase):
24 def test_get_hash_algo(self):
25 # Tests here assume ALGO is used for default namespaces, check this
27 self.assertIs(isolated_format.get_hash_algo('default'), ALGO)
28 self.assertIs(isolated_format.get_hash_algo('default-gzip'), ALGO)
31 class SymlinkTest(unittest.TestCase):
33 super(SymlinkTest, self).setUp()
34 self.old_cwd = os.getcwd()
35 self.cwd = tempfile.mkdtemp(prefix='isolate_')
36 # Everything should work even from another directory.
41 os.chdir(self.old_cwd)
42 shutil.rmtree(self.cwd)
44 super(SymlinkTest, self).tearDown()
46 if sys.platform == 'darwin':
47 def test_expand_symlinks_path_case(self):
48 # Ensures that the resulting path case is fixed on case insensitive file
50 os.symlink('dest', os.path.join(self.cwd, 'link'))
51 os.mkdir(os.path.join(self.cwd, 'Dest'))
52 open(os.path.join(self.cwd, 'Dest', 'file.txt'), 'w').close()
54 result = isolated_format.expand_symlinks(unicode(self.cwd), 'link')
55 self.assertEqual((u'Dest', [u'link']), result)
56 result = isolated_format.expand_symlinks(
57 unicode(self.cwd), 'link/File.txt')
58 self.assertEqual((u'Dest/file.txt', [u'link']), result)
60 def test_expand_directories_and_symlinks_path_case(self):
61 # Ensures that the resulting path case is fixed on case insensitive file
62 # system. A superset of test_expand_symlinks_path_case.
63 # Create *all* the paths with the wrong path case.
64 basedir = os.path.join(self.cwd, 'baseDir')
65 os.mkdir(basedir.lower())
66 subdir = os.path.join(basedir, 'subDir')
67 os.mkdir(subdir.lower())
68 open(os.path.join(subdir, 'Foo.txt'), 'w').close()
69 os.symlink('subDir', os.path.join(basedir, 'linkdir'))
70 actual = isolated_format.expand_directories_and_symlinks(
71 unicode(self.cwd), [u'baseDir/'], lambda _: None, True, False)
74 u'basedir/subdir/Foo.txt',
75 u'basedir/subdir/Foo.txt',
77 self.assertEqual(expected, actual)
79 def test_file_to_metadata_path_case_simple(self):
80 # Ensure the symlink dest is saved in the right path case.
81 subdir = os.path.join(self.cwd, 'subdir')
83 linkdir = os.path.join(self.cwd, 'linkdir')
84 os.symlink('subDir', linkdir)
85 actual = isolated_format.file_to_metadata(
86 unicode(linkdir.upper()), {}, True, ALGO)
87 expected = {'l': u'subdir', 'm': 360, 't': int(os.stat(linkdir).st_mtime)}
88 self.assertEqual(expected, actual)
90 def test_file_to_metadata_path_case_complex(self):
91 # Ensure the symlink dest is saved in the right path case. This includes 2
93 basedir = os.path.join(self.cwd, 'basebir')
96 linkeddir2 = os.path.join(self.cwd, 'linkeddir2')
99 linkeddir1 = os.path.join(basedir, 'linkeddir1')
100 os.symlink('../linkedDir2', linkeddir1)
102 subsymlinkdir = os.path.join(basedir, 'symlinkdir')
103 os.symlink('linkedDir1', subsymlinkdir)
105 actual = isolated_format.file_to_metadata(
106 unicode(subsymlinkdir.upper()), {}, True, ALGO)
108 'l': u'linkeddir1', 'm': 360, 't': int(os.stat(subsymlinkdir).st_mtime),
110 self.assertEqual(expected, actual)
112 actual = isolated_format.file_to_metadata(
113 unicode(linkeddir1.upper()), {}, True, ALGO)
115 'l': u'../linkeddir2', 'm': 360, 't': int(os.stat(linkeddir1).st_mtime),
117 self.assertEqual(expected, actual)
119 if sys.platform != 'win32':
120 def test_symlink_input_absolute_path(self):
121 # A symlink is outside of the checkout, it should be treated as a normal
124 # .../src/out -> .../tmp/foo
127 src = os.path.join(self.cwd, u'src')
128 src_out = os.path.join(src, 'out')
129 tmp = os.path.join(self.cwd, 'tmp')
130 tmp_foo = os.path.join(tmp, 'foo')
134 # The problem was that it's an absolute path, so it must be considered a
136 os.symlink(tmp, src_out)
137 open(os.path.join(tmp_foo, 'bar.txt'), 'w').close()
138 actual = isolated_format.expand_symlinks(src, u'out/foo/bar.txt')
139 self.assertEqual((u'out/foo/bar.txt', []), actual)
142 if __name__ == '__main__':
144 unittest.TestCase.maxDiff = None
146 level=(logging.DEBUG if '-v' in sys.argv else logging.ERROR))