Skip to content

Commit 59fbffc

Browse files
more fixes?
1 parent 345e034 commit 59fbffc

File tree

2 files changed

+98
-59
lines changed

2 files changed

+98
-59
lines changed

crates/djls-workspace/src/fs.rs

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -166,54 +166,56 @@ mod tests {
166166
use crate::document::TextDocument;
167167
use crate::language::LanguageId;
168168

169+
// Helper to create platform-appropriate test paths
170+
fn test_file_path(name: &str) -> PathBuf {
171+
#[cfg(windows)]
172+
return PathBuf::from(format!("C:\\temp\\{name}"));
173+
#[cfg(not(windows))]
174+
return PathBuf::from(format!("/tmp/{name}"));
175+
}
176+
169177
#[test]
170178
fn test_reads_from_buffer_when_present() {
171179
let disk = Arc::new(InMemoryFileSystem::new());
172180
let buffers = Buffers::new();
173181
let fs = WorkspaceFileSystem::new(buffers.clone(), disk);
174182

175183
// Add file to buffer
176-
let url = Url::from_file_path("/test.py").unwrap();
184+
let path = test_file_path("test.py");
185+
let url = Url::from_file_path(&path).unwrap();
177186
let doc = TextDocument::new("buffer content".to_string(), 1, LanguageId::Python);
178187
buffers.open(url, doc);
179188

180-
assert_eq!(
181-
fs.read_to_string(Path::new("/test.py")).unwrap(),
182-
"buffer content"
183-
);
189+
assert_eq!(fs.read_to_string(&path).unwrap(), "buffer content");
184190
}
185191

186192
#[test]
187193
fn test_reads_from_disk_when_no_buffer() {
188194
let mut disk_fs = InMemoryFileSystem::new();
189-
disk_fs.add_file("/test.py".into(), "disk content".to_string());
195+
let path = test_file_path("test.py");
196+
disk_fs.add_file(path.clone(), "disk content".to_string());
190197

191198
let buffers = Buffers::new();
192199
let fs = WorkspaceFileSystem::new(buffers, Arc::new(disk_fs));
193200

194-
assert_eq!(
195-
fs.read_to_string(Path::new("/test.py")).unwrap(),
196-
"disk content"
197-
);
201+
assert_eq!(fs.read_to_string(&path).unwrap(), "disk content");
198202
}
199203

200204
#[test]
201205
fn test_buffer_overrides_disk() {
202206
let mut disk_fs = InMemoryFileSystem::new();
203-
disk_fs.add_file("/test.py".into(), "disk content".to_string());
207+
let path = test_file_path("test.py");
208+
disk_fs.add_file(path.clone(), "disk content".to_string());
204209

205210
let buffers = Buffers::new();
206211
let fs = WorkspaceFileSystem::new(buffers.clone(), Arc::new(disk_fs));
207212

208213
// Add buffer with different content
209-
let url = Url::from_file_path("/test.py").unwrap();
214+
let url = Url::from_file_path(&path).unwrap();
210215
let doc = TextDocument::new("buffer content".to_string(), 1, LanguageId::Python);
211216
buffers.open(url, doc);
212217

213-
assert_eq!(
214-
fs.read_to_string(Path::new("/test.py")).unwrap(),
215-
"buffer content"
216-
);
218+
assert_eq!(fs.read_to_string(&path).unwrap(), "buffer content");
217219
}
218220

219221
#[test]
@@ -222,39 +224,42 @@ mod tests {
222224
let buffers = Buffers::new();
223225
let fs = WorkspaceFileSystem::new(buffers.clone(), disk);
224226

225-
// Add file only to buffer
226-
let url = Url::from_file_path("/buffer_only.py").unwrap();
227+
// Add file to buffer only
228+
let path = test_file_path("buffer_only.py");
229+
let url = Url::from_file_path(&path).unwrap();
227230
let doc = TextDocument::new("content".to_string(), 1, LanguageId::Python);
228231
buffers.open(url, doc);
229232

230-
assert!(fs.exists(Path::new("/buffer_only.py")));
233+
assert!(fs.exists(&path));
231234
}
232235

233236
#[test]
234237
fn test_exists_for_disk_only_file() {
235238
let mut disk_fs = InMemoryFileSystem::new();
236-
disk_fs.add_file("/disk_only.py".into(), "content".to_string());
239+
let path = test_file_path("disk_only.py");
240+
disk_fs.add_file(path.clone(), "content".to_string());
237241

238242
let buffers = Buffers::new();
239243
let fs = WorkspaceFileSystem::new(buffers, Arc::new(disk_fs));
240244

241-
assert!(fs.exists(Path::new("/disk_only.py")));
245+
assert!(fs.exists(&path));
242246
}
243247

244248
#[test]
245249
fn test_exists_for_both_buffer_and_disk() {
246250
let mut disk_fs = InMemoryFileSystem::new();
247-
disk_fs.add_file("/both.py".into(), "disk".to_string());
251+
let path = test_file_path("both.py");
252+
disk_fs.add_file(path.clone(), "disk".to_string());
248253

249254
let buffers = Buffers::new();
250255
let fs = WorkspaceFileSystem::new(buffers.clone(), Arc::new(disk_fs));
251256

252257
// Also add to buffer
253-
let url = Url::from_file_path("/both.py").unwrap();
258+
let url = Url::from_file_path(&path).unwrap();
254259
let doc = TextDocument::new("buffer".to_string(), 1, LanguageId::Python);
255260
buffers.open(url, doc);
256261

257-
assert!(fs.exists(Path::new("/both.py")));
262+
assert!(fs.exists(&path));
258263
}
259264

260265
#[test]
@@ -263,7 +268,8 @@ mod tests {
263268
let buffers = Buffers::new();
264269
let fs = WorkspaceFileSystem::new(buffers, disk);
265270

266-
assert!(!fs.exists(Path::new("/nowhere.py")));
271+
let path = test_file_path("nowhere.py");
272+
assert!(!fs.exists(&path));
267273
}
268274

269275
#[test]
@@ -272,7 +278,8 @@ mod tests {
272278
let buffers = Buffers::new();
273279
let fs = WorkspaceFileSystem::new(buffers, disk);
274280

275-
let result = fs.read_to_string(Path::new("/missing.py"));
281+
let path = test_file_path("missing.py");
282+
let result = fs.read_to_string(&path);
276283
assert!(result.is_err());
277284
assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
278285
}
@@ -283,49 +290,39 @@ mod tests {
283290
let buffers = Buffers::new();
284291
let fs = WorkspaceFileSystem::new(buffers.clone(), disk);
285292

286-
let url = Url::from_file_path("/test.py").unwrap();
293+
let path = test_file_path("test.py");
294+
let url = Url::from_file_path(&path).unwrap();
287295

288296
// Initial buffer content
289297
let doc1 = TextDocument::new("version 1".to_string(), 1, LanguageId::Python);
290298
buffers.open(url.clone(), doc1);
291-
assert_eq!(
292-
fs.read_to_string(Path::new("/test.py")).unwrap(),
293-
"version 1"
294-
);
299+
assert_eq!(fs.read_to_string(&path).unwrap(), "version 1");
295300

296301
// Update buffer content
297302
let doc2 = TextDocument::new("version 2".to_string(), 2, LanguageId::Python);
298303
buffers.update(url, doc2);
299-
assert_eq!(
300-
fs.read_to_string(Path::new("/test.py")).unwrap(),
301-
"version 2"
302-
);
304+
assert_eq!(fs.read_to_string(&path).unwrap(), "version 2");
303305
}
304306

305307
#[test]
306308
fn test_handles_buffer_removal() {
307309
let mut disk_fs = InMemoryFileSystem::new();
308-
disk_fs.add_file("/test.py".into(), "disk content".to_string());
310+
let path = test_file_path("test.py");
311+
disk_fs.add_file(path.clone(), "disk content".to_string());
309312

310313
let buffers = Buffers::new();
311314
let fs = WorkspaceFileSystem::new(buffers.clone(), Arc::new(disk_fs));
312315

313-
let url = Url::from_file_path("/test.py").unwrap();
316+
let url = Url::from_file_path(&path).unwrap();
314317

315318
// Add buffer
316319
let doc = TextDocument::new("buffer content".to_string(), 1, LanguageId::Python);
317320
buffers.open(url.clone(), doc);
318-
assert_eq!(
319-
fs.read_to_string(Path::new("/test.py")).unwrap(),
320-
"buffer content"
321-
);
321+
assert_eq!(fs.read_to_string(&path).unwrap(), "buffer content");
322322

323323
// Remove buffer
324324
let _ = buffers.close(&url);
325-
assert_eq!(
326-
fs.read_to_string(Path::new("/test.py")).unwrap(),
327-
"disk content"
328-
);
325+
assert_eq!(fs.read_to_string(&path).unwrap(), "disk content");
329326
}
330327
}
331328
}

