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