Skip to content

Commit 38e8267

Browse files
committed
Review feedback
1 parent ca06894 commit 38e8267

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

crates/red_knot_test/src/db.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,22 @@ impl Db {
2828
let rule_selection = RuleSelection::from_registry(default_lint_registry());
2929

3030
Self {
31-
system: MdtestSystem::in_memory(SystemPathBuf::from("/")),
31+
system: MdtestSystem::in_memory(),
3232
storage: salsa::Storage::default(),
3333
vendored: red_knot_vendored::file_system().clone(),
3434
files: Files::default(),
3535
rule_selection: Arc::new(rule_selection),
3636
}
3737
}
3838

39-
pub(crate) fn reset_fs(&mut self) {
40-
match &*self.system.0 {
41-
MdtestSystemInner::InMemory(in_memory) => {
42-
in_memory.fs().remove_all();
43-
}
44-
MdtestSystemInner::Os { .. } => {
45-
self.system = MdtestSystem::in_memory(SystemPathBuf::from("/"));
46-
}
47-
}
48-
39+
pub(crate) fn use_os_system_with_temp_dir(&mut self, cwd: SystemPathBuf, temp_dir: TempDir) {
40+
self.system.with_os(cwd, temp_dir);
4941
Files::sync_all(self);
5042
}
5143

52-
pub(crate) fn with_tempdir_fs(&mut self, cwd: SystemPathBuf, temp_dir: TempDir) {
53-
self.system.with_os(cwd, temp_dir);
44+
pub(crate) fn use_in_memory_system(&mut self) {
45+
self.system.with_in_memory();
46+
Files::sync_all(self);
5447
}
5548

5649
pub(crate) fn create_directory_all(&self, path: &SystemPath) -> ruff_db::system::Result<()> {
@@ -122,10 +115,10 @@ enum MdtestSystemInner {
122115
}
123116

124117
impl MdtestSystem {
125-
fn in_memory(cwd: SystemPathBuf) -> Self {
126-
Self(Arc::new(MdtestSystemInner::InMemory(InMemorySystem::new(
127-
cwd,
128-
))))
118+
fn in_memory() -> Self {
119+
Self(Arc::new(MdtestSystemInner::InMemory(
120+
InMemorySystem::default(),
121+
)))
129122
}
130123

131124
fn as_system(&self) -> &dyn WritableSystem {
@@ -142,6 +135,14 @@ impl MdtestSystem {
142135
});
143136
}
144137

138+
fn with_in_memory(&mut self) {
139+
if let MdtestSystemInner::InMemory(in_memory) = &*self.0 {
140+
in_memory.fs().remove_all();
141+
} else {
142+
self.0 = Arc::new(MdtestSystemInner::InMemory(InMemorySystem::default()));
143+
}
144+
}
145+
145146
fn normalize_path<'a>(&self, path: &'a SystemPath) -> Cow<'a, SystemPath> {
146147
match &*self.0 {
147148
MdtestSystemInner::InMemory(_) => Cow::Borrowed(path),

crates/red_knot_test/src/lib.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ pub fn run(
5757
Log::Filter(filter) => setup_logging_with_filter(filter),
5858
});
5959

60-
// Remove all files so that the db is in a "fresh" state.
61-
db.reset_fs();
62-
6360
if let Err(failures) = run_test(&mut db, relative_fixture_path, snapshot_path, &test) {
6461
any_failures = true;
6562
println!("\n{}\n", test.name().bold().underline());
@@ -104,16 +101,22 @@ fn run_test(
104101
snapshot_path: &Utf8Path,
105102
test: &parser::MarkdownTest,
106103
) -> Result<(), Failures> {
107-
if SystemKind::Os == test.configuration().system.unwrap_or_default() {
108-
let dir = tempfile::TempDir::new().expect("Creating a temporary directory to succeed");
109-
let root_path = dir
110-
.path()
111-
.canonicalize()
112-
.expect("Canonicalizing to succeed");
113-
let root_path = SystemPathBuf::from_path_buf(root_path)
114-
.expect("Temp directory to be a valid UTF8 path");
115-
116-
db.with_tempdir_fs(root_path, dir);
104+
// Initialize the system and remove all files and directories to reset the system to a clean state.
105+
match test.configuration().system.unwrap_or_default() {
106+
SystemKind::InMemory => {
107+
db.use_in_memory_system();
108+
}
109+
SystemKind::Os => {
110+
let dir = tempfile::TempDir::new().expect("Creating a temporary directory to succeed");
111+
let root_path = dir
112+
.path()
113+
.canonicalize()
114+
.expect("Canonicalizing to succeed");
115+
let root_path = SystemPathBuf::from_path_buf(root_path)
116+
.expect("Temp directory to be a valid UTF8 path");
117+
118+
db.use_os_system_with_temp_dir(root_path, dir);
119+
}
117120
}
118121

119122
let project_root = SystemPathBuf::from("/src");

0 commit comments

Comments
 (0)