Skip to content

Commit 9f2d0a3

Browse files
committed
test: Added test to verify registry cache write error emit warnings
1 parent 08facee commit 9f2d0a3

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

tests/testsuite/registry.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use std::fmt::Write;
44
use std::fs::{self, File};
5-
use std::path::Path;
5+
use std::os::unix::fs::PermissionsExt;
6+
use std::path::{Path, PathBuf};
67
use std::sync::Arc;
78
use std::sync::Mutex;
89

@@ -3097,6 +3098,68 @@ fn readonly_registry_still_works() {
30973098
}
30983099
}
30993100

3101+
#[cargo_test]
3102+
fn unaccessible_registry_cache_still_works() {
3103+
Package::new("foo", "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+
"#,
3118+
)
3119+
.file("src/lib.rs", "")
3120+
.build();
3121+
3122+
p.cargo("generate-lockfile").run();
3123+
p.cargo("fetch --locked").run();
3124+
3125+
let cache_path = inner_dir(&paths::cargo_home().join("registry/index")).join(".cache");
3126+
let f_cache_path = cache_path.join("3/f");
3127+
3128+
// Remove the permissions from the cache path that contains the "foo" crate
3129+
set_permissions(&f_cache_path, 0o000);
3130+
3131+
// Now run a build and make sure we properly build and warn the user
3132+
p.cargo("build")
3133+
.with_stderr_data(str![[r#"
3134+
[WARNING] failed to write cache, path: [ROOT]/home/.cargo/registry/index/-[HASH]/.cache/3/f/foo, [ERROR] Permission denied (os error 13)
3135+
[COMPILING] foo v0.1.0
3136+
[COMPILING] a v0.5.0 ([ROOT]/foo)
3137+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
3138+
3139+
"#]])
3140+
.run();
3141+
// make sure we add the permissions to the files afterwards so "cargo clean" can remove them (#6934)
3142+
set_permissions(&f_cache_path, 0o777);
3143+
3144+
fn set_permissions(path: &Path, permissions: u32) {
3145+
let mut perms = t!(path.metadata()).permissions();
3146+
perms.set_mode(permissions);
3147+
t!(fs::set_permissions(path, perms));
3148+
}
3149+
3150+
fn inner_dir(path: &Path) -> PathBuf {
3151+
for entry in t!(path.read_dir()) {
3152+
let path = t!(entry).path();
3153+
3154+
if path.is_dir() {
3155+
return path;
3156+
}
3157+
}
3158+
3159+
panic!("could not find inner directory of {path:?}");
3160+
}
3161+
}
3162+
31003163
#[cargo_test]
31013164
fn registry_index_rejected_http() {
31023165
let _server = setup_http();

0 commit comments

Comments
 (0)