This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 653
test(rome_cli): add snapshots for file system and console session #3035
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,115 @@ | ||
use rome_console::fmt::{Formatter, Termcolor}; | ||
use rome_console::{markup, BufferConsole, Markup}; | ||
use rome_diagnostics::termcolor::NoColor; | ||
use rome_fs::{FileSystemExt, MemoryFileSystem}; | ||
use std::collections::HashMap; | ||
use std::fmt::Write as _; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Default)] | ||
struct CliSnapshot { | ||
/// the configuration, if set | ||
pub configuration: Option<String>, | ||
/// file name -> content | ||
pub files: HashMap<String, String>, | ||
/// messages written in console | ||
pub messages: Vec<String>, | ||
} | ||
|
||
impl CliSnapshot { | ||
fn emit_content_snapshot(&self) -> String { | ||
let mut content = String::new(); | ||
|
||
if let Some(configuration) = &self.configuration { | ||
content.push_str("## `rome.json`\n\n"); | ||
content.push_str("```json"); | ||
content.push('\n'); | ||
content.push_str(configuration); | ||
content.push('\n'); | ||
content.push_str("```"); | ||
content.push_str("\n\n") | ||
} | ||
|
||
for (name, file_content) in &self.files { | ||
if !name.starts_with("rome.json") { | ||
let extension = name.split('.').last().unwrap(); | ||
|
||
let _ = write!(content, "## `{name}`\n\n"); | ||
let _ = write!(content, "```{extension}"); | ||
content.push('\n'); | ||
content.push_str(&*file_content); | ||
content.push('\n'); | ||
content.push_str("```"); | ||
content.push_str("\n\n") | ||
} | ||
} | ||
|
||
if !self.messages.is_empty() { | ||
content.push_str("## Messages\n\n"); | ||
|
||
for message in &self.messages { | ||
let message_content = &*message; | ||
// There are some logs that print the timing, and we can't snapshot that message | ||
// otherwise at each run we invalid the previous snapshot. | ||
// | ||
// This is a workaround, and it might not work for all cases. | ||
if !message_content.contains("files") { | ||
content.push_str("```block"); | ||
content.push('\n'); | ||
content.push_str(message_content); | ||
content.push('\n'); | ||
content.push_str("```"); | ||
content.push_str("\n\n") | ||
} | ||
} | ||
} | ||
|
||
content | ||
} | ||
} | ||
|
||
fn markup_to_string(markup: Markup) -> String { | ||
let mut buffer = Vec::new(); | ||
let mut write = Termcolor(NoColor::new(&mut buffer)); | ||
let mut fmt = Formatter::new(&mut write); | ||
fmt.write_markup(markup).unwrap(); | ||
|
||
String::from_utf8(buffer).unwrap() | ||
} | ||
|
||
/// Function used to snapshot a session test of the a CLI run. | ||
pub fn assert_cli_snapshot(test_name: &str, fs: MemoryFileSystem, console: BufferConsole) { | ||
let mut cli_snapshot = CliSnapshot::default(); | ||
let config_path = PathBuf::from("rome.json"); | ||
let configuration = fs.read(&config_path).ok(); | ||
if let Some(mut configuration) = configuration { | ||
let mut buffer = String::new(); | ||
if configuration.read_to_string(&mut buffer).is_ok() { | ||
cli_snapshot.configuration = Some(buffer); | ||
} | ||
} | ||
|
||
for (file, entry) in fs.files() { | ||
let content = entry.lock(); | ||
let content = std::str::from_utf8(content.as_slice()).unwrap(); | ||
|
||
cli_snapshot | ||
.files | ||
.insert(file.to_str().unwrap().to_string(), String::from(content)); | ||
} | ||
|
||
for message in console.buffer { | ||
let content = markup_to_string(markup! { | ||
{message.content} | ||
}); | ||
cli_snapshot.messages.push(content) | ||
} | ||
|
||
let content = cli_snapshot.emit_content_snapshot(); | ||
|
||
insta::with_settings!({ | ||
prepend_module_to_snapshot => false, | ||
}, { | ||
insta::assert_snapshot!(test_name, content); | ||
}); | ||
} |
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,22 @@ | ||
--- | ||
source: crates/rome_cli/tests/snap_test.rs | ||
assertion_line: 113 | ||
expression: content | ||
--- | ||
## `fix.js` | ||
|
||
```js | ||
|
||
if(a != 0) {} | ||
|
||
``` | ||
|
||
## Messages | ||
|
||
```block | ||
Skipped 1 suggested fixes. | ||
If you wish to apply the suggested fixes, use the command rome check --apply-suggested | ||
|
||
``` | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: wrap the messages in a code block to have them rendered in a
<pre>
block in markdown previews and respect the line breaks