Skip to content

Commit a3182a6

Browse files
committed
feat: add tempfile as a dev-dependency and implement tests for WinPython locator
1 parent 7391b02 commit a3182a6

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pet-winpython/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ pet-virtualenv = { path = "../pet-virtualenv" }
1212
log = "0.4.21"
1313
lazy_static = "1.4.0"
1414
regex = "1.10.4"
15+
16+
[dev-dependencies]
17+
tempfile = "3.10"

crates/pet-winpython/src/lib.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ fn get_winpython_search_paths() -> Vec<PathBuf> {
346346
#[cfg(test)]
347347
mod tests {
348348
use super::*;
349+
use std::fs::File;
350+
use tempfile::tempdir;
349351

350352
#[test]
351353
fn test_is_winpython_dir_name() {
@@ -395,4 +397,144 @@ mod tests {
395397
assert_eq!(version_from_folder_name("python"), None);
396398
assert_eq!(version_from_folder_name("not-python-3.9.0"), None);
397399
}
400+
401+
#[test]
402+
fn test_get_display_name() {
403+
let path = PathBuf::from("C:\\WPy64-31300");
404+
assert_eq!(
405+
get_display_name(&path, Some("3.13.0")),
406+
Some("WinPython 3.13.0".to_string())
407+
);
408+
assert_eq!(
409+
get_display_name(&path, None),
410+
Some("WinPython (WPy64-31300)".to_string())
411+
);
412+
}
413+
414+
#[test]
415+
fn test_is_winpython_root_with_marker() {
416+
let dir = tempdir().unwrap();
417+
let winpython_marker = dir.path().join(".winpython");
418+
File::create(&winpython_marker).unwrap();
419+
420+
assert!(is_winpython_root(dir.path()));
421+
}
422+
423+
#[test]
424+
fn test_is_winpython_root_with_ini_marker() {
425+
let dir = tempdir().unwrap();
426+
let winpython_ini = dir.path().join("winpython.ini");
427+
File::create(&winpython_ini).unwrap();
428+
429+
assert!(is_winpython_root(dir.path()));
430+
}
431+
432+
#[test]
433+
fn test_is_winpython_root_without_marker() {
434+
let dir = tempdir().unwrap();
435+
assert!(!is_winpython_root(dir.path()));
436+
}
437+
438+
#[test]
439+
#[cfg(windows)]
440+
fn test_find_python_folder_in_winpython() {
441+
let dir = tempdir().unwrap();
442+
let python_folder = dir.path().join("python-3.13.0.amd64");
443+
fs::create_dir_all(&python_folder).unwrap();
444+
445+
// Create python.exe
446+
let python_exe = python_folder.join("python.exe");
447+
File::create(&python_exe).unwrap();
448+
449+
let result = find_python_folder_in_winpython(dir.path());
450+
assert!(result.is_some());
451+
assert_eq!(result.unwrap(), python_folder);
452+
}
453+
454+
#[test]
455+
fn test_find_python_folder_missing_exe() {
456+
let dir = tempdir().unwrap();
457+
let python_folder = dir.path().join("python-3.13.0.amd64");
458+
fs::create_dir_all(&python_folder).unwrap();
459+
460+
// No python.exe created
461+
let result = find_python_folder_in_winpython(dir.path());
462+
assert!(result.is_none());
463+
}
464+
465+
#[test]
466+
#[cfg(windows)]
467+
fn test_find_winpython_root_with_marker() {
468+
let dir = tempdir().unwrap();
469+
470+
// Create WinPython structure with marker
471+
let winpython_root = dir.path().join("WPy64-31300");
472+
fs::create_dir_all(&winpython_root).unwrap();
473+
File::create(winpython_root.join(".winpython")).unwrap();
474+
475+
let python_folder = winpython_root.join("python-3.13.0.amd64");
476+
fs::create_dir_all(&python_folder).unwrap();
477+
let python_exe = python_folder.join("python.exe");
478+
File::create(&python_exe).unwrap();
479+
480+
let result = find_winpython_root(&python_exe);
481+
assert!(result.is_some());
482+
let (root, folder) = result.unwrap();
483+
assert_eq!(root, winpython_root);
484+
assert_eq!(folder, python_folder);
485+
}
486+
487+
#[test]
488+
#[cfg(windows)]
489+
fn test_find_winpython_root_by_dir_name() {
490+
let dir = tempdir().unwrap();
491+
492+
// Create WinPython structure without marker (relying on dir name)
493+
let winpython_root = dir.path().join("WPy64-31300");
494+
fs::create_dir_all(&winpython_root).unwrap();
495+
496+
let python_folder = winpython_root.join("python-3.13.0.amd64");
497+
fs::create_dir_all(&python_folder).unwrap();
498+
let python_exe = python_folder.join("python.exe");
499+
File::create(&python_exe).unwrap();
500+
501+
let result = find_winpython_root(&python_exe);
502+
assert!(result.is_some());
503+
let (root, folder) = result.unwrap();
504+
assert_eq!(root, winpython_root);
505+
assert_eq!(folder, python_folder);
506+
}
507+
508+
#[test]
509+
fn test_find_winpython_root_not_winpython() {
510+
let dir = tempdir().unwrap();
511+
512+
// Create a regular Python structure (not WinPython)
513+
let python_folder = dir.path().join("some-random-folder");
514+
fs::create_dir_all(&python_folder).unwrap();
515+
516+
#[cfg(windows)]
517+
let python_exe = python_folder.join("python.exe");
518+
#[cfg(not(windows))]
519+
let python_exe = python_folder.join("python");
520+
521+
File::create(&python_exe).unwrap();
522+
523+
let result = find_winpython_root(&python_exe);
524+
assert!(result.is_none());
525+
}
526+
527+
#[test]
528+
fn test_winpython_locator_kind() {
529+
let locator = WinPython::new();
530+
assert_eq!(locator.get_kind(), LocatorKind::WinPython);
531+
}
532+
533+
#[test]
534+
fn test_winpython_supported_categories() {
535+
let locator = WinPython::new();
536+
let categories = locator.supported_categories();
537+
assert_eq!(categories.len(), 1);
538+
assert_eq!(categories[0], PythonEnvironmentKind::WinPython);
539+
}
398540
}

0 commit comments

Comments
 (0)