testing/mozbase/manifestdestiny/tests/test_convert_directory.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/manifestdestiny/tests/test_convert_directory.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     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 +
    1.15 +from manifestparser import convert
    1.16 +from manifestparser import ManifestParser
    1.17 +from StringIO import StringIO
    1.18 +
    1.19 +here = os.path.dirname(os.path.abspath(__file__))
    1.20 +
    1.21 +class TestDirectoryConversion(unittest.TestCase):
    1.22 +    """test conversion of a directory tree to a manifest structure"""
    1.23 +
    1.24 +    def create_stub(self, directory=None):
    1.25 +        """stub out a directory with files in it"""
    1.26 +
    1.27 +        files = ('foo', 'bar', 'fleem')
    1.28 +        if directory is None:
    1.29 +            directory = tempfile.mkdtemp()
    1.30 +        for i in files:
    1.31 +            file(os.path.join(directory, i), 'w').write(i)
    1.32 +        subdir = os.path.join(directory, 'subdir')
    1.33 +        os.mkdir(subdir)
    1.34 +        file(os.path.join(subdir, 'subfile'), 'w').write('baz')
    1.35 +        return directory
    1.36 +
    1.37 +    def test_directory_to_manifest(self):
    1.38 +        """
    1.39 +        Test our ability to convert a static directory structure to a
    1.40 +        manifest.
    1.41 +        """
    1.42 +
    1.43 +        # create a stub directory
    1.44 +        stub = self.create_stub()
    1.45 +        try:
    1.46 +            stub = stub.replace(os.path.sep, "/")
    1.47 +            self.assertTrue(os.path.exists(stub) and os.path.isdir(stub))
    1.48 +
    1.49 +            # Make a manifest for it
    1.50 +            manifest = convert([stub])
    1.51 +            self.assertEqual(str(manifest),
    1.52 +"""[%(stub)s/bar]
    1.53 +subsuite = 
    1.54 +
    1.55 +[%(stub)s/fleem]
    1.56 +subsuite = 
    1.57 +
    1.58 +[%(stub)s/foo]
    1.59 +subsuite = 
    1.60 +
    1.61 +[%(stub)s/subdir/subfile]
    1.62 +subsuite = 
    1.63 +
    1.64 +""" % dict(stub=stub))
    1.65 +        except:
    1.66 +            raise
    1.67 +        finally:
    1.68 +            shutil.rmtree(stub) # cleanup
    1.69 +
    1.70 +    def test_convert_directory_manifests_in_place(self):
    1.71 +        """
    1.72 +        keep the manifests in place
    1.73 +        """
    1.74 +
    1.75 +        stub = self.create_stub()
    1.76 +        try:
    1.77 +            ManifestParser.populate_directory_manifests([stub], filename='manifest.ini')
    1.78 +            self.assertEqual(sorted(os.listdir(stub)),
    1.79 +                             ['bar', 'fleem', 'foo', 'manifest.ini', 'subdir'])
    1.80 +            parser = ManifestParser()
    1.81 +            parser.read(os.path.join(stub, 'manifest.ini'))
    1.82 +            self.assertEqual([i['name'] for i in parser.tests],
    1.83 +                             ['subfile', 'bar', 'fleem', 'foo'])
    1.84 +            parser = ManifestParser()
    1.85 +            parser.read(os.path.join(stub, 'subdir', 'manifest.ini'))
    1.86 +            self.assertEqual(len(parser.tests), 1)
    1.87 +            self.assertEqual(parser.tests[0]['name'], 'subfile')
    1.88 +        except:
    1.89 +            raise
    1.90 +        finally:
    1.91 +            shutil.rmtree(stub)
    1.92 +
    1.93 +    def test_manifest_ignore(self):
    1.94 +        """test manifest `ignore` parameter for ignoring directories"""
    1.95 +
    1.96 +        stub = self.create_stub()
    1.97 +        try:
    1.98 +            ManifestParser.populate_directory_manifests([stub], filename='manifest.ini', ignore=('subdir',))
    1.99 +            parser = ManifestParser()
   1.100 +            parser.read(os.path.join(stub, 'manifest.ini'))
   1.101 +            self.assertEqual([i['name'] for i in parser.tests],
   1.102 +                             ['bar', 'fleem', 'foo'])
   1.103 +            self.assertFalse(os.path.exists(os.path.join(stub, 'subdir', 'manifest.ini')))
   1.104 +        except:
   1.105 +            raise
   1.106 +        finally:
   1.107 +            shutil.rmtree(stub)
   1.108 +
   1.109 +    def test_pattern(self):
   1.110 +        """test directory -> manifest with a file pattern"""
   1.111 +
   1.112 +        stub = self.create_stub()
   1.113 +        try:
   1.114 +            parser = convert([stub], pattern='f*', relative_to=stub)
   1.115 +            self.assertEqual([i['name'] for i in parser.tests],
   1.116 +                             ['fleem', 'foo'])
   1.117 +
   1.118 +            # test multiple patterns
   1.119 +            parser = convert([stub], pattern=('f*', 's*'), relative_to=stub)
   1.120 +            self.assertEqual([i['name'] for i in parser.tests],
   1.121 +                             ['fleem', 'foo', 'subdir/subfile'])
   1.122 +        except:
   1.123 +            raise
   1.124 +        finally:
   1.125 +            shutil.rmtree(stub)
   1.126 +
   1.127 +    def test_update(self):
   1.128 +        """
   1.129 +        Test our ability to update tests from a manifest and a directory of
   1.130 +        files
   1.131 +        """
   1.132 +
   1.133 +        # boilerplate
   1.134 +        tempdir = tempfile.mkdtemp()
   1.135 +        for i in range(10):
   1.136 +            file(os.path.join(tempdir, str(i)), 'w').write(str(i))
   1.137 +
   1.138 +        # otherwise empty directory with a manifest file
   1.139 +        newtempdir = tempfile.mkdtemp()
   1.140 +        manifest_file = os.path.join(newtempdir, 'manifest.ini')
   1.141 +        manifest_contents = str(convert([tempdir], relative_to=tempdir))
   1.142 +        with file(manifest_file, 'w') as f:
   1.143 +            f.write(manifest_contents)
   1.144 +
   1.145 +        # get the manifest
   1.146 +        manifest = ManifestParser(manifests=(manifest_file,))
   1.147 +
   1.148 +        # All of the tests are initially missing:
   1.149 +        paths = [str(i) for i in range(10)]
   1.150 +        self.assertEqual([i['name'] for i in manifest.missing()],
   1.151 +                         paths)
   1.152 +
   1.153 +        # But then we copy one over:
   1.154 +        self.assertEqual(manifest.get('name', name='1'), ['1'])
   1.155 +        manifest.update(tempdir, name='1')
   1.156 +        self.assertEqual(sorted(os.listdir(newtempdir)),
   1.157 +                        ['1', 'manifest.ini'])
   1.158 +
   1.159 +        # Update that one file and copy all the "tests":
   1.160 +        file(os.path.join(tempdir, '1'), 'w').write('secret door')
   1.161 +        manifest.update(tempdir)
   1.162 +        self.assertEqual(sorted(os.listdir(newtempdir)),
   1.163 +                        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'manifest.ini'])
   1.164 +        self.assertEqual(file(os.path.join(newtempdir, '1')).read().strip(),
   1.165 +                        'secret door')
   1.166 +
   1.167 +        # clean up:
   1.168 +        shutil.rmtree(tempdir)
   1.169 +        shutil.rmtree(newtempdir)
   1.170 +
   1.171 +
   1.172 +if __name__ == '__main__':
   1.173 +    unittest.main()

mercurial