Skip to content
89 changes: 89 additions & 0 deletions crates/socket-patch-cli/tests/e2e_golang_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,95 @@ async fn agent_get_preserves_native_vendor_warning() {
chmod_writable(tmp.path());
}

/// #2413: with a go.work in effect a scan asks the patch API about the
/// modules the workspace builds, as a plain module's scan does. A module
/// another project left in the machine's module cache is the machine's,
/// not the project's: it is not named to the API, and it is not offered a
/// patch whose `replace` the build would ignore.
#[tokio::test(flavor = "multi_thread")]
async fn agent_scan_in_a_workspace_asks_only_about_the_modules_it_builds() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
if !golang_e2e_matrix::toolchain_ready("agent workspace scan") {
return;
}
let tmp = tempfile::tempdir().unwrap();
let (consumer, cache, _proxy) = stage(tmp.path());
// What another project's build extracted into the same cache.
let stranger = cache.join("example.com/stranger@v1.0.0");
std::fs::create_dir_all(&stranger).unwrap();
std::fs::write(stranger.join("go.mod"), "module example.com/stranger\n").unwrap();

let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v0/orgs/test/patches/batch"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"packages": [], "canAccessPaidPatches": true
})))
.mount(&server)
.await;
let uri = server.uri();
let asked = |label: &str| {
let (code, so, se) = common::run_with_env(
&consumer,
&[
"scan",
"--mode",
"agent",
"--json",
"--yes",
"--ecosystems",
"golang",
"--api-url",
&uri,
"--org",
"test",
"--api-token",
"fake",
],
&[("GOMODCACHE", cache.to_str().unwrap())],
);
assert_eq!(code, 0, "{label}: {so} {se}");
so
};
// The control: the module on its own.
asked("module mode");
// The same module as the one member of a workspace, then as a member
// of a workspace rooted above it.
std::fs::write(consumer.join("go.work"), "go 1.21\nuse .\n").unwrap();
asked("go.work beside go.mod");
std::fs::remove_file(consumer.join("go.work")).unwrap();
let above = consumer.parent().unwrap().join("go.work");
let member = consumer.file_name().unwrap().to_str().unwrap();
std::fs::write(&above, format!("go 1.21\nuse ./{member}\n")).unwrap();
asked("go.work above the member");
std::fs::remove_file(&above).unwrap();

let batches: Vec<Vec<String>> = server
.received_requests()
.await
.unwrap()
.iter()
.map(|r| {
let body: serde_json::Value = serde_json::from_slice(&r.body).unwrap();
let mut purls: Vec<String> = body["components"]
.as_array()
.unwrap()
.iter()
.map(|c| c["purl"].as_str().unwrap().to_string())
.collect();
purls.sort();
purls
})
.collect();
assert_eq!(
batches,
vec![vec![UPURL.to_string()]; 3],
"each scan names the one module the build links"
);
chmod_writable(tmp.path());
}

#[test]
fn native_vendor_projection_and_workspace_selection() {
if !golang_e2e_matrix::toolchain_ready("native vendor projection") {
Expand Down
Loading
Loading