Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions crates/socket-patch-cli/tests/vendor_jvm_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,140 @@ fn maven_reactor_uncertain_property_version_withholds_vex() {
}
}

/// #2378: Maven reads the version a module declares before any pin, so a
/// module that requires the base version as the hard range `[1.0]`, or
/// selects it from a range of its own, keeps the unpatched jar. `vex` does
/// not attest it, `vendor --check` says so for the hard range (what an
/// open range selects depends on the local repository, which the check
/// does not read), and with nothing else to wire `vendor` refuses rather
/// than counting the patch as applied over a pin that nothing reads.
#[test]
fn maven_reactor_declared_range_on_the_base_withholds_vex() {
// (what `a` declares, a sibling at the literal base, `b` added after
// vendoring and what it declares)
let cases: [(&str, bool, Option<&str>); 5] = [
("[1.0]", false, None),
("[1.0]", true, None),
("1.0", false, Some("[1.0]")),
("1.0", false, Some("[1.0,2.0)")),
("1.0", false, Some("[2.0]")),
];
for (declared, sibling, late) in cases {
let case = format!("a: {declared}, sibling: {sibling}, late: {late:?}");
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
fixture(root, Shape::Reactor, &[("foo", FOO_UUID)]);
// What Maven lists when it resolves a range of `foo`.
write(
root,
"m2/org/example/foo/maven-metadata-local.xml",
b"<metadata><groupId>org.example</groupId><artifactId>foo</artifactId>\
<versioning><versions><version>1.0</version></versions></versioning></metadata>\n",
);
let pristine = std::fs::read_to_string(root.join("proj/a/pom.xml")).unwrap();
let declaring = |name: &str, version: &str| {
pristine
.replace(
"<artifactId>a</artifactId>",
&format!("<artifactId>{name}</artifactId>"),
)
.replace(
"<version>1.0</version></dependency>",
&format!("<version>{version}</version></dependency>"),
)
};
let add_b = |version: &str| {
let agg = std::fs::read_to_string(root.join("proj/pom.xml")).unwrap();
let agg = agg.replace(
"<module>a</module>",
"<module>a</module>\n <module>b</module>",
);
write(root, "proj/pom.xml", agg.as_bytes());
write(root, "proj/b/pom.xml", declaring("b", version).as_bytes());
};
let a = declaring("a", declared);
write(root, "proj/a/pom.xml", a.as_bytes());
if sibling {
add_b("1.0");
}
let vex = root.join("vex.json");
let vex_args = [
"vex",
"-O",
vex.to_str().unwrap(),
"--product",
"pkg:generic/x@1",
];
if declared != "1.0" && !sibling {
assert_refused(root, "nothing_wired", "reason: range: a/pom.xml:6: ");
let (_, env) = socket(root, &vex_args);
assert!(!env.to_string().contains("not_affected"), "{case}: {env}");
assert!(!vex.exists(), "{case}: {env}");
continue;
}
let env = ok(root, &["vendor"]);
assert_eq!(
env.to_string().contains("reason: range: a/pom.xml:6: "),
declared != "1.0",
"{case}: {env}"
);
assert_eq!(
std::fs::read_to_string(root.join("proj/a/pom.xml")).unwrap() == a,
declared != "1.0",
"{case}"
);
assert!(wiring_text(root).contains("-socket."), "{case}: wired");
if let Some(version) = late {
add_b(version);
}

// The entry is intact and wired either way: the check passes, and
// names the declaration it can tell keeps the base version.
let env = ok(root, &["vendor", "--check"]);
let unattested: Vec<&str> = env["warnings"]
.as_array()
.into_iter()
.flatten()
.filter(|w| w["code"] == "vendor_jvm_unattested")
.filter_map(|w| w["detail"].as_str())
.collect();
let hard = match (declared, late) {
("[1.0]", _) => Some("a/pom.xml:6"),
(_, Some("[1.0]")) => Some("b/pom.xml:6"),
_ => None,
};
match hard {
Some(at) => assert!(
unattested.len() == 1
&& unattested[0].contains(&format!("{at} may still build org.example:foo:1.0")),
"{case}: {env}"
),
None => assert!(unattested.is_empty(), "{case}: {env}"),
}

let (code, env) = socket(root, &vex_args);
if late == Some("[2.0]") {
// `b` requires another version: nothing of this patch.
assert_eq!(code, Some(0), "{case}: {env}");
let doc: serde_json::Value =
serde_json::from_slice(&std::fs::read(&vex).unwrap()).unwrap();
assert_eq!(doc["statements"][0]["status"], "not_affected", "{case}");
continue;
}
let withheld = match hard {
Some(_) => "vendor_unwired",
None => "vendor_jvm_shape_unsupported",
};
assert_eq!(
events(&env),
[(purl("foo"), "skipped".to_string(), withheld.to_string())],
"{case}: {env}"
);
assert!(!env.to_string().contains("not_affected"), "{case}: {env}");
assert!(!vex.exists(), "{case}: {env}");
}
}

/// #584: a module added after vendoring that declares the base literally
/// builds the unpatched jar. `vendor --check` flags the drift and `vex`
/// withholds attestation until a re-vendor rewrites the module.
Expand Down
Loading
Loading