From eb1ecee3fe9ab0c2e7606ed31bb26d1bd38a86cc Mon Sep 17 00:00:00 2001 From: ematipico Date: Fri, 9 Sep 2022 09:52:57 +0100 Subject: [PATCH 1/3] fix(rome_cli): issue #3175 --- crates/rome_cli/src/commands/format.rs | 21 ++++ crates/rome_cli/tests/configs.rs | 16 ++++ crates/rome_cli/tests/main.rs | 95 ++++++++++++++++++- ...ration_over_config_file_issue_3175_v1.snap | 26 +++++ ...ration_over_config_file_issue_3175_v2.snap | 28 ++++++ .../src/configuration/javascript.rs | 9 ++ .../src/file_handlers/javascript.rs | 1 + 7 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v1.snap create mode 100644 crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v2.snap diff --git a/crates/rome_cli/src/commands/format.rs b/crates/rome_cli/src/commands/format.rs index 26508e99dea..3ba927359a8 100644 --- a/crates/rome_cli/src/commands/format.rs +++ b/crates/rome_cli/src/commands/format.rs @@ -13,6 +13,7 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> { let configuration = load_config(&session.app.fs, None)?; let configuration = apply_format_settings_from_cli(&mut session, configuration)?; + dbg!(&configuration); session .app .workspace @@ -105,6 +106,14 @@ pub(crate) fn apply_format_settings_from_cli( source, })?; + // if at least one argument is passed via CLI and no "formatter" configuration was passed + // via `rome.json`, we need to create it + if (line_width.is_some() | indent_style.is_some() | size.is_some()) + && configuration.formatter.is_none() + { + configuration.formatter = Some(FormatterConfiguration::default()); + } + if let Some(formatter) = configuration.formatter.as_mut() { match indent_style { Some(IndentStyle::Tab) => { @@ -137,6 +146,18 @@ pub(crate) fn apply_format_settings_from_cli( argument: "--quote-style", source, })?; + + // if at least one argument is passed via CLI and no "javascript.formatter" configuration was passed + // via `rome.json`, we need to create it + if quote_style.is_some() | quote_properties.is_some() { + if configuration.javascript.is_none() { + configuration.javascript = Some(JavascriptConfiguration::with_formatter()) + } else if let Some(javascript) = configuration.javascript.as_mut() { + if javascript.formatter.is_none() { + javascript.formatter = Some(JavascriptFormatter::default()); + } + } + } if let Some(javascript) = configuration .javascript .as_mut() diff --git a/crates/rome_cli/tests/configs.rs b/crates/rome_cli/tests/configs.rs index d4fc35b3b42..cd14e368269 100644 --- a/crates/rome_cli/tests/configs.rs +++ b/crates/rome_cli/tests/configs.rs @@ -144,3 +144,19 @@ pub const CONFIG_INCORRECT_GLOBALS_V2: &str = r#"{ } } }"#; + +pub const CONFIG_ISSUE_3175_1: &str = r#"{ + "formatter": { + "indentStyle": "space", + "indentSize": 2, + "lineWidth": 120 + } +}"#; + +pub const CONFIG_ISSUE_3175_2: &str = r#"{ + "javascript": { + "formatter": { + "quoteStyle": "single" + } + } +}"#; diff --git a/crates/rome_cli/tests/main.rs b/crates/rome_cli/tests/main.rs index 309ade47aa9..592b1606a8e 100644 --- a/crates/rome_cli/tests/main.rs +++ b/crates/rome_cli/tests/main.rs @@ -734,7 +734,9 @@ mod ci { mod format { use super::*; - use crate::configs::{CONFIG_DISABLED_FORMATTER, CONFIG_FORMAT}; + use crate::configs::{ + CONFIG_DISABLED_FORMATTER, CONFIG_FORMAT, CONFIG_ISSUE_3175_1, CONFIG_ISSUE_3175_2, + }; use crate::snap_test::markup_to_string; use rome_console::markup; use rome_fs::FileSystemExt; @@ -934,6 +936,97 @@ mod format { ); } + #[test] + fn applies_custom_configuration_over_config_file_issue_3175_v1() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let file_path = Path::new("rome.json"); + fs.insert(file_path.into(), CONFIG_ISSUE_3175_1.as_bytes()); + + let file_path = Path::new("file.js"); + fs.insert(file_path.into(), "import React from 'react';\n".as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + DynRef::Borrowed(&mut console), + Arguments::from_vec(vec![ + OsString::from("format"), + OsString::from("--quote-style"), + OsString::from("single"), + file_path.as_os_str().into(), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + let mut file = fs + .open(file_path) + .expect("formatting target file was removed by the CLI"); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("failed to read file from memory FS"); + + assert_eq!(content, "import React from 'react';\n"); + + drop(file); + assert_cli_snapshot( + module_path!(), + "applies_custom_configuration_over_config_file_issue_3175_v1", + fs, + console, + ); + } + + #[test] + fn applies_custom_configuration_over_config_file_issue_3175_v2() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let source = r#"function f() { + return 'hey'; +} +"#; + + let file_path = Path::new("rome.json"); + fs.insert(file_path.into(), CONFIG_ISSUE_3175_2.as_bytes()); + + let file_path = Path::new("file.js"); + fs.insert(file_path.into(), source.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + DynRef::Borrowed(&mut console), + Arguments::from_vec(vec![ + OsString::from("format"), + OsString::from("--indent-style"), + OsString::from("space"), + file_path.as_os_str().into(), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + let mut file = fs + .open(file_path) + .expect("formatting target file was removed by the CLI"); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("failed to read file from memory FS"); + + assert_eq!(content, source); + + drop(file); + assert_cli_snapshot( + module_path!(), + "applies_custom_configuration_over_config_file_issue_3175_v2", + fs, + console, + ); + } + #[test] fn applies_custom_quote_style() { let mut fs = MemoryFileSystem::default(); diff --git a/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v1.snap b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v1.snap new file mode 100644 index 00000000000..4819d371e9f --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v1.snap @@ -0,0 +1,26 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `rome.json` + +```json +{ + "formatter": { + "indentStyle": "space", + "indentSize": 2, + "lineWidth": 120 + } +} +``` + +## `file.js` + +```js +import React from 'react'; + +``` + +# Emitted Messages + + diff --git a/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v2.snap b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v2.snap new file mode 100644 index 00000000000..cdf17b21be4 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v2.snap @@ -0,0 +1,28 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `rome.json` + +```json +{ + "javascript": { + "formatter": { + "quoteStyle": "single" + } + } +} +``` + +## `file.js` + +```js +function f() { + return 'hey'; +} + +``` + +# Emitted Messages + + diff --git a/crates/rome_service/src/configuration/javascript.rs b/crates/rome_service/src/configuration/javascript.rs index 0fcecf0d344..de2e197fa17 100644 --- a/crates/rome_service/src/configuration/javascript.rs +++ b/crates/rome_service/src/configuration/javascript.rs @@ -23,6 +23,15 @@ pub struct JavascriptConfiguration { pub globals: Option>, } +impl JavascriptConfiguration { + pub fn with_formatter() -> Self { + Self { + formatter: Some(JavascriptFormatter::default()), + ..JavascriptConfiguration::default() + } + } +} + pub(crate) fn deserialize_globals<'de, D>( deserializer: D, ) -> Result>, D::Error> diff --git a/crates/rome_service/src/file_handlers/javascript.rs b/crates/rome_service/src/file_handlers/javascript.rs index 9301855d1c6..3cebb58a821 100644 --- a/crates/rome_service/src/file_handlers/javascript.rs +++ b/crates/rome_service/src/file_handlers/javascript.rs @@ -381,6 +381,7 @@ fn format( ) -> Result { let options = settings.format_options::(rome_path); + dbg!(&options); let tree = parse.syntax(); let formatted = format_node(options, &tree)?; let printed = formatted.print(); From 855609de55afdaf76ce2c3e8f2ac52f7a2a38b29 Mon Sep 17 00:00:00 2001 From: ematipico Date: Fri, 9 Sep 2022 11:35:16 +0100 Subject: [PATCH 2/3] chore: refactor testing --- crates/rome_cli/src/commands/format.rs | 77 +++----- .../src/file_handlers/javascript.rs | 1 - .../tests/__snapshots__/wasm.test.mjs.snap | 39 ---- npm/rome/tests/daemon.test.mjs | 173 ------------------ .../formatContent.test.mjs.snap} | 0 npm/rome/tests/daemon/formatContent.test.mjs | 135 ++++++++++++++ npm/rome/tests/daemon/formatFiles.test.mjs | 49 +++++ npm/rome/tests/wasm.test.mjs | 139 -------------- 8 files changed, 210 insertions(+), 403 deletions(-) delete mode 100644 npm/rome/tests/__snapshots__/wasm.test.mjs.snap delete mode 100644 npm/rome/tests/daemon.test.mjs rename npm/rome/tests/{__snapshots__/daemon.test.mjs.snap => daemon/__snapshots__/formatContent.test.mjs.snap} (100%) create mode 100644 npm/rome/tests/daemon/formatContent.test.mjs create mode 100644 npm/rome/tests/daemon/formatFiles.test.mjs delete mode 100644 npm/rome/tests/wasm.test.mjs diff --git a/crates/rome_cli/src/commands/format.rs b/crates/rome_cli/src/commands/format.rs index 3ba927359a8..03b4ecfb206 100644 --- a/crates/rome_cli/src/commands/format.rs +++ b/crates/rome_cli/src/commands/format.rs @@ -13,7 +13,6 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> { let configuration = load_config(&session.app.fs, None)?; let configuration = apply_format_settings_from_cli(&mut session, configuration)?; - dbg!(&configuration); session .app .workspace @@ -72,15 +71,11 @@ pub(crate) fn apply_format_settings_from_cli( let mut configuration = if let Some(configuration) = configuration { configuration } else { - Configuration { - formatter: Some(FormatterConfiguration::default()), - javascript: Some(JavascriptConfiguration { - formatter: Some(JavascriptFormatter::default()), - globals: None, - }), - ..Configuration::default() - } + Configuration::default() }; + let formatter = configuration + .formatter + .get_or_insert_with(FormatterConfiguration::default); let size = session .args @@ -106,29 +101,19 @@ pub(crate) fn apply_format_settings_from_cli( source, })?; - // if at least one argument is passed via CLI and no "formatter" configuration was passed - // via `rome.json`, we need to create it - if (line_width.is_some() | indent_style.is_some() | size.is_some()) - && configuration.formatter.is_none() - { - configuration.formatter = Some(FormatterConfiguration::default()); - } - - if let Some(formatter) = configuration.formatter.as_mut() { - match indent_style { - Some(IndentStyle::Tab) => { - formatter.indent_style = PlainIndentStyle::Tab; - } - Some(IndentStyle::Space(default_size)) => { - formatter.indent_style = PlainIndentStyle::Space; - formatter.indent_size = size.unwrap_or(default_size); - } - None => {} + match indent_style { + Some(IndentStyle::Tab) => { + formatter.indent_style = PlainIndentStyle::Tab; } - - if let Some(line_width) = line_width { - formatter.line_width = line_width; + Some(IndentStyle::Space(default_size)) => { + formatter.indent_style = PlainIndentStyle::Space; + formatter.indent_size = size.unwrap_or(default_size); } + None => {} + } + + if let Some(line_width) = line_width { + formatter.line_width = line_width; } let quote_properties = session @@ -147,29 +132,19 @@ pub(crate) fn apply_format_settings_from_cli( source, })?; - // if at least one argument is passed via CLI and no "javascript.formatter" configuration was passed - // via `rome.json`, we need to create it - if quote_style.is_some() | quote_properties.is_some() { - if configuration.javascript.is_none() { - configuration.javascript = Some(JavascriptConfiguration::with_formatter()) - } else if let Some(javascript) = configuration.javascript.as_mut() { - if javascript.formatter.is_none() { - javascript.formatter = Some(JavascriptFormatter::default()); - } - } - } - if let Some(javascript) = configuration + let javascript = configuration .javascript - .as_mut() - .and_then(|j| j.formatter.as_mut()) - { - if let Some(quote_properties) = quote_properties { - javascript.quote_properties = quote_properties; - } + .get_or_insert_with(JavascriptConfiguration::default); + let javascript_formatter = javascript + .formatter + .get_or_insert_with(JavascriptFormatter::default); - if let Some(quote_style) = quote_style { - javascript.quote_style = quote_style; - } + if let Some(quote_properties) = quote_properties { + javascript_formatter.quote_properties = quote_properties; + } + + if let Some(quote_style) = quote_style { + javascript_formatter.quote_style = quote_style; } Ok(configuration) diff --git a/crates/rome_service/src/file_handlers/javascript.rs b/crates/rome_service/src/file_handlers/javascript.rs index 3cebb58a821..9301855d1c6 100644 --- a/crates/rome_service/src/file_handlers/javascript.rs +++ b/crates/rome_service/src/file_handlers/javascript.rs @@ -381,7 +381,6 @@ fn format( ) -> Result { let options = settings.format_options::(rome_path); - dbg!(&options); let tree = parse.syntax(); let formatted = format_node(options, &tree)?; let printed = formatted.print(); diff --git a/npm/rome/tests/__snapshots__/wasm.test.mjs.snap b/npm/rome/tests/__snapshots__/wasm.test.mjs.snap deleted file mode 100644 index b8187942d45..00000000000 --- a/npm/rome/tests/__snapshots__/wasm.test.mjs.snap +++ /dev/null @@ -1,39 +0,0 @@ -// Vitest Snapshot v1 - -exports[`Rome WebAssembly formatter > should not format and have diagnostics > syntax error 1`] = ` -[ - { - "children": [], - "code": "SyntaxError", - "code_link": null, - "file_id": 0, - "footers": [], - "primary": { - "msg": [ - { - "content": "", - "elements": [], - }, - ], - "severity": "Error", - "span": { - "file": 0, - "range": [ - 11, - 12, - ], - }, - }, - "severity": "Error", - "suggestions": [], - "summary": null, - "tag": null, - "title": [ - { - "content": "expected a name for the function in a function declaration, but found none", - "elements": [], - }, - ], - }, -] -`; diff --git a/npm/rome/tests/daemon.test.mjs b/npm/rome/tests/daemon.test.mjs deleted file mode 100644 index 787bfca123e..00000000000 --- a/npm/rome/tests/daemon.test.mjs +++ /dev/null @@ -1,173 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { BackendKind, Rome } from "../dist"; -import { resolve } from "path"; -import { fileURLToPath } from "url"; - -const target = process.env.CI ? "target/release/rome" : "target/debug/rome"; - -describe("Rome Deamon formatter", () => { - it("should not format files", async () => { - const pathToBinary = resolve( - fileURLToPath(import.meta.url), - "../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary, - }); - - let result = await rome.formatFiles(["./path/to/file.js"]); - - expect(result.content).toEqual(""); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format files in debug mode", async () => { - const pathToBinary = resolve( - fileURLToPath(import.meta.url), - "../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary, - }); - - let result = await rome.formatFiles(["./path/to/file.js"], { - debug: true, - }); - - expect(result.content).toEqual(""); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toEqual(""); - }); - - it("should format content", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - let result = await rome.formatContent("function f () { }", { - filePath: "example.js", - }); - - expect(result.content).toEqual("function f() {}\n"); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format and have diagnostics", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - - let content = "function () { }"; - let result = await rome.formatContent(content, { - filePath: "example.js", - }); - - expect(result.content).toEqual(content); - expect(result.diagnostics).toHaveLength(1); - expect(result.diagnostics[0].title[0].content).toContain( - "expected a name for the function in a function declaration, but found none", - ); - expect(result.diagnostics).toMatchSnapshot("syntax error"); - }); - - it("should format content in debug mode", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - - let result = await rome.formatContent("function f() {}", { - filePath: "example.js", - debug: true, - }); - - expect(result.content).toEqual("function f() {}\n"); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toMatchInlineSnapshot( - '"[\\"function\\", \\" \\", \\"f\\", group([\\"(\\", \\")\\"]), \\" \\", \\"{\\", \\"}\\", hard_line_break]"', - ); - }); - - it("should not format content with range", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - let result = await rome.formatContent("let a ; function g () { }", { - filePath: "file.js", - range: [20, 25], - }); - - expect(result.content).toEqual("function g() {}"); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format content with range in debug mode", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - let result = await rome.formatContent("let a ; function g () { }", { - filePath: "file.js", - range: [20, 25], - debug: true, - }); - - expect(result.content).toEqual("function g() {}"); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toMatchInlineSnapshot( - ` - "[ - group([\\"let\\", \\" \\", \\"a\\"]), - \\";\\", - hard_line_break, - \\"function\\", - \\" \\", - \\"g\\", - group([\\"(\\", \\")\\"]), - \\" \\", - \\"{\\", - \\"}\\", - hard_line_break - ]" - `, - ); - }); -}); diff --git a/npm/rome/tests/__snapshots__/daemon.test.mjs.snap b/npm/rome/tests/daemon/__snapshots__/formatContent.test.mjs.snap similarity index 100% rename from npm/rome/tests/__snapshots__/daemon.test.mjs.snap rename to npm/rome/tests/daemon/__snapshots__/formatContent.test.mjs.snap diff --git a/npm/rome/tests/daemon/formatContent.test.mjs b/npm/rome/tests/daemon/formatContent.test.mjs new file mode 100644 index 00000000000..83d2c3df045 --- /dev/null +++ b/npm/rome/tests/daemon/formatContent.test.mjs @@ -0,0 +1,135 @@ +import { describe, expect, it } from "vitest"; +import { BackendKind, Rome } from "../../dist"; +import { resolve } from "path"; +import { fileURLToPath } from "url"; + +const target = process.env.CI ? "target/release/rome" : "target/debug/rome"; + +describe("Rome Deamon formatter", () => { + + it("should format content", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + let result = await rome.formatContent("function f () { }", { + filePath: "example.js", + }); + + expect(result.content).toEqual("function f() {}\n"); + expect(result.diagnostics).toEqual([]); + }); + + it("should not format and have diagnostics", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + + let content = "function () { }"; + let result = await rome.formatContent(content, { + filePath: "example.js", + }); + + expect(result.content).toEqual(content); + expect(result.diagnostics).toHaveLength(1); + expect(result.diagnostics[0].title[0].content).toContain( + "expected a name for the function in a function declaration, but found none", + ); + expect(result.diagnostics).toMatchSnapshot("syntax error"); + }); + + it("should format content in debug mode", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + + let result = await rome.formatContent("function f() {}", { + filePath: "example.js", + debug: true, + }); + + expect(result.content).toEqual("function f() {}\n"); + expect(result.diagnostics).toEqual([]); + expect(result.ir).toMatchInlineSnapshot( + '"[\\"function\\", \\" \\", \\"f\\", group([\\"(\\", \\")\\"]), \\" \\", \\"{\\", \\"}\\", hard_line_break]"', + ); + }); + + it("should not format content with range", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + let result = await rome.formatContent("let a ; function g () { }", { + filePath: "file.js", + range: [20, 25], + }); + + expect(result.content).toEqual("function g() {}"); + expect(result.diagnostics).toEqual([]); + }); + + it("should not format content with range in debug mode", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + let result = await rome.formatContent("let a ; function g () { }", { + filePath: "file.js", + range: [20, 25], + debug: true, + }); + + expect(result.content).toEqual("function g() {}"); + expect(result.diagnostics).toEqual([]); + expect(result.ir).toMatchInlineSnapshot( + ` + "[ + group([\\"let\\", \\" \\", \\"a\\"]), + \\";\\", + hard_line_break, + \\"function\\", + \\" \\", + \\"g\\", + group([\\"(\\", \\")\\"]), + \\" \\", + \\"{\\", + \\"}\\", + hard_line_break + ]" + `, + ); + }); +}); diff --git a/npm/rome/tests/daemon/formatFiles.test.mjs b/npm/rome/tests/daemon/formatFiles.test.mjs new file mode 100644 index 00000000000..ca10b3282de --- /dev/null +++ b/npm/rome/tests/daemon/formatFiles.test.mjs @@ -0,0 +1,49 @@ +import { describe, expect, it } from "vitest"; +import { BackendKind, Rome } from "../../dist"; +import { resolve } from "path"; +import { fileURLToPath } from "url"; + +const target = process.env.CI ? "target/release/rome" : "target/debug/rome"; + +describe("Rome Deamon formatter", () => { + it("should not format files", async () => { + const pathToBinary = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + console.log(pathToBinary); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary, + }); + + let result = await rome.formatFiles(["./path/to/file.js"]); + + expect(result.content).toEqual(""); + expect(result.diagnostics).toEqual([]); + }); + + it("should not format files in debug mode", async () => { + const pathToBinary = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary, + }); + + let result = await rome.formatFiles(["./path/to/file.js"], { + debug: true, + }); + + expect(result.content).toEqual(""); + expect(result.diagnostics).toEqual([]); + expect(result.ir).toEqual(""); + }); +}); diff --git a/npm/rome/tests/wasm.test.mjs b/npm/rome/tests/wasm.test.mjs deleted file mode 100644 index 73308e16d6e..00000000000 --- a/npm/rome/tests/wasm.test.mjs +++ /dev/null @@ -1,139 +0,0 @@ -import { describe, it, expect } from "vitest"; -import { BackendKind, Rome } from "../dist"; - -describe("Rome WebAssembly formatter", () => { - it("should not format files", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let result = await rome.formatFiles(["./path/to/file.js"]); - - expect(result.content).toEqual(""); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format files in debug mode", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let result = await rome.formatFiles(["./path/to/file.js"], { - debug: true, - }); - - expect(result.content).toEqual(""); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toEqual(""); - }); - - it("should format content", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let result = await rome.formatContent("function f () { }", { - filePath: "example.js", - }); - - expect(result.content).toEqual("function f() {}\n"); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format and have diagnostics", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let content = "function () { }"; - let result = await rome.formatContent(content, { - filePath: "example.js", - }); - - expect(result.content).toEqual(content); - expect(result.diagnostics).toHaveLength(1); - expect(result.diagnostics[0].title[0].content).toContain( - "expected a name for the function in a function declaration, but found none", - ); - expect(result.diagnostics).toMatchSnapshot("syntax error"); - }); - - it("should format content in debug mode", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let result = await rome.formatContent("function f() {}", { - filePath: "example.js", - debug: true, - }); - - expect(result.content).toEqual("function f() {}\n"); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toMatchInlineSnapshot( - '"[\\"function\\", \\" \\", \\"f\\", group([\\"(\\", \\")\\"]), \\" \\", \\"{\\", \\"}\\", hard_line_break]"', - ); - }); - - it("should not format content with range", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let result = await rome.formatContent("let a ; function g () { }", { - filePath: "file.js", - range: [20, 25], - }); - - expect(result.content).toEqual("function g() {}"); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format content with range in debug mode", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let result = await rome.formatContent("let a ; function g () { }", { - filePath: "file.js", - range: [20, 25], - debug: true, - }); - - expect(result.content).toEqual("function g() {}"); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toMatchInlineSnapshot( - ` - "[ - group([\\"let\\", \\" \\", \\"a\\"]), - \\";\\", - hard_line_break, - \\"function\\", - \\" \\", - \\"g\\", - group([\\"(\\", \\")\\"]), - \\" \\", - \\"{\\", - \\"}\\", - hard_line_break - ]" - `, - ); - }); -}); - -describe("Rome parser", () => { - it("should not parse content", async () => { - const rome = await Rome.create({ - backendKind: BackendKind.NODE, - }); - - let result = await rome.parseContent("function f() {}", { - filePath: "example.js", - }); - - expect(result.ast).toEqual(""); - expect(result.cst).toEqual(""); - expect(result.diagnostics).toEqual([]); - }); -}); From 54488e313d7000a29ffaa79fbd5a54a7f0bb61e0 Mon Sep 17 00:00:00 2001 From: ematipico Date: Fri, 9 Sep 2022 11:42:49 +0100 Subject: [PATCH 3/3] chore: add timeout --- npm/rome/tests/daemon/formatContent.test.mjs | 225 +++++++++---------- npm/rome/tests/daemon/formatFiles.test.mjs | 80 +++---- 2 files changed, 152 insertions(+), 153 deletions(-) diff --git a/npm/rome/tests/daemon/formatContent.test.mjs b/npm/rome/tests/daemon/formatContent.test.mjs index 83d2c3df045..8e27eab2ca8 100644 --- a/npm/rome/tests/daemon/formatContent.test.mjs +++ b/npm/rome/tests/daemon/formatContent.test.mjs @@ -6,116 +6,115 @@ import { fileURLToPath } from "url"; const target = process.env.CI ? "target/release/rome" : "target/debug/rome"; describe("Rome Deamon formatter", () => { - - it("should format content", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - let result = await rome.formatContent("function f () { }", { - filePath: "example.js", - }); - - expect(result.content).toEqual("function f() {}\n"); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format and have diagnostics", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - - let content = "function () { }"; - let result = await rome.formatContent(content, { - filePath: "example.js", - }); - - expect(result.content).toEqual(content); - expect(result.diagnostics).toHaveLength(1); - expect(result.diagnostics[0].title[0].content).toContain( - "expected a name for the function in a function declaration, but found none", - ); - expect(result.diagnostics).toMatchSnapshot("syntax error"); - }); - - it("should format content in debug mode", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - - let result = await rome.formatContent("function f() {}", { - filePath: "example.js", - debug: true, - }); - - expect(result.content).toEqual("function f() {}\n"); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toMatchInlineSnapshot( - '"[\\"function\\", \\" \\", \\"f\\", group([\\"(\\", \\")\\"]), \\" \\", \\"{\\", \\"}\\", hard_line_break]"', - ); - }); - - it("should not format content with range", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - let result = await rome.formatContent("let a ; function g () { }", { - filePath: "file.js", - range: [20, 25], - }); - - expect(result.content).toEqual("function g() {}"); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format content with range in debug mode", async () => { - const command = resolve( - fileURLToPath(import.meta.url), - "../../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary: command, - }); - let result = await rome.formatContent("let a ; function g () { }", { - filePath: "file.js", - range: [20, 25], - debug: true, - }); - - expect(result.content).toEqual("function g() {}"); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toMatchInlineSnapshot( - ` + it("should format content", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + let result = await rome.formatContent("function f () { }", { + filePath: "example.js", + }); + + expect(result.content).toEqual("function f() {}\n"); + expect(result.diagnostics).toEqual([]); + }); + + it("should not format and have diagnostics", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + + let content = "function () { }"; + let result = await rome.formatContent(content, { + filePath: "example.js", + }); + + expect(result.content).toEqual(content); + expect(result.diagnostics).toHaveLength(1); + expect(result.diagnostics[0].title[0].content).toContain( + "expected a name for the function in a function declaration, but found none", + ); + expect(result.diagnostics).toMatchSnapshot("syntax error"); + }); + + it("should format content in debug mode", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + + let result = await rome.formatContent("function f() {}", { + filePath: "example.js", + debug: true, + }); + + expect(result.content).toEqual("function f() {}\n"); + expect(result.diagnostics).toEqual([]); + expect(result.ir).toMatchInlineSnapshot( + '"[\\"function\\", \\" \\", \\"f\\", group([\\"(\\", \\")\\"]), \\" \\", \\"{\\", \\"}\\", hard_line_break]"', + ); + }); + + it("should not format content with range", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + let result = await rome.formatContent("let a ; function g () { }", { + filePath: "file.js", + range: [20, 25], + }); + + expect(result.content).toEqual("function g() {}"); + expect(result.diagnostics).toEqual([]); + }); + + it("should not format content with range in debug mode", async () => { + const command = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary: command, + }); + let result = await rome.formatContent("let a ; function g () { }", { + filePath: "file.js", + range: [20, 25], + debug: true, + }); + + expect(result.content).toEqual("function g() {}"); + expect(result.diagnostics).toEqual([]); + expect(result.ir).toMatchInlineSnapshot( + ` "[ group([\\"let\\", \\" \\", \\"a\\"]), \\";\\", @@ -130,6 +129,6 @@ describe("Rome Deamon formatter", () => { hard_line_break ]" `, - ); - }); -}); + ); + }); +}, 1500); diff --git a/npm/rome/tests/daemon/formatFiles.test.mjs b/npm/rome/tests/daemon/formatFiles.test.mjs index ca10b3282de..d55376807ff 100644 --- a/npm/rome/tests/daemon/formatFiles.test.mjs +++ b/npm/rome/tests/daemon/formatFiles.test.mjs @@ -6,44 +6,44 @@ import { fileURLToPath } from "url"; const target = process.env.CI ? "target/release/rome" : "target/debug/rome"; describe("Rome Deamon formatter", () => { - it("should not format files", async () => { - const pathToBinary = resolve( - fileURLToPath(import.meta.url), - "../../../../..", - target, - ); - - console.log(pathToBinary); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary, - }); - - let result = await rome.formatFiles(["./path/to/file.js"]); - - expect(result.content).toEqual(""); - expect(result.diagnostics).toEqual([]); - }); - - it("should not format files in debug mode", async () => { - const pathToBinary = resolve( - fileURLToPath(import.meta.url), - "../../../../..", - target, - ); - - const rome = await Rome.create({ - backendKind: BackendKind.DAEMON, - pathToBinary, - }); - - let result = await rome.formatFiles(["./path/to/file.js"], { - debug: true, - }); - - expect(result.content).toEqual(""); - expect(result.diagnostics).toEqual([]); - expect(result.ir).toEqual(""); - }); + it("should not format files", async () => { + const pathToBinary = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + console.log(pathToBinary); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary, + }); + + let result = await rome.formatFiles(["./path/to/file.js"]); + + expect(result.content).toEqual(""); + expect(result.diagnostics).toEqual([]); + }); + + it("should not format files in debug mode", async () => { + const pathToBinary = resolve( + fileURLToPath(import.meta.url), + "../../../../..", + target, + ); + + const rome = await Rome.create({ + backendKind: BackendKind.DAEMON, + pathToBinary, + }); + + let result = await rome.formatFiles(["./path/to/file.js"], { + debug: true, + }); + + expect(result.content).toEqual(""); + expect(result.diagnostics).toEqual([]); + expect(result.ir).toEqual(""); + }); });