Skip to content

Commit 47b946d

Browse files
committed
test(linter): use TesterFileSystem for Runtimes filesystem (#10829)
1 parent 243c247 commit 47b946d

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

crates/oxc_linter/src/service/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use oxc_diagnostics::DiagnosticSender;
88
use runtime::Runtime;
9+
pub use runtime::RuntimeFileSystem;
910

1011
use crate::Linter;
1112

@@ -74,10 +75,13 @@ impl LintService {
7475
Self { runtime }
7576
}
7677

77-
#[cfg(test)]
78-
pub(crate) fn from_linter(linter: Linter, options: LintServiceOptions) -> Self {
79-
let runtime = Runtime::new(linter, options);
80-
Self { runtime }
78+
#[must_use]
79+
pub fn with_file_system(
80+
mut self,
81+
file_system: Box<dyn RuntimeFileSystem + Sync + Send>,
82+
) -> Self {
83+
self.runtime = self.runtime.with_file_system(file_system);
84+
self
8185
}
8286

8387
pub fn linter(&self) -> &Linter {
@@ -105,10 +109,9 @@ impl LintService {
105109
pub(crate) fn run_test_source<'a>(
106110
&mut self,
107111
allocator: &'a oxc_allocator::Allocator,
108-
source_text: &str,
109112
check_syntax_errors: bool,
110113
tx_error: &DiagnosticSender,
111114
) -> Vec<crate::Message<'a>> {
112-
self.runtime.run_test_source(allocator, source_text, check_syntax_errors, tx_error)
115+
self.runtime.run_test_source(allocator, check_syntax_errors, tx_error)
113116
}
114117
}

crates/oxc_linter/src/service/runtime.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ pub struct Runtime {
5151
// This is required to support `run: "onType"` configuration
5252
#[cfg(feature = "language_server")]
5353
source_text_cache: FxHashMap<Arc<OsStr>, String>,
54-
55-
#[cfg(test)]
56-
pub(super) test_source: std::sync::RwLock<Option<String>>,
5754
}
5855

5956
/// Output of `Runtime::process_path`
@@ -184,12 +181,9 @@ impl Runtime {
184181
file_system: Box::new(OsFileSystem),
185182
#[cfg(feature = "language_server")]
186183
source_text_cache: FxHashMap::default(),
187-
#[cfg(test)]
188-
test_source: std::sync::RwLock::new(None),
189184
}
190185
}
191186

192-
#[expect(dead_code)]
193187
pub fn with_file_system(
194188
mut self,
195189
file_system: Box<dyn RuntimeFileSystem + Sync + Send>,
@@ -236,13 +230,6 @@ impl Runtime {
236230
}
237231
let source_type = source_type.unwrap_or_default();
238232

239-
#[cfg(test)]
240-
if let (true, Some(test_source)) =
241-
(self.paths.contains(path.as_os_str()), &*self.test_source.read().unwrap())
242-
{
243-
return Some(Ok((source_type, test_source.clone())));
244-
}
245-
246233
// The language server uses more up to date source_text provided by `workspace/didChange` request.
247234
// This is required to support `run: "onType"` configuration
248235
#[cfg(feature = "language_server")]
@@ -697,15 +684,12 @@ impl Runtime {
697684
pub(super) fn run_test_source<'a>(
698685
&mut self,
699686
allocator: &'a Allocator,
700-
source_text: &str,
701687
check_syntax_errors: bool,
702688
tx_error: &DiagnosticSender,
703689
) -> Vec<Message<'a>> {
704690
use oxc_allocator::CloneIn;
705691
use std::sync::Mutex;
706692

707-
*self.test_source.write().unwrap() = Some(source_text.to_owned());
708-
709693
let messages = Mutex::new(Vec::<Message<'a>>::new());
710694
rayon::scope(|scope| {
711695
self.resolve_modules(scope, check_syntax_errors, tx_error, |me, mut module| {

crates/oxc_linter/src/tester.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use crate::{
1717
Oxlintrc, RuleEnum, RuleWithSeverity,
1818
fixer::{FixKind, Fixer},
1919
options::LintOptions,
20+
read_to_string,
2021
rules::RULES,
22+
service::RuntimeFileSystem,
2123
};
2224

2325
#[derive(Eq, PartialEq)]
@@ -165,6 +167,31 @@ where
165167
}
166168
}
167169

170+
struct TesterFileSystem {
171+
path_to_lint: PathBuf,
172+
source_text: String,
173+
}
174+
175+
impl TesterFileSystem {
176+
pub fn new(path_to_lint: PathBuf, source_text: String) -> Self {
177+
Self { path_to_lint, source_text }
178+
}
179+
}
180+
181+
impl RuntimeFileSystem for TesterFileSystem {
182+
fn read_to_string(&self, path: &Path) -> Result<String, std::io::Error> {
183+
if path == self.path_to_lint {
184+
return Ok(self.source_text.clone());
185+
}
186+
187+
read_to_string(path)
188+
}
189+
190+
fn write_file(&self, _path: &Path, _content: String) -> Result<(), std::io::Error> {
191+
panic!("writing file should not be allowed in Tester");
192+
}
193+
}
194+
168195
pub struct Tester {
169196
rule_name: &'static str,
170197
plugin_name: &'static str,
@@ -484,9 +511,12 @@ impl Tester {
484511
let paths = vec![Arc::<OsStr>::from(path_to_lint.as_os_str())];
485512
let options =
486513
LintServiceOptions::new(cwd, paths).with_cross_module(self.plugins.has_import());
487-
let mut lint_service = LintService::from_linter(linter, options);
514+
let mut lint_service = LintService::new(linter, options).with_file_system(Box::new(
515+
TesterFileSystem::new(path_to_lint, source_text.to_string()),
516+
));
517+
488518
let (sender, _receiver) = mpsc::channel();
489-
let result = lint_service.run_test_source(&allocator, source_text, false, &sender);
519+
let result = lint_service.run_test_source(&allocator, false, &sender);
490520

491521
if result.is_empty() {
492522
return TestResult::Passed;

0 commit comments

Comments
 (0)