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 | from manifestparser import convert |
michael@0 | 12 | from manifestparser import ManifestParser |
michael@0 | 13 | from StringIO import StringIO |
michael@0 | 14 | |
michael@0 | 15 | here = os.path.dirname(os.path.abspath(__file__)) |
michael@0 | 16 | |
michael@0 | 17 | class TestManifestParser(unittest.TestCase): |
michael@0 | 18 | """ |
michael@0 | 19 | Test the manifest parser |
michael@0 | 20 | |
michael@0 | 21 | You must have ManifestDestiny installed before running these tests. |
michael@0 | 22 | Run ``python manifestparser.py setup develop`` with setuptools installed. |
michael@0 | 23 | """ |
michael@0 | 24 | |
michael@0 | 25 | def test_sanity(self): |
michael@0 | 26 | """Ensure basic parser is sane""" |
michael@0 | 27 | |
michael@0 | 28 | parser = ManifestParser() |
michael@0 | 29 | mozmill_example = os.path.join(here, 'mozmill-example.ini') |
michael@0 | 30 | parser.read(mozmill_example) |
michael@0 | 31 | tests = parser.tests |
michael@0 | 32 | self.assertEqual(len(tests), len(file(mozmill_example).read().strip().splitlines())) |
michael@0 | 33 | |
michael@0 | 34 | # Ensure that capitalization and order aren't an issue: |
michael@0 | 35 | lines = ['[%s]' % test['name'] for test in tests] |
michael@0 | 36 | self.assertEqual(lines, file(mozmill_example).read().strip().splitlines()) |
michael@0 | 37 | |
michael@0 | 38 | # Show how you select subsets of tests: |
michael@0 | 39 | mozmill_restart_example = os.path.join(here, 'mozmill-restart-example.ini') |
michael@0 | 40 | parser.read(mozmill_restart_example) |
michael@0 | 41 | restart_tests = parser.get(type='restart') |
michael@0 | 42 | self.assertTrue(len(restart_tests) < len(parser.tests)) |
michael@0 | 43 | self.assertEqual(len(restart_tests), len(parser.get(manifest=mozmill_restart_example))) |
michael@0 | 44 | self.assertFalse([test for test in restart_tests |
michael@0 | 45 | if test['manifest'] != os.path.join(here, 'mozmill-restart-example.ini')]) |
michael@0 | 46 | self.assertEqual(parser.get('name', tags=['foo']), |
michael@0 | 47 | ['restartTests/testExtensionInstallUninstall/test2.js', |
michael@0 | 48 | 'restartTests/testExtensionInstallUninstall/test1.js']) |
michael@0 | 49 | self.assertEqual(parser.get('name', foo='bar'), |
michael@0 | 50 | ['restartTests/testExtensionInstallUninstall/test2.js']) |
michael@0 | 51 | |
michael@0 | 52 | def test_include(self): |
michael@0 | 53 | """Illustrate how include works""" |
michael@0 | 54 | |
michael@0 | 55 | include_example = os.path.join(here, 'include-example.ini') |
michael@0 | 56 | parser = ManifestParser(manifests=(include_example,)) |
michael@0 | 57 | |
michael@0 | 58 | # All of the tests should be included, in order: |
michael@0 | 59 | self.assertEqual(parser.get('name'), |
michael@0 | 60 | ['crash-handling', 'fleem', 'flowers']) |
michael@0 | 61 | self.assertEqual([(test['name'], os.path.basename(test['manifest'])) for test in parser.tests], |
michael@0 | 62 | [('crash-handling', 'bar.ini'), ('fleem', 'include-example.ini'), ('flowers', 'foo.ini')]) |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | # The manifests should be there too: |
michael@0 | 66 | self.assertEqual(len(parser.manifests()), 3) |
michael@0 | 67 | |
michael@0 | 68 | # We already have the root directory: |
michael@0 | 69 | self.assertEqual(here, parser.rootdir) |
michael@0 | 70 | |
michael@0 | 71 | |
michael@0 | 72 | # DEFAULT values should persist across includes, unless they're |
michael@0 | 73 | # overwritten. In this example, include-example.ini sets foo=bar, but |
michael@0 | 74 | # it's overridden to fleem in bar.ini |
michael@0 | 75 | self.assertEqual(parser.get('name', foo='bar'), |
michael@0 | 76 | ['fleem', 'flowers']) |
michael@0 | 77 | self.assertEqual(parser.get('name', foo='fleem'), |
michael@0 | 78 | ['crash-handling']) |
michael@0 | 79 | |
michael@0 | 80 | # Passing parameters in the include section allows defining variables in |
michael@0 | 81 | #the submodule scope: |
michael@0 | 82 | self.assertEqual(parser.get('name', tags=['red']), |
michael@0 | 83 | ['flowers']) |
michael@0 | 84 | |
michael@0 | 85 | # However, this should be overridable from the DEFAULT section in the |
michael@0 | 86 | # included file and that overridable via the key directly connected to |
michael@0 | 87 | # the test: |
michael@0 | 88 | self.assertEqual(parser.get(name='flowers')[0]['blue'], |
michael@0 | 89 | 'ocean') |
michael@0 | 90 | self.assertEqual(parser.get(name='flowers')[0]['yellow'], |
michael@0 | 91 | 'submarine') |
michael@0 | 92 | |
michael@0 | 93 | # You can query multiple times if you need to: |
michael@0 | 94 | flowers = parser.get(foo='bar') |
michael@0 | 95 | self.assertEqual(len(flowers), 2) |
michael@0 | 96 | |
michael@0 | 97 | # Using the inverse flag should invert the set of tests returned: |
michael@0 | 98 | self.assertEqual(parser.get('name', inverse=True, tags=['red']), |
michael@0 | 99 | ['crash-handling', 'fleem']) |
michael@0 | 100 | |
michael@0 | 101 | # All of the included tests actually exist: |
michael@0 | 102 | self.assertEqual([i['name'] for i in parser.missing()], []) |
michael@0 | 103 | |
michael@0 | 104 | # Write the output to a manifest: |
michael@0 | 105 | buffer = StringIO() |
michael@0 | 106 | parser.write(fp=buffer, global_kwargs={'foo': 'bar'}) |
michael@0 | 107 | self.assertEqual(buffer.getvalue().strip(), |
michael@0 | 108 | '[DEFAULT]\nfoo = bar\n\n[fleem]\nsubsuite = \n\n[include/flowers]\nblue = ocean\nred = roses\nsubsuite = \nyellow = submarine') |
michael@0 | 109 | |
michael@0 | 110 | def test_copy(self): |
michael@0 | 111 | """Test our ability to copy a set of manifests""" |
michael@0 | 112 | |
michael@0 | 113 | tempdir = tempfile.mkdtemp() |
michael@0 | 114 | include_example = os.path.join(here, 'include-example.ini') |
michael@0 | 115 | manifest = ManifestParser(manifests=(include_example,)) |
michael@0 | 116 | manifest.copy(tempdir) |
michael@0 | 117 | self.assertEqual(sorted(os.listdir(tempdir)), |
michael@0 | 118 | ['fleem', 'include', 'include-example.ini']) |
michael@0 | 119 | self.assertEqual(sorted(os.listdir(os.path.join(tempdir, 'include'))), |
michael@0 | 120 | ['bar.ini', 'crash-handling', 'flowers', 'foo.ini']) |
michael@0 | 121 | from_manifest = ManifestParser(manifests=(include_example,)) |
michael@0 | 122 | to_manifest = os.path.join(tempdir, 'include-example.ini') |
michael@0 | 123 | to_manifest = ManifestParser(manifests=(to_manifest,)) |
michael@0 | 124 | self.assertEqual(to_manifest.get('name'), from_manifest.get('name')) |
michael@0 | 125 | shutil.rmtree(tempdir) |
michael@0 | 126 | |
michael@0 | 127 | def test_path_override(self): |
michael@0 | 128 | """You can override the path in the section too. |
michael@0 | 129 | This shows that you can use a relative path""" |
michael@0 | 130 | path_example = os.path.join(here, 'path-example.ini') |
michael@0 | 131 | manifest = ManifestParser(manifests=(path_example,)) |
michael@0 | 132 | self.assertEqual(manifest.tests[0]['path'], |
michael@0 | 133 | os.path.join(here, 'fleem')) |
michael@0 | 134 | |
michael@0 | 135 | def test_relative_path(self): |
michael@0 | 136 | """ |
michael@0 | 137 | Relative test paths are correctly calculated. |
michael@0 | 138 | """ |
michael@0 | 139 | relative_path = os.path.join(here, 'relative-path.ini') |
michael@0 | 140 | manifest = ManifestParser(manifests=(relative_path,)) |
michael@0 | 141 | self.assertEqual(manifest.tests[0]['path'], |
michael@0 | 142 | os.path.join(os.path.dirname(here), 'fleem')) |
michael@0 | 143 | self.assertEqual(manifest.tests[0]['relpath'], |
michael@0 | 144 | os.path.join('..', 'fleem')) |
michael@0 | 145 | self.assertEqual(manifest.tests[1]['relpath'], |
michael@0 | 146 | os.path.join('..', 'testsSIBLING', 'example')) |
michael@0 | 147 | |
michael@0 | 148 | def test_path_from_fd(self): |
michael@0 | 149 | """ |
michael@0 | 150 | Test paths are left untouched when manifest is a file-like object. |
michael@0 | 151 | """ |
michael@0 | 152 | fp = StringIO("[section]\npath=fleem") |
michael@0 | 153 | manifest = ManifestParser(manifests=(fp,)) |
michael@0 | 154 | self.assertEqual(manifest.tests[0]['path'], 'fleem') |
michael@0 | 155 | self.assertEqual(manifest.tests[0]['relpath'], 'fleem') |
michael@0 | 156 | self.assertEqual(manifest.tests[0]['manifest'], None) |
michael@0 | 157 | |
michael@0 | 158 | def test_comments(self): |
michael@0 | 159 | """ |
michael@0 | 160 | ensure comments work, see |
michael@0 | 161 | https://bugzilla.mozilla.org/show_bug.cgi?id=813674 |
michael@0 | 162 | """ |
michael@0 | 163 | comment_example = os.path.join(here, 'comment-example.ini') |
michael@0 | 164 | manifest = ManifestParser(manifests=(comment_example,)) |
michael@0 | 165 | self.assertEqual(len(manifest.tests), 8) |
michael@0 | 166 | names = [i['name'] for i in manifest.tests] |
michael@0 | 167 | self.assertFalse('test_0202_app_launch_apply_update_dirlocked.js' in names) |
michael@0 | 168 | |
michael@0 | 169 | def test_verifyDirectory(self): |
michael@0 | 170 | |
michael@0 | 171 | directory = os.path.join(here, 'verifyDirectory') |
michael@0 | 172 | |
michael@0 | 173 | # correct manifest |
michael@0 | 174 | manifest_path = os.path.join(directory, 'verifyDirectory.ini') |
michael@0 | 175 | manifest = ManifestParser(manifests=(manifest_path,)) |
michael@0 | 176 | missing = manifest.verifyDirectory(directory, extensions=('.js',)) |
michael@0 | 177 | self.assertEqual(missing, (set(), set())) |
michael@0 | 178 | |
michael@0 | 179 | # manifest is missing test_1.js |
michael@0 | 180 | test_1 = os.path.join(directory, 'test_1.js') |
michael@0 | 181 | manifest_path = os.path.join(directory, 'verifyDirectory_incomplete.ini') |
michael@0 | 182 | manifest = ManifestParser(manifests=(manifest_path,)) |
michael@0 | 183 | missing = manifest.verifyDirectory(directory, extensions=('.js',)) |
michael@0 | 184 | self.assertEqual(missing, (set(), set([test_1]))) |
michael@0 | 185 | |
michael@0 | 186 | # filesystem is missing test_notappearinginthisfilm.js |
michael@0 | 187 | missing_test = os.path.join(directory, 'test_notappearinginthisfilm.js') |
michael@0 | 188 | manifest_path = os.path.join(directory, 'verifyDirectory_toocomplete.ini') |
michael@0 | 189 | manifest = ManifestParser(manifests=(manifest_path,)) |
michael@0 | 190 | missing = manifest.verifyDirectory(directory, extensions=('.js',)) |
michael@0 | 191 | self.assertEqual(missing, (set([missing_test]), set())) |
michael@0 | 192 | |
michael@0 | 193 | def test_just_defaults(self): |
michael@0 | 194 | """Ensure a manifest with just a DEFAULT section exposes that data.""" |
michael@0 | 195 | |
michael@0 | 196 | parser = ManifestParser() |
michael@0 | 197 | manifest = os.path.join(here, 'just-defaults.ini') |
michael@0 | 198 | parser.read(manifest) |
michael@0 | 199 | self.assertEqual(len(parser.tests), 0) |
michael@0 | 200 | self.assertTrue(manifest in parser.manifest_defaults) |
michael@0 | 201 | self.assertEquals(parser.manifest_defaults[manifest]['foo'], 'bar') |
michael@0 | 202 | |
michael@0 | 203 | def test_manifest_list(self): |
michael@0 | 204 | """ |
michael@0 | 205 | Ensure a manifest with just a DEFAULT section still returns |
michael@0 | 206 | itself from the manifests() method. |
michael@0 | 207 | """ |
michael@0 | 208 | |
michael@0 | 209 | parser = ManifestParser() |
michael@0 | 210 | manifest = os.path.join(here, 'no-tests.ini') |
michael@0 | 211 | parser.read(manifest) |
michael@0 | 212 | self.assertEqual(len(parser.tests), 0) |
michael@0 | 213 | self.assertTrue(len(parser.manifests()) == 1) |
michael@0 | 214 | |
michael@0 | 215 | if __name__ == '__main__': |
michael@0 | 216 | unittest.main() |