1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/mozbuild/mozpack/test/test_unify.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,128 @@ 1.4 +# This Source Code Form is subject to the terms of the Mozilla Public 1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + 1.8 +from mozbuild.util import ensureParentDir 1.9 + 1.10 +from mozpack.unify import ( 1.11 + UnifiedFinder, 1.12 + UnifiedBuildFinder, 1.13 +) 1.14 +import mozunit 1.15 +from mozpack.test.test_files import TestWithTmpDir 1.16 +from mozpack.files import FileFinder 1.17 +from mozpack.mozjar import JarWriter 1.18 +from mozpack.test.test_files import MockDest 1.19 +from cStringIO import StringIO 1.20 +import os 1.21 +import sys 1.22 +from mozpack.errors import ( 1.23 + ErrorMessage, 1.24 + AccumulatedErrors, 1.25 + errors, 1.26 +) 1.27 + 1.28 + 1.29 +class TestUnified(TestWithTmpDir): 1.30 + def create_one(self, which, path, content): 1.31 + file = self.tmppath(os.path.join(which, path)) 1.32 + ensureParentDir(file) 1.33 + open(file, 'wb').write(content) 1.34 + 1.35 + def create_both(self, path, content): 1.36 + for p in ['a', 'b']: 1.37 + self.create_one(p, path, content) 1.38 + 1.39 + 1.40 +class TestUnifiedFinder(TestUnified): 1.41 + def test_unified_finder(self): 1.42 + self.create_both('foo/bar', 'foobar') 1.43 + self.create_both('foo/baz', 'foobaz') 1.44 + self.create_one('a', 'bar', 'bar') 1.45 + self.create_one('b', 'baz', 'baz') 1.46 + self.create_one('a', 'qux', 'foobar') 1.47 + self.create_one('b', 'qux', 'baz') 1.48 + self.create_one('a', 'test/foo', 'a\nb\nc\n') 1.49 + self.create_one('b', 'test/foo', 'b\nc\na\n') 1.50 + self.create_both('test/bar', 'a\nb\nc\n') 1.51 + 1.52 + finder = UnifiedFinder(FileFinder(self.tmppath('a')), 1.53 + FileFinder(self.tmppath('b')), 1.54 + sorted=['test']) 1.55 + self.assertEqual(sorted([(f, c.open().read()) 1.56 + for f, c in finder.find('foo')]), 1.57 + [('foo/bar', 'foobar'), ('foo/baz', 'foobaz')]) 1.58 + self.assertRaises(ErrorMessage, any, finder.find('bar')) 1.59 + self.assertRaises(ErrorMessage, any, finder.find('baz')) 1.60 + self.assertRaises(ErrorMessage, any, finder.find('qux')) 1.61 + self.assertEqual(sorted([(f, c.open().read()) 1.62 + for f, c in finder.find('test')]), 1.63 + [('test/bar', 'a\nb\nc\n'), 1.64 + ('test/foo', 'a\nb\nc\n')]) 1.65 + 1.66 + 1.67 +class TestUnifiedBuildFinder(TestUnified): 1.68 + def test_unified_build_finder(self): 1.69 + self.create_both('chrome.manifest', 'a\nb\nc\n') 1.70 + self.create_one('a', 'chrome/chrome.manifest', 'a\nb\nc\n') 1.71 + self.create_one('b', 'chrome/chrome.manifest', 'b\nc\na\n') 1.72 + self.create_one('a', 'chrome/browser/foo/buildconfig.html', 1.73 + '\n'.join([ 1.74 + '<html>', 1.75 + '<body>', 1.76 + '<h1>about:buildconfig</h1>', 1.77 + '<div>foo</div>', 1.78 + '</body>', 1.79 + '</html>', 1.80 + ])) 1.81 + self.create_one('b', 'chrome/browser/foo/buildconfig.html', 1.82 + '\n'.join([ 1.83 + '<html>', 1.84 + '<body>', 1.85 + '<h1>about:buildconfig</h1>', 1.86 + '<div>bar</div>', 1.87 + '</body>', 1.88 + '</html>', 1.89 + ])) 1.90 + finder = UnifiedBuildFinder(FileFinder(self.tmppath('a')), 1.91 + FileFinder(self.tmppath('b'))) 1.92 + self.assertEqual(sorted([(f, c.open().read()) for f, c in 1.93 + finder.find('**/chrome.manifest')]), 1.94 + [('chrome.manifest', 'a\nb\nc\n'), 1.95 + ('chrome/chrome.manifest', 'a\nb\nc\n')]) 1.96 + 1.97 + self.assertEqual(sorted([(f, c.open().read()) for f, c in 1.98 + finder.find('**/buildconfig.html')]), 1.99 + [('chrome/browser/foo/buildconfig.html', '\n'.join([ 1.100 + '<html>', 1.101 + '<body>', 1.102 + '<h1>about:buildconfig</h1>', 1.103 + '<div>foo</div>', 1.104 + '<hr> </hr>', 1.105 + '<div>bar</div>', 1.106 + '</body>', 1.107 + '</html>', 1.108 + ]))]) 1.109 + 1.110 + xpi = MockDest() 1.111 + with JarWriter(fileobj=xpi, compress=True) as jar: 1.112 + jar.add('foo', 'foo') 1.113 + jar.add('bar', 'bar') 1.114 + foo_xpi = xpi.read() 1.115 + self.create_both('foo.xpi', foo_xpi) 1.116 + 1.117 + with JarWriter(fileobj=xpi, compress=True) as jar: 1.118 + jar.add('foo', 'bar') 1.119 + self.create_one('a', 'bar.xpi', foo_xpi) 1.120 + self.create_one('b', 'bar.xpi', xpi.read()) 1.121 + 1.122 + errors.out = StringIO() 1.123 + with self.assertRaises(AccumulatedErrors), errors.accumulate(): 1.124 + self.assertEqual([(f, c.open().read()) for f, c in 1.125 + finder.find('*.xpi')], 1.126 + [('foo.xpi', foo_xpi)]) 1.127 + errors.out = sys.stderr 1.128 + 1.129 + 1.130 +if __name__ == '__main__': 1.131 + mozunit.main()