def test_copy_input(self):
        """Ensure a copied .webidl file is handled properly."""

        # This test simulates changing the type of a WebIDL from static to
        # preprocessed. In that scenario, the original file still exists but
        # it should no longer be consulted during codegen.

        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        old_path = None
        for p in args['inputs'][0]:
            if p.endswith('Parent.webidl'):
                old_path = p
                break
        self.assertIsNotNone(old_path)

        new_path = mozpath.join(args['cache_dir'], 'Parent.webidl')
        shutil.copy2(old_path, new_path)

        args['inputs'][0].remove(old_path)
        args['inputs'][0].add(new_path)

        m2 = WebIDLCodegenManager(**args)
        result = m2.generate_build_files()
        self.assertEqual(len(result.updated), 0)
Exemple #2
0
    def test_generate_build_files_load_state(self):
        """State should be equivalent when instantiating a new instance."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        self.assertEqual(len(m1._state['webidls']), 0)
        m1.generate_build_files()

        m2 = WebIDLCodegenManager(**args)
        self.assertGreater(len(m2._state['webidls']), 2)
        self.assertEqual(m1._state, m2._state)
    def test_generate_build_files_load_state(self):
        """State should be equivalent when instantiating a new instance."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        self.assertEqual(len(m1._state['webidls']), 0)
        m1.generate_build_files()

        m2 = WebIDLCodegenManager(**args)
        self.assertGreater(len(m2._state['webidls']), 2)
        self.assertEqual(m1._state, m2._state)
    def test_no_change_no_writes(self):
        """If nothing changes, no files should be updated."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        m2 = WebIDLCodegenManager(**args)
        result = m2.generate_build_files()

        self.assertEqual(len(result.inputs), 0)
        self.assertEqual(len(result.created), 0)
        self.assertEqual(len(result.updated), 0)
    def test_python_change_regenerate_everything(self):
        """If a Python file changes, we should attempt to rebuild everything."""

        # We don't want to mutate files in the source directory because we want
        # to be able to build from a read-only filesystem. So, we install a
        # dummy module and rewrite the metadata to say it comes from the source
        # directory.
        #
        # Hacking imp to accept a MockedFile doesn't appear possible. So for
        # the first iteration we read from a temp file. The second iteration
        # doesn't need to import, so we are fine with a mocked file.
        fake_path = mozpath.join(OUR_DIR, 'fakemodule.py')
        with NamedTemporaryFile('wt') as fh:
            fh.write('# Original content')
            fh.flush()
            mod = imp.load_source('mozwebidlcodegen.fakemodule', fh.name)
            mod.__file__ = fake_path

            args = self._get_manager_args()
            m1 = WebIDLCodegenManager(**args)
            with MockedOpen({fake_path: '# Original content'}):
                old_exists = os.path.exists
                try:
                    def exists(p):
                        if p == fake_path:
                            return True
                        return old_exists(p)

                    os.path.exists = exists

                    result = m1.generate_build_files()
                    l = len(result.inputs)

                    with open(fake_path, 'wt') as fh:
                        fh.write('# Modified content')

                    m2 = WebIDLCodegenManager(**args)
                    result = m2.generate_build_files()
                    self.assertEqual(len(result.inputs), l)

                    result = m2.generate_build_files()
                    self.assertEqual(len(result.inputs), 0)
                finally:
                    os.path.exists = old_exists
                    del sys.modules['mozwebidlcodegen.fakemodule']
Exemple #6
0
    def test_copy_input(self):
        """Ensure a copied .webidl file is handled properly."""

        # This test simulates changing the type of a WebIDL from static to
        # preprocessed. In that scenario, the original file still exists but
        # it should no longer be consulted during codegen.

        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        old_path = None
        for p in args['inputs'][0]:
            if p.endswith('Parent.webidl'):
                old_path = p
                break
        self.assertIsNotNone(old_path)

        new_path = mozpath.join(args['cache_dir'], 'Parent.webidl')
        shutil.copy2(old_path, new_path)

        args['inputs'][0].remove(old_path)
        args['inputs'][0].add(new_path)

        m2 = WebIDLCodegenManager(**args)
        result = m2.generate_build_files()
        self.assertEqual(len(result.updated), 0)
    def test_output_file_regenerated(self):
        """If an output file disappears, it is regenerated."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        rm_count = 0
        for p in m1._state['webidls']['Child.webidl']['outputs']:
            rm_count += 1
            os.unlink(p)

        for p in m1.GLOBAL_DECLARE_FILES:
            rm_count += 1
            os.unlink(mozpath.join(m1._exported_header_dir, p))

        m2 = WebIDLCodegenManager(**args)
        result = m2.generate_build_files()
        self.assertEqual(len(result.created), rm_count)
    def test_only_rebuild_self(self):
        """If an input file changes, only rebuild that one file."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        child_path = None
        for p in m1._input_paths:
            if p.endswith('Child.webidl'):
                child_path = p
                break

        self.assertIsNotNone(child_path)
        child_content = open(child_path, 'rb').read()

        with MockedOpen({child_path: child_content + '\n/* */'}):
            m2 = WebIDLCodegenManager(**args)
            result = m2.generate_build_files()
            self.assertEqual(result.inputs, set([child_path]))
            self.assertEqual(len(result.updated), 0)
            self.assertEqual(len(result.created), 0)
Exemple #9
0
    def test_no_change_no_writes(self):
        """If nothing changes, no files should be updated."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        m2 = WebIDLCodegenManager(**args)
        result = m2.generate_build_files()

        self.assertEqual(len(result.inputs), 0)
        self.assertEqual(len(result.created), 0)
        self.assertEqual(len(result.updated), 0)
    def test_rebuild_dependencies(self):
        """Ensure an input file used by others results in others rebuilding."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        parent_path = None
        child_path = None
        for p in m1._input_paths:
            if p.endswith('Parent.webidl'):
                parent_path = p
            elif p.endswith('Child.webidl'):
                child_path = p

        self.assertIsNotNone(parent_path)
        parent_content = open(parent_path, 'rb').read()

        with MockedOpen({parent_path: parent_content + '\n/* */'}):
            m2 = WebIDLCodegenManager(**args)
            result = m2.generate_build_files()
            self.assertEqual(result.inputs, {child_path, parent_path})
            self.assertEqual(len(result.updated), 0)
            self.assertEqual(len(result.created), 0)
Exemple #11
0
    def test_unknown_state_version(self):
        """Loading a state file with a too new version resets state."""
        args = self._get_manager_args()

        p = args['state_path']

        with open(p, 'wb') as fh:
            json.dump({
                'version': WebIDLCodegenManagerState.VERSION + 1,
                'foobar': '1',
            }, fh)

        manager = WebIDLCodegenManager(**args)

        self.assertEqual(manager._state['version'],
                         WebIDLCodegenManagerState.VERSION)
        self.assertNotIn('foobar', manager._state)
    def test_python_change_regenerate_everything(self):
        """If a Python file changes, we should attempt to rebuild everything."""

        # We don't want to mutate files in the source directory because we want
        # to be able to build from a read-only filesystem. So, we install a
        # dummy module and rewrite the metadata to say it comes from the source
        # directory.
        #
        # Hacking imp to accept a MockedFile doesn't appear possible. So for
        # the first iteration we read from a temp file. The second iteration
        # doesn't need to import, so we are fine with a mocked file.
        fake_path = mozpath.join(OUR_DIR, 'fakemodule.py')
        with NamedTemporaryFile('wt') as fh:
            fh.write('# Original content')
            fh.flush()
            mod = imp.load_source('mozwebidlcodegen.fakemodule', fh.name)
            mod.__file__ = fake_path

            args = self._get_manager_args()
            m1 = WebIDLCodegenManager(**args)
            with MockedOpen({fake_path: '# Original content'}):
                old_exists = os.path.exists
                try:

                    def exists(p):
                        if p == fake_path:
                            return True
                        return old_exists(p)

                    os.path.exists = exists

                    result = m1.generate_build_files()
                    l = len(result.inputs)

                    with open(fake_path, 'wt') as fh:
                        fh.write('# Modified content')

                    m2 = WebIDLCodegenManager(**args)
                    result = m2.generate_build_files()
                    self.assertEqual(len(result.inputs), l)

                    result = m2.generate_build_files()
                    self.assertEqual(len(result.inputs), 0)
                finally:
                    os.path.exists = old_exists
                    del sys.modules['mozwebidlcodegen.fakemodule']
Exemple #13
0
    def test_output_file_regenerated(self):
        """If an output file disappears, it is regenerated."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        rm_count = 0
        for p in m1._state['webidls']['Child.webidl']['outputs']:
            rm_count += 1
            os.unlink(p)

        for p in m1.GLOBAL_DECLARE_FILES:
            rm_count += 1
            os.unlink(mozpath.join(m1._exported_header_dir, p))

        m2 = WebIDLCodegenManager(**args)
        result = m2.generate_build_files()
        self.assertEqual(len(result.created), rm_count)
