Skip to content

Commit 6aec07d

Browse files
committed
test: standardize test crate path resolution using CARGO_MANIFEST_DIR
1 parent 45c1010 commit 6aec07d

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;
@@ -162,7 +163,7 @@ mod tests {
162163
#[test]
163164
fn test_find_bridge_pyo3() {
164165
let pyo3_mixed = MetadataCommand::new()
165-
.manifest_path(Path::new("test-crates/pyo3-mixed").join("Cargo.toml"))
166+
.manifest_path(test_crate_path("pyo3-mixed").join("Cargo.toml"))
166167
.exec()
167168
.unwrap();
168169

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

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

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

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

216217
let pyo3_pure = MetadataCommand::new()
217-
.manifest_path(Path::new("test-crates/pyo3-feature").join("Cargo.toml"))
218+
.manifest_path(test_crate_path("pyo3-feature").join("Cargo.toml"))
218219
.other_options(vec!["--features=pyo3".to_string()])
219220
.exec()
220221
.unwrap();
@@ -228,7 +229,7 @@ mod tests {
228229
#[test]
229230
fn test_find_bridge_cffi() {
230231
let cffi_pure = MetadataCommand::new()
231-
.manifest_path(Path::new("test-crates/cffi-pure").join("Cargo.toml"))
232+
.manifest_path(test_crate_path("cffi-pure").join("Cargo.toml"))
232233
.exec()
233234
.unwrap();
234235

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

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

265266
let pyo3_bin = MetadataCommand::new()
266-
.manifest_path(Path::new("test-crates/pyo3-bin").join("Cargo.toml"))
267+
.manifest_path(test_crate_path("pyo3-bin").join("Cargo.toml"))
267268
.exec()
268269
.unwrap();
269270
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)