1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/manifestdestiny/tests/test_manifestparser.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,216 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# This Source Code Form is subject to the terms of the Mozilla Public 1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 +# You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + 1.10 +import os 1.11 +import shutil 1.12 +import tempfile 1.13 +import unittest 1.14 +from manifestparser import convert 1.15 +from manifestparser import ManifestParser 1.16 +from StringIO import StringIO 1.17 + 1.18 +here = os.path.dirname(os.path.abspath(__file__)) 1.19 + 1.20 +class TestManifestParser(unittest.TestCase): 1.21 + """ 1.22 + Test the manifest parser 1.23 + 1.24 + You must have ManifestDestiny installed before running these tests. 1.25 + Run ``python manifestparser.py setup develop`` with setuptools installed. 1.26 + """ 1.27 + 1.28 + def test_sanity(self): 1.29 + """Ensure basic parser is sane""" 1.30 + 1.31 + parser = ManifestParser() 1.32 + mozmill_example = os.path.join(here, 'mozmill-example.ini') 1.33 + parser.read(mozmill_example) 1.34 + tests = parser.tests 1.35 + self.assertEqual(len(tests), len(file(mozmill_example).read().strip().splitlines())) 1.36 + 1.37 + # Ensure that capitalization and order aren't an issue: 1.38 + lines = ['[%s]' % test['name'] for test in tests] 1.39 + self.assertEqual(lines, file(mozmill_example).read().strip().splitlines()) 1.40 + 1.41 + # Show how you select subsets of tests: 1.42 + mozmill_restart_example = os.path.join(here, 'mozmill-restart-example.ini') 1.43 + parser.read(mozmill_restart_example) 1.44 + restart_tests = parser.get(type='restart') 1.45 + self.assertTrue(len(restart_tests) < len(parser.tests)) 1.46 + self.assertEqual(len(restart_tests), len(parser.get(manifest=mozmill_restart_example))) 1.47 + self.assertFalse([test for test in restart_tests 1.48 + if test['manifest'] != os.path.join(here, 'mozmill-restart-example.ini')]) 1.49 + self.assertEqual(parser.get('name', tags=['foo']), 1.50 + ['restartTests/testExtensionInstallUninstall/test2.js', 1.51 + 'restartTests/testExtensionInstallUninstall/test1.js']) 1.52 + self.assertEqual(parser.get('name', foo='bar'), 1.53 + ['restartTests/testExtensionInstallUninstall/test2.js']) 1.54 + 1.55 + def test_include(self): 1.56 + """Illustrate how include works""" 1.57 + 1.58 + include_example = os.path.join(here, 'include-example.ini') 1.59 + parser = ManifestParser(manifests=(include_example,)) 1.60 + 1.61 + # All of the tests should be included, in order: 1.62 + self.assertEqual(parser.get('name'), 1.63 + ['crash-handling', 'fleem', 'flowers']) 1.64 + self.assertEqual([(test['name'], os.path.basename(test['manifest'])) for test in parser.tests], 1.65 + [('crash-handling', 'bar.ini'), ('fleem', 'include-example.ini'), ('flowers', 'foo.ini')]) 1.66 + 1.67 + 1.68 + # The manifests should be there too: 1.69 + self.assertEqual(len(parser.manifests()), 3) 1.70 + 1.71 + # We already have the root directory: 1.72 + self.assertEqual(here, parser.rootdir) 1.73 + 1.74 + 1.75 + # DEFAULT values should persist across includes, unless they're 1.76 + # overwritten. In this example, include-example.ini sets foo=bar, but 1.77 + # it's overridden to fleem in bar.ini 1.78 + self.assertEqual(parser.get('name', foo='bar'), 1.79 + ['fleem', 'flowers']) 1.80 + self.assertEqual(parser.get('name', foo='fleem'), 1.81 + ['crash-handling']) 1.82 + 1.83 + # Passing parameters in the include section allows defining variables in 1.84 + #the submodule scope: 1.85 + self.assertEqual(parser.get('name', tags=['red']), 1.86 + ['flowers']) 1.87 + 1.88 + # However, this should be overridable from the DEFAULT section in the 1.89 + # included file and that overridable via the key directly connected to 1.90 + # the test: 1.91 + self.assertEqual(parser.get(name='flowers')[0]['blue'], 1.92 + 'ocean') 1.93 + self.assertEqual(parser.get(name='flowers')[0]['yellow'], 1.94 + 'submarine') 1.95 + 1.96 + # You can query multiple times if you need to: 1.97 + flowers = parser.get(foo='bar') 1.98 + self.assertEqual(len(flowers), 2) 1.99 + 1.100 + # Using the inverse flag should invert the set of tests returned: 1.101 + self.assertEqual(parser.get('name', inverse=True, tags=['red']), 1.102 + ['crash-handling', 'fleem']) 1.103 + 1.104 + # All of the included tests actually exist: 1.105 + self.assertEqual([i['name'] for i in parser.missing()], []) 1.106 + 1.107 + # Write the output to a manifest: 1.108 + buffer = StringIO() 1.109 + parser.write(fp=buffer, global_kwargs={'foo': 'bar'}) 1.110 + self.assertEqual(buffer.getvalue().strip(), 1.111 + '[DEFAULT]\nfoo = bar\n\n[fleem]\nsubsuite = \n\n[include/flowers]\nblue = ocean\nred = roses\nsubsuite = \nyellow = submarine') 1.112 + 1.113 + def test_copy(self): 1.114 + """Test our ability to copy a set of manifests""" 1.115 + 1.116 + tempdir = tempfile.mkdtemp() 1.117 + include_example = os.path.join(here, 'include-example.ini') 1.118 + manifest = ManifestParser(manifests=(include_example,)) 1.119 + manifest.copy(tempdir) 1.120 + self.assertEqual(sorted(os.listdir(tempdir)), 1.121 + ['fleem', 'include', 'include-example.ini']) 1.122 + self.assertEqual(sorted(os.listdir(os.path.join(tempdir, 'include'))), 1.123 + ['bar.ini', 'crash-handling', 'flowers', 'foo.ini']) 1.124 + from_manifest = ManifestParser(manifests=(include_example,)) 1.125 + to_manifest = os.path.join(tempdir, 'include-example.ini') 1.126 + to_manifest = ManifestParser(manifests=(to_manifest,)) 1.127 + self.assertEqual(to_manifest.get('name'), from_manifest.get('name')) 1.128 + shutil.rmtree(tempdir) 1.129 + 1.130 + def test_path_override(self): 1.131 + """You can override the path in the section too. 1.132 + This shows that you can use a relative path""" 1.133 + path_example = os.path.join(here, 'path-example.ini') 1.134 + manifest = ManifestParser(manifests=(path_example,)) 1.135 + self.assertEqual(manifest.tests[0]['path'], 1.136 + os.path.join(here, 'fleem')) 1.137 + 1.138 + def test_relative_path(self): 1.139 + """ 1.140 + Relative test paths are correctly calculated. 1.141 + """ 1.142 + relative_path = os.path.join(here, 'relative-path.ini') 1.143 + manifest = ManifestParser(manifests=(relative_path,)) 1.144 + self.assertEqual(manifest.tests[0]['path'], 1.145 + os.path.join(os.path.dirname(here), 'fleem')) 1.146 + self.assertEqual(manifest.tests[0]['relpath'], 1.147 + os.path.join('..', 'fleem')) 1.148 + self.assertEqual(manifest.tests[1]['relpath'], 1.149 + os.path.join('..', 'testsSIBLING', 'example')) 1.150 + 1.151 + def test_path_from_fd(self): 1.152 + """ 1.153 + Test paths are left untouched when manifest is a file-like object. 1.154 + """ 1.155 + fp = StringIO("[section]\npath=fleem") 1.156 + manifest = ManifestParser(manifests=(fp,)) 1.157 + self.assertEqual(manifest.tests[0]['path'], 'fleem') 1.158 + self.assertEqual(manifest.tests[0]['relpath'], 'fleem') 1.159 + self.assertEqual(manifest.tests[0]['manifest'], None) 1.160 + 1.161 + def test_comments(self): 1.162 + """ 1.163 + ensure comments work, see 1.164 + https://bugzilla.mozilla.org/show_bug.cgi?id=813674 1.165 + """ 1.166 + comment_example = os.path.join(here, 'comment-example.ini') 1.167 + manifest = ManifestParser(manifests=(comment_example,)) 1.168 + self.assertEqual(len(manifest.tests), 8) 1.169 + names = [i['name'] for i in manifest.tests] 1.170 + self.assertFalse('test_0202_app_launch_apply_update_dirlocked.js' in names) 1.171 + 1.172 + def test_verifyDirectory(self): 1.173 + 1.174 + directory = os.path.join(here, 'verifyDirectory') 1.175 + 1.176 + # correct manifest 1.177 + manifest_path = os.path.join(directory, 'verifyDirectory.ini') 1.178 + manifest = ManifestParser(manifests=(manifest_path,)) 1.179 + missing = manifest.verifyDirectory(directory, extensions=('.js',)) 1.180 + self.assertEqual(missing, (set(), set())) 1.181 + 1.182 + # manifest is missing test_1.js 1.183 + test_1 = os.path.join(directory, 'test_1.js') 1.184 + manifest_path = os.path.join(directory, 'verifyDirectory_incomplete.ini') 1.185 + manifest = ManifestParser(manifests=(manifest_path,)) 1.186 + missing = manifest.verifyDirectory(directory, extensions=('.js',)) 1.187 + self.assertEqual(missing, (set(), set([test_1]))) 1.188 + 1.189 + # filesystem is missing test_notappearinginthisfilm.js 1.190 + missing_test = os.path.join(directory, 'test_notappearinginthisfilm.js') 1.191 + manifest_path = os.path.join(directory, 'verifyDirectory_toocomplete.ini') 1.192 + manifest = ManifestParser(manifests=(manifest_path,)) 1.193 + missing = manifest.verifyDirectory(directory, extensions=('.js',)) 1.194 + self.assertEqual(missing, (set([missing_test]), set())) 1.195 + 1.196 + def test_just_defaults(self): 1.197 + """Ensure a manifest with just a DEFAULT section exposes that data.""" 1.198 + 1.199 + parser = ManifestParser() 1.200 + manifest = os.path.join(here, 'just-defaults.ini') 1.201 + parser.read(manifest) 1.202 + self.assertEqual(len(parser.tests), 0) 1.203 + self.assertTrue(manifest in parser.manifest_defaults) 1.204 + self.assertEquals(parser.manifest_defaults[manifest]['foo'], 'bar') 1.205 + 1.206 + def test_manifest_list(self): 1.207 + """ 1.208 + Ensure a manifest with just a DEFAULT section still returns 1.209 + itself from the manifests() method. 1.210 + """ 1.211 + 1.212 + parser = ManifestParser() 1.213 + manifest = os.path.join(here, 'no-tests.ini') 1.214 + parser.read(manifest) 1.215 + self.assertEqual(len(parser.tests), 0) 1.216 + self.assertTrue(len(parser.manifests()) == 1) 1.217 + 1.218 +if __name__ == '__main__': 1.219 + unittest.main()