-
-
Notifications
You must be signed in to change notification settings - Fork 933
test(language_server): add Tester for ServerFormatter #13969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
graphite-app
merged 1 commit into
main
from
09-21-test_language_server_add_tester_for_serverformatter
Sep 21, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
crates/oxc_language_server/fixtures/formatter/basic/basic.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class C1 { | ||
| #name: string; | ||
|
|
||
| public get name() { | ||
| return this.#name; | ||
| } | ||
|
|
||
| private set name(name: string) { | ||
| this.#name = name; | ||
| } | ||
| } | ||
|
|
||
| class C2 { | ||
| #name: string; | ||
|
|
||
| private set name(name: string) { | ||
| this.#name = name; | ||
| } | ||
|
|
||
| public get name() { | ||
| return this.#name; | ||
| } | ||
| } | ||
|
|
||
| const c1 = new C1(); | ||
| const c2 = new C2(); | ||
|
|
||
|
|
||
| // no error | ||
| c1.name; | ||
|
|
||
| // no error | ||
| c2.name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| pub mod options; | ||
| pub mod server_formatter; | ||
| #[cfg(test)] | ||
| mod tester; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| use std::{fmt::Write, path::PathBuf}; | ||
|
|
||
| use tower_lsp_server::{ | ||
| UriExt, | ||
| lsp_types::{TextEdit, Uri}, | ||
| }; | ||
|
|
||
| use crate::{formatter::options::FormatOptions, options::Options, worker::WorkspaceWorker}; | ||
|
|
||
| /// Given a file path relative to the crate root directory, return the absolute path of the file. | ||
| pub fn get_file_path(relative_file_path: &str) -> PathBuf { | ||
| std::env::current_dir().expect("could not get current dir").join(relative_file_path) | ||
| } | ||
|
|
||
| /// Given a file path relative to the crate root directory, return the URI of the file. | ||
| pub fn get_file_uri(relative_file_path: &str) -> Uri { | ||
| Uri::from_file_path(get_file_path(relative_file_path)) | ||
| .expect("failed to convert file path to URL") | ||
| } | ||
|
|
||
| fn get_snapshot_from_text_edits(edits: &[TextEdit]) -> String { | ||
| edits | ||
| .iter() | ||
| .map(|edit| format!("{:#?},\n\n{:?}", edit.range, edit.new_text)) | ||
| .collect::<Vec<_>>() | ||
| .join("\n") | ||
| } | ||
|
|
||
| /// Testing struct for the [formatter server][crate::formatter::server_formatter::ServerFormatter]. | ||
| pub struct Tester<'t> { | ||
| relative_root_dir: &'t str, | ||
| options: Option<FormatOptions>, | ||
| } | ||
|
|
||
| impl Tester<'_> { | ||
| pub fn new(relative_root_dir: &'static str, options: Option<FormatOptions>) -> Self { | ||
| Self { relative_root_dir, options } | ||
| } | ||
|
|
||
| async fn create_workspace_worker(&self) -> WorkspaceWorker { | ||
| let absolute_path = std::env::current_dir() | ||
| .expect("could not get current dir") | ||
| .join(self.relative_root_dir); | ||
| let uri = Uri::from_file_path(absolute_path).expect("could not convert current dir to uri"); | ||
| let worker = WorkspaceWorker::new(uri); | ||
| let option = | ||
| &Options { format: self.options.clone().unwrap_or_default(), ..Default::default() }; | ||
| worker.start_worker(option).await; | ||
|
|
||
| worker | ||
| } | ||
|
|
||
| pub fn format_and_snapshot_single_file(&self, relative_file_path: &str) { | ||
| self.format_and_snapshot_multiple_file(&[relative_file_path]); | ||
| } | ||
|
|
||
| #[expect(clippy::disallowed_methods)] | ||
| pub fn format_and_snapshot_multiple_file(&self, relative_file_paths: &[&str]) { | ||
| let mut snapshot_result = String::new(); | ||
| for relative_file_path in relative_file_paths { | ||
| let uri = get_file_uri(&format!("{}/{}", self.relative_root_dir, relative_file_path)); | ||
| let formatted = tokio::runtime::Runtime::new().unwrap().block_on(async { | ||
| self.create_workspace_worker().await.format_file(&uri, None).await | ||
| }); | ||
|
|
||
| let snapshot = if let Some(formatted) = formatted { | ||
| get_snapshot_from_text_edits(&formatted) | ||
| } else { | ||
| "File is ignored".to_string() | ||
| }; | ||
|
|
||
| let _ = write!( | ||
| snapshot_result, | ||
| "########## \nfile: {}/{relative_file_path}\n----------\n{snapshot}\n", | ||
| self.relative_root_dir | ||
| ); | ||
| } | ||
|
|
||
| let snapshot_name = self.relative_root_dir.replace('/', "_"); | ||
| let mut settings = insta::Settings::clone_current(); | ||
| settings.set_snapshot_path( | ||
| std::env::current_dir().expect("could not get current dir").join("src/snapshots"), | ||
| ); | ||
| settings.set_prepend_module_to_snapshot(false); | ||
| settings.set_omit_expression(true); | ||
| settings.set_snapshot_suffix(relative_file_paths.join("_").replace('\\', "/")); | ||
| settings.bind(|| { | ||
| insta::assert_snapshot!(snapshot_name, snapshot_result); | ||
| }); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
crates/oxc_language_server/src/snapshots/fixtures_formatter_basic@basic.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| source: crates/oxc_language_server/src/formatter/tester.rs | ||
| --- | ||
| ########## | ||
| file: fixtures/formatter/basic/basic.ts | ||
| ---------- | ||
| Range { | ||
| start: Position { | ||
| line: 1, | ||
| character: 2, | ||
| }, | ||
| end: Position { | ||
| line: 26, | ||
| character: 0, | ||
| }, | ||
| }, | ||
|
|
||
| "#name: string;\n\n public get name() {\n return this.#name;\n }\n\n private set name(name: string) {\n this.#name = name;\n }\n}\n\nclass C2 {\n #name: string;\n\n private set name(name: string) {\n this.#name = name;\n }\n\n public get name() {\n return this.#name;\n }\n}\n\nconst c1 = new C1();\nconst c2 = new C2();" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.