Skip to content

Commit

Permalink
Remove workspace.members in Cargo.toml from sdist if there isn't …
Browse files Browse the repository at this point in the history
…any path dependency
  • Loading branch information
messense committed Oct 30, 2022
1 parent e8b6df4 commit 0d18bda
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 13 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add `--src` option to generate src layout for mixed Python/Rust projects in [#1189](https://github.com/PyO3/maturin/pull/1189)
* Add Python metadata support for `license-file` field of `Cargo.toml` in [#1195](https://github.com/PyO3/maturin/pull/1195)
* Upgrade to clap 4.0 in [#1197](https://github.com/PyO3/maturin/pull/1197). This bumps MSRV to 1.61.0.
* Remove `workspace.members` in `Cargo.toml` from sdist if there isn't any path dependency in #[1227](https://github.com/PyO3/maturin/pull/1227)

## [0.13.7] - 2022-10-29

Expand Down
33 changes: 22 additions & 11 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,31 @@ fn rewrite_cargo_toml(
// Update workspace members
if let Some(workspace) = data.get_mut("workspace").and_then(|x| x.as_table_mut()) {
if let Some(members) = workspace.get_mut("members").and_then(|x| x.as_array_mut()) {
let mut new_members = toml_edit::Array::new();
for member in members.iter() {
if let toml_edit::Value::String(ref s) = member {
let path = Path::new(s.value());
if let Some(name) = path.file_name().and_then(|x| x.to_str()) {
if known_path_deps.contains_key(name) {
new_members.push(format!("{}/{}", LOCAL_DEPENDENCIES_FOLDER, name));
if known_path_deps.is_empty() {
// Remove workspace members when there isn't any path dep
workspace.remove("members");
if workspace.is_empty() {
// Remove workspace all together if it's empty
data.remove("workspace");
}
rewritten = true;
} else {
let mut new_members = toml_edit::Array::new();
for member in members.iter() {
if let toml_edit::Value::String(ref s) = member {
let path = Path::new(s.value());
if let Some(name) = path.file_name().and_then(|x| x.to_str()) {
if known_path_deps.contains_key(name) {
new_members
.push(format!("{}/{}", LOCAL_DEPENDENCIES_FOLDER, name));
}
}
}
}
}
if !new_members.is_empty() {
workspace["members"] = toml_edit::value(new_members);
rewritten = true;
if !new_members.is_empty() {
workspace["members"] = toml_edit::value(new_members);
rewritten = true;
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test-crates/pyo3-pure/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test-crates/pyo3-pure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pyo3 = { version = "0.17.2", features = ["abi3-py37", "extension-module", "gener
[lib]
name = "pyo3_pure"
crate-type = ["cdylib"]

[workspace]
members = [".", "local-test"]
7 changes: 7 additions & 0 deletions test-crates/pyo3-pure/local-test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test-crates/pyo3-pure/local-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "local-test"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
14 changes: 14 additions & 0 deletions test-crates/pyo3-pure/local-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
18 changes: 17 additions & 1 deletion tests/common/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use flate2::read::GzDecoder;
use maturin::{BuildOptions, CargoOptions};
use pretty_assertions::assert_eq;
use std::collections::BTreeSet;
use std::io::Read;
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use tar::Archive;
Expand Down Expand Up @@ -114,6 +115,7 @@ pub fn test_workspace_cargo_lock() -> Result<()> {
pub fn test_source_distribution(
package: impl AsRef<Path>,
expected_files: Vec<&str>,
expected_cargo_toml: Option<(&Path, &str)>,
unique_name: &str,
) -> Result<()> {
let manifest_path = package.as_ref().join("Cargo.toml");
Expand Down Expand Up @@ -142,16 +144,30 @@ pub fn test_source_distribution(
let mut archive = Archive::new(tar);
let mut files = BTreeSet::new();
let mut file_count = 0;
let mut cargo_toml = None;
for entry in archive.entries()? {
let entry = entry?;
let mut entry = entry?;
files.insert(format!("{}", entry.path()?.display()));
file_count += 1;
if let Some(cargo_toml_path) = expected_cargo_toml.as_ref().map(|(p, _)| *p) {
if entry.path()? == cargo_toml_path {
let mut contents = String::new();
entry.read_to_string(&mut contents)?;
cargo_toml = Some(contents);
}
}
}
assert_eq!(
files,
BTreeSet::from_iter(expected_files.into_iter().map(ToString::to_string))
);
assert_eq!(file_count, files.len(), "duplicated files found in sdist");

if let Some((cargo_toml_path, expected)) = expected_cargo_toml {
let cargo_toml = cargo_toml
.with_context(|| format!("{} not found in sdist", cargo_toml_path.display()))?;
assert_eq!(cargo_toml, expected);
}
Ok(())
}

Expand Down
48 changes: 47 additions & 1 deletion tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use common::{
develop, editable, errors, get_python_implementation, handle_result, integration, other,
test_python_path,
};
use indoc::indoc;
use maturin::Target;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

mod common;

Expand Down Expand Up @@ -375,6 +376,47 @@ fn workspace_cargo_lock() {
handle_result(other::test_workspace_cargo_lock())
}

#[test]
fn workspace_members_non_local_dep_sdist() {
let cargo_toml = indoc!(
r#"
[package]
authors = ["konstin <[email protected]>"]
name = "pyo3-pure"
version = "2.1.2"
edition = "2018"
description = "Implements a dummy function (get_fortytwo.DummyClass.get_42()) in rust"
license = "MIT"
[dependencies]
pyo3 = { version = "0.17.2", features = ["abi3-py37", "extension-module", "generate-import-lib"] }
[lib]
name = "pyo3_pure"
crate-type = ["cdylib"]
"#
);
handle_result(other::test_source_distribution(
"test-crates/pyo3-pure",
vec![
"pyo3_pure-0.1.0+abc123de/Cargo.lock",
"pyo3_pure-0.1.0+abc123de/Cargo.toml",
"pyo3_pure-0.1.0+abc123de/LICENSE",
"pyo3_pure-0.1.0+abc123de/PKG-INFO",
"pyo3_pure-0.1.0+abc123de/README.md",
"pyo3_pure-0.1.0+abc123de/Readme.md",
"pyo3_pure-0.1.0+abc123de/check_installed/check_installed.py",
"pyo3_pure-0.1.0+abc123de/pyo3_pure.pyi",
"pyo3_pure-0.1.0+abc123de/pyproject.toml",
"pyo3_pure-0.1.0+abc123de/src/lib.rs",
"pyo3_pure-0.1.0+abc123de/tests/test_pyo3_pure.py",
"pyo3_pure-0.1.0+abc123de/tox.ini",
],
Some((Path::new("pyo3_pure-0.1.0+abc123de/Cargo.toml"), cargo_toml)),
"sdist-workspace-members-non-local-dep",
))
}

#[test]
fn lib_with_path_dep_sdist() {
handle_result(other::test_source_distribution(
Expand All @@ -390,6 +432,7 @@ fn lib_with_path_dep_sdist() {
"sdist_with_path_dep-0.1.0/src/lib.rs",
"sdist_with_path_dep-0.1.0/PKG-INFO",
],
None,
"sdist-lib-with-path-dep",
))
}
Expand All @@ -408,6 +451,7 @@ fn pyo3_mixed_src_layout_sdist() {
"pyo3_mixed_src-2.1.3/rust/src/lib.rs",
"pyo3_mixed_src-2.1.3/PKG-INFO",
],
None,
"sdist-pyo3-mixed-src-layout",
))
}
Expand All @@ -426,6 +470,7 @@ fn workspace_with_path_dep_sdist() {
"workspace_with_path_dep-0.1.0/src/lib.rs",
"workspace_with_path_dep-0.1.0/PKG-INFO",
],
None,
"sdist-workspace-with-path-dep",
))
}
Expand All @@ -443,6 +488,7 @@ fn workspace_inheritance_sdist() {
"workspace_inheritance-0.1.0/src/lib.rs",
"workspace_inheritance-0.1.0/PKG-INFO",
],
None,
"sdist-workspace-inheritance",
))
}
Expand Down

0 comments on commit 0d18bda

Please sign in to comment.