OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import os |
| 4 import shutil |
| 5 import tempfile |
| 6 import unittest |
| 7 from manifestparser import convert |
| 8 from manifestparser import ManifestParser |
| 9 from StringIO import StringIO |
| 10 |
| 11 here = os.path.dirname(os.path.abspath(__file__)) |
| 12 |
| 13 class TestManifestparser(unittest.TestCase): |
| 14 """ |
| 15 Test the manifest parser |
| 16 |
| 17 You must have ManifestDestiny installed before running these tests. |
| 18 Run ``python manifestparser.py setup develop`` with setuptools installed. |
| 19 """ |
| 20 |
| 21 def test_sanity(self): |
| 22 """Ensure basic parser is sane""" |
| 23 |
| 24 parser = ManifestParser() |
| 25 mozmill_example = os.path.join(here, 'mozmill-example.ini') |
| 26 parser.read(mozmill_example) |
| 27 tests = parser.tests |
| 28 self.assertEqual(len(tests), len(file(mozmill_example).read().strip().sp
litlines())) |
| 29 |
| 30 # Ensure that capitalization and order aren't an issue: |
| 31 lines = ['[%s]' % test['name'] for test in tests] |
| 32 self.assertEqual(lines, file(mozmill_example).read().strip().splitlines(
)) |
| 33 |
| 34 # Show how you select subsets of tests: |
| 35 mozmill_restart_example = os.path.join(here, 'mozmill-restart-example.in
i') |
| 36 parser.read(mozmill_restart_example) |
| 37 restart_tests = parser.get(type='restart') |
| 38 self.assertTrue(len(restart_tests) < len(parser.tests)) |
| 39 self.assertEqual(len(restart_tests), len(parser.get(manifest=mozmill_res
tart_example))) |
| 40 self.assertFalse([test for test in restart_tests |
| 41 if test['manifest'] != os.path.join(here, 'mozmill-res
tart-example.ini')]) |
| 42 self.assertEqual(parser.get('name', tags=['foo']), |
| 43 ['restartTests/testExtensionInstallUninstall/test2.js', |
| 44 'restartTests/testExtensionInstallUninstall/test1.js']
) |
| 45 self.assertEqual(parser.get('name', foo='bar'), |
| 46 ['restartTests/testExtensionInstallUninstall/test2.js']
) |
| 47 |
| 48 def test_include(self): |
| 49 """Illustrate how include works""" |
| 50 |
| 51 include_example = os.path.join(here, 'include-example.ini') |
| 52 parser = ManifestParser(manifests=(include_example,)) |
| 53 |
| 54 # All of the tests should be included, in order: |
| 55 self.assertEqual(parser.get('name'), |
| 56 ['crash-handling', 'fleem', 'flowers']) |
| 57 self.assertEqual([(test['name'], os.path.basename(test['manifest'])) for
test in parser.tests], |
| 58 [('crash-handling', 'bar.ini'), ('fleem', 'include-exam
ple.ini'), ('flowers', 'foo.ini')]) |
| 59 |
| 60 |
| 61 # The manifests should be there too: |
| 62 self.assertEqual(len(parser.manifests()), 3) |
| 63 |
| 64 # We already have the root directory: |
| 65 self.assertEqual(here, parser.rootdir) |
| 66 |
| 67 |
| 68 # DEFAULT values should persist across includes, unless they're |
| 69 # overwritten. In this example, include-example.ini sets foo=bar, but |
| 70 # it's overridden to fleem in bar.ini |
| 71 self.assertEqual(parser.get('name', foo='bar'), |
| 72 ['fleem', 'flowers']) |
| 73 self.assertEqual(parser.get('name', foo='fleem'), |
| 74 ['crash-handling']) |
| 75 |
| 76 # Passing parameters in the include section allows defining variables in |
| 77 #the submodule scope: |
| 78 self.assertEqual(parser.get('name', tags=['red']), |
| 79 ['flowers']) |
| 80 |
| 81 # However, this should be overridable from the DEFAULT section in the |
| 82 # included file and that overridable via the key directly connected to |
| 83 # the test: |
| 84 self.assertEqual(parser.get(name='flowers')[0]['blue'], |
| 85 'ocean') |
| 86 self.assertEqual(parser.get(name='flowers')[0]['yellow'], |
| 87 'submarine') |
| 88 |
| 89 # You can query multiple times if you need to:: |
| 90 flowers = parser.get(foo='bar') |
| 91 self.assertEqual(len(flowers), 2) |
| 92 |
| 93 # Using the inverse flag should invert the set of tests returned: |
| 94 self.assertEqual(parser.get('name', inverse=True, tags=['red']), |
| 95 ['crash-handling', 'fleem']) |
| 96 |
| 97 # All of the included tests actually exist:: |
| 98 self.assertEqual([i['name'] for i in parser.missing()], []) |
| 99 |
| 100 # Write the output to a manifest: |
| 101 buffer = StringIO() |
| 102 parser.write(fp=buffer, global_kwargs={'foo': 'bar'}) |
| 103 self.assertEqual(buffer.getvalue().strip(), |
| 104 '[DEFAULT]\nfoo = bar\n\n[fleem]\n\n[include/flowers]\n
blue = ocean\nred = roses\nyellow = submarine') |
| 105 |
| 106 |
| 107 def test_directory_to_manifest(self): |
| 108 """ |
| 109 Test our ability to convert a static directory structure to a |
| 110 manifest. |
| 111 """ |
| 112 |
| 113 # First, stub out a directory with files in it:: |
| 114 def create_stub(): |
| 115 directory = tempfile.mkdtemp() |
| 116 for i in 'foo', 'bar', 'fleem': |
| 117 file(os.path.join(directory, i), 'w').write(i) |
| 118 subdir = os.path.join(directory, 'subdir') |
| 119 os.mkdir(subdir) |
| 120 file(os.path.join(subdir, 'subfile'), 'w').write('baz') |
| 121 return directory |
| 122 stub = create_stub() |
| 123 self.assertTrue(os.path.exists(stub) and os.path.isdir(stub)) |
| 124 |
| 125 # Make a manifest for it: |
| 126 self.assertEqual(convert([stub]), |
| 127 """[bar] |
| 128 [fleem] |
| 129 [foo] |
| 130 [subdir/subfile]""") |
| 131 shutil.rmtree(stub) # cleanup |
| 132 |
| 133 # Now do the same thing but keep the manifests in place: |
| 134 stub = create_stub() |
| 135 convert([stub], write='manifest.ini') |
| 136 self.assertEqual(sorted(os.listdir(stub)), |
| 137 ['bar', 'fleem', 'foo', 'manifest.ini', 'subdir']) |
| 138 parser = ManifestParser() |
| 139 parser.read(os.path.join(stub, 'manifest.ini')) |
| 140 self.assertEqual([i['name'] for i in parser.tests], |
| 141 ['subfile', 'bar', 'fleem', 'foo']) |
| 142 parser = ManifestParser() |
| 143 parser.read(os.path.join(stub, 'subdir', 'manifest.ini')) |
| 144 self.assertEqual(len(parser.tests), 1) |
| 145 self.assertEqual(parser.tests[0]['name'], 'subfile') |
| 146 shutil.rmtree(stub) |
| 147 |
| 148 |
| 149 def test_copy(self): |
| 150 """Test our ability to copy a set of manifests""" |
| 151 |
| 152 tempdir = tempfile.mkdtemp() |
| 153 include_example = os.path.join(here, 'include-example.ini') |
| 154 manifest = ManifestParser(manifests=(include_example,)) |
| 155 manifest.copy(tempdir) |
| 156 self.assertEqual(sorted(os.listdir(tempdir)), |
| 157 ['fleem', 'include', 'include-example.ini']) |
| 158 self.assertEqual(sorted(os.listdir(os.path.join(tempdir, 'include'))), |
| 159 ['bar.ini', 'crash-handling', 'flowers', 'foo.ini']) |
| 160 from_manifest = ManifestParser(manifests=(include_example,)) |
| 161 to_manifest = os.path.join(tempdir, 'include-example.ini') |
| 162 to_manifest = ManifestParser(manifests=(to_manifest,)) |
| 163 self.assertEqual(to_manifest.get('name'), from_manifest.get('name')) |
| 164 shutil.rmtree(tempdir) |
| 165 |
| 166 |
| 167 def test_update(self): |
| 168 """ |
| 169 Test our ability to update tests from a manifest and a directory of |
| 170 files |
| 171 """ |
| 172 |
| 173 # boilerplate |
| 174 tempdir = tempfile.mkdtemp() |
| 175 for i in range(10): |
| 176 file(os.path.join(tempdir, str(i)), 'w').write(str(i)) |
| 177 |
| 178 # First, make a manifest: |
| 179 manifest = convert([tempdir]) |
| 180 newtempdir = tempfile.mkdtemp() |
| 181 manifest_file = os.path.join(newtempdir, 'manifest.ini') |
| 182 file(manifest_file,'w').write(manifest) |
| 183 manifest = ManifestParser(manifests=(manifest_file,)) |
| 184 self.assertEqual(manifest.get('name'), |
| 185 [str(i) for i in range(10)]) |
| 186 |
| 187 # All of the tests are initially missing: |
| 188 self.assertEqual([i['name'] for i in manifest.missing()], |
| 189 [str(i) for i in range(10)]) |
| 190 |
| 191 # But then we copy one over: |
| 192 self.assertEqual(manifest.get('name', name='1'), ['1']) |
| 193 manifest.update(tempdir, name='1') |
| 194 self.assertEqual(sorted(os.listdir(newtempdir)), |
| 195 ['1', 'manifest.ini']) |
| 196 |
| 197 # Update that one file and copy all the "tests": |
| 198 file(os.path.join(tempdir, '1'), 'w').write('secret door') |
| 199 manifest.update(tempdir) |
| 200 self.assertEqual(sorted(os.listdir(newtempdir)), |
| 201 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'man
ifest.ini']) |
| 202 self.assertEqual(file(os.path.join(newtempdir, '1')).read().strip(), |
| 203 'secret door') |
| 204 |
| 205 # clean up: |
| 206 shutil.rmtree(tempdir) |
| 207 shutil.rmtree(newtempdir) |
| 208 |
| 209 def test_path_override(self): |
| 210 """You can override the path in the section too. |
| 211 This shows that you can use a relative path""" |
| 212 path_example = os.path.join(here, 'path-example.ini') |
| 213 manifest = ManifestParser(manifests=(path_example,)) |
| 214 self.assertEqual(manifest.tests[0]['path'], |
| 215 os.path.join(here, 'fleem')) |
| 216 |
| 217 |
| 218 if __name__ == '__main__': |
| 219 unittest.main() |
OLD | NEW |