Skip to content

Commit d8ccff7

Browse files
committed
test(oxlint): add Tester::test_fix mehod (oxc-project#12754)
1 parent 1e97e35 commit d8ccff7

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

apps/oxlint/src/lint.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -863,29 +863,7 @@ mod test {
863863

864864
#[test]
865865
fn test_fix() {
866-
use std::fs;
867-
let file = "fixtures/linter/fix.js";
868-
let args = &["--fix", file];
869-
let content_original = fs::read_to_string(file).unwrap();
870-
#[expect(clippy::disallowed_methods)]
871-
let content = content_original.replace("\r\n", "\n");
872-
assert_eq!(&content, "debugger\n");
873-
874-
// Apply fix to the file.
875-
Tester::new().test(args);
876-
#[expect(clippy::disallowed_methods)]
877-
let new_content = fs::read_to_string(file).unwrap().replace("\r\n", "\n");
878-
assert_eq!(new_content, "\n");
879-
880-
// File should not be modified if no fix is applied.
881-
let modified_before: std::time::SystemTime =
882-
fs::metadata(file).unwrap().modified().unwrap();
883-
Tester::new().test(args);
884-
let modified_after = fs::metadata(file).unwrap().modified().unwrap();
885-
assert_eq!(modified_before, modified_after);
886-
887-
// Write the file back.
888-
fs::write(file, content_original).unwrap();
866+
Tester::test_fix("fixtures/linter/fix.js", "debugger\n", "\n");
889867
}
890868

891869
#[test]

apps/oxlint/src/tester.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ impl Tester {
4141
let _ = LintRunner::new(options, None).with_cwd(self.cwd.clone()).run(&mut output);
4242
}
4343

44+
pub fn test_fix(file: &str, before: &str, after: &str) {
45+
use std::fs;
46+
#[expect(clippy::disallowed_methods)]
47+
let content_original = fs::read_to_string(file).unwrap().replace("\r\n", "\n");
48+
assert_eq!(content_original, before);
49+
50+
Tester::new().test(&["--fix", file]);
51+
52+
#[expect(clippy::disallowed_methods)]
53+
let new_content = fs::read_to_string(file).unwrap().replace("\r\n", "\n");
54+
assert_eq!(new_content, after);
55+
56+
Tester::new().test(&["--fix", file]);
57+
58+
// File should not be modified if no fix is applied.
59+
let modified_before: std::time::SystemTime =
60+
fs::metadata(file).unwrap().modified().unwrap();
61+
let modified_after = fs::metadata(file).unwrap().modified().unwrap();
62+
assert_eq!(modified_before, modified_after);
63+
64+
// Write the file back.
65+
fs::write(file, before).unwrap();
66+
}
67+
4468
pub fn test_and_snapshot(&self, args: &[&str]) {
4569
self.test_and_snapshot_multiple(&[args]);
4670
}

0 commit comments

Comments
 (0)