Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
130 changes: 76 additions & 54 deletions apps/oxfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tester::Tester;
fn single_file() {
// Test different flags on the same file
Tester::new().with_cwd(PathBuf::from("tests/fixtures/single_file")).test_and_snapshot_multiple(
"single_file",
&[&["--check", "simple.js"], &["--list-different", "simple.js"]],
);
}
Expand All @@ -17,31 +18,37 @@ fn multiple_files() {
// Test different ways to specify multiple files
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/multiple_files"))
.test_and_snapshot_multiple(&[
// Explicit file list
&["--check", "simple.js", "arrow.js"],
// Default to current directory
&["--check"],
// Explicit cwd
&["--check", "."],
&["--check", "./"],
]);
.test_and_snapshot_multiple(
"multiple_files",
&[
// Explicit file list
&["--check", "simple.js", "arrow.js"],
// Default to current directory
&["--check"],
// Explicit cwd
&["--check", "."],
&["--check", "./"],
],
);
}

#[test]
fn no_error_on_unmatched_pattern() {
// Test both with and without --no-error-on-unmatched-pattern flag
Tester::new().test_and_snapshot_multiple(&[
&["--check", "--no-error-on-unmatched-pattern", "__non__existent__file.js"],
&["--check", "__non__existent__file.js"],
]);
Tester::new().test_and_snapshot_multiple(
"no_error_on_unmatched_pattern",
&[
&["--check", "--no-error-on-unmatched-pattern", "__non__existent__file.js"],
&["--check", "__non__existent__file.js"],
],
);
}

#[test]
fn supported_extensions() {
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/extensions"))
.test_and_snapshot_multiple(&[&["--check"]]);
.test_and_snapshot_multiple("supported_extensions", &[&["--check"]]);
}

