test: Add test demonstrating cargo add replaces symlinks

This test shows the current behavior where cargo add replaces
symlinked Cargo.toml files with regular files. The test passes,
documenting this problematic behavior.
This commit is contained in:
Raghavender Singh 2025-05-30 00:25:18 +05:30
parent 6aa6f44048
commit 35d2a30a5c
2 changed files with 53 additions and 0 deletions

View File

@ -145,6 +145,7 @@ mod script_bare;
mod script_frontmatter;
mod script_shebang;
mod sorted_table_with_dotted_item;
mod symlink;
mod target;
mod target_cfg;
mod unknown_inherited_feature;

View File

@ -0,0 +1,52 @@
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::registry;
use std::fs;
#[cargo_test]
fn symlink_case() {
if !cargo_test_support::symlink_supported() {
return;
}
registry::init();
registry::Package::new("test-dep", "1.0.0").publish();
let project = project().file("src/lib.rs", "").build();
let target_dir = project.root().join("target_dir");
fs::create_dir_all(&target_dir).unwrap();
fs::copy(
project.root().join("Cargo.toml"),
target_dir.join("Cargo.toml"),
)
.unwrap();
fs::remove_file(project.root().join("Cargo.toml")).unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
symlink(
target_dir.join("Cargo.toml"),
project.root().join("Cargo.toml"),
)
.unwrap();
}
#[cfg(windows)]
{
use std::os::windows::fs::symlink_file;
symlink_file(
target_dir.join("Cargo.toml"),
project.root().join("Cargo.toml"),
)
.unwrap();
}
project.cargo("add test-dep").run();
// Current behavior: symlink is NOT preserved (gets replaced)
assert!(!project.root().join("Cargo.toml").is_symlink());
}