Skip to content

Commit b476ae8

Browse files
committed
Switch to test-fork for relative path tests
We have tests that check that handling of relative paths works as it should. These tests need to modify the process' current working directory, which is global state and can conceivably cause conflicts. Make sure to run these tests in a separate process by harnessing the test-fork crate.
1 parent b452f83 commit b476ae8

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

Cargo.lock

Lines changed: 28 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ tracing-subscriber = {version = "0.3", default-features = false, features = ["an
4646
warp = {version = "0.3", default-features = false}
4747

4848
[dev-dependencies]
49-
pathdiff = "0.2"
50-
tempfile = {version = "3.1"}
49+
tempfile = "3.1"
50+
test-fork = "0.1.3"
5151
tokio = {version = "1.34", default-features = false, features = ["macros", "rt"]}
5252

5353
# A set of unused dependencies that we require to force correct minimum

src/index.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2020-2024 The cargo-http-registry Developers
1+
// Copyright (C) 2020-2025 The cargo-http-registry Developers
22
// SPDX-License-Identifier: GPL-3.0-or-later
33

44
use std::collections::BTreeMap;
@@ -395,6 +395,8 @@ mod tests {
395395

396396
use tempfile::tempdir;
397397

398+
use test_fork::fork;
399+
398400

399401
#[test]
400402
fn url_port_parsing() {
@@ -488,15 +490,19 @@ mod tests {
488490
}
489491

490492
/// Ensure we can use some special names as relative index root.
493+
///
494+
/// Needs to run in separate process because it changes the working
495+
/// directory.
496+
#[fork]
491497
#[test]
492498
fn index_root_relative_path() {
493499
let base = tempdir().unwrap();
494-
env::set_current_dir(&base).unwrap();
500+
let () = env::set_current_dir(&base).unwrap();
495501
let addr = "127.0.0.1:0".parse().unwrap();
496502

497503
for special_name in ["config.json", "index"] {
498504
let relative_index_root = Path::new(special_name);
499-
create_dir_all(relative_index_root).unwrap();
505+
let () = create_dir_all(relative_index_root).unwrap();
500506

501507
let index = Index::new(relative_index_root, &addr).unwrap();
502508
// The repository should be clean.

tests/end-to-end.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ use std::process::Command;
1515
use anyhow::bail;
1616
use anyhow::Context as _;
1717
use anyhow::Result;
18-
use pathdiff::diff_paths;
18+
1919
use tempfile::tempdir;
2020

21+
use test_fork::fork;
22+
2123
use tokio::spawn;
2224
use tokio::task::JoinHandle;
25+
use tokio::test;
2326

2427
use cargo_http_registry::serve;
2528

@@ -167,11 +170,20 @@ where
167170

168171

169172
/// Serve our registry.
173+
///
174+
/// # Notes
175+
/// When invoked with `RegistryRootPath::Relative` this function changes
176+
/// the process' current working directory.
170177
fn serve_registry(root_path: RegistryRootPath) -> (JoinHandle<()>, PathBuf, SocketAddr) {
171178
let root = tempdir().unwrap();
172179
let path = match root_path {
173180
RegistryRootPath::Absolute => root.path().to_owned(),
174-
RegistryRootPath::Relative => diff_paths(root.path(), env::current_dir().unwrap()).unwrap(),
181+
RegistryRootPath::Relative => {
182+
let base_dir = root.path().parent().unwrap();
183+
let () = env::set_current_dir(base_dir).unwrap();
184+
let index_dir = root.path().file_name().unwrap();
185+
PathBuf::from(index_dir)
186+
},
175187
};
176188
let addr = "127.0.0.1:0".parse().unwrap();
177189

@@ -189,7 +201,11 @@ fn serve_registry(root_path: RegistryRootPath) -> (JoinHandle<()>, PathBuf, Sock
189201

190202

191203
/// Check that we can publish a crate.
192-
#[tokio::test]
204+
///
205+
/// Needs to run in separate process because it changes the working
206+
/// directory.
207+
#[test]
208+
#[fork]
193209
async fn publish() {
194210
async fn test(root_path: RegistryRootPath) {
195211
let (_handle, _reg_root, addr) = serve_registry(root_path);
@@ -220,7 +236,7 @@ async fn publish() {
220236

221237

222238
/// Check that we can publish crates with a renamed dependency.
223-
#[tokio::test]
239+
#[test]
224240
async fn publish_renamed() {
225241
let (_handle, _reg_root, addr) = serve_registry(RegistryRootPath::Absolute);
226242

@@ -319,15 +335,15 @@ async fn test_publish_and_consume(registry_locator: Locator) {
319335

320336

321337
/// Check that we can consume a published crate over HTTP.
322-
#[tokio::test]
338+
#[test]
323339
async fn get_http() {
324340
let (_handle, _, addr) = serve_registry(RegistryRootPath::Absolute);
325341
test_publish_and_consume(Locator::Socket(addr)).await
326342
}
327343

328344

329345
/// Check that we can consume a published crate through the file system.
330-
#[tokio::test]
346+
#[test]
331347
async fn get_filesystem() {
332348
let (_handle, root, _) = serve_registry(RegistryRootPath::Absolute);
333349
test_publish_and_consume(Locator::Path(root)).await

0 commit comments

Comments
 (0)