From 0d85d6f75ac569f2037a6193e80ba7763a130e17 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 12 Jan 2024 11:33:32 +0000 Subject: [PATCH] fix(cli): return the original content when a protected file is encountered (#1542) --- CHANGELOG.md | 1 + crates/biome_cli/src/execute/std_in.rs | 14 +++++-- .../biome_cli/tests/cases/protected_files.rs | 40 ++++++++++++++++++- .../not_process_file_from_stdin_format.snap | 6 +-- .../not_process_file_from_stdin_lint.snap | 6 +-- ...rocess_file_from_stdin_verbose_format.snap | 4 ++ ..._process_file_from_stdin_verbose_lint.snap | 4 ++ ..._content_of_protected_files_via_stdin.snap | 17 ++++++++ .../src/content/docs/internals/changelog.mdx | 1 + 9 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 crates/biome_cli/tests/snapshots/main_cases_protected_files/should_return_the_content_of_protected_files_via_stdin.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 348d64aca7ae..5bfd35833300 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom - Fix [#1512](https://github.com/biomejs/biome/issues/1512) by skipping verbose diagnostics from the count. Contributed by @ematipico - Don't handle CSS files, the formatter isn't ready yet. Contributed by @ematipico - The file `biome.json` can't be ignored anymore. Contributed by @ematipico +- Fix [#1541](https://github.com/biomejs/biome/issues/1541) where the content of protected files wasn't returned to `stdout`. Contributed by @ematipico ### Configuration diff --git a/crates/biome_cli/src/execute/std_in.rs b/crates/biome_cli/src/execute/std_in.rs index 22ba82361223..3eee44968d74 100644 --- a/crates/biome_cli/src/execute/std_in.rs +++ b/crates/biome_cli/src/execute/std_in.rs @@ -34,11 +34,14 @@ pub(crate) fn run<'a>( if file_features.is_protected() { let protected_diagnostic = WorkspaceError::protected_file(rome_path.display().to_string()); - if protected_diagnostic.tags().is_verbose() && verbose { - console.error(markup! {{PrintDiagnostic::verbose(&protected_diagnostic)}}) + if protected_diagnostic.tags().is_verbose() { + if verbose { + console.error(markup! {{PrintDiagnostic::verbose(&protected_diagnostic)}}) + } } else { console.error(markup! {{PrintDiagnostic::simple(&protected_diagnostic)}}) } + console.append(markup! {{content}}); return Ok(()); }; if file_features.supports_for(&FeatureName::Format) { @@ -84,11 +87,14 @@ pub(crate) fn run<'a>( if file_features.is_protected() { let protected_diagnostic = WorkspaceError::protected_file(rome_path.display().to_string()); - if protected_diagnostic.tags().is_verbose() && verbose { - console.error(markup! {{PrintDiagnostic::verbose(&protected_diagnostic)}}) + if protected_diagnostic.tags().is_verbose() { + if verbose { + console.error(markup! {{PrintDiagnostic::verbose(&protected_diagnostic)}}) + } } else { console.error(markup! {{PrintDiagnostic::simple(&protected_diagnostic)}}) } + console.append(markup! {{content}}); return Ok(()); }; diff --git a/crates/biome_cli/tests/cases/protected_files.rs b/crates/biome_cli/tests/cases/protected_files.rs index 70ea18f06437..01bebf9dc4dd 100644 --- a/crates/biome_cli/tests/cases/protected_files.rs +++ b/crates/biome_cli/tests/cases/protected_files.rs @@ -1,6 +1,6 @@ use crate::run_cli; -use crate::snap_test::{assert_cli_snapshot, SnapshotPayload}; -use biome_console::BufferConsole; +use crate::snap_test::{assert_cli_snapshot, markup_to_string, SnapshotPayload}; +use biome_console::{markup, BufferConsole}; use biome_fs::MemoryFileSystem; use biome_service::DynRef; use bpaf::Args; @@ -157,3 +157,39 @@ fn not_process_file_from_cli_verbose() { result, )); } + +#[test] +fn should_return_the_content_of_protected_files_via_stdin() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + console + .in_buffer + .push(r#"{ "name": "something" }"#.to_string()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from([("format"), ("--stdin-file-path"), ("package.json")].as_slice()), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + let message = console + .out_buffer + .first() + .expect("Console should have written a message"); + + let content = markup_to_string(markup! { + {message.content} + }); + + assert_eq!(content, r#"{ "name": "something" }"#); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "should_return_the_content_of_protected_files_via_stdin", + fs, + console, + result, + )); +} diff --git a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_format.snap b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_format.snap index c21b2dfe0113..efe739098c67 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_format.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_format.snap @@ -11,11 +11,7 @@ expression: content # Emitted Messages ```block -package.json project VERBOSE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - i The file package.json is protected because is handled by another tool. Biome won't process it. - - +{ "name": "test" } ``` diff --git a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_lint.snap b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_lint.snap index c21b2dfe0113..efe739098c67 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_lint.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_lint.snap @@ -11,11 +11,7 @@ expression: content # Emitted Messages ```block -package.json project VERBOSE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - i The file package.json is protected because is handled by another tool. Biome won't process it. - - +{ "name": "test" } ``` diff --git a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_format.snap b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_format.snap index e376ab28faf4..ca7e2a7a8e7a 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_format.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_format.snap @@ -22,4 +22,8 @@ package.json project VERBOSE ━━━━━━━━━━━━━━━━ ``` +```block +{ "name": "test" } +``` + diff --git a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_lint.snap b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_lint.snap index e376ab28faf4..ca7e2a7a8e7a 100644 --- a/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_lint.snap +++ b/crates/biome_cli/tests/snapshots/main_cases_protected_files/not_process_file_from_stdin_verbose_lint.snap @@ -22,4 +22,8 @@ package.json project VERBOSE ━━━━━━━━━━━━━━━━ ``` +```block +{ "name": "test" } +``` + diff --git a/crates/biome_cli/tests/snapshots/main_cases_protected_files/should_return_the_content_of_protected_files_via_stdin.snap b/crates/biome_cli/tests/snapshots/main_cases_protected_files/should_return_the_content_of_protected_files_via_stdin.snap new file mode 100644 index 000000000000..7b4b4adff262 --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_cases_protected_files/should_return_the_content_of_protected_files_via_stdin.snap @@ -0,0 +1,17 @@ +--- +source: crates/biome_cli/tests/snap_test.rs +expression: content +--- +# Input messages + +```block +{ "name": "something" } +``` + +# Emitted Messages + +```block +{ "name": "something" } +``` + + diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index 729bddb5ee72..ca96dc34bc2c 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -23,6 +23,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom - Fix [#1512](https://github.com/biomejs/biome/issues/1512) by skipping verbose diagnostics from the count. Contributed by @ematipico - Don't handle CSS files, the formatter isn't ready yet. Contributed by @ematipico - The file `biome.json` can't be ignored anymore. Contributed by @ematipico +- Fix [#1541](https://github.com/biomejs/biome/issues/1541) where the content of protected files wasn't returned to `stdout`. Contributed by @ematipico ### Configuration