def test_exit_with_parent_process_died( language_server_process: IRobocorpLanguageServerClient, language_server_io, ws_root_path, initialization_options, ): """ :note: Only check with the language_server_io (because that's in another process). """ from robocorp_ls_core.subprocess_wrapper import subprocess from robocorp_ls_core.basic import is_process_alive from robocorp_ls_core.basic import kill_process_and_subprocesses from robocorp_ls_core.unittest_tools.fixtures import wait_for_test_condition language_server = language_server_io dummy_process = subprocess.Popen( [sys.executable, "-c", "import time;time.sleep(10000)"]) language_server.initialize( ws_root_path, process_id=dummy_process.pid, initialization_options=initialization_options, ) assert is_process_alive(dummy_process.pid) assert is_process_alive(language_server_process.pid) kill_process_and_subprocesses(dummy_process.pid) wait_for_test_condition(lambda: not is_process_alive(dummy_process.pid)) wait_for_test_condition( lambda: not is_process_alive(language_server_process.pid)) language_server_io.require_exit_messages = False
def test_remote_fs_observer(remote_fs_observer, tmpdir): from robocorp_ls_core import watchdog_wrapper from robocorp_ls_core.watchdog_wrapper import PathInfo from robocorp_ls_core.unittest_tools.fixtures import wait_for_test_condition from robocorp_ls_core.watchdog_wrapper import IFSObserver tmpdir.join("dir_not_rec").mkdir() tmpdir.join("dir_rec").mkdir() found = [] def on_change(filepath, *args): found.append(filepath) assert args == ("foo", "bar") notifier = watchdog_wrapper.create_notifier(on_change, timeout=0.1) observer: IFSObserver = remote_fs_observer watch = observer.notify_on_any_change( [ PathInfo(tmpdir.join("dir_not_rec"), False), PathInfo(tmpdir.join("dir_rec"), True), ], notifier.on_change, call_args=("foo", "bar"), ) try: tmpdir.join("dir_not_rec").join("mya.txt").write("foo") tmpdir.join("dir_not_rec").join("mya.libspec").write("foo") tmpdir.join("dir_rec").join("myb.txt").write("foo") tmpdir.join("dir_rec").join("myb.libspec").write("foo") def collect_basenames(): import os.path basenames = [os.path.basename(x) for x in found] return set(basenames) def check1(): expected = {"myb.txt", "mya.libspec", "myb.libspec", "mya.txt"} return collect_basenames().issuperset(expected) wait_for_test_condition( check1, msg=lambda: f"Basenames found: {collect_basenames()}" ) finally: watch.stop_tracking() notifier.dispose() observer.dispose()
def test_libspec_manager_caches(libspec_manager, workspace_dir): from robocorp_ls_core import uris import os.path from robotframework_ls_tests.fixtures import LIBSPEC_1 from robotframework_ls_tests.fixtures import LIBSPEC_2 from robotframework_ls_tests.fixtures import LIBSPEC_2_A import time from robocorp_ls_core.unittest_tools.fixtures import wait_for_test_condition workspace_dir_a = os.path.join(workspace_dir, "workspace_dir_a") os.makedirs(workspace_dir_a) with open(os.path.join(workspace_dir_a, "my.libspec"), "w") as stream: stream.write(LIBSPEC_1) libspec_manager.add_workspace_folder(uris.from_fs_path(workspace_dir_a)) assert libspec_manager.get_library_info("case1_library", create=False) is not None libspec_manager.remove_workspace_folder(uris.from_fs_path(workspace_dir_a)) library_info = libspec_manager.get_library_info("case1_library", create=False) if library_info is not None: raise AssertionError( "Expected: %s to be None after removing %s" % (library_info, uris.from_fs_path(workspace_dir_a))) libspec_manager.add_workspace_folder(uris.from_fs_path(workspace_dir_a)) assert libspec_manager.get_library_info("case1_library", create=False) is not None # Give a timeout so that the next write will have at least 1 second # difference (1s is the minimum for poll to work). time.sleep(1.1) with open(os.path.join(workspace_dir_a, "my2.libspec"), "w") as stream: stream.write(LIBSPEC_2) def check_spec_found(): library_info = libspec_manager.get_library_info("case2_library", create=False) return library_info is not None # Updating is done in a thread. wait_for_test_condition(check_spec_found, sleep=1 / 5.0) library_info = libspec_manager.get_library_info("case2_library", create=False) assert set(x.name for x in library_info.keywords) == set( ["Case 2 Verify Another Model", "Case 2 Verify Model"]) # Give a timeout so that the next write will have at least 1 second # difference (1s is the minimum for poll to work). time.sleep(1) with open(os.path.join(workspace_dir_a, "my2.libspec"), "w") as stream: stream.write(LIBSPEC_2_A) def check_spec_2_a(): library_info = libspec_manager.get_library_info("case2_library", create=False) if library_info: return set(x.name for x in library_info.keywords) == set( ["Case 2 A Verify Another Model", "Case 2 A Verify Model"]) # Updating is done in a thread. wait_for_test_condition(check_spec_2_a, sleep=1 / 5.0)
def test_watchdog_extensions(tmpdir): from robocorp_ls_core import watchdog_wrapper from robocorp_ls_core.watchdog_wrapper import PathInfo from robocorp_ls_core.unittest_tools.fixtures import wait_for_test_condition tmpdir.join("dir_not_rec").mkdir() tmpdir.join("dir_rec").mkdir() found = [] def on_change(filepath, *args): found.append(filepath) assert args == ("foo", "bar") notifier = watchdog_wrapper.create_notifier(on_change, timeout=0.1) observer = watchdog_wrapper.create_observer() watch = observer.notify_on_extensions_change( [ PathInfo(tmpdir.join("dir_not_rec"), False), PathInfo(tmpdir.join("dir_rec"), True), ], ["libspec"], notifier.on_change, call_args=("foo", "bar"), ) try: tmpdir.join("my.txt").write("foo") tmpdir.join("my.libspec").write("foo") tmpdir.join("dir_not_rec").join("mya.txt").write("foo") tmpdir.join("dir_not_rec").join("mya.libspec").write("foo") tmpdir.join("dir_rec").join("myb.txt").write("foo") tmpdir.join("dir_rec").join("myb.libspec").write("foo") def check1(): found_my_a = False found_my_b = False for filepath in found: if not filepath.endswith(".libspec"): raise AssertionError( "Expected only libspec files to be tracked.") if filepath.endswith("my.libspec"): raise AssertionError("Wrong folder tracked.") found_my_a = found_my_a or "mya.libspec" in filepath found_my_b = found_my_b or "myb.libspec" in filepath return found_my_a and found_my_b wait_for_test_condition( check1, msg=lambda: "Expected to find mya.libspec and myb.libspec. Found:\n%s" % ("\n".join(found), ), ) # not listened tmpdir.join("dir_not_rec").join("another").mkdir() tmpdir.join("dir_not_rec").join("another").join("myc.txt").write("foo") tmpdir.join("dir_not_rec").join("another").join("myc.libspec").write( "foo") # listened tmpdir.join("dir_rec").join("another").mkdir() tmpdir.join("dir_rec").join("another").join("myd.txt").write("foo") tmpdir.join("dir_rec").join("another").join("myd.libspec").write("foo") del found[:] def check2(): found_my_d = False for filepath in found: if not filepath.endswith(".libspec"): raise AssertionError( "Expected only libspec files to be tracked.") if filepath.endswith("myc.libspec"): raise AssertionError("Wrong folder tracked.") found_my_d = found_my_d or "myd.libspec" in filepath return found_my_d wait_for_test_condition( check2, msg=lambda: "Expected to find myd.libspec. Found:\n%s" % ("\n".join(found), ), ) watch.stop_tracking() del found[:] tmpdir.join("dir_rec").join("mye.txt").write("foo") tmpdir.join("dir_rec").join("mye.libspec").write("foo") # Give time to check if some change arrives. time.sleep(1) assert not found finally: notifier.dispose() observer.dispose()