#[test]
Expand All @@ -55,20 +62,21 @@ fn write_mode() {
fn config_file_auto_discovery() {
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/config_file"))
.test_and_snapshot_multiple(&[&["--check"]]);
.test_and_snapshot_multiple("config_file_auto_discovery", &[&["--check"]]);

Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/config_file/nested"))
.test_and_snapshot_multiple(&[&["--check"]]);
.test_and_snapshot_multiple("config_file_auto_discovery_nested", &[&["--check"]]);

Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/config_file/nested/deep"))
.test_and_snapshot_multiple(&[&["--check"]]);
.test_and_snapshot_multiple("config_file_auto_discovery_nested_deep", &[&["--check"]]);
}

#[test]
fn config_file_explicit() {
Tester::new().with_cwd(PathBuf::from("tests/fixtures/config_file")).test_and_snapshot_multiple(
"config_file_explicit",
&[
&["--check", "--config", "./fmt.json"],
&["--check", "--config", "./fmt.jsonc"],
Expand All @@ -83,7 +91,7 @@ fn vcs_dirs_ignored() {
// but regular directories and root files are processed
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/vcs_dirs"))
.test_and_snapshot_multiple(&[&["--check"]]);
.test_and_snapshot_multiple("vcs_dirs_ignored", &[&["--check"]]);
}

#[test]
Expand All @@ -92,7 +100,10 @@ fn node_modules_ignored() {
// but can be included with `--with-node-modules` flag
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/node_modules_dirs"))
.test_and_snapshot_multiple(&[&["--check"], &["--check", "--with-node-modules"]]);
.test_and_snapshot_multiple(
"node_modules_ignored",
&[&["--check"], &["--check", "--with-node-modules"]],
);
}

#[test]
Expand All @@ -102,38 +113,46 @@ fn exclude_nested_paths() {
// All these cases should not report parse error from `foo/bar/error.js`
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
.test_and_snapshot_multiple(&[
&["--check", "!foo/bar/error.js"],
&["--check", "!foo/bar"],
&["--check", "!foo"],
&["--check", "!**/error.js"],
&["--check", "foo", "!foo/bar/error.js"],
&["--check", "foo", "!foo/bar"],
&["--check", "foo", "!**/bar/error.js"],
&["--check", "foo", "!**/bar/*"],
]);
.test_and_snapshot_multiple(
"exclude_nested_paths",
&[
&["--check", "!foo/bar/error.js"],
&["--check", "!foo/bar"],
&["--check", "!foo"],
&["--check", "!**/error.js"],
&["--check", "foo", "!foo/bar/error.js"],
&["--check", "foo", "!foo/bar"],
&["--check", "foo", "!**/bar/error.js"],
&["--check", "foo", "!**/bar/*"],
],
);
}
#[test]
fn exclude_nested_paths_with_dot() {
// All these cases should not report parse error from `foo/bar/error.js`
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
.test_and_snapshot_multiple(&[
&["--check", ".", "!foo/bar/error.js"],
&["--check", ".", "!foo/bar"],
&["--check", ".", "!foo"],
&["--check", ".", "!**/error.js"],
]);
// Split to avoid too long file name error...
.test_and_snapshot_multiple(
"exclude_nested_paths_with_dot_1",
&[
&["--check", ".", "!foo/bar/error.js"],
&["--check", ".", "!foo/bar"],
&["--check", ".", "!foo"],
&["--check", ".", "!**/error.js"],
],
);
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
.test_and_snapshot_multiple(&[
&["--check", "./foo", "!**/bar/error.js"],
&["--check", "./foo", "!**/error.js"],
&["--check", "./foo", "!**/bar/*"],
&["--check", "./foo", "!foo/bar/error.js"],
&["--check", "./foo", "!foo/bar"],
]);
.test_and_snapshot_multiple(
"exclude_nested_paths_with_dot_2",
&[
&["--check", "./foo", "!**/bar/error.js"],
&["--check", "./foo", "!**/error.js"],
&["--check", "./foo", "!**/bar/*"],
&["--check", "./foo", "!foo/bar/error.js"],
&["--check", "./foo", "!foo/bar"],
],
);
}

#[test]
Expand All @@ -145,15 +164,18 @@ fn ignore_patterns() {
// custom.ignore contains: ignored/ (only)
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/ignore_patterns"))
.test_and_snapshot_multiple(&[
// Default: auto-detects only cwd/.prettierignore (ignores not-formatted/ dir)
// Note: not-formatted/.prettierignore exists but should be ignored
&["--check"],
// Explicit: uses gitignore.txt (ignores ignored/ dir, checks not-formatted/)
&["--check", "--ignore-path", "gitignore.txt"],
// Multiple files: ignores both dirs
&["--check", "--ignore-path", "gitignore.txt", "--ignore-path", ".prettierignore"],
// Nonexistent file should error
&["--check", "--ignore-path", "nonexistent.ignore"],
]);
.test_and_snapshot_multiple(
"ignore_patterns",
&[
// Default: auto-detects only cwd/.prettierignore (ignores not-formatted/ dir)
// Note: not-formatted/.prettierignore exists but should be ignored
&["--check"],
// Explicit: uses gitignore.txt (ignores ignored/ dir, checks not-formatted/)
&["--check", "--ignore-path", "gitignore.txt"],
// Multiple files: ignores both dirs
&["--check", "--ignore-path", "gitignore.txt", "--ignore-path", ".prettierignore"],
// Nonexistent file should error
&["--check", "--ignore-path", "nonexistent.ignore"],
],
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: apps/oxfmt/tests/tester.rs
assertion_line: 102
---
##########
arguments: --check
Expand Down
12 changes: 2 additions & 10 deletions apps/oxfmt/tests/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Tester {
///
/// # Panics
/// Panics if the current working directory cannot be determined or if command parsing fails.
pub fn test_and_snapshot_multiple(&self, multiple_args: &[&[&str]]) {
pub fn test_and_snapshot_multiple(&self, snapshot_name: &str, multiple_args: &[&[&str]]) {
let mut output: Vec<u8> = Vec::new();
let current_cwd = std::env::current_dir().unwrap();
let relative_dir = self.cwd.strip_prefix(&current_cwd).unwrap_or(&self.cwd);
Expand Down Expand Up @@ -90,16 +90,8 @@ impl Tester {
let test_cwd_string = self.cwd.to_str().unwrap().cow_replace('\\', "/").to_string();
let output_string = output_string.cow_replace(&test_cwd_string, "<cwd>");

let full_args_list =
multiple_args.iter().map(|args| args.join(" ")).collect::<Vec<String>>().join(" ");

let snapshot_file_name = format!("{}_{}", relative_dir.to_str().unwrap(), full_args_list);

// windows can not handle filenames with *
// allow replace instead of cow_replace. It only test
let snapshot_file_name = snapshot_file_name.cow_replace('*', "_").to_string();
settings.bind(|| {
insta::assert_snapshot!(snapshot_file_name, output_string);
insta::assert_snapshot!(snapshot_name, output_string);
});
}

Expand Down
Loading