From 0c0eb99b81256acf1487d0ff9ed9f472087cc150 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 25 Apr 2024 17:34:49 +0100 Subject: [PATCH] fix: regression of latest refactor (#2594) --- crates/biome_formatter_test/src/spec.rs | 10 +- crates/biome_service/tests/workspace.rs | 336 +++++++++--------- .../backend-jsonrpc/tests/workspace.test.mjs | 12 +- 3 files changed, 191 insertions(+), 167 deletions(-) diff --git a/crates/biome_formatter_test/src/spec.rs b/crates/biome_formatter_test/src/spec.rs index edf0c32c6676..a389314d3b17 100644 --- a/crates/biome_formatter_test/src/spec.rs +++ b/crates/biome_formatter_test/src/spec.rs @@ -11,7 +11,9 @@ use biome_fs::BiomePath; use biome_parser::AnyParse; use biome_rowan::{TextRange, TextSize}; use biome_service::settings::{ServiceLanguage, Settings}; -use biome_service::workspace::{DocumentFileSource, FeaturesBuilder, SupportsFeatureParams}; +use biome_service::workspace::{ + DocumentFileSource, FeaturesBuilder, RegisterProjectFolderParams, SupportsFeatureParams, +}; use biome_service::App; use std::ops::Range; use std::path::{Path, PathBuf}; @@ -40,6 +42,12 @@ impl<'a> SpecTestFile<'a> { spec_input_file.display() ); + app.workspace + .register_project_folder(RegisterProjectFolderParams { + set_as_current_workspace: true, + path: None, + }) + .unwrap(); let mut input_file = BiomePath::new(file_path); let can_format = app .workspace diff --git a/crates/biome_service/tests/workspace.rs b/crates/biome_service/tests/workspace.rs index 8623f3c92678..7250cb393dc8 100644 --- a/crates/biome_service/tests/workspace.rs +++ b/crates/biome_service/tests/workspace.rs @@ -1,162 +1,176 @@ -use biome_fs::BiomePath; -use biome_js_syntax::{JsFileSource, TextSize}; -use biome_service::file_handlers::DocumentFileSource; -use biome_service::workspace::{server, FileGuard, OpenFileParams}; - -#[test] -fn debug_control_flow() { - const SOURCE: &str = "function test () { return; }"; - const GRAPH: &str = "flowchart TB - block_0[\"block_0
Return(JS_RETURN_STATEMENT 19..26)
Return\"] - -"; - - let workspace = server(); - - let file = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("file.js"), - content: SOURCE.into(), - version: 0, - document_file_source: Some(DocumentFileSource::from(JsFileSource::default())), - }, - ) - .unwrap(); - - let cfg = file.get_control_flow_graph(TextSize::from(20)).unwrap(); - - assert_eq!(cfg, GRAPH); -} - -#[test] -fn recognize_typescript_definition_file() { - let workspace = server(); - - let file = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("file.d.ts"), - // the following code snippet can be correctly parsed in .d.ts file but not in .ts file - content: "export const foo: number".into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - - assert!(file.format_file().is_ok()); -} - -#[test] -fn correctly_handle_json_files() { - let workspace = server(); - - // ".json" file - let json_file = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("a.json"), - content: r#"{"a": 42}"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(json_file.format_file().is_ok()); - - // ".json" file doesn't allow comments - let json_file_with_comments = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("b.json"), - content: r#"{"a": 42}//comment"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(json_file_with_comments.format_file().is_err()); - - // ".json" file doesn't allow trailing commas - let json_file_with_trailing_commas = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("c.json"), - content: r#"{"a": 42,}"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(json_file_with_trailing_commas.format_file().is_err()); - - // ".jsonc" file allows comments - let jsonc_file = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("d.jsonc"), - content: r#"{"a": 42}//comment"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(jsonc_file.format_file().is_ok()); - - // ".jsonc" file allow trailing commas - let jsonc_file = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("e.jsonc"), - content: r#"{"a": 42,}"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(jsonc_file.format_file().is_ok()); - - // well-known json-with-comments file allows comments - let well_known_json_with_comments_file = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new(".eslintrc.json"), - content: r#"{"a": 42}//comment"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(well_known_json_with_comments_file.format_file().is_ok()); - - // well-known json-with-comments file doesn't allow trailing commas - let well_known_json_with_comments_file_with_trailing_commas = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("dir/.eslintrc.json"), - content: r#"{"a": 42,}"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(well_known_json_with_comments_file_with_trailing_commas - .format_file() - .is_err()); - - // well-known json-with-comments-and-trailing-commas file allows comments and trailing commas - let well_known_json_with_comments_and_trailing_commas_file = FileGuard::open( - workspace.as_ref(), - OpenFileParams { - path: BiomePath::new("tsconfig.json"), - content: r#"{"a": 42,}//comment"#.into(), - version: 0, - document_file_source: None, - }, - ) - .unwrap(); - assert!(well_known_json_with_comments_and_trailing_commas_file - .format_file() - .is_ok()); +#[cfg(test)] +mod test { + use biome_fs::BiomePath; + use biome_js_syntax::{JsFileSource, TextSize}; + use biome_service::file_handlers::DocumentFileSource; + use biome_service::workspace::{ + server, FileGuard, OpenFileParams, RegisterProjectFolderParams, + }; + use biome_service::Workspace; + fn create_server() -> Box { + let workspace = server(); + workspace + .register_project_folder(RegisterProjectFolderParams { + set_as_current_workspace: true, + path: None, + }) + .unwrap(); + + workspace + } + + #[test] + fn debug_control_flow() { + const SOURCE: &str = "function test () { return; }"; + const GRAPH: &str = "flowchart TB + block_0[\"block_0
Return(JS_RETURN_STATEMENT 19..26)
Return\"]\n\n"; + + let workspace = create_server(); + let file = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("file.js"), + content: SOURCE.into(), + version: 0, + document_file_source: Some(DocumentFileSource::from(JsFileSource::default())), + }, + ) + .unwrap(); + + let cfg = file.get_control_flow_graph(TextSize::from(20)).unwrap(); + + assert_eq!(cfg, GRAPH); + } + + #[test] + fn recognize_typescript_definition_file() { + let workspace = create_server(); + + let file = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("file.d.ts"), + // the following code snippet can be correctly parsed in .d.ts file but not in .ts file + content: "export const foo: number".into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + + assert!(file.format_file().is_ok()); + } + + #[test] + fn correctly_handle_json_files() { + let workspace = create_server(); + + // ".json" file + let json_file = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("a.json"), + content: r#"{"a": 42}"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(json_file.format_file().is_ok()); + + // ".json" file doesn't allow comments + let json_file_with_comments = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("b.json"), + content: r#"{"a": 42}//comment"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(json_file_with_comments.format_file().is_err()); + + // ".json" file doesn't allow trailing commas + let json_file_with_trailing_commas = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("c.json"), + content: r#"{"a": 42,}"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(json_file_with_trailing_commas.format_file().is_err()); + + // ".jsonc" file allows comments + let jsonc_file = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("d.jsonc"), + content: r#"{"a": 42}//comment"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(jsonc_file.format_file().is_ok()); + + // ".jsonc" file allow trailing commas + let jsonc_file = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("e.jsonc"), + content: r#"{"a": 42,}"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(jsonc_file.format_file().is_ok()); + + // well-known json-with-comments file allows comments + let well_known_json_with_comments_file = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new(".eslintrc.json"), + content: r#"{"a": 42}//comment"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(well_known_json_with_comments_file.format_file().is_ok()); + + // well-known json-with-comments file doesn't allow trailing commas + let well_known_json_with_comments_file_with_trailing_commas = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("dir/.eslintrc.json"), + content: r#"{"a": 42,}"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(well_known_json_with_comments_file_with_trailing_commas + .format_file() + .is_err()); + + // well-known json-with-comments-and-trailing-commas file allows comments and trailing commas + let well_known_json_with_comments_and_trailing_commas_file = FileGuard::open( + workspace.as_ref(), + OpenFileParams { + path: BiomePath::new("tsconfig.json"), + content: r#"{"a": 42,}//comment"#.into(), + version: 0, + document_file_source: None, + }, + ) + .unwrap(); + assert!(well_known_json_with_comments_and_trailing_commas_file + .format_file() + .is_ok()); + } } diff --git a/packages/@biomejs/backend-jsonrpc/tests/workspace.test.mjs b/packages/@biomejs/backend-jsonrpc/tests/workspace.test.mjs index 0b28f79b74e7..38e7d461e1e1 100644 --- a/packages/@biomejs/backend-jsonrpc/tests/workspace.test.mjs +++ b/packages/@biomejs/backend-jsonrpc/tests/workspace.test.mjs @@ -1,8 +1,8 @@ -import { resolve } from "node:path"; -import { fileURLToPath } from "node:url"; -import { describe, expect, it } from "vitest"; +import {resolve} from "node:path"; +import {fileURLToPath} from "node:url"; +import {describe, expect, it} from "vitest"; -import { createWorkspaceWithBinary } from "../dist/index.js"; +import {createWorkspaceWithBinary} from "../dist/index.js"; describe("Workspace API", () => { it("should process remote requests", async () => { @@ -14,7 +14,9 @@ describe("Workspace API", () => { ); const workspace = await createWorkspaceWithBinary(command); - + workspace.registerProjectFolder({ + setAsCurrentWorkspace: true + }); await workspace.openFile({ path: { path: "test.js",