Skip to content

Commit 66b62d2

Browse files
Muscraftepage
authored andcommitted
test(publish): Demonstrate the impact of non-blocking publish
1 parent 8fadfe7 commit 66b62d2

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

tests/testsuite/publish.rs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,3 +2261,211 @@ fn http_api_not_noop() {
22612261

22622262
p.cargo("build").run();
22632263
}
2264+
2265+
#[cargo_test]
2266+
fn delayed_publish_errors() {
2267+
// Counter for number of tries before the package is "published"
2268+
let arc: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
2269+
let arc2 = arc.clone();
2270+
2271+
// Registry returns an invalid response.
2272+
let registry = registry::RegistryBuilder::new()
2273+
.http_index()
2274+
.http_api()
2275+
.add_responder("/index/de/la/delay", move |req, server| {
2276+
let mut lock = arc.lock().unwrap();
2277+
*lock += 1;
2278+
// if the package name contains _ or -
2279+
if *lock <= 1 {
2280+
server.not_found(req)
2281+
} else {
2282+
server.index(req)
2283+
}
2284+
})
2285+
.build();
2286+
2287+
// The sparse-registry test server does not know how to publish on its own.
2288+
// So let us call publish for it.
2289+
Package::new("delay", "0.0.1")
2290+
.file("src/lib.rs", "")
2291+
.publish();
2292+
2293+
let p = project()
2294+
.file(
2295+
"Cargo.toml",
2296+
r#"
2297+
[package]
2298+
name = "delay"
2299+
version = "0.0.1"
2300+
authors = []
2301+
license = "MIT"
2302+
description = "foo"
2303+
2304+
"#,
2305+
)
2306+
.file("src/lib.rs", "")
2307+
.build();
2308+
2309+
p.cargo("publish --no-verify -Z sparse-registry")
2310+
.masquerade_as_nightly_cargo(&["sparse-registry"])
2311+
.replace_crates_io(registry.index_url())
2312+
.with_status(0)
2313+
.with_stderr(
2314+
"\
2315+
[UPDATING] `crates-io` index
2316+
[WARNING] manifest has no documentation, [..]
2317+
See [..]
2318+
[PACKAGING] delay v0.0.1 ([CWD])
2319+
[UPLOADING] delay v0.0.1 ([CWD])
2320+
",
2321+
)
2322+
.run();
2323+
2324+
// Check nothing has touched the responder
2325+
let lock = arc2.lock().unwrap();
2326+
assert_eq!(*lock, 0);
2327+
drop(lock);
2328+
2329+
let p = project()
2330+
.file(
2331+
"Cargo.toml",
2332+
r#"
2333+
[package]
2334+
name = "foo"
2335+
version = "0.0.1"
2336+
authors = []
2337+
[dependencies]
2338+
delay = "0.0.1"
2339+
"#,
2340+
)
2341+
.file("src/main.rs", "fn main() {}")
2342+
.build();
2343+
2344+
p.cargo("build -Z sparse-registry")
2345+
.masquerade_as_nightly_cargo(&["sparse-registry"])
2346+
.with_status(101)
2347+
.with_stderr(
2348+
"\
2349+
[UPDATING] [..]
2350+
[ERROR] no matching package named `delay` found
2351+
location searched: registry `crates-io`
2352+
required by package `foo v0.0.1 ([..]/foo)`
2353+
",
2354+
)
2355+
.run();
2356+
2357+
let lock = arc2.lock().unwrap();
2358+
assert_eq!(*lock, 1);
2359+
drop(lock);
2360+
2361+
p.cargo("build -Z sparse-registry")
2362+
.masquerade_as_nightly_cargo(&["sparse-registry"])
2363+
.with_status(0)
2364+
.run();
2365+
}
2366+
2367+
/// A separate test is needed for package names with - or _ as they hit
2368+
/// the responder twice per cargo invocation. If that ever gets changed
2369+
/// this test will need to be changed accordingly.
2370+
#[cargo_test]
2371+
fn delayed_publish_errors_underscore() {
2372+
// Counter for number of tries before the package is "published"
2373+
let arc: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
2374+
let arc2 = arc.clone();
2375+
2376+
// Registry returns an invalid response.
2377+
let registry = registry::RegistryBuilder::new()
2378+
.http_index()
2379+
.http_api()
2380+
.add_responder("/index/de/la/delay_with_underscore", move |req, server| {
2381+
let mut lock = arc.lock().unwrap();
2382+
*lock += 1;
2383+
// package names with - or _ hit the responder twice per cargo invocation
2384+
if *lock <= 2 {
2385+
server.not_found(req)
2386+
} else {
2387+
server.index(req)
2388+
}
2389+
})
2390+
.build();
2391+
2392+
// The sparse-registry test server does not know how to publish on its own.
2393+
// So let us call publish for it.
2394+
Package::new("delay_with_underscore", "0.0.1")
2395+
.file("src/lib.rs", "")
2396+
.publish();
2397+
2398+
let p = project()
2399+
.file(
2400+
"Cargo.toml",
2401+
r#"
2402+
[package]
2403+
name = "delay_with_underscore"
2404+
version = "0.0.1"
2405+
authors = []
2406+
license = "MIT"
2407+
description = "foo"
2408+
2409+
"#,
2410+
)
2411+
.file("src/lib.rs", "")
2412+
.build();
2413+
2414+
p.cargo("publish --no-verify -Z sparse-registry")
2415+
.masquerade_as_nightly_cargo(&["sparse-registry"])
2416+
.replace_crates_io(registry.index_url())
2417+
.with_status(0)
2418+
.with_stderr(
2419+
"\
2420+
[UPDATING] `crates-io` index
2421+
[WARNING] manifest has no documentation, [..]
2422+
See [..]
2423+
[PACKAGING] delay_with_underscore v0.0.1 ([CWD])
2424+
[UPLOADING] delay_with_underscore v0.0.1 ([CWD])
2425+
",
2426+
)
2427+
.run();
2428+
2429+
// Check nothing has touched the responder
2430+
let lock = arc2.lock().unwrap();
2431+
assert_eq!(*lock, 0);
2432+
drop(lock);
2433+
2434+
let p = project()
2435+
.file(
2436+
"Cargo.toml",
2437+
r#"
2438+
[package]
2439+
name = "foo"
2440+
version = "0.0.1"
2441+
authors = []
2442+
[dependencies]
2443+
delay_with_underscore = "0.0.1"
2444+
"#,
2445+
)
2446+
.file("src/main.rs", "fn main() {}")
2447+
.build();
2448+
2449+
p.cargo("build -Z sparse-registry")
2450+
.masquerade_as_nightly_cargo(&["sparse-registry"])
2451+
.with_status(101)
2452+
.with_stderr(
2453+
"\
2454+
[UPDATING] [..]
2455+
[ERROR] no matching package named `delay_with_underscore` found
2456+
location searched: registry `crates-io`
2457+
required by package `foo v0.0.1 ([..]/foo)`
2458+
",
2459+
)
2460+
.run();
2461+
2462+
let lock = arc2.lock().unwrap();
2463+
// package names with - or _ hit the responder twice per cargo invocation
2464+
assert_eq!(*lock, 2);
2465+
drop(lock);
2466+
2467+
p.cargo("build -Z sparse-registry")
2468+
.masquerade_as_nightly_cargo(&["sparse-registry"])
2469+
.with_status(0)
2470+
.run();
2471+
}

0 commit comments

Comments
 (0)