Skip to content

Commit b7f18b4

Browse files
committed
Add source distribution test cases
1 parent 84cab30 commit b7f18b4

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

test-crates/lib_with_path_dep/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/common/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub fn pyo3_no_extension_module() -> Result<()> {
6262
///
6363
/// https://github.com/PyO3/maturin/issues/472
6464
pub fn locked_doesnt_build_without_cargo_lock() -> Result<()> {
65+
// rm Cargo.lock first
66+
let _ = fs_err::remove_file("test-crates/lib_with_path_dep/Cargo.lock");
6567
// The first argument is ignored by clap
6668
let cli = vec![
6769
"build",

tests/common/other.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
2+
use cargo_metadata::MetadataCommand;
3+
use flate2::read::GzDecoder;
24
use maturin::BuildOptions;
5+
use maturin::{source_distribution, CargoToml, Metadata21};
6+
use std::collections::HashSet;
7+
use std::iter::FromIterator;
8+
use std::path::Path;
39
use structopt::StructOpt;
10+
use tar::Archive;
411

512
/// Tries to compile a sample crate (pyo3-pure) for musl,
613
/// given that rustup and the the musl target are installed
@@ -97,3 +104,42 @@ pub fn test_workspace_cargo_lock() -> Result<()> {
97104

98105
Ok(())
99106
}
107+
108+
pub fn test_source_distribution(
109+
package: impl AsRef<Path>,
110+
expected_files: Vec<&str>,
111+
) -> Result<()> {
112+
let manifest_dir = package.as_ref();
113+
let manifest_path = manifest_dir.join("Cargo.toml");
114+
let cargo_toml = CargoToml::from_path(&manifest_path)?;
115+
let metadata21 = Metadata21::from_cargo_toml(&cargo_toml, &manifest_dir)
116+
.context("Failed to parse Cargo.toml into python metadata")?;
117+
let cargo_metadata = MetadataCommand::new()
118+
.manifest_path(&manifest_path)
119+
.exec()
120+
.context("Cargo metadata failed. Do you have cargo in your PATH?")?;
121+
122+
let sdist_directory = tempfile::tempdir()?;
123+
let path = source_distribution(
124+
&sdist_directory,
125+
&metadata21,
126+
&manifest_path,
127+
&cargo_metadata,
128+
None,
129+
)
130+
.context("Failed to build source distribution")?;
131+
132+
let tar_gz = fs_err::File::open(path)?;
133+
let tar = GzDecoder::new(tar_gz);
134+
let mut archive = Archive::new(tar);
135+
let mut files = HashSet::new();
136+
for entry in archive.entries()? {
137+
let entry = entry?;
138+
files.insert(format!("{}", entry.path()?.display()));
139+
}
140+
assert_eq!(
141+
files,
142+
HashSet::from_iter(expected_files.into_iter().map(ToString::to_string))
143+
);
144+
Ok(())
145+
}

tests/run.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,38 @@ fn musl() {
152152
fn workspace_cargo_lock() {
153153
handle_result(other::test_workspace_cargo_lock())
154154
}
155+
156+
#[test]
157+
fn lib_with_path_dep_sdist() {
158+
handle_result(other::test_source_distribution(
159+
"test-crates/lib_with_path_dep",
160+
vec![
161+
"lib_with_path_dep-0.1.0/local_dependencies/some_path_dep/Cargo.toml",
162+
"lib_with_path_dep-0.1.0/local_dependencies/some_path_dep/src/lib.rs",
163+
"lib_with_path_dep-0.1.0/local_dependencies/transitive_path_dep/Cargo.toml",
164+
"lib_with_path_dep-0.1.0/local_dependencies/transitive_path_dep/src/lib.rs",
165+
"lib_with_path_dep-0.1.0/Cargo.toml",
166+
"lib_with_path_dep-0.1.0/pyproject.toml",
167+
"lib_with_path_dep-0.1.0/src/lib.rs",
168+
"lib_with_path_dep-0.1.0/test.sh",
169+
"lib_with_path_dep-0.1.0/PKG-INFO",
170+
],
171+
))
172+
}
173+
174+
#[test]
175+
fn workspace_with_path_dep_sdist() {
176+
handle_result(other::test_source_distribution(
177+
"test-crates/workspace_with_path_dep/python",
178+
vec![
179+
"workspace_with_path_dep-0.1.0/local_dependencies/generic_lib/Cargo.toml",
180+
"workspace_with_path_dep-0.1.0/local_dependencies/generic_lib/src/lib.rs",
181+
"workspace_with_path_dep-0.1.0/local_dependencies/transitive_lib/Cargo.toml",
182+
"workspace_with_path_dep-0.1.0/local_dependencies/transitive_lib/src/lib.rs",
183+
"workspace_with_path_dep-0.1.0/Cargo.toml",
184+
"workspace_with_path_dep-0.1.0/pyproject.toml",
185+
"workspace_with_path_dep-0.1.0/src/lib.rs",
186+
"workspace_with_path_dep-0.1.0/PKG-INFO",
187+
],
188+
))
189+
}

0 commit comments

Comments
 (0)