|
| 1 | +//===- Transport.cpp - LSP JSON transport unit tests ----------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "mlir/Tools/lsp-server-support/Transport.h" |
| 10 | +#include "mlir/Tools/lsp-server-support/Logging.h" |
| 11 | +#include "mlir/Tools/lsp-server-support/Protocol.h" |
| 12 | +#include "llvm/Support/FileSystem.h" |
| 13 | +#include "gmock/gmock.h" |
| 14 | +#include "gtest/gtest.h" |
| 15 | + |
| 16 | +using namespace mlir; |
| 17 | +using namespace mlir::lsp; |
| 18 | +using namespace testing; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +TEST(TransportTest, SendReply) { |
| 23 | + std::string out; |
| 24 | + llvm::raw_string_ostream os(out); |
| 25 | + JSONTransport transport(nullptr, os); |
| 26 | + MessageHandler handler(transport); |
| 27 | + |
| 28 | + transport.reply(1989, nullptr); |
| 29 | + EXPECT_THAT(out, HasSubstr("\"id\":1989")); |
| 30 | + EXPECT_THAT(out, HasSubstr("\"result\":null")); |
| 31 | +} |
| 32 | + |
| 33 | +class TransportInputTest : public Test { |
| 34 | + llvm::SmallVector<char> inputPath; |
| 35 | + std::FILE *in = nullptr; |
| 36 | + std::string output = ""; |
| 37 | + llvm::raw_string_ostream os; |
| 38 | + std::optional<JSONTransport> transport = std::nullopt; |
| 39 | + std::optional<MessageHandler> messageHandler = std::nullopt; |
| 40 | + |
| 41 | +protected: |
| 42 | + TransportInputTest() : os(output) {} |
| 43 | + |
| 44 | + void SetUp() override { |
| 45 | + std::error_code ec = |
| 46 | + llvm::sys::fs::createTemporaryFile("lsp-unittest", "json", inputPath); |
| 47 | + ASSERT_FALSE(ec) << "Could not create temporary file: " << ec.message(); |
| 48 | + |
| 49 | + in = std::fopen(inputPath.data(), "r"); |
| 50 | + ASSERT_TRUE(in) << "Could not open temporary file: " |
| 51 | + << std::strerror(errno); |
| 52 | + transport.emplace(in, os, JSONStreamStyle::Delimited); |
| 53 | + messageHandler.emplace(*transport); |
| 54 | + } |
| 55 | + |
| 56 | + void TearDown() override { |
| 57 | + EXPECT_EQ(std::fclose(in), 0) |
| 58 | + << "Could not close temporary file FD: " << std::strerror(errno); |
| 59 | + std::error_code ec = |
| 60 | + llvm::sys::fs::remove(inputPath, /*IgnoreNonExisting=*/false); |
| 61 | + EXPECT_FALSE(ec) << "Could not remove temporary file '" << inputPath.data() |
| 62 | + << "': " << ec.message(); |
| 63 | + } |
| 64 | + |
| 65 | + void writeInput(StringRef buffer) { |
| 66 | + std::error_code ec; |
| 67 | + llvm::raw_fd_ostream os(inputPath.data(), ec); |
| 68 | + ASSERT_FALSE(ec) << "Could not write to '" << inputPath.data() |
| 69 | + << "': " << ec.message(); |
| 70 | + os << buffer; |
| 71 | + os.close(); |
| 72 | + } |
| 73 | + |
| 74 | + StringRef getOutput() const { return output; } |
| 75 | + MessageHandler &getMessageHandler() { return *messageHandler; } |
| 76 | + |
| 77 | + void runTransport() { |
| 78 | + bool gotEOF = false; |
| 79 | + llvm::Error err = llvm::handleErrors( |
| 80 | + transport->run(*messageHandler), [&](const llvm::ECError &ecErr) { |
| 81 | + gotEOF = ecErr.convertToErrorCode() == std::errc::io_error; |
| 82 | + }); |
| 83 | + llvm::consumeError(std::move(err)); |
| 84 | + EXPECT_TRUE(gotEOF); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +TEST_F(TransportInputTest, RequestWithInvalidParams) { |
| 89 | + struct Handler { |
| 90 | + void onMethod(const TextDocumentItem ¶ms, |
| 91 | + mlir::lsp::Callback<TextDocumentIdentifier> callback) {} |
| 92 | + } handler; |
| 93 | + getMessageHandler().method("invalid-params-request", &handler, |
| 94 | + &Handler::onMethod); |
| 95 | + |
| 96 | + writeInput("{\"jsonrpc\":\"2.0\",\"id\":92," |
| 97 | + "\"method\":\"invalid-params-request\",\"params\":{}}\n"); |
| 98 | + runTransport(); |
| 99 | + EXPECT_THAT(getOutput(), HasSubstr("error")); |
| 100 | + EXPECT_THAT(getOutput(), HasSubstr("missing value at (root).uri")); |
| 101 | +} |
| 102 | + |
| 103 | +TEST_F(TransportInputTest, NotificationWithInvalidParams) { |
| 104 | + // JSON parsing errors are only reported via error logging. As a result, this |
| 105 | + // test can't make any expectations -- but it prints the output anyway, by way |
| 106 | + // of demonstration. |
| 107 | + Logger::setLogLevel(Logger::Level::Error); |
| 108 | + |
| 109 | + struct Handler { |
| 110 | + void onNotification(const TextDocumentItem ¶ms) {} |
| 111 | + } handler; |
| 112 | + getMessageHandler().notification("invalid-params-notification", &handler, |
| 113 | + &Handler::onNotification); |
| 114 | + |
| 115 | + writeInput("{\"jsonrpc\":\"2.0\",\"method\":\"invalid-params-notification\"," |
| 116 | + "\"params\":{}}\n"); |
| 117 | + runTransport(); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_F(TransportInputTest, MethodNotFound) { |
| 121 | + writeInput("{\"jsonrpc\":\"2.0\",\"id\":29,\"method\":\"ack\"}\n"); |
| 122 | + runTransport(); |
| 123 | + EXPECT_THAT(getOutput(), HasSubstr("\"id\":29")); |
| 124 | + EXPECT_THAT(getOutput(), HasSubstr("\"error\"")); |
| 125 | + EXPECT_THAT(getOutput(), HasSubstr("\"message\":\"method not found: ack\"")); |
| 126 | +} |
| 127 | + |
| 128 | +TEST_F(TransportInputTest, OutgoingNotification) { |
| 129 | + auto notifyFn = getMessageHandler().outgoingNotification<CompletionList>( |
| 130 | + "outgoing-notification"); |
| 131 | + notifyFn(CompletionList{}); |
| 132 | + EXPECT_THAT(getOutput(), HasSubstr("\"method\":\"outgoing-notification\"")); |
| 133 | +} |
| 134 | +} // namespace |
0 commit comments