Exemple #1
0
    def test_module_caching(self, _fob, _):
        lso = local_sources_watcher.LocalSourcesWatcher(REPORT)
        lso.register_file_change_callback(NOOP_CALLBACK)

        register = MagicMock()
        lso._register_necessary_watchers = register

        # Updates modules on first run
        lso.update_watched_modules()
        register.assert_called_once()

        # Skips update when module list hasn't changed
        register.reset_mock()
        lso.update_watched_modules()
        register.assert_not_called()

        # Invalidates cache when a new module is imported
        register.reset_mock()
        sys.modules["DUMMY_MODULE_2"] = DUMMY_MODULE_2
        lso.update_watched_modules()
        register.assert_called_once()

        # Skips update when new module is part of cache
        register.reset_mock()
        lso.update_watched_modules()
        register.assert_not_called()
Exemple #2
0
    def test_script_and_2_modules_at_once(self, fob, _):
        lso = local_sources_watcher.LocalSourcesWatcher(REPORT)
        lso.register_file_change_callback(NOOP_CALLBACK)

        fob.assert_called_once()

        sys.modules["DUMMY_MODULE_1"] = DUMMY_MODULE_1
        sys.modules["DUMMY_MODULE_2"] = DUMMY_MODULE_2

        fob.reset_mock()
        lso.update_watched_modules()

        self.assertEqual(fob.call_count, 3)  # dummy modules and __init__.py

        method_type = type(self.setUp)

        call_args_list = sort_args_list(fob.call_args_list)

        args, _ = call_args_list[0]
        self.assertTrue("__init__.py" in args[0])
        args, _ = call_args_list[1]
        self.assertEqual(args[0], DUMMY_MODULE_1_FILE)
        self.assertEqual(type(args[1]), method_type)
        args, _ = call_args_list[2]
        self.assertEqual(args[0], DUMMY_MODULE_2_FILE)
        self.assertEqual(type(args[1]), method_type)

        fob.reset_mock()
        lso.update_watched_modules()

        self.assertEqual(fob.call_count, 0)
    def test_misbehaved_module(self, fob, _):
        lso = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        fob.assert_called_once()

        sys.modules["MISBEHAVED_MODULE"] = MISBEHAVED_MODULE.MisbehavedModule
        fob.reset_mock()
        lso.update_watched_modules()

        fob.assert_called_once()  # Just __init__.py
Exemple #4
0
    def test_misbehaved_module(self, fob, patched_logger, _):
        lso = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        fob.assert_called_once()

        sys.modules["MISBEHAVED_MODULE"] = MISBEHAVED_MODULE.MisbehavedModule
        fob.reset_mock()
        lso.update_watched_modules()

        fob.assert_called_once()  # Just __init__.py

        patched_logger.warning.assert_called_once_with(
            "Examining the path of MisbehavedModule raised: Oh noes!")
Exemple #5
0
    def test_just_script(self, fob, _):
        lso = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        fob.assert_called_once()
        args, _ = fob.call_args
        self.assertEqual(args[0], REPORT_PATH)
        method_type = type(self.setUp)
        self.assertEqual(type(args[1]), method_type)

        fob.reset_mock()
        lso.update_watched_modules()
        lso.update_watched_modules()
        lso.update_watched_modules()
        lso.update_watched_modules()

        self.assertEqual(fob.call_count, 1)  # __init__.py
Exemple #6
0
    def test_namespace_package_unloaded(self, fob, _):
        import tests.streamlit.watcher.test_data.namespace_package as pkg

        pkg_path = os.path.abspath(pkg.__path__._path[0])

        lsw = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        fob.assert_called_once()

        with patch("sys.modules", {"pkg": pkg}):
            lsw.update_watched_modules()

            # Simulate a change to the child module
            lsw.on_file_changed(pkg_path)

            # Assert that both the parent and child are unloaded, ready for reload
            self.assertNotIn("pkg", sys.modules)

        del sys.modules["tests.streamlit.watcher.test_data.namespace_package"]
Exemple #7
0
    def test_config_blacklist(self, fob, _):
        """Test server.folderWatchBlacklist"""
        prev_blacklist = config.get_option("server.folderWatchBlacklist")

        config.set_option("server.folderWatchBlacklist",
                          [os.path.dirname(DUMMY_MODULE_1.__file__)])

        lso = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        fob.assert_called_once()

        sys.modules["DUMMY_MODULE_1"] = DUMMY_MODULE_1
        fob.reset_mock()

        lso.update_watched_modules()

        fob.assert_not_called()

        # Reset the config object.
        config.set_option("server.folderWatchBlacklist", prev_blacklist)
Exemple #8
0
    def test_nested_module_parent_unloaded(self, fob, _):
        lso = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        fob.assert_called_once()

        with patch(
            "sys.modules",
            {
                "DUMMY_MODULE_1": DUMMY_MODULE_1,
                "NESTED_MODULE_PARENT": NESTED_MODULE_PARENT,
                "NESTED_MODULE_CHILD": NESTED_MODULE_CHILD,
            },
        ):
            lso.update_watched_modules()

            # Simulate a change to the child module
            lso.on_file_changed(NESTED_MODULE_CHILD_FILE)

            # Assert that both the parent and child are unloaded, ready for reload
            self.assertNotIn("NESTED_MODULE_CHILD", sys.modules)
            self.assertNotIn("NESTED_MODULE_PARENT", sys.modules)
Exemple #9
0
 def test_permission_error(self, fob, _):
     fob.side_effect = PermissionError("This error should be caught!")
     lso = local_sources_watcher.LocalSourcesWatcher(REPORT)
     lso.register_file_change_callback(NOOP_CALLBACK)
Exemple #10
0
 def test_does_nothing_if_NoOpFileWatcher(self, _):
     lsw = local_sources_watcher.LocalSourcesWatcher(REPORT)
     lsw.register_file_change_callback(NOOP_CALLBACK)
     lsw.update_watched_modules()
     self.assertEqual(len(lsw._watched_modules), 0)
Exemple #11
0
 def test_permission_error(self, fob, _):
     fob.side_effect = PermissionError("This error should be caught!")
     lso = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)