[ty] Implement mock language server for testing (#19391)

## Summary

Closes: astral-sh/ty#88

This PR implements an initial version of a mock language server that can
be used to write e2e tests using the real server running in the
background.

The way it works is that you'd use the `TestServerBuilder` to help
construct the `TestServer` with the setup data. This could be the
workspace folders, populating the file and it's content in the memory
file system, setting the right client capabilities to make the server
respond correctly, etc. This can be expanded as we write more test
cases.

There are still a few things to follow-up on:
- ~In the `Drop` implementation, we should assert that there are no
pending notification, request and responses from the server that the
test code hasn't handled yet~ Implemented in [`afd1f82`
(#19391)](afd1f82bde)
- Reduce the setup boilerplate in any way we can
- Improve the final assertion, currently I'm just snapshotting the final
output

## Test Plan

Written a few test cases.
This commit is contained in:
Dhruv Manilawala 2025-07-23 12:26:58 +05:30 committed by GitHub
parent 385d6fa608
commit 53d795da67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1430 additions and 51 deletions

View file

@ -1,7 +1,8 @@
use std::num::NonZeroUsize;
use std::{num::NonZeroUsize, sync::Arc};
use anyhow::Context;
use lsp_server::Connection;
use ruff_db::system::{OsSystem, SystemPathBuf};
use crate::server::Server;
pub use document::{NotebookDocument, PositionEncoding, TextDocument};
@ -13,6 +14,9 @@ mod server;
mod session;
mod system;
#[cfg(test)]
pub mod test;
pub(crate) const SERVER_NAME: &str = "ty";
pub(crate) const DIAGNOSTIC_NAME: &str = "ty";
@ -30,7 +34,21 @@ pub fn run_server() -> anyhow::Result<()> {
let (connection, io_threads) = Connection::stdio();
let server_result = Server::new(worker_threads, connection)
let cwd = {
let cwd = std::env::current_dir().context("Failed to get the current working directory")?;
SystemPathBuf::from_path_buf(cwd).map_err(|path| {
anyhow::anyhow!(
"The current working directory `{}` contains non-Unicode characters. \
ty only supports Unicode paths.",
path.display()
)
})?
};
// This is to complement the `LSPSystem` if the document is not available in the index.
let fallback_system = Arc::new(OsSystem::new(cwd));
let server_result = Server::new(worker_threads, connection, fallback_system, true)
.context("Failed to start server")?
.run();