#[cfg(all(windows, feature = "junctions"))]
#[test]
fn test_junction_inside_boundary_prefix_mismatch() {
use crate::PathBoundary;
let td = tempfile::tempdir().unwrap();
let test_dir = PathBoundary::<()>::try_new_create(td.path()).unwrap();
let target_dir = test_dir.strict_join("target").unwrap();
target_dir.create_dir_all().unwrap();
target_dir
.strict_join("file.txt")
.unwrap()
.write(b"content")
.unwrap();
let link_dir = test_dir.strict_join("link").unwrap();
if let Err(e) = target_dir.strict_junction(link_dir.interop_path()) {
eprintln!("Skipping test: failed to create junction: {e:?}");
return;
}
match test_dir.strict_join("link/file.txt") {
Ok(path) => {
let path_str = path.strictpath_to_string_lossy();
println!("Resolved path: {path_str}");
assert!(path.strictpath_starts_with(test_dir.interop_path()));
assert!(path.exists());
}
Err(e) => {
panic!("Failed to join path through junction: {e:?}");
}
}
}
#[cfg(all(windows, feature = "junctions"))]
#[test]
fn test_junction_within_anchor_no_duplication() {
use crate::PathBoundary;
let td = tempfile::tempdir().unwrap();
let anchor = td.path();
let test_dir = PathBoundary::<()>::try_new_create(anchor).unwrap();
let data_target = test_dir.strict_join("data/target").unwrap();
data_target.create_dir_all().unwrap();
let links_dir = test_dir.strict_join("links").unwrap();
links_dir.create_dir_all().unwrap();
let junc_path = links_dir.strict_join("junc").unwrap();
if let Err(e) = data_target.strict_junction(junc_path.interop_path()) {
eprintln!("Skipping test: failed to create junction: {e:?}");
return;
}
let result = test_dir.strict_join("links/junc").unwrap();
let result_str = result.strictpath_to_string_lossy();
println!("Resolved path: {result_str}");
assert_eq!(
result_str.matches("data").count(),
1,
"Path should contain 'data' exactly once: {result_str}"
);
assert!(
result_str.contains("data") && result_str.contains("target"),
"Result should resolve through junction to data/target: {result_str}"
);
}
#[cfg(all(windows, feature = "junctions"))]
#[test]
fn test_junction_creation_with_verbatim_target() {
let td = tempfile::tempdir().unwrap();
let target_dir = td.path().join("target");
std::fs::create_dir(&target_dir).unwrap();
let canonical_target = std::fs::canonicalize(&target_dir).unwrap();
let canonical_str = canonical_target.to_string_lossy();
println!("Canonical target: {canonical_str}");
assert!(
canonical_str.starts_with(r"\\?\"),
"Canonicalized path should be verbatim, got: {canonical_str}"
);
let junction_path = td.path().join("test_junction");
if let Err(e) = junction::create(&canonical_target, &junction_path) {
panic!("Junction creation with verbatim target failed (DK26 fork should fix this): {e:?}");
}
std::fs::write(target_dir.join("test.txt"), "hello").unwrap();
let content = std::fs::read_to_string(junction_path.join("test.txt")).unwrap();
assert_eq!(content, "hello", "Junction should be functional");
println!("Junction with verbatim target works correctly");
}
#[cfg(all(windows, feature = "junctions"))]
#[test]
fn test_soft_canonicalize_handles_junction_prefix_mismatch() {
use soft_canonicalize::anchored_canonicalize;
let td = tempfile::tempdir().unwrap();
let anchor = std::fs::canonicalize(td.path()).unwrap();
let anchor_str = anchor.to_string_lossy();
println!("Canonicalized anchor: {anchor_str}");
assert!(
anchor_str.starts_with(r"\\?\"),
"Anchor should be verbatim for this test: {anchor_str}"
);
std::fs::create_dir_all(anchor.join("real_data")).unwrap();
if let Err(e) = junction::create(anchor.join("real_data"), anchor.join("link_to_data")) {
eprintln!("Skipping test: failed to create junction: {e:?}");
return;
}
let result = anchored_canonicalize(&anchor, "link_to_data").unwrap();
let result_str = result.to_string_lossy();
println!("anchored_canonicalize result: {result_str}");
assert!(
result_str.contains("real_data"),
"Result should contain 'real_data': {result_str}"
);
assert_eq!(
result_str.matches("real_data").count(),
1,
"Result should contain 'real_data' exactly once: {result_str}"
);
assert!(result.exists(), "Resolved path should exist: {result_str}");
}
#[cfg(all(windows, feature = "virtual-path", feature = "junctions"))]
#[test]
fn test_virtual_path_junction_prefix_mismatch() {
use crate::VirtualRoot;
let td = tempfile::tempdir().unwrap();
let vroot = VirtualRoot::<()>::try_new_create(td.path()).unwrap();
let target_dir = vroot.virtual_join("real_data").unwrap();
target_dir.create_dir_all().unwrap();
target_dir
.virtual_join("secret.txt")
.unwrap()
.write(b"secret content")
.unwrap();
let link_path = vroot.virtual_join("link_to_data").unwrap();
if let Err(e) = target_dir.virtual_junction(link_path.interop_path()) {
eprintln!("Skipping test: failed to create junction: {e:?}");
return;
}
let file_through_link = vroot.virtual_join("link_to_data/secret.txt").unwrap();
let virtual_display = file_through_link.virtualpath_display().to_string();
println!("Virtual display: {virtual_display}");
assert!(
virtual_display.starts_with('/'),
"Virtual display should be rooted: {virtual_display}"
);
let system_str = file_through_link
.as_unvirtual()
.strictpath_to_string_lossy();
println!("System path: {system_str}");
assert_eq!(
system_str.matches("real_data").count(),
1,
"System path should contain 'real_data' exactly once: {system_str}"
);
let content = file_through_link.read_to_string().unwrap();
assert_eq!(content, "secret content");
}