Skip to content

Commit 2845375

Browse files
committed
fix(command-vendor): reproduce the panic of cp_sources
1 parent 28163c0 commit 2845375

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/cargo/ops/vendor.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::collections::HashSet;
2020
use std::collections::{BTreeMap, BTreeSet, HashMap};
2121
use std::ffi::OsStr;
2222
use std::fs::{self, File, OpenOptions};
23-
use std::io::{Read, Write};
23+
use std::io::{self, Read, Write};
2424
use std::path::{Path, PathBuf};
2525

2626
pub struct VendorOptions<'a> {
@@ -291,7 +291,20 @@ fn sync(
291291
.tempdir_in(vendor_dir)?;
292292
let unpacked_src =
293293
registry.unpack_package_in(id, staging_dir.path(), &vendor_this)?;
294-
if let Err(e) = fs::rename(&unpacked_src, &dst) {
294+
295+
let rename_result = if gctx
296+
.get_env_os("__CARGO_TEST_VENDOR_FALLBACK_CP_SOURCES")
297+
.is_some()
298+
{
299+
Err(io::Error::new(
300+
io::ErrorKind::Other,
301+
"simulated rename error for testing",
302+
))
303+
} else {
304+
fs::rename(&unpacked_src, &dst)
305+
};
306+
307+
if let Err(e) = rename_result {
295308
// This fallback is mainly for Windows 10 versions earlier than 1607.
296309
// The destination of `fs::rename` can't be a directory in older versions.
297310
// Can be removed once the minimal supported Windows version gets bumped.

tests/testsuite/vendor.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,3 +2086,38 @@ Caused by:
20862086
"#]])
20872087
.run();
20882088
}
2089+
2090+
#[cargo_test]
2091+
fn vendor_rename_fallback() {
2092+
let p = project()
2093+
.file(
2094+
"Cargo.toml",
2095+
r#"
2096+
[package]
2097+
name = "foo"
2098+
version = "0.1.0"
2099+
2100+
[dependencies]
2101+
log = "0.3.5"
2102+
"#,
2103+
)
2104+
.file("src/lib.rs", "")
2105+
.build();
2106+
2107+
Package::new("log", "0.3.5").publish();
2108+
2109+
p.cargo("vendor --respect-source-config --no-delete")
2110+
.env("CARGO_LOG", "cargo::ops::vendor=warn")
2111+
.env("__CARGO_TEST_VENDOR_FALLBACK_CP_SOURCES", "true")
2112+
.with_status(101)
2113+
.with_stderr_data(str![[r#"
2114+
...
2115+
[..]failed to `mv "[..]vendor[..].vendor-staging[..]log-0.3.5" "[..]vendor[..]log"`: simulated rename error for testing
2116+
...
2117+
[..]StripPrefixError[..]
2118+
...
2119+
"#]])
2120+
.run();
2121+
2122+
assert!(!p.root().join("vendor/log/Cargo.toml").exists());
2123+
}

0 commit comments

Comments
 (0)