Skip to content

Commit 1d9a087

Browse files
committed
test: Added test to verify registry cache write error emit warnings
1 parent 2939e96 commit 1d9a087

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

tests/testsuite/registry.rs

+73-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt::Write;
44
use std::fs::{self, File};
5-
use std::path::Path;
5+
use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77
use std::sync::Mutex;
88

@@ -3097,6 +3097,78 @@ fn readonly_registry_still_works() {
30973097
}
30983098
}
30993099

3100+
#[cargo_test(ignore_windows = "On Windows setting file attributes is a bit complicated")]
3101+
fn unaccessible_registry_cache_still_works() {
3102+
Package::new("foo", "0.1.0").publish();
3103+
Package::new("fo2", "0.1.0").publish();
3104+
3105+
let p = project()
3106+
.file(
3107+
"Cargo.toml",
3108+
r#"
3109+
[package]
3110+
name = "a"
3111+
version = "0.5.0"
3112+
edition = "2015"
3113+
authors = []
3114+
3115+
[dependencies]
3116+
foo = '0.1.0'
3117+
fo2 = '0.1.0'
3118+
"#,
3119+
)
3120+
.file("src/lib.rs", "")
3121+
.build();
3122+
3123+
p.cargo("generate-lockfile").run();
3124+
p.cargo("fetch --locked").run();
3125+
3126+
let cache_path = inner_dir(&paths::cargo_home().join("registry/index")).join(".cache");
3127+
let f_cache_path = cache_path.join("3/f");
3128+
3129+
// Remove the permissions from the cache path that contains the "foo" crate
3130+
set_permissions(&f_cache_path, 0o000);
3131+
3132+
// Now run a build and make sure we properly build and warn the user
3133+
p.cargo("build")
3134+
.with_stderr_data(str![[r#"
3135+
[WARNING] failed to write cache, path: [ROOT]/home/.cargo/registry/index/-[HASH]/.cache/3/f/fo[..], [ERROR] Permission denied (os error 13)
3136+
[COMPILING] fo[..] v0.1.0
3137+
[COMPILING] fo[..] v0.1.0
3138+
[COMPILING] a v0.5.0 ([ROOT]/foo)
3139+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
3140+
3141+
"#]])
3142+
.run();
3143+
// make sure we add the permissions to the files afterwards so "cargo clean" can remove them (#6934)
3144+
set_permissions(&f_cache_path, 0o777);
3145+
3146+
fn set_permissions(path: &Path, permissions: u32) {
3147+
#[cfg(not(windows))]
3148+
{
3149+
use std::os::unix::fs::PermissionsExt;
3150+
let mut perms = t!(path.metadata()).permissions();
3151+
perms.set_mode(permissions);
3152+
t!(fs::set_permissions(path, perms));
3153+
}
3154+
3155+
#[cfg(windows)]
3156+
panic!("This test is not supported on windows. See the reason in the #[cargo_test] macro");
3157+
}
3158+
3159+
fn inner_dir(path: &Path) -> PathBuf {
3160+
for entry in t!(path.read_dir()) {
3161+
let path = t!(entry).path();
3162+
3163+
if path.is_dir() {
3164+
return path;
3165+
}
3166+
}
3167+
3168+
panic!("could not find inner directory of {path:?}");
3169+
}
3170+
}
3171+
31003172
#[cargo_test]
31013173
fn registry_index_rejected_http() {
31023174
let _server = setup_http();

0 commit comments

Comments
 (0)