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