From b03cec6ae97d2b0fd5c49efd605d8250d05e4717 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:38:19 +0000 Subject: [PATCH] test(oxlint): add `--fix` test case (#6747) closes #6061 --- apps/oxlint/fixtures/linter/fix.js | 1 + apps/oxlint/src/lint.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 apps/oxlint/fixtures/linter/fix.js diff --git a/apps/oxlint/fixtures/linter/fix.js b/apps/oxlint/fixtures/linter/fix.js new file mode 100644 index 000000000..a163e71b8 --- /dev/null +++ b/apps/oxlint/fixtures/linter/fix.js @@ -0,0 +1 @@ +debugger diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 65b1f4a48..e2cd45cb6 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -265,8 +265,8 @@ mod test { let args = &["fixtures/linter"]; let result = test(args); assert!(result.number_of_rules > 0); - assert_eq!(result.number_of_files, 2); - assert_eq!(result.number_of_warnings, 2); + assert_eq!(result.number_of_files, 3); + assert_eq!(result.number_of_warnings, 3); assert_eq!(result.number_of_errors, 0); } @@ -564,4 +564,26 @@ mod test { assert_eq!(result.number_of_warnings, 0); assert_eq!(result.number_of_errors, 1); } + + #[test] + fn test_fix() { + use std::fs; + let file = "fixtures/linter/fix.js"; + let args = &["--fix", file]; + let content = fs::read_to_string(file).unwrap(); + assert_eq!(&content, "debugger\n"); + + // Apply fix to the file. + let _ = test(args); + assert_eq!(fs::read_to_string(file).unwrap(), "\n"); + + // File should not be modified if no fix is applied. + let modified_before = fs::metadata(file).unwrap().modified().unwrap(); + let _ = test(args); + let modified_after = fs::metadata(file).unwrap().modified().unwrap(); + assert_eq!(modified_before, modified_after); + + // Write the file back. + fs::write(file, content).unwrap(); + } }