From 77babc8727fdc0d4e64df8753222691106a7acab Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 22:17:17 +0200 Subject: Merging upstream version 0.1.26. Signed-off-by: Daniel Baumann --- tests/lsp_tests/conftest.py | 12 +- tests/lsp_tests/lsp_tutil.py | 6 +- tests/lsp_tests/test_lsp_dctrl.py | 2 +- .../test_lsp_debputy_manifest_completer.py | 598 +++++++++++++++++++++ 4 files changed, 614 insertions(+), 4 deletions(-) create mode 100644 tests/lsp_tests/test_lsp_debputy_manifest_completer.py (limited to 'tests') diff --git a/tests/lsp_tests/conftest.py b/tests/lsp_tests/conftest.py index 8b42582..2aff4a2 100644 --- a/tests/lsp_tests/conftest.py +++ b/tests/lsp_tests/conftest.py @@ -1,5 +1,7 @@ import pytest + from debputy.plugin.api.feature_set import PluginProvidedFeatureSet +from debputy.util import setup_logging try: from pygls.server import LanguageServer @@ -22,8 +24,16 @@ except ImportError: HAS_PYGLS = False +@pytest.fixture(scope="session") +def enable_logging() -> None: + setup_logging(log_only_to_stderr=True, reconfigure_logging=True) + + @pytest.fixture() -def ls(debputy_plugin_feature_set: PluginProvidedFeatureSet) -> "LanguageServer": +def ls( + enable_logging, + debputy_plugin_feature_set: PluginProvidedFeatureSet, +) -> "LanguageServer": if not HAS_PYGLS: pytest.skip("Missing pygls") ls = LanguageServer("debputy", "v") diff --git a/tests/lsp_tests/lsp_tutil.py b/tests/lsp_tests/lsp_tutil.py index 1e509af..2873f72 100644 --- a/tests/lsp_tests/lsp_tutil.py +++ b/tests/lsp_tests/lsp_tutil.py @@ -30,10 +30,12 @@ def put_doc_with_cursor( uri: str, language_id: str, content: str, - *, - doc_version: int = 1, ) -> "Position": cleaned_content, cursor_pos = _locate_cursor(content) + doc_version = 1 + existing = ls.workspace.text_documents.get(uri) + if existing is not None: + doc_version = existing.version + 1 ls.workspace.put_text_document( TextDocumentItem( uri, diff --git a/tests/lsp_tests/test_lsp_dctrl.py b/tests/lsp_tests/test_lsp_dctrl.py index 2a2466f..d258e8f 100644 --- a/tests/lsp_tests/test_lsp_dctrl.py +++ b/tests/lsp_tests/test_lsp_dctrl.py @@ -39,7 +39,7 @@ def test_dctrl_complete_field(ls: "LanguageServer") -> None: ls, CompletionParams(TextDocumentIdentifier(dctrl_uri), cursor_pos), ) - assert matches + assert isinstance(matches, list) keywords = {m.label for m in matches} assert "Multi-Arch" in keywords assert "Architecture" in keywords diff --git a/tests/lsp_tests/test_lsp_debputy_manifest_completer.py b/tests/lsp_tests/test_lsp_debputy_manifest_completer.py new file mode 100644 index 0000000..c72e791 --- /dev/null +++ b/tests/lsp_tests/test_lsp_debputy_manifest_completer.py @@ -0,0 +1,598 @@ +import textwrap + +import pytest + +from lsp_tests.lsp_tutil import put_doc_with_cursor + +try: + from pygls.server import LanguageServer + from lsprotocol.types import ( + InitializeParams, + ClientCapabilities, + GeneralClientCapabilities, + PositionEncodingKind, + TextDocumentItem, + Position, + CompletionParams, + TextDocumentIdentifier, + HoverParams, + MarkupContent, + ) + from debputy.lsp.lsp_debian_debputy_manifest import debputy_manifest_completer + + HAS_PYGLS = True +except ImportError: + HAS_PYGLS = False + + +def test_basic_debputy_completer_empty(ls: "LanguageServer") -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "definitions:" in keywords + assert "manifest-version:" in keywords + assert "installations:" in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manif +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "manifest-version:" in keywords + assert "packages:" in keywords + # We rely on client side filtering + assert "installations:" in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "definitions:" in keywords + assert "installations:" in keywords + assert "packages:" in keywords + # Already completed + assert "manifest-version:" not in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install-docs: + sources: + - foo + - bar + +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "definitions:" in keywords + assert "packages:" in keywords + # Already completed + assert "manifest-version:" not in keywords + assert "installations:" not in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install-docs: + sources: + - foo + - bar + packa +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "definitions:" in keywords + assert "packages:" in keywords + # Already completed + assert "manifest-version:" not in keywords + assert "installations:" not in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install-docs: + sources: + - foo + - bar + packages: + foo: + services: + - service: foo + service-scope: user + +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "definitions:" in keywords + # Already completed + assert "manifest-version:" not in keywords + assert "installations:" not in keywords + assert "packages:" not in keywords + + +def test_basic_debputy_completer_manifest_variable_value(ls: "LanguageServer") -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "0.1" in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0. +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "0.1" in keywords + + +def test_basic_debputy_completer_install_rule_dispatch_key( + ls: "LanguageServer", +) -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "install:" in keywords + assert "install-doc:" in keywords + assert "install-docs:" in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - i +""" + ), + ) + + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "install:" in keywords + assert "install-doc:" in keywords + assert "install-docs:" in keywords + + +def test_basic_debputy_completer_install_rule_install_keys( + ls: "LanguageServer", +) -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install: + +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "source:" in keywords + assert "sources:" in keywords + assert "as:" in keywords + assert "dest-dir:" in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install: + sources: + - foo + - bar + +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "dest-dir:" in keywords + # Already completed + assert "sources:" not in keywords + + # Not possible (conflict) + assert "source:" not in keywords + assert "as:" not in keywords + + +def test_basic_debputy_completer_packages_foo( + ls: "LanguageServer", +) -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + packages: + foo: + +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "binary-version:" in keywords + assert "services:" in keywords + assert "transformations:" in keywords + + +def test_basic_debputy_completer_packages_foo_xfail( + ls: "LanguageServer", +) -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + packages: + foo: + bin +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "binary-version:" in keywords + assert "services:" in keywords + assert "transformations:" in keywords + + +def test_basic_debputy_completer_services_service_scope_values( + ls: "LanguageServer", +) -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + packages: + foo: + services: + - service: foo + service-scope: +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert keywords == {"system", "user"} + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + packages: + foo: + services: + - service: foo + service-scope: s +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert keywords == {"system", "user"} + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + packages: + foo: + services: + - service: foo + service-scope: system + enable-on-install: +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert keywords == {"true", "false"} + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + packages: + foo: + services: + - service: foo + service-scope: system + enable-on-install: tr +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + # "false" is ok, because we rely on client side filtering + assert keywords == {"true", "false"} + + +def test_basic_debputy_completer_manifest_conditions( + ls: "LanguageServer", +) -> None: + debputy_manifest_uri = "file:///nowhere/debian/debputy.manifest" + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install-docs: + when: +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "cross-compiling" in keywords + # Mapping-only forms are not applicable here + assert "not" not in keywords + assert "not:" not in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install-docs: + when: c +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "cross-compiling" in keywords + # Mapping-only forms are not applicable here + assert "not" not in keywords + assert "not:" not in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install-docs: + when: + +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "not:" in keywords + # str-only forms are not applicable here + assert "cross-compiling" not in keywords + + cursor_pos = put_doc_with_cursor( + ls, + debputy_manifest_uri, + "debian/debputy.manifest", + textwrap.dedent( + """\ + manifest-version: 0.1 + installations: + - install-docs: + when: + n +""" + ), + ) + completions = debputy_manifest_completer( + ls, + CompletionParams(TextDocumentIdentifier(debputy_manifest_uri), cursor_pos), + ) + assert isinstance(completions, list) + keywords = {m.label for m in completions} + assert "not:" in keywords + # str-only forms are not applicable here + assert "cross-compiling" not in keywords -- cgit v1.2.3