Skip to content

Commit f3b9cbb

Browse files
committed
test: standardize test crate path resolution using CARGO_MANIFEST_DIR
1 parent 493268b commit f3b9cbb

5 files changed

Lines changed: 28 additions & 14 deletions

File tree

src/build_options.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ mod tests {
153153
use super::*;
154154
use crate::bridge::{Abi3Version, PyO3, PyO3Crate, find_bridge};
155155
use crate::python_interpreter::InterpreterResolver;
156+
use crate::test_utils::test_crate_path;
156157
use crate::{BridgeModel, Target};
157158
use cargo_metadata::MetadataCommand;
158159
use insta::assert_snapshot;
@@ -161,7 +162,7 @@ mod tests {
161162
#[test]
162163
fn test_find_bridge_pyo3() {
163164
let pyo3_mixed = MetadataCommand::new()
164-
.manifest_path(Path::new("test-crates/pyo3-mixed").join("Cargo.toml"))
165+
.manifest_path(test_crate_path("pyo3-mixed").join("Cargo.toml"))
165166
.exec()
166167
.unwrap();
167168

@@ -180,7 +181,7 @@ mod tests {
180181
use crate::bridge::{PyO3Metadata, PyO3VersionMetadata};
181182

182183
let pyo3_pure = MetadataCommand::new()
183-
.manifest_path(Path::new("test-crates/pyo3-pure").join("Cargo.toml"))
184+
.manifest_path(test_crate_path("pyo3-pure").join("Cargo.toml"))
184185
.exec()
185186
.unwrap();
186187

@@ -206,14 +207,14 @@ mod tests {
206207
#[test]
207208
fn test_find_bridge_pyo3_feature() {
208209
let pyo3_pure = MetadataCommand::new()
209-
.manifest_path(Path::new("test-crates/pyo3-feature").join("Cargo.toml"))
210+
.manifest_path(test_crate_path("pyo3-feature").join("Cargo.toml"))
210211
.exec()
211212
.unwrap();
212213

213214
assert!(find_bridge(&pyo3_pure, None, None).is_err());
214215

215216
let pyo3_pure = MetadataCommand::new()
216-
.manifest_path(Path::new("test-crates/pyo3-feature").join("Cargo.toml"))
217+
.manifest_path(test_crate_path("pyo3-feature").join("Cargo.toml"))
217218
.other_options(vec!["--features=pyo3".to_string()])
218219
.exec()
219220
.unwrap();
@@ -227,7 +228,7 @@ mod tests {
227228
#[test]
228229
fn test_find_bridge_cffi() {
229230
let cffi_pure = MetadataCommand::new()
230-
.manifest_path(Path::new("test-crates/cffi-pure").join("Cargo.toml"))
231+
.manifest_path(test_crate_path("cffi-pure").join("Cargo.toml"))
231232
.exec()
232233
.unwrap();
233234

@@ -246,7 +247,7 @@ mod tests {
246247
#[test]
247248
fn test_find_bridge_bin() {
248249
let hello_world = MetadataCommand::new()
249-
.manifest_path(Path::new("test-crates/hello-world").join("Cargo.toml"))
250+
.manifest_path(test_crate_path("hello-world").join("Cargo.toml"))
250251
.exec()
251252
.unwrap();
252253

@@ -262,7 +263,7 @@ mod tests {
262263
assert!(find_bridge(&hello_world, Some("pyo3"), None).is_err());
263264

264265
let pyo3_bin = MetadataCommand::new()
265-
.manifest_path(Path::new("test-crates/pyo3-bin").join("Cargo.toml"))
266+
.manifest_path(test_crate_path("pyo3-bin").join("Cargo.toml"))
266267
.exec()
267268
.unwrap();
268269
assert!(matches!(

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ mod python_interpreter;
7878
mod sbom;
7979
mod source_distribution;
8080
mod target;
81+
#[cfg(test)]
82+
mod test_utils;
8183
#[cfg(feature = "upload")]
8284
mod upload;
8385
pub(crate) mod util;

src/metadata.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ fn fold_header(text: &str) -> String {
842842
#[cfg(test)]
843843
mod tests {
844844
use super::*;
845+
use crate::test_utils::test_crate_path;
845846
use cargo_metadata::MetadataCommand;
846847
use expect_test::{Expect, expect};
847848
use indoc::indoc;
@@ -966,7 +967,7 @@ A test project
966967

967968
#[test]
968969
fn test_merge_metadata_from_pyproject_toml() {
969-
let manifest_dir = PathBuf::from("test-crates").join("pyo3-pure");
970+
let manifest_dir = test_crate_path("pyo3-pure");
970971
let cargo_metadata = MetadataCommand::new()
971972
.manifest_path(manifest_dir.join("Cargo.toml"))
972973
.exec()
@@ -982,7 +983,7 @@ A test project
982983
);
983984
assert_eq!(
984985
metadata.description,
985-
Some(fs_err::read_to_string("test-crates/pyo3-pure/README.md").unwrap())
986+
Some(fs_err::read_to_string(manifest_dir.join("README.md")).unwrap())
986987
);
987988
assert_eq!(metadata.classifiers, &["Programming Language :: Rust"]);
988989
assert_eq!(
@@ -1015,7 +1016,7 @@ A test project
10151016

10161017
#[test]
10171018
fn test_merge_metadata_from_pyproject_toml_with_customized_python_source_dir() {
1018-
let manifest_dir = PathBuf::from("test-crates").join("pyo3-mixed-py-subdir");
1019+
let manifest_dir = test_crate_path("pyo3-mixed-py-subdir");
10191020
let cargo_metadata = MetadataCommand::new()
10201021
.manifest_path(manifest_dir.join("Cargo.toml"))
10211022
.exec()
@@ -1036,7 +1037,7 @@ A test project
10361037

10371038
#[test]
10381039
fn test_implicit_readme() {
1039-
let manifest_dir = PathBuf::from("test-crates").join("pyo3-mixed");
1040+
let manifest_dir = test_crate_path("pyo3-mixed");
10401041
let cargo_metadata = MetadataCommand::new()
10411042
.manifest_path(manifest_dir.join("Cargo.toml"))
10421043
.exec()
@@ -1051,7 +1052,7 @@ A test project
10511052

10521053
#[test]
10531054
fn test_pep639() {
1054-
let manifest_dir = PathBuf::from("test-crates").join("pyo3-mixed");
1055+
let manifest_dir = test_crate_path("pyo3-mixed");
10551056
let cargo_metadata = MetadataCommand::new()
10561057
.manifest_path(manifest_dir.join("Cargo.toml"))
10571058
.exec()
@@ -1068,7 +1069,7 @@ A test project
10681069

10691070
#[test]
10701071
fn test_merge_metadata_from_pyproject_dynamic_license_test() {
1071-
let manifest_dir = PathBuf::from("test-crates").join("license-test");
1072+
let manifest_dir = test_crate_path("license-test");
10721073
let cargo_metadata = MetadataCommand::new()
10731074
.manifest_path(manifest_dir.join("Cargo.toml"))
10741075
.exec()

src/pyproject_toml.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ impl PyProjectToml {
773773

774774
#[cfg(test)]
775775
mod tests {
776+
use crate::test_utils::test_crate_path;
776777
use crate::{
777778
PyProjectToml,
778779
pyproject_toml::{
@@ -852,7 +853,8 @@ mod tests {
852853

853854
#[test]
854855
fn test_warn_missing_maturin_version() {
855-
let with_constraint = PyProjectToml::new("test-crates/pyo3-pure/pyproject.toml").unwrap();
856+
let with_constraint =
857+
PyProjectToml::new(test_crate_path("pyo3-pure").join("pyproject.toml")).unwrap();
856858
assert!(with_constraint.warn_bad_maturin_version());
857859
let without_constraint_dir = TempDir::new().unwrap();
858860
let pyproject_file = without_constraint_dir.path().join("pyproject.toml");

src/test_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::path::{Path, PathBuf};
2+
3+
/// Resolve the path to a test crate.
4+
pub fn test_crate_path(name: &str) -> PathBuf {
5+
Path::new(env!("CARGO_MANIFEST_DIR"))
6+
.join("test-crates")
7+
.join(name)
8+
}

0 commit comments

Comments
 (0)