Exemple #14
0
    def test_unknown_state_version(self):
        """Loading a state file with a too new version resets state."""
        args = self._get_manager_args()

        p = args["state_path"]

        with io.open(p, "w", newline="\n") as fh:
            json.dump(
                {
                    "version": WebIDLCodegenManagerState.VERSION + 1,
                    "foobar": "1",
                },
                fh,
            )

        manager = WebIDLCodegenManager(**args)

        self.assertEqual(manager._state["version"],
                         WebIDLCodegenManagerState.VERSION)
        self.assertNotIn("foobar", manager._state)
Exemple #15
0
    def test_only_rebuild_self(self):
        """If an input file changes, only rebuild that one file."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        child_path = None
        for p in m1._input_paths:
            if p.endswith('Child.webidl'):
                child_path = p
                break

        self.assertIsNotNone(child_path)
        child_content = open(child_path, 'rb').read()

        with MockedOpen({child_path: child_content + '\n/* */'}):
            m2 = WebIDLCodegenManager(**args)
            result = m2.generate_build_files()
            self.assertEqual(result.inputs, set([child_path]))
            self.assertEqual(len(result.updated), 0)
            self.assertEqual(len(result.created), 0)
Exemple #16
0
    def test_rebuild_dependencies(self):
        """Ensure an input file used by others results in others rebuilding."""
        args = self._get_manager_args()
        m1 = WebIDLCodegenManager(**args)
        m1.generate_build_files()

        parent_path = None
        child_path = None
        for p in m1._input_paths:
            if p.endswith('Parent.webidl'):
                parent_path = p
            elif p.endswith('Child.webidl'):
                child_path = p

        self.assertIsNotNone(parent_path)
        parent_content = open(parent_path, 'rb').read()

        with MockedOpen({parent_path: parent_content + '\n/* */'}):
            m2 = WebIDLCodegenManager(**args)
            result = m2.generate_build_files()
            self.assertEqual(result.inputs, {child_path, parent_path})
            self.assertEqual(len(result.updated), 0)
            self.assertEqual(len(result.created), 0)
Exemple #17
0
 def _get_manager(self):
     return WebIDLCodegenManager(**self._get_manager_args())