Skip to content
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

refactor: small test infrastructure #672

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions crates/biome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,27 +389,8 @@ function f() {\n\targuments;\n}

assert!(result.is_err(), "run_cli returned {result:?}");

let mut file = fs
.open(test1)
.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, expected);
drop(file);

content.clear();

let mut file = fs
.open(test2)
.expect("formatting target file was removed by the CLI");

file.read_to_string(&mut content)
.expect("failed to read file from memory FS");

drop(file);
assert_file_contents(&fs, test1, expected);
assert_file_contents(&fs, test2, expected);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand Down
19 changes: 10 additions & 9 deletions crates/biome_cli/tests/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
UNFORMATTED,
};
use biome_console::{BufferConsole, MarkupBuf};
use biome_fs::{FileSystemExt, MemoryFileSystem};
use biome_fs::MemoryFileSystem;
use biome_service::DynRef;
use bpaf::Args;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -677,6 +677,14 @@ import { B, C } from "b.js"
import A from "a.js"


something( )
"#;

let expect = r#"
import { B, C } from "b.js"
import A from "a.js"


something( )
"#;

Expand All @@ -694,15 +702,8 @@ something( )

assert!(result.is_err(), "run_cli returned {result:?}");

let mut file = fs
.open(file_path)
.expect("ci 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_file_contents(&fs, file_path, expect);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you went beyond the task; fantastic work! Not sure why the old code is like this... we completely forgot to do the content assertion!


drop(file);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"ci_formatter_linter_organize_imports",
Expand Down
25 changes: 4 additions & 21 deletions crates/biome_cli/tests/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::configs::{CONFIG_INIT_DEFAULT, CONFIG_INIT_DEFAULT_WHEN_INSTALLED};
use crate::run_cli;
use crate::snap_test::{assert_cli_snapshot, SnapshotPayload};
use crate::snap_test::{assert_cli_snapshot, assert_file_contents, SnapshotPayload};
use biome_console::BufferConsole;
use biome_fs::{FileSystemExt, MemoryFileSystem};
use biome_fs::MemoryFileSystem;
use biome_json_formatter::context::JsonFormatOptions;
use biome_json_parser::{parse_json, JsonParserOptions};
use biome_service::DynRef;
Expand Down Expand Up @@ -44,23 +44,14 @@ fn creates_config_file() {
assert!(result.is_ok(), "run_cli returned {result:?}");

let file_path = Path::new("biome.json");

let mut file = fs
.open(file_path)
.expect("configuration file was not written on disk");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
let parsed = parse_json(CONFIG_INIT_DEFAULT, JsonParserOptions::default());
let formatted =
biome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax())
.expect("valid format document")
.print()
.expect("valid format document");
assert_eq!(content, formatted.as_code());

drop(file);
assert_file_contents(&fs, file_path, formatted.as_code());

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand Down Expand Up @@ -88,13 +79,6 @@ fn creates_config_file_when_biome_installed_via_package_manager() {

let file_path = Path::new("biome.json");

let mut file = fs
.open(file_path)
.expect("configuration file was not written on disk");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
let parsed = parse_json(
CONFIG_INIT_DEFAULT_WHEN_INSTALLED,
JsonParserOptions::default(),
Expand All @@ -104,9 +88,8 @@ fn creates_config_file_when_biome_installed_via_package_manager() {
.expect("valid format document")
.print()
.expect("valid format document");
assert_eq!(content, formatted.as_code());

drop(file);
assert_file_contents(&fs, file_path, formatted.as_code());

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand Down
25 changes: 3 additions & 22 deletions crates/biome_cli/tests/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::configs::{
CONFIG_LINTER_SUPPRESSED_GROUP, CONFIG_LINTER_SUPPRESSED_RULE,
CONFIG_LINTER_UPGRADE_DIAGNOSTIC, CONFIG_RECOMMENDED_GROUP,
};
use crate::snap_test::{markup_to_string, SnapshotPayload};
use crate::snap_test::{assert_file_contents, markup_to_string, SnapshotPayload};
use crate::{assert_cli_snapshot, run_cli, FORMATTED, LINT_ERROR, PARSE_ERROR};
use biome_console::{markup, BufferConsole, LogLevel, MarkupBuf};
use biome_fs::{ErrorEntry, FileSystemExt, MemoryFileSystem, OsFileSystem};
Expand Down Expand Up @@ -384,27 +384,8 @@ function f() { arguments; }

assert!(result.is_err(), "run_cli returned {result:?}");

let mut file = fs
.open(test1)
.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, expected);
drop(file);

content.clear();

let mut file = fs
.open(test2)
.expect("formatting target file was removed by the CLI");

file.read_to_string(&mut content)
.expect("failed to read file from memory FS");

drop(file);
assert_file_contents(&fs, test1, expected);
assert_file_contents(&fs, test2, expected);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand Down