Skip to content

Commit 80a2469

Browse files
dhruvmanilaAlexWaygood
authored andcommitted
[ty] Move server tests as integration tests (#19522)
## Summary Reference: #19391 (comment)
1 parent a5b53fb commit 80a2469

15 files changed

+172
-128
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ty_server/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ tracing = { workspace = true }
3838
tracing-subscriber = { workspace = true, features = ["chrono"] }
3939

4040
[dev-dependencies]
41+
ty_server = { workspace = true, features = ["testing"] }
42+
4143
dunce = { workspace = true }
4244
insta = { workspace = true, features = ["filters", "json"] }
4345
regex = { workspace = true }
4446
tempfile = { workspace = true }
4547

48+
[features]
49+
testing = []
50+
4651
[target.'cfg(target_vendor = "apple")'.dependencies]
4752
libc = { workspace = true }
4853

crates/ty_server/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use anyhow::Context;
44
use lsp_server::Connection;
55
use ruff_db::system::{OsSystem, SystemPathBuf};
66

7-
use crate::server::Server;
7+
pub use crate::logging::{LogLevel, init_logging};
8+
pub use crate::server::Server;
9+
pub use crate::session::ClientOptions;
810
pub use document::{NotebookDocument, PositionEncoding, TextDocument};
911
pub(crate) use session::{DocumentQuery, Session};
1012

@@ -14,9 +16,6 @@ mod server;
1416
mod session;
1517
mod system;
1618

17-
#[cfg(test)]
18-
pub mod test;
19-
2019
pub(crate) const SERVER_NAME: &str = "ty";
2120
pub(crate) const DIAGNOSTIC_NAME: &str = "ty";
2221

crates/ty_server/src/logging.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tracing_subscriber::fmt::time::ChronoLocal;
1414
use tracing_subscriber::fmt::writer::BoxMakeWriter;
1515
use tracing_subscriber::layer::SubscriberExt;
1616

17-
pub(crate) fn init_logging(log_level: LogLevel, log_file: Option<&SystemPath>) {
17+
pub fn init_logging(log_level: LogLevel, log_file: Option<&SystemPath>) {
1818
let log_file = log_file
1919
.map(|path| {
2020
// this expands `logFile` so that tildes and environment variables
@@ -66,7 +66,7 @@ pub(crate) fn init_logging(log_level: LogLevel, log_file: Option<&SystemPath>) {
6666
/// The default log level is `info`.
6767
#[derive(Clone, Copy, Debug, Deserialize, Default, PartialEq, Eq, PartialOrd, Ord)]
6868
#[serde(rename_all = "lowercase")]
69-
pub(crate) enum LogLevel {
69+
pub enum LogLevel {
7070
Error,
7171
Warn,
7272
#[default]

crates/ty_server/src/server.rs

Lines changed: 3 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) use api::publish_settings_diagnostics;
2626
pub(crate) use main_loop::{Action, ConnectionSender, Event, MainLoopReceiver, MainLoopSender};
2727
pub(crate) type Result<T> = std::result::Result<T, api::Error>;
2828

29-
pub(crate) struct Server {
29+
pub struct Server {
3030
connection: Connection,
3131
client_capabilities: ClientCapabilities,
3232
worker_threads: NonZeroUsize,
@@ -36,7 +36,7 @@ pub(crate) struct Server {
3636
}
3737

3838
impl Server {
39-
pub(crate) fn new(
39+
pub fn new(
4040
worker_threads: NonZeroUsize,
4141
connection: Connection,
4242
native_system: Arc<dyn System + 'static + Send + Sync + RefUnwindSafe>,
@@ -161,7 +161,7 @@ impl Server {
161161
})
162162
}
163163

164-
pub(crate) fn run(mut self) -> crate::Result<()> {
164+
pub fn run(mut self) -> crate::Result<()> {
165165
let client = Client::new(
166166
self.main_loop_sender.clone(),
167167
self.connection.sender.clone(),
@@ -302,89 +302,3 @@ impl Drop for ServerPanicHookHandler {
302302
}
303303
}
304304
}
305-
306-
#[cfg(test)]
307-
mod tests {
308-
use anyhow::Result;
309-
use lsp_types::notification::PublishDiagnostics;
310-
use ruff_db::system::SystemPath;
311-
312-
use crate::session::ClientOptions;
313-
use crate::test::TestServerBuilder;
314-
315-
#[test]
316-
fn initialization() -> Result<()> {
317-
let server = TestServerBuilder::new()?
318-
.build()?
319-
.wait_until_workspaces_are_initialized()?;
320-
321-
let initialization_result = server.initialization_result().unwrap();
322-
323-
insta::assert_json_snapshot!("initialization", initialization_result);
324-
325-
Ok(())
326-
}
327-
328-
#[test]
329-
fn initialization_with_workspace() -> Result<()> {
330-
let workspace_root = SystemPath::new("foo");
331-
let server = TestServerBuilder::new()?
332-
.with_workspace(workspace_root, ClientOptions::default())?
333-
.build()?
334-
.wait_until_workspaces_are_initialized()?;
335-
336-
let initialization_result = server.initialization_result().unwrap();
337-
338-
insta::assert_json_snapshot!("initialization_with_workspace", initialization_result);
339-
340-
Ok(())
341-
}
342-
343-
#[test]
344-
fn publish_diagnostics_on_did_open() -> Result<()> {
345-
let workspace_root = SystemPath::new("src");
346-
let foo = SystemPath::new("src/foo.py");
347-
let foo_content = "\
348-
def foo() -> str:
349-
return 42
350-
";
351-
352-
let mut server = TestServerBuilder::new()?
353-
.with_workspace(workspace_root, ClientOptions::default())?
354-
.with_file(foo, foo_content)?
355-
.enable_pull_diagnostics(false)
356-
.build()?
357-
.wait_until_workspaces_are_initialized()?;
358-
359-
server.open_text_document(foo, &foo_content, 1);
360-
let diagnostics = server.await_notification::<PublishDiagnostics>()?;
361-
362-
insta::assert_debug_snapshot!(diagnostics);
363-
364-
Ok(())
365-
}
366-
367-
#[test]
368-
fn pull_diagnostics_on_did_open() -> Result<()> {
369-
let workspace_root = SystemPath::new("src");
370-
let foo = SystemPath::new("src/foo.py");
371-
let foo_content = "\
372-
def foo() -> str:
373-
return 42
374-
";
375-
376-
let mut server = TestServerBuilder::new()?
377-
.with_workspace(workspace_root, ClientOptions::default())?
378-
.with_file(foo, foo_content)?
379-
.enable_pull_diagnostics(true)
380-
.build()?
381-
.wait_until_workspaces_are_initialized()?;
382-
383-
server.open_text_document(foo, &foo_content, 1);
384-
let diagnostics = server.document_diagnostic_request(foo)?;
385-
386-
insta::assert_debug_snapshot!(diagnostics);
387-
388-
Ok(())
389-
}
390-
}

crates/ty_server/src/session.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use ty_project::{ChangeResult, Db as _, ProjectDatabase, ProjectMetadata};
2121

2222
pub(crate) use self::capabilities::ResolvedClientCapabilities;
2323
pub(crate) use self::index::DocumentQuery;
24-
pub(crate) use self::options::{AllOptions, ClientOptions, DiagnosticMode};
24+
pub use self::options::ClientOptions;
25+
pub(crate) use self::options::{AllOptions, DiagnosticMode};
2526
pub(crate) use self::settings::ClientSettings;
2627
use crate::document::{DocumentKey, DocumentVersion, NotebookDocument};
2728
use crate::server::publish_settings_diagnostics;

crates/ty_server/src/session/options.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ struct WorkspaceOptions {
4949

5050
/// This is a direct representation of the settings schema sent by the client.
5151
#[derive(Clone, Debug, Deserialize, Default)]
52-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
52+
#[cfg_attr(test, derive(PartialEq, Eq))]
53+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
5354
#[serde(rename_all = "camelCase")]
54-
pub(crate) struct ClientOptions {
55+
pub struct ClientOptions {
5556
/// Settings under the `python.*` namespace in VS Code that are useful for the ty language
5657
/// server.
5758
python: Option<Python>,
@@ -63,7 +64,8 @@ pub(crate) struct ClientOptions {
6364

6465
/// Diagnostic mode for the language server.
6566
#[derive(Clone, Copy, Debug, Default, Deserialize)]
66-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
67+
#[cfg_attr(test, derive(PartialEq, Eq))]
68+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
6769
#[serde(rename_all = "camelCase")]
6870
pub(crate) enum DiagnosticMode {
6971
/// Check only currently open files.
@@ -147,21 +149,24 @@ impl ClientOptions {
147149
// all settings and not just the ones in "python.*".
148150

149151
#[derive(Clone, Debug, Deserialize, Default)]
150-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
152+
#[cfg_attr(test, derive(PartialEq, Eq))]
153+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
151154
#[serde(rename_all = "camelCase")]
152155
struct Python {
153156
ty: Option<Ty>,
154157
}
155158

156159
#[derive(Clone, Debug, Deserialize, Default)]
157-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
160+
#[cfg_attr(test, derive(PartialEq, Eq))]
161+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
158162
#[serde(rename_all = "camelCase")]
159163
struct PythonExtension {
160164
active_environment: Option<ActiveEnvironment>,
161165
}
162166

163167
#[derive(Clone, Debug, Deserialize)]
164-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
168+
#[cfg_attr(test, derive(PartialEq, Eq))]
169+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
165170
#[serde(rename_all = "camelCase")]
166171
pub(crate) struct ActiveEnvironment {
167172
pub(crate) executable: PythonExecutable,
@@ -170,7 +175,8 @@ pub(crate) struct ActiveEnvironment {
170175
}
171176

172177
#[derive(Clone, Debug, Deserialize)]
173-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
178+
#[cfg_attr(test, derive(PartialEq, Eq))]
179+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
174180
#[serde(rename_all = "camelCase")]
175181
pub(crate) struct EnvironmentVersion {
176182
pub(crate) major: i64,
@@ -182,7 +188,8 @@ pub(crate) struct EnvironmentVersion {
182188
}
183189

184190
#[derive(Clone, Debug, Deserialize)]
185-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
191+
#[cfg_attr(test, derive(PartialEq, Eq))]
192+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
186193
#[serde(rename_all = "camelCase")]
187194
pub(crate) struct PythonEnvironment {
188195
pub(crate) folder_uri: Url,
@@ -194,7 +201,8 @@ pub(crate) struct PythonEnvironment {
194201
}
195202

196203
#[derive(Clone, Debug, Deserialize)]
197-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
204+
#[cfg_attr(test, derive(PartialEq, Eq))]
205+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
198206
#[serde(rename_all = "camelCase")]
199207
pub(crate) struct PythonExecutable {
200208
#[allow(dead_code)]
@@ -203,7 +211,8 @@ pub(crate) struct PythonExecutable {
203211
}
204212

205213
#[derive(Clone, Debug, Deserialize, Default)]
206-
#[cfg_attr(test, derive(serde::Serialize, PartialEq, Eq))]
214+
#[cfg_attr(test, derive(PartialEq, Eq))]
215+
#[cfg_attr(feature = "testing", derive(serde::Serialize))]
207216
#[serde(rename_all = "camelCase")]
208217
struct Ty {
209218
disable_language_services: Option<bool>,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use anyhow::Result;
2+
use ruff_db::system::SystemPath;
3+
use ty_server::ClientOptions;
4+
5+
use crate::TestServerBuilder;
6+
7+
#[test]
8+
fn empty_workspace_folders() -> Result<()> {
9+
let server = TestServerBuilder::new()?
10+
.build()?
11+
.wait_until_workspaces_are_initialized()?;
12+
13+
let initialization_result = server.initialization_result().unwrap();
14+
15+
insta::assert_json_snapshot!("initialization", initialization_result);
16+
17+
Ok(())
18+
}
19+
20+
#[test]
21+
fn single_workspace_folder() -> Result<()> {
22+
let workspace_root = SystemPath::new("foo");
23+
let server = TestServerBuilder::new()?
24+
.with_workspace(workspace_root, ClientOptions::default())?
25+
.build()?
26+
.wait_until_workspaces_are_initialized()?;
27+
28+
let initialization_result = server.initialization_result().unwrap();
29+
30+
insta::assert_json_snapshot!("initialization_with_workspace", initialization_result);
31+
32+
Ok(())
33+
}

0 commit comments

Comments
 (0)