Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | # You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 6 | |
michael@0 | 7 | import os |
michael@0 | 8 | import shutil |
michael@0 | 9 | import tempfile |
michael@0 | 10 | import unittest |
michael@0 | 11 | |
michael@0 | 12 | from manifestparser import convert |
michael@0 | 13 | from manifestparser import ManifestParser |
michael@0 | 14 | from StringIO import StringIO |
michael@0 | 15 | |
michael@0 | 16 | here = os.path.dirname(os.path.abspath(__file__)) |
michael@0 | 17 | |
michael@0 | 18 | class TestDirectoryConversion(unittest.TestCase): |
michael@0 | 19 | """test conversion of a directory tree to a manifest structure""" |
michael@0 | 20 | |
michael@0 | 21 | def create_stub(self, directory=None): |
michael@0 | 22 | """stub out a directory with files in it""" |
michael@0 | 23 | |
michael@0 | 24 | files = ('foo', 'bar', 'fleem') |
michael@0 | 25 | if directory is None: |
michael@0 | 26 | directory = tempfile.mkdtemp() |
michael@0 | 27 | for i in files: |
michael@0 | 28 | file(os.path.join(directory, i), 'w').write(i) |
michael@0 | 29 | subdir = os.path.join(directory, 'subdir') |
michael@0 | 30 | os.mkdir(subdir) |
michael@0 | 31 | file(os.path.join(subdir, 'subfile'), 'w').write('baz') |
michael@0 | 32 | return directory |
michael@0 | 33 | |
michael@0 | 34 | def test_directory_to_manifest(self): |
michael@0 | 35 | """ |
michael@0 | 36 | Test our ability to convert a static directory structure to a |
michael@0 | 37 | manifest. |
michael@0 | 38 | """ |
michael@0 | 39 | |
michael@0 | 40 | # create a stub directory |
michael@0 | 41 | stub = self.create_stub() |
michael@0 | 42 | try: |
michael@0 | 43 | stub = stub.replace(os.path.sep, "/") |
michael@0 | 44 | self.assertTrue(os.path.exists(stub) and os.path.isdir(stub)) |
michael@0 | 45 | |
michael@0 | 46 | # Make a manifest for it |
michael@0 | 47 | manifest = convert([stub]) |
michael@0 | 48 | self.assertEqual(str(manifest), |
michael@0 | 49 | """[%(stub)s/bar] |
michael@0 | 50 | subsuite = |
michael@0 | 51 | |
michael@0 | 52 | [%(stub)s/fleem] |
michael@0 | 53 | subsuite = |
michael@0 | 54 | |
michael@0 | 55 | [%(stub)s/foo] |
michael@0 | 56 | subsuite = |
michael@0 | 57 | |
michael@0 | 58 | [%(stub)s/subdir/subfile] |
michael@0 | 59 | subsuite = |
michael@0 | 60 | |
michael@0 | 61 | """ % dict(stub=stub)) |
michael@0 | 62 | except: |
michael@0 | 63 | raise |
michael@0 | 64 | finally: |
michael@0 | 65 | shutil.rmtree(stub) # cleanup |
michael@0 | 66 | |
michael@0 | 67 | def test_convert_directory_manifests_in_place(self): |
michael@0 | 68 | """ |
michael@0 | 69 | keep the manifests in place |
michael@0 | 70 | """ |
michael@0 | 71 | |
michael@0 | 72 | stub = self.create_stub() |
michael@0 | 73 | try: |
michael@0 | 74 | ManifestParser.populate_directory_manifests([stub], filename='manifest.ini') |
michael@0 | 75 | self.assertEqual(sorted(os.listdir(stub)), |
michael@0 | 76 | ['bar', 'fleem', 'foo', 'manifest.ini', 'subdir']) |
michael@0 | 77 | parser = ManifestParser() |
michael@0 | 78 | parser.read(os.path.join(stub, 'manifest.ini')) |
michael@0 | 79 | self.assertEqual([i['name'] for i in parser.tests], |
michael@0 | 80 | ['subfile', 'bar', 'fleem', 'foo']) |
michael@0 | 81 | parser = ManifestParser() |
michael@0 | 82 | parser.read(os.path.join(stub, 'subdir', 'manifest.ini')) |
michael@0 | 83 | self.assertEqual(len(parser.tests), 1) |
michael@0 | 84 | self.assertEqual(parser.tests[0]['name'], 'subfile') |
michael@0 | 85 | except: |
michael@0 | 86 | raise |
michael@0 | 87 | finally: |
michael@0 | 88 | shutil.rmtree(stub) |
michael@0 | 89 | |
michael@0 | 90 | def test_manifest_ignore(self): |
michael@0 | 91 | """test manifest `ignore` parameter for ignoring directories""" |
michael@0 | 92 | |
michael@0 | 93 | stub = self.create_stub() |
michael@0 | 94 | try: |
michael@0 | 95 | ManifestParser.populate_directory_manifests([stub], filename='manifest.ini', ignore=('subdir',)) |
michael@0 | 96 | parser = ManifestParser() |
michael@0 | 97 | parser.read(os.path.join(stub, 'manifest.ini')) |
michael@0 | 98 | self.assertEqual([i['name'] for i in parser.tests], |
michael@0 | 99 | ['bar', 'fleem', 'foo']) |
michael@0 | 100 | self.assertFalse(os.path.exists(os.path.join(stub, 'subdir', 'manifest.ini'))) |
michael@0 | 101 | except: |
michael@0 | 102 | raise |
michael@0 | 103 | finally: |
michael@0 | 104 | shutil.rmtree(stub) |
michael@0 | 105 | |
michael@0 | 106 | def test_pattern(self): |
michael@0 | 107 | """test directory -> manifest with a file pattern""" |
michael@0 | 108 | |
michael@0 | 109 | stub = self.create_stub() |
michael@0 | 110 | try: |
michael@0 | 111 | parser = convert([stub], pattern='f*', relative_to=stub) |
michael@0 | 112 | self.assertEqual([i['name'] for i in parser.tests], |
michael@0 | 113 | ['fleem', 'foo']) |
michael@0 | 114 | |
michael@0 | 115 | # test multiple patterns |
michael@0 | 116 | parser = convert([stub], pattern=('f*', 's*'), relative_to=stub) |
michael@0 | 117 | self.assertEqual([i['name'] for i in parser.tests], |
michael@0 | 118 | ['fleem', 'foo', 'subdir/subfile']) |
michael@0 | 119 | except: |
michael@0 | 120 | raise |
michael@0 | 121 | finally: |
michael@0 | 122 | shutil.rmtree(stub) |
michael@0 | 123 | |
michael@0 | 124 | def test_update(self): |
michael@0 | 125 | """ |
michael@0 | 126 | Test our ability to update tests from a manifest and a directory of |
michael@0 | 127 | files |
michael@0 | 128 | """ |
michael@0 | 129 | |
michael@0 | 130 | # boilerplate |
michael@0 | 131 | tempdir = tempfile.mkdtemp() |
michael@0 | 132 | for i in range(10): |
michael@0 | 133 | file(os.path.join(tempdir, str(i)), 'w').write(str(i)) |
michael@0 | 134 | |
michael@0 | 135 | # otherwise empty directory with a manifest file |
michael@0 | 136 | newtempdir = tempfile.mkdtemp() |
michael@0 | 137 | manifest_file = os.path.join(newtempdir, 'manifest.ini') |
michael@0 | 138 | manifest_contents = str(convert([tempdir], relative_to=tempdir)) |
michael@0 | 139 | with file(manifest_file, 'w') as f: |
michael@0 | 140 | f.write(manifest_contents) |
michael@0 | 141 | |
michael@0 | 142 | # get the manifest |
michael@0 | 143 | manifest = ManifestParser(manifests=(manifest_file,)) |
michael@0 | 144 | |
michael@0 | 145 | # All of the tests are initially missing: |
michael@0 | 146 | paths = [str(i) for i in range(10)] |
michael@0 | 147 | self.assertEqual([i['name'] for i in manifest.missing()], |
michael@0 | 148 | paths) |
michael@0 | 149 | |
michael@0 | 150 | # But then we copy one over: |
michael@0 | 151 | self.assertEqual(manifest.get('name', name='1'), ['1']) |
michael@0 | 152 | manifest.update(tempdir, name='1') |
michael@0 | 153 | self.assertEqual(sorted(os.listdir(newtempdir)), |
michael@0 | 154 | ['1', 'manifest.ini']) |
michael@0 | 155 | |
michael@0 | 156 | # Update that one file and copy all the "tests": |
michael@0 | 157 | file(os.path.join(tempdir, '1'), 'w').write('secret door') |
michael@0 | 158 | manifest.update(tempdir) |
michael@0 | 159 | self.assertEqual(sorted(os.listdir(newtempdir)), |
michael@0 | 160 | ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'manifest.ini']) |
michael@0 | 161 | self.assertEqual(file(os.path.join(newtempdir, '1')).read().strip(), |
michael@0 | 162 | 'secret door') |
michael@0 | 163 | |
michael@0 | 164 | # clean up: |
michael@0 | 165 | shutil.rmtree(tempdir) |
michael@0 | 166 | shutil.rmtree(newtempdir) |
michael@0 | 167 | |
michael@0 | 168 | |
michael@0 | 169 | if __name__ == '__main__': |
michael@0 | 170 | unittest.main() |