From 2d3fbbd91bd1997f2383d0934f50a8ba188bac4e Mon Sep 17 00:00:00 2001 From: ea1thoobeloa9aer Date: Sat, 21 Jan 2023 20:44:44 +0100 Subject: [PATCH] fix(rome_fs): Allow to ignore patterns to symbolic links (symlinks) --- crates/rome_cli/tests/commands/check.rs | 105 +++++++++++++++++- crates/rome_cli/tests/configs.rs | 8 ++ .../fs_files_ignore_symlink.snap | 18 +++ crates/rome_fs/src/fs/os.rs | 25 ++++- 4 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 crates/rome_cli/tests/snapshots/main_commands_check/fs_files_ignore_symlink.snap diff --git a/crates/rome_cli/tests/commands/check.rs b/crates/rome_cli/tests/commands/check.rs index 952b2927dab..780187658fb 100644 --- a/crates/rome_cli/tests/commands/check.rs +++ b/crates/rome_cli/tests/commands/check.rs @@ -1,7 +1,9 @@ use pico_args::Arguments; +use std::env; use std::env::temp_dir; use std::ffi::OsString; -use std::fs::{create_dir, create_dir_all, remove_dir_all}; +use std::fs::{create_dir, create_dir_all, remove_dir_all, File}; +use std::io::Write; #[cfg(target_family = "unix")] use std::os::unix::fs::symlink; #[cfg(target_os = "windows")] @@ -9,8 +11,8 @@ use std::os::windows::fs::{symlink_dir, symlink_file}; use std::path::{Path, PathBuf}; use crate::configs::{ - CONFIG_FILE_SIZE_LIMIT, CONFIG_LINTER_AND_FILES_IGNORE, CONFIG_LINTER_DISABLED, - CONFIG_LINTER_DOWNGRADE_DIAGNOSTIC, CONFIG_LINTER_IGNORED_FILES, + CONFIG_FILE_SIZE_LIMIT, CONFIG_IGNORE_SYMLINK, CONFIG_LINTER_AND_FILES_IGNORE, + CONFIG_LINTER_DISABLED, CONFIG_LINTER_DOWNGRADE_DIAGNOSTIC, CONFIG_LINTER_IGNORED_FILES, CONFIG_LINTER_SUPPRESSED_GROUP, CONFIG_LINTER_SUPPRESSED_RULE, CONFIG_LINTER_UPGRADE_DIAGNOSTIC, CONFIG_RECOMMENDED_GROUP, }; @@ -855,6 +857,103 @@ fn fs_error_unknown() { )); } +#[test] +fn fs_files_ignore_symlink() { + let fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let root_path = temp_dir().join("rome_test_files_ignore_symlink"); + let project_path = root_path.join("project"); + + let testcase1_path = root_path.join("hidden_testcase1"); + let testcase1_sub_path = testcase1_path.join("test"); + let testcase2_path = root_path.join("hidden_testcase2"); + + let nested_path = root_path.join("hidden_nested"); + let nested_sub_path = nested_path.join("test"); + + #[allow(unused_must_use)] + { + remove_dir_all(root_path.clone()); + } + create_dir(root_path.clone()).unwrap(); + create_dir(project_path.clone()).unwrap(); + create_dir_all(testcase1_sub_path.clone()).unwrap(); + create_dir(testcase2_path.clone()).unwrap(); + create_dir_all(nested_sub_path.clone()).unwrap(); + + // project/symlink_testcase1_1 + let symlink_testcase1_1_path = project_path.join("symlink_testcase1_1"); + // hidden_nested/test/symlink_testcase1_2 + let symlink_testcase1_2_path = nested_sub_path.join("symlink_testcase1_2"); + // project/symlink_testcase2 + let symlink_testcase2_path = project_path.join("symlink_testcase2"); + + #[cfg(target_family = "unix")] + { + // project/test/symlink_testcase1_1 -> hidden_nested + symlink(nested_path, symlink_testcase1_1_path).unwrap(); + // hidden_nested/test/symlink_testcase1_2 -> hidden_testcase1 + symlink(testcase1_path, symlink_testcase1_2_path).unwrap(); + // project/symlink_testcase2 -> hidden_testcase2 + symlink(testcase2_path.clone(), symlink_testcase2_path).unwrap(); + } + + #[cfg(target_os = "windows")] + { + check_windows_symlink!(symlink_dir(nested_path.clone(), symlink_testcase1_1_path)); + check_windows_symlink!(symlink_dir( + testcase1_path.clone(), + symlink_testcase1_2_path + )); + check_windows_symlink!(symlink_dir(testcase2_path.clone(), symlink_testcase2_path)); + } + + let config_path = project_path.join("rome.json"); + let mut config_file = File::create(config_path).unwrap(); + config_file + .write_all(CONFIG_IGNORE_SYMLINK.as_bytes()) + .unwrap(); + + let files: [PathBuf; 4] = [ + testcase1_sub_path.join("test.js"), // ok + testcase2_path.join("test.js"), // ok + testcase2_path.join("test1.ts"), // ignored + testcase2_path.join("test2.ts"), // ignored + ]; + + for file_path in files { + let mut file = File::create(file_path).unwrap(); + file.write_all(APPLY_SUGGESTED_BEFORE.as_bytes()).unwrap(); + } + + // Change the current working directory + // TODO: Remove this once PR #4158 was merged + assert!(env::set_current_dir(&project_path).is_ok()); + + let result = run_cli( + DynRef::Owned(Box::new(OsFileSystem)), + &mut console, + Arguments::from_vec(vec![ + OsString::from("check"), + OsString::from("--apply"), + OsString::from(project_path), + ]), + ); + + remove_dir_all(root_path).unwrap(); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "fs_files_ignore_symlink", + fs, + console, + result, + )); +} + #[test] fn file_too_large() { let mut fs = MemoryFileSystem::default(); diff --git a/crates/rome_cli/tests/configs.rs b/crates/rome_cli/tests/configs.rs index 22e8dc32b51..c05d23544a9 100644 --- a/crates/rome_cli/tests/configs.rs +++ b/crates/rome_cli/tests/configs.rs @@ -245,3 +245,11 @@ pub const CONFIG_FILE_SIZE_LIMIT: &str = r#"{ "maxSize": 16 } }"#; + +pub const CONFIG_IGNORE_SYMLINK: &str = r#"{ + "files": { + "ignore": [ + "symlink_testcase2/**/*.ts" + ] + } +}"#; diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/fs_files_ignore_symlink.snap b/crates/rome_cli/tests/snapshots/main_commands_check/fs_files_ignore_symlink.snap new file mode 100644 index 00000000000..d4044a3242a --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_check/fs_files_ignore_symlink.snap @@ -0,0 +1,18 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +assertion_line: 335 +expression: content +--- +# Emitted Messages + +```block +Skipped 4 suggested fixes. +If you wish to apply the suggested (unsafe) fixes, use the command rome check --apply-unsafe + +``` + +```block +Fixed 2 file(s) in