def test_interpolate_variables_multiple(self) -> None: variables = {"foo": "bar", "baz": "qux"} payload = { "hello": "${foo} ${baz}", "nested": { "foo": "${foo}" }, "in key: ${baz}": True, } expected = { "hello": "bar qux", "nested": { "foo": "bar" }, "in key: qux": True } self.assertEqual(interpolate_variables(payload, variables), expected) self.assertEqual(uninterpolate_variables(expected, variables), payload)
def _describe_response_for_remediation( self, variables: VariableMap, request: "_RequestSpec", # pyre-fixme[11]: Annotation `Json` is not defined as a type. actual_response: Json, ) -> str: method = request.method params = request.params result = uninterpolate_variables(payload=actual_response.get("result"), variables=variables) powered_by = actual_response.get("powered_by") request_snippet = f"""\ .request( line=line(),""" if request.comment is not None: request_snippet += f""" comment={request.comment!r},""" request_snippet += f""" method={method!r}, params={params!r}, result={result!r},""" if request.wait_id is not None: request_snippet += f""" wait_id={request.wait_id!r},""" if powered_by is not None: request_snippet += f""" powered_by={powered_by!r},""" request_snippet += f""" )""" remediation = f"""\ 1) If this was unexpected, then the language server is buggy and should be fixed. 2) If this was expected, you can update your request with the following code to make it match: {request_snippet} """ return remediation
def _flag_unhandled_messages( self, handled_entries: AbstractSet[str], variables: VariableMap, transcript: Transcript, # pyre-fixme[11]: Annotation `_LspIdMap` is not defined as a type. lsp_id_map: _LspIdMap, ) -> Iterable["_ErrorDescription"]: for transcript_id, entry in transcript.items(): if transcript_id in handled_entries: continue received = entry.received if received is None: continue if entry.sent is not None: # We received a request and responded to it. continue method = received["method"] params = received["params"] payload = self._pretty_print_snippet(received) if "id" in received: description = f"""\ An unexpected request of type {method!r} was sent by the language server. Here is the request payload: {payload} """ at_nocommit = "@" + "nocommit" remediation = f"""\ 1) If this was unexpected, then the language server is buggy and should be fixed. 2) If all requests of type {method!r} with theses params should be ignored, add this directive anywhere in your test: .{self.ignore_requests.__name__}(method={method!r}, params={params!r}) 3) To handle this request, add this directive to your test to wait for it and respond to it before proceeding: .{self.wait_for_server_request.__name__}( method={method!r}, params={params!r}, result={{ "{at_nocommit}": "fill in request data here", }}, ) """ else: if any( isinstance(message, _WaitForNotificationSpec) and message.method == method and interpolate_variables(payload=message.params, variables=variables) == params for message in self._messages): # This was a notification we we explicitly waiting for, so skip # it. continue uninterpolated_params = uninterpolate_variables( payload=params, variables=variables) description = f"""\ An unexpected notification of type {method!r} was sent by the language server. Here is the notification payload: {payload} """ remediation = f"""\ 1) If this was unexpected, then the language server is buggy and should be fixed. 2) If all notifications of type {method!r} should be ignored, add this directive anywhere in your test: .{self.ignore_notifications.__name__}(method={method!r}) 3) If this single instance of the notification was expected, add this directive to your test to wait for it before proceeding: .{self.wait_for_notification.__name__}( method={method!r}, params={uninterpolated_params!r}, ) """ previous_request = self._find_previous_request( transcript, lsp_id_map, current_id=transcript_id) if previous_request is not None: request_context = self._get_context_for_call_site_info( previous_request.call_site_info) else: request_context = "<no previous request was found>" context = f"""\ This was the most recent request issued from the language client before it received the notification: {request_context}""" yield _ErrorDescription(description=description, context=context, remediation=remediation)
def test_interpolate_variables_simple(self) -> None: variables = {"foo": "bar"} payload = {"hello": "hi ${foo} hi"} expected = {"hello": "hi bar hi"} self.assertEqual(interpolate_variables(payload, variables), expected) self.assertEqual(uninterpolate_variables(expected, variables), payload)