|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 |
|
5 from mozbuild.util import ensureParentDir |
|
6 |
|
7 from mozpack.unify import ( |
|
8 UnifiedFinder, |
|
9 UnifiedBuildFinder, |
|
10 ) |
|
11 import mozunit |
|
12 from mozpack.test.test_files import TestWithTmpDir |
|
13 from mozpack.files import FileFinder |
|
14 from mozpack.mozjar import JarWriter |
|
15 from mozpack.test.test_files import MockDest |
|
16 from cStringIO import StringIO |
|
17 import os |
|
18 import sys |
|
19 from mozpack.errors import ( |
|
20 ErrorMessage, |
|
21 AccumulatedErrors, |
|
22 errors, |
|
23 ) |
|
24 |
|
25 |
|
26 class TestUnified(TestWithTmpDir): |
|
27 def create_one(self, which, path, content): |
|
28 file = self.tmppath(os.path.join(which, path)) |
|
29 ensureParentDir(file) |
|
30 open(file, 'wb').write(content) |
|
31 |
|
32 def create_both(self, path, content): |
|
33 for p in ['a', 'b']: |
|
34 self.create_one(p, path, content) |
|
35 |
|
36 |
|
37 class TestUnifiedFinder(TestUnified): |
|
38 def test_unified_finder(self): |
|
39 self.create_both('foo/bar', 'foobar') |
|
40 self.create_both('foo/baz', 'foobaz') |
|
41 self.create_one('a', 'bar', 'bar') |
|
42 self.create_one('b', 'baz', 'baz') |
|
43 self.create_one('a', 'qux', 'foobar') |
|
44 self.create_one('b', 'qux', 'baz') |
|
45 self.create_one('a', 'test/foo', 'a\nb\nc\n') |
|
46 self.create_one('b', 'test/foo', 'b\nc\na\n') |
|
47 self.create_both('test/bar', 'a\nb\nc\n') |
|
48 |
|
49 finder = UnifiedFinder(FileFinder(self.tmppath('a')), |
|
50 FileFinder(self.tmppath('b')), |
|
51 sorted=['test']) |
|
52 self.assertEqual(sorted([(f, c.open().read()) |
|
53 for f, c in finder.find('foo')]), |
|
54 [('foo/bar', 'foobar'), ('foo/baz', 'foobaz')]) |
|
55 self.assertRaises(ErrorMessage, any, finder.find('bar')) |
|
56 self.assertRaises(ErrorMessage, any, finder.find('baz')) |
|
57 self.assertRaises(ErrorMessage, any, finder.find('qux')) |
|
58 self.assertEqual(sorted([(f, c.open().read()) |
|
59 for f, c in finder.find('test')]), |
|
60 [('test/bar', 'a\nb\nc\n'), |
|
61 ('test/foo', 'a\nb\nc\n')]) |
|
62 |
|
63 |
|
64 class TestUnifiedBuildFinder(TestUnified): |
|
65 def test_unified_build_finder(self): |
|
66 self.create_both('chrome.manifest', 'a\nb\nc\n') |
|
67 self.create_one('a', 'chrome/chrome.manifest', 'a\nb\nc\n') |
|
68 self.create_one('b', 'chrome/chrome.manifest', 'b\nc\na\n') |
|
69 self.create_one('a', 'chrome/browser/foo/buildconfig.html', |
|
70 '\n'.join([ |
|
71 '<html>', |
|
72 '<body>', |
|
73 '<h1>about:buildconfig</h1>', |
|
74 '<div>foo</div>', |
|
75 '</body>', |
|
76 '</html>', |
|
77 ])) |
|
78 self.create_one('b', 'chrome/browser/foo/buildconfig.html', |
|
79 '\n'.join([ |
|
80 '<html>', |
|
81 '<body>', |
|
82 '<h1>about:buildconfig</h1>', |
|
83 '<div>bar</div>', |
|
84 '</body>', |
|
85 '</html>', |
|
86 ])) |
|
87 finder = UnifiedBuildFinder(FileFinder(self.tmppath('a')), |
|
88 FileFinder(self.tmppath('b'))) |
|
89 self.assertEqual(sorted([(f, c.open().read()) for f, c in |
|
90 finder.find('**/chrome.manifest')]), |
|
91 [('chrome.manifest', 'a\nb\nc\n'), |
|
92 ('chrome/chrome.manifest', 'a\nb\nc\n')]) |
|
93 |
|
94 self.assertEqual(sorted([(f, c.open().read()) for f, c in |
|
95 finder.find('**/buildconfig.html')]), |
|
96 [('chrome/browser/foo/buildconfig.html', '\n'.join([ |
|
97 '<html>', |
|
98 '<body>', |
|
99 '<h1>about:buildconfig</h1>', |
|
100 '<div>foo</div>', |
|
101 '<hr> </hr>', |
|
102 '<div>bar</div>', |
|
103 '</body>', |
|
104 '</html>', |
|
105 ]))]) |
|
106 |
|
107 xpi = MockDest() |
|
108 with JarWriter(fileobj=xpi, compress=True) as jar: |
|
109 jar.add('foo', 'foo') |
|
110 jar.add('bar', 'bar') |
|
111 foo_xpi = xpi.read() |
|
112 self.create_both('foo.xpi', foo_xpi) |
|
113 |
|
114 with JarWriter(fileobj=xpi, compress=True) as jar: |
|
115 jar.add('foo', 'bar') |
|
116 self.create_one('a', 'bar.xpi', foo_xpi) |
|
117 self.create_one('b', 'bar.xpi', xpi.read()) |
|
118 |
|
119 errors.out = StringIO() |
|
120 with self.assertRaises(AccumulatedErrors), errors.accumulate(): |
|
121 self.assertEqual([(f, c.open().read()) for f, c in |
|
122 finder.find('*.xpi')], |
|
123 [('foo.xpi', foo_xpi)]) |
|
124 errors.out = sys.stderr |
|
125 |
|
126 |
|
127 if __name__ == '__main__': |
|
128 mozunit.main() |