Skip to content

Commit 263131b

Browse files
StantonMattbbqsrc
authored andcommitted
test: cover output-dir cdylib package filtering
Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>
1 parent a674fea commit 263131b

1 file changed

Lines changed: 75 additions & 5 deletions

File tree

src/cli/mod.rs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -755,11 +755,7 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
755755
}
756756
}
757757

758-
for artifact in artifacts
759-
.iter()
760-
.filter(|a| artifact_is_cdylib(a))
761-
.filter(|a| a.package_id == current_package_id)
762-
{
758+
for artifact in copyable_cdylib_artifacts(artifacts, &current_package_id) {
763759
let Some(file) = artifact
764760
.filenames
765761
.iter()
@@ -824,6 +820,16 @@ fn artifact_is_cdylib(artifact: &Artifact) -> bool {
824820
artifact.target.crate_types.contains(&CrateType::CDyLib)
825821
}
826822

823+
fn copyable_cdylib_artifacts<'a>(
824+
artifacts: &'a [Artifact],
825+
current_package_id: &'a cargo_metadata::PackageId,
826+
) -> impl Iterator<Item = &'a Artifact> + 'a {
827+
artifacts
828+
.iter()
829+
.filter(|a| artifact_is_cdylib(a))
830+
.filter(move |a| &a.package_id == current_package_id)
831+
}
832+
827833
/// Check if the source file has changed and should be copied over to the destination path.
828834
#[inline]
829835
fn is_fresh(src: &Path, dest: &Path) -> anyhow::Result<bool> {
@@ -846,3 +852,67 @@ fn is_fresh(src: &Path, dest: &Path) -> anyhow::Result<bool> {
846852

847853
Ok(src <= dest)
848854
}
855+
856+
#[cfg(test)]
857+
mod tests {
858+
use cargo_metadata::Artifact;
859+
use serde_json::json;
860+
861+
use super::copyable_cdylib_artifacts;
862+
863+
fn artifact(package_id: &str, crate_types: &[&str], filenames: &[&str]) -> Artifact {
864+
serde_json::from_value(json!({
865+
"package_id": package_id,
866+
"manifest_path": format!("/workspace/{package_id}/Cargo.toml"),
867+
"target": {
868+
"kind": ["lib"],
869+
"crate_types": crate_types,
870+
"name": package_id,
871+
"src_path": format!("/workspace/{package_id}/src/lib.rs"),
872+
"edition": "2024",
873+
"doc": true,
874+
"doctest": true,
875+
"test": true
876+
},
877+
"profile": {
878+
"opt_level": "0",
879+
"debuginfo": 0,
880+
"debug_assertions": true,
881+
"overflow_checks": true,
882+
"test": false
883+
},
884+
"features": [],
885+
"filenames": filenames,
886+
"executable": null,
887+
"fresh": false
888+
}))
889+
.expect("test artifact should deserialize")
890+
}
891+
892+
#[test]
893+
fn copyable_cdylib_artifacts_ignores_workspace_cdylibs_from_other_packages() {
894+
let current = artifact(
895+
"path+file:///workspace/app#0.1.0",
896+
&["cdylib"],
897+
&["target/libapp.so"],
898+
);
899+
let sibling_without_so = artifact(
900+
"path+file:///workspace/helper#0.1.0",
901+
&["cdylib"],
902+
&["target/helper.dll"],
903+
);
904+
let sibling_with_so = artifact(
905+
"path+file:///workspace/other#0.1.0",
906+
&["cdylib"],
907+
&["target/libother.so"],
908+
);
909+
let current_package_id = current.package_id.clone();
910+
let artifacts = vec![sibling_without_so, sibling_with_so, current];
911+
912+
let selected = copyable_cdylib_artifacts(&artifacts, &current_package_id)
913+
.map(|artifact| artifact.package_id.repr.as_str())
914+
.collect::<Vec<_>>();
915+
916+
assert_eq!(selected, vec![current_package_id.repr.as_str()]);
917+
}
918+
}

0 commit comments

Comments
 (0)