crates/djls-workspace/src/paths.rs

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,20 @@ pub fn url_to_path(url: &Url) -> Option<PathBuf> {
2727

2828
#[cfg(windows)]
2929
let path = {
30-
// Remove leading '/' for paths like /C:/...
31-
path.strip_prefix('/').unwrap_or(&path)
30+
// Remove leading '/' only for Windows drive paths like /C:/...
31+
// Check if it matches the pattern /X:/ where X is a drive letter
32+
if path.len() >= 3 {
33+
let bytes = path.as_bytes();
34+
if bytes[0] == b'/' && bytes[2] == b':' && bytes[1].is_ascii_alphabetic() {
35+
// It's a drive path like /C:/, strip the leading /
36+
&path[1..]
37+
} else {
38+
// Keep as-is for other paths
39+
&path
40+
}
41+
} else {
42+
&path
43+
}
3244
};
3345

3446
Some(PathBuf::from(&*path))
@@ -132,8 +144,16 @@ mod tests {
132144

133145
#[test]
134146
fn test_url_to_path_valid_file_url() {
135-
let url = Url::parse("file:///home/user/test.py").unwrap();
136-
assert_eq!(url_to_path(&url), Some(PathBuf::from("/home/user/test.py")));
147+
#[cfg(not(windows))]
148+
{
149+
let url = Url::parse("file:///home/user/test.py").unwrap();
150+
assert_eq!(url_to_path(&url), Some(PathBuf::from("/home/user/test.py")));
151+
}
152+
#[cfg(windows)]
153+
{
154+
let url = Url::parse("file:///C:/Users/test.py").unwrap();
155+
assert_eq!(url_to_path(&url), Some(PathBuf::from("C:/Users/test.py")));
156+
}
137157
}
138158

139159
#[test]
@@ -144,11 +164,22 @@ mod tests {
144164

145165
#[test]
146166
fn test_url_to_path_percent_encoded() {
147-
let url = Url::parse("file:///home/user/test%20file.py").unwrap();
148-
assert_eq!(
149-
url_to_path(&url),
150-
Some(PathBuf::from("/home/user/test file.py"))
151-
);
167+
#[cfg(not(windows))]
168+
{
169+
let url = Url::parse("file:///home/user/test%20file.py").unwrap();
170+
assert_eq!(
171+
url_to_path(&url),
172+
Some(PathBuf::from("/home/user/test file.py"))
173+
);
174+
}
175+
#[cfg(windows)]
176+
{
177+
let url = Url::parse("file:///C:/Users/test%20file.py").unwrap();
178+
assert_eq!(
179+
url_to_path(&url),
180+
Some(PathBuf::from("C:/Users/test file.py"))
181+
);
182+
}
152183
}
153184

154185
#[test]
@@ -169,11 +200,22 @@ mod tests {
169200
// lsp_uri_to_path tests
170201
#[test]
171202
fn test_lsp_uri_to_path_valid_file() {
172-
let uri = lsp_types::Uri::from_str("file:///home/user/test.py").unwrap();
173-
assert_eq!(
174-
lsp_uri_to_path(&uri),
175-
Some(PathBuf::from("/home/user/test.py"))
176-
);
203+
#[cfg(not(windows))]
204+
{
205+
let uri = lsp_types::Uri::from_str("file:///home/user/test.py").unwrap();
206+
assert_eq!(
207+
lsp_uri_to_path(&uri),
208+
Some(PathBuf::from("/home/user/test.py"))
209+
);
210+
}
211+
#[cfg(windows)]
212+
{
213+
let uri = lsp_types::Uri::from_str("file:///C:/Users/test.py").unwrap();
214+
assert_eq!(
215+
lsp_uri_to_path(&uri),
216+
Some(PathBuf::from("C:/Users/test.py"))
217+
);
218+
}
177219
}
178220

179221
#[test]

0 commit comments

Comments
 (0)