def test_exit_with_parent_process_died( language_server_process: IRobocodeLanguageServerClient, language_server_io, ws_root_path, ): """ :note: Only check with the language_server_io (because that's in another process). """ from robocode_ls_core.subprocess_wrapper import subprocess from robocode_ls_core.basic import is_process_alive from robocode_ls_core.basic import kill_process_and_subprocesses from robocode_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) 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_log(tmpdir): from robocode_ls_core.robotframework_log import get_logger, configure_logger from robocode_ls_core.unittest_tools.fixtures import wait_for_test_condition somedir = str(tmpdir.join("somedir")) configure_logger("test", 2, os.path.join(somedir, "foo.log")) log = get_logger("my_logger") log.info("something\nfoo\nbar") try: raise AssertionError("someerror") except: log.exception("rara: %s - %s", "str1", "str2") def get_log_files(): log_files = [ x for x in os.listdir(somedir) if x.startswith("foo") and x.endswith(".log") ] return log_files if log_files else None wait_for_test_condition( get_log_files, msg=lambda: "Found: %s in %s" % (get_log_files(), somedir) ) log_files = get_log_files() with open(os.path.join(somedir, log_files[0]), "r") as stream: contents = stream.read() assert "someerror" in contents assert "something" in contents assert "rara" in contents assert "rara: str1 - str2" in contents
def test_libspec_manager_caches(libspec_manager, workspace_dir): from robocode_ls_core import uris from os.path import os 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 robocode_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(tmpdir): from robocode_ls_core import watchdog_wrapper from robocode_ls_core.watchdog_wrapper import PathInfo from robocode_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()