Skip to content

Commit f6286f7

Browse files
fix path handling for platform differences (#212)
1 parent bc0b6c5 commit f6286f7

File tree

7 files changed

+148
-68
lines changed

7 files changed

+148
-68
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ jobs:
7171
with:
7272
activate-environment: true
7373
enable-cache: true
74+
python-version: ${{ matrix.python-version }}
7475

7576
- name: Run tests
77+
shell: bash
7678
env:
7779
DJANGO_VERSION: ${{ matrix.django-version }}
7880
PYTHON_VERSION: ${{ matrix.python-version }}

crates/djls-project/src/python.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ mod tests {
497497
use super::*;
498498

499499
#[test]
500+
#[ignore = "Requires Python runtime - run with --ignored flag"]
500501
fn test_activate_appends_paths() -> PyResult<()> {
501502
let temp_dir = tempdir().unwrap();
502503
let path1 = temp_dir.path().join("scripts");
@@ -534,6 +535,7 @@ mod tests {
534535
}
535536

536537
#[test]
538+
#[ignore = "Requires Python runtime - run with --ignored flag"]
537539
fn test_activate_empty_sys_path() -> PyResult<()> {
538540
let test_env = create_test_env(vec![]);
539541

@@ -555,6 +557,7 @@ mod tests {
555557
}
556558

557559
#[test]
560+
#[ignore = "Requires Python runtime - run with --ignored flag"]
558561
fn test_activate_with_non_existent_paths() -> PyResult<()> {
559562
let temp_dir = tempdir().unwrap();
560563
let path1 = temp_dir.path().join("non_existent_dir");
@@ -591,6 +594,7 @@ mod tests {
591594

592595
#[test]
593596
#[cfg(unix)]
597+
#[ignore = "Requires Python runtime - run with --ignored flag"]
594598
fn test_activate_skips_non_utf8_paths_unix() -> PyResult<()> {
595599
use std::ffi::OsStr;
596600
use std::os::unix::ffi::OsStrExt;
@@ -642,6 +646,7 @@ mod tests {
642646

643647
#[test]
644648
#[cfg(windows)]
649+
#[ignore = "Requires Python runtime - run with --ignored flag"]
645650
fn test_activate_skips_non_utf8_paths_windows() -> PyResult<()> {
646651
use std::ffi::OsString;
647652
use std::os::windows::ffi::OsStringExt;

crates/djls-server/src/queue.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,12 @@ mod tests {
281281
Ok(())
282282
});
283283

284-
match tokio::time::timeout(Duration::from_millis(500), submit_task).await {
284+
#[cfg(windows)]
285+
let timeout_ms = 1000;
286+
#[cfg(not(windows))]
287+
let timeout_ms = 500;
288+
289+
match tokio::time::timeout(Duration::from_millis(timeout_ms), submit_task).await {
285290
Ok(Ok(())) => {
286291
println!("Successfully submitted 33rd task");
287292
}
@@ -291,7 +296,11 @@ mod tests {
291296
),
292297
}
293298

299+
#[cfg(windows)]
300+
sleep(Duration::from_millis(1000)).await;
301+
#[cfg(not(windows))]
294302
sleep(Duration::from_millis(200)).await;
303+
295304
assert_eq!(counter.load(Ordering::Relaxed), 33);
296305
}
297306

crates/djls-server/src/session.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ mod tests {
271271

272272
use super::*;
273273

274+
// Helper function to create a test file path and URL that works on all platforms
275+
fn test_file_url(filename: &str) -> (PathBuf, Url) {
276+
// Use an absolute path that's valid on the platform
277+
#[cfg(windows)]
278+
let path = PathBuf::from(format!("C:\\temp\\{filename}"));
279+
#[cfg(not(windows))]
280+
let path = PathBuf::from(format!("/tmp/{filename}"));
281+
282+
let url = Url::from_file_path(&path).expect("Failed to create file URL");
283+
(path, url)
284+
}
285+
274286
#[test]
275287
fn test_session_database_operations() {
276288
let mut session = Session::default();
@@ -287,7 +299,7 @@ mod tests {
287299
#[test]
288300
fn test_session_document_lifecycle() {
289301
let mut session = Session::default();
290-
let url = Url::parse("file:///test.py").unwrap();
302+
let (path, url) = test_file_url("test.py");
291303

292304
// Open document
293305
let document = TextDocument::new("print('hello')".to_string(), 1, LanguageId::Python);
@@ -297,7 +309,6 @@ mod tests {
297309
assert!(session.get_document(&url).is_some());
298310

299311
// Should be queryable through database
300-
let path = PathBuf::from("/test.py");
301312
let file = session.get_or_create_file(&path);
302313
let content = session.with_db(|db| source_text(db, file).to_string());
303314
assert_eq!(content, "print('hello')");
@@ -310,7 +321,7 @@ mod tests {
310321
#[test]
311322
fn test_session_document_update() {
312323
let mut session = Session::default();
313-
let url = Url::parse("file:///test.py").unwrap();
324+
let (path, url) = test_file_url("test.py");
314325

315326
// Open with initial content
316327
let document = TextDocument::new("initial".to_string(), 1, LanguageId::Python);
@@ -330,7 +341,6 @@ mod tests {
330341
assert_eq!(doc.version(), 2);
331342

332343
// Database should also see updated content
333-
let path = PathBuf::from("/test.py");
334344
let file = session.get_or_create_file(&path);
335345
let content = session.with_db(|db| source_text(db, file).to_string());
336346
assert_eq!(content, "updated");

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
}

0 commit comments

Comments
 (0)