|
| 1 | +//! The `by/transpile` and `by/transpileForBuild` requests, exercised through the |
| 2 | +//! server rather than through the library they call. |
| 3 | +//! |
| 4 | +//! That distinction is the whole reason these exist. The transpiler builds a |
| 5 | +//! database of its own whenever it lowers rewritten source, and a request handler |
| 6 | +//! runs with the project database already attached — so the nested one is a second |
| 7 | +//! attachment on the same thread, which salsa refuses with "Cannot change database |
| 8 | +//! mid-query". Nothing about it reproduces from the command line, where nothing is |
| 9 | +//! attached, and the `crates/ty` tests that cover the same operations therefore |
| 10 | +//! passed throughout. Only a request does it. |
| 11 | +
|
| 12 | +use anyhow::Result; |
| 13 | +use lsp_types::{LspRequestMethod, MessageDirection, Request, TextDocumentIdentifier, Uri}; |
| 14 | +use ruff_db::system::SystemPath; |
| 15 | + |
| 16 | +use crate::TestServerBuilder; |
| 17 | + |
| 18 | +#[derive(Debug, serde::Deserialize, serde::Serialize)] |
| 19 | +#[serde(rename_all = "camelCase")] |
| 20 | +struct TranspileParams { |
| 21 | + text_document: TextDocumentIdentifier, |
| 22 | + #[serde(skip_serializing_if = "std::ops::Not::not")] |
| 23 | + reverse: bool, |
| 24 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 25 | + source: Option<String>, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, serde::Deserialize, serde::Serialize)] |
| 29 | +#[serde(rename_all = "camelCase")] |
| 30 | +struct TranspileResponse { |
| 31 | + #[serde(default)] |
| 32 | + source: Option<String>, |
| 33 | + #[serde(default)] |
| 34 | + error: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +enum Transpile {} |
| 38 | + |
| 39 | +impl Request for Transpile { |
| 40 | + type Params = TranspileParams; |
| 41 | + type Result = Option<TranspileResponse>; |
| 42 | + const METHOD: LspRequestMethod<'static> = LspRequestMethod::Custom("by/transpile"); |
| 43 | + const MESSAGE_DIRECTION: MessageDirection = MessageDirection::ClientToServer; |
| 44 | +} |
| 45 | + |
| 46 | +fn params(uri: &Uri, source: Option<&str>) -> TranspileParams { |
| 47 | + TranspileParams { |
| 48 | + text_document: TextDocumentIdentifier { uri: uri.clone() }, |
| 49 | + reverse: false, |
| 50 | + source: source.map(str::to_owned), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// The fragment path: `source` is supplied, so the whole transpile runs over text |
| 55 | +/// rather than the project, and it builds an in-memory database to check that text |
| 56 | +/// against. That database is queried while the project's is attached. |
| 57 | +#[test] |
| 58 | +fn transpiling_a_fragment_answers_rather_than_panicking() -> Result<()> { |
| 59 | + let workspace_root = SystemPath::new("src"); |
| 60 | + let main = SystemPath::new("src/main.by"); |
| 61 | + let content = "def f(a: int?) -> int:\n return a ?? 0\n"; |
| 62 | + |
| 63 | + let mut server = TestServerBuilder::new()? |
| 64 | + .with_workspace(workspace_root, None)? |
| 65 | + .with_file(main, content)? |
| 66 | + .build() |
| 67 | + .wait_until_workspaces_are_initialized(); |
| 68 | + |
| 69 | + server.open_text_document(main, content, 1); |
| 70 | + let uri = server.file_uri(main); |
| 71 | + |
| 72 | + let response = server |
| 73 | + .send_request_await::<Transpile>(params(&uri, Some(content))) |
| 74 | + .expect("the server answered nothing at all"); |
| 75 | + |
| 76 | + assert!( |
| 77 | + response.error.is_none(), |
| 78 | + "transpile failed: {:?}", |
| 79 | + response.error |
| 80 | + ); |
| 81 | + let generated = response.source.expect("no generated source"); |
| 82 | + assert!( |
| 83 | + generated.contains("def f("), |
| 84 | + "unexpected output:\n{generated}" |
| 85 | + ); |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
| 89 | +/// The whole-document path, which goes through the project database — and then |
| 90 | +/// still builds one of its own, because a pre-pass rewrote the source it lowers. |
| 91 | +/// `enum class` is such a pre-pass. |
| 92 | +#[test] |
| 93 | +fn transpiling_a_document_answers_rather_than_panicking() -> Result<()> { |
| 94 | + let workspace_root = SystemPath::new("src"); |
| 95 | + let main = SystemPath::new("src/main.by"); |
| 96 | + let content = "enum class Colour:\n case Red\n case Green\n\nlet chosen: Colour = Red\n"; |
| 97 | + |
| 98 | + let mut server = TestServerBuilder::new()? |
| 99 | + .with_workspace(workspace_root, None)? |
| 100 | + .with_file(main, content)? |
| 101 | + .build() |
| 102 | + .wait_until_workspaces_are_initialized(); |
| 103 | + |
| 104 | + server.open_text_document(main, content, 1); |
| 105 | + let uri = server.file_uri(main); |
| 106 | + |
| 107 | + let response = server |
| 108 | + .send_request_await::<Transpile>(params(&uri, None)) |
| 109 | + .expect("the server answered nothing at all"); |
| 110 | + |
| 111 | + assert!( |
| 112 | + response.error.is_none(), |
| 113 | + "transpile failed: {:?}", |
| 114 | + response.error |
| 115 | + ); |
| 116 | + let generated = response.source.expect("no generated source"); |
| 117 | + assert!( |
| 118 | + generated.contains("Colour"), |
| 119 | + "unexpected output:\n{generated}" |
| 120 | + ); |
| 121 | + Ok(()) |
| 122 | +} |
0 commit comments