Skip to content
Open
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
80 changes: 45 additions & 35 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions crates/socket-patch-cli/src/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7885,8 +7885,12 @@ mod scope_and_hint_tests {
/// where it can: that file is no file of the pin it restores. A pin
/// of that entry alone is ejected only where the hub is the project's
/// one build, and is none of the pins of a project that Maven builds.
/// Whether a hub is a build is read with `SOCKET_NO_BAZEL` from the
/// environment, so the test holds it unset.
#[test]
#[serial_test::serial]
fn a_takeover_leaves_the_entry_of_a_bazel_hub() {
let _unset = crate::test_env::EnvGuard::unset(socket_patch_core::bazel::OFF_ENV);
let pin = |purl: &str, files: &[&str]| HostedPin {
purl: purl.to_string(),
uuid: "1d3c1fd2-7b4e-4c1a-9f0e-2a3b4c5d6e7f".to_string(),
Expand Down
4 changes: 4 additions & 0 deletions crates/socket-patch-cli/src/commands/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4033,8 +4033,12 @@ mod bazel_tests {
/// project's, changes nothing. Nor does a run with `--vex-no-bazel`,
/// whose document is about the installed tree: the step returns before
/// it asks about a package, and every set it was given is as it was.
/// The lookup of the workspace reads `SOCKET_NO_BAZEL` from the
/// environment, so the test holds it unset.
#[tokio::test]
#[serial_test::serial]
async fn the_bazel_step_finds_what_no_hub_builds_in_a_project_run_only() {
let _unset = crate::test_env::EnvGuard::unset(socket_patch_core::bazel::OFF_ENV);
let tmp = tempfile::tempdir().unwrap();
let root = std::fs::canonicalize(tmp.path()).unwrap();
let module = "module(name = \"shop\")\n\
Expand Down
6 changes: 6 additions & 0 deletions crates/socket-patch-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ pub use socket_patch_core::hosted::memory as hosted_memory;
pub mod json_envelope;
pub mod path_scope;
pub(crate) mod telemetry_outcomes;
/// The core unit tests' env guard, compiled into this crate's unit tests
/// too: a test that judges by a variable holds it through the guard.
#[cfg(test)]
#[allow(dead_code)]
#[path = "../../socket-patch-core/src/test_env.rs"]
pub(crate) mod test_env;
pub mod ui;
pub mod update_notifier;

Expand Down
48 changes: 39 additions & 9 deletions crates/socket-patch-cli/tests/e2e_bazel_build/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@
//! the same.
//! - Bazel would read that file as a package of the user's workspace. The
//! crate has a dependency ([`DEP`]), so the file names a repository of
//! the hub, which the main repository does not see, and the commands
//! that analyse every target fail on it: `bazel cquery //...` and
//! `bazel query 'deps(//...)'` ([`analyses`]). The line that the scan
//! wrote into `.bazelignore` keeps the copies out of Bazel's packages:
//! the hub, which the main repository does not see, as it does not see
//! `platforms`, which the file names too. The commands that analyse
//! every target fail on the first of the two that Bazel meets:
//! `bazel cquery //...` and `bazel query 'deps(//...)'` ([`analyses`]).
//! The line that the scan wrote into `.bazelignore` keeps the copies out
//! of Bazel's packages:
//! `//...` is the user's own target ([`every_target`]), both commands
//! pass, and the build is the patched one, because the hub reads the
//! copy by its path, not as a package. A user who comments the line out
Expand Down Expand Up @@ -872,15 +874,42 @@ fn every_target(cell: &Cell) -> Vec<String> {
targets
}

/// Whether `stderr` says that the main repository does not see a
/// repository that the build file in the copy names: one of the hub, or
/// `platforms`. Bazel names the first one it meets, and which one that is
/// differs from run to run. The same words about another repository's view
/// are another failure.
fn unseen(stderr: &str) -> bool {
(stderr.split("No repository visible as '@").skip(1))
.filter_map(|rest| rest.split_once("' from main repository"))
.any(|(repo, _)| {
!repo.contains('\'') && (repo == "platforms" || repo.starts_with("crates__"))
})
}

/// [`unseen`] takes the two messages Bazel was seen to print, and neither
/// another repository's name nor another repository's view.
#[test]
fn an_unseen_repository_is_one_the_copy_names_and_the_main_repository_misses() {
let said = |repo: &str, from: &str| {
format!("ERROR: no such package: No repository visible as '@{repo}' from {from}\n")
};
assert!(unseen(&said("platforms", "main repository")));
assert!(unseen(&said("crates__dep-1.0.0", "main repository")));
assert!(!unseen(&said("rules_cc", "main repository")));
assert!(!unseen(&said("platforms", "repository '@@rules_rust+'")));
assert!(!unseen("Loading: 0 packages loaded\n"));
}

/// Whether the commands that analyse every target of the checkout of
/// `cell` pass: `bazel cquery //...` and `bazel query 'deps(//...)'`, which
/// agree. One that fails has met a repository of the hub, which the main
/// repository does not see.
/// agree. One that fails has met a repository that the build file in the
/// copy names and the main repository does not see ([`unseen`]).
fn analyses(cell: &Cell) -> bool {
let runs = [("cquery", "//..."), ("query", "deps(//...)")];
let runs = runs.map(|(command, targets)| cell.bazel(command, &[targets]));
for run in &runs {
let unseen = run.stderr().contains("No repository visible as '@crates__");
let unseen = unseen(&run.stderr());
assert_ne!(run.ok(), unseen, "{}: {}", cell.test, run.dump());
}
assert_eq!(runs[0].ok(), runs[1].ok(), "{}", cell.test);
Expand Down Expand Up @@ -1194,8 +1223,9 @@ fn vendored_from_cargo_with_config() {
// package: `//...` is the consumer alone, and the commands that analyse
// every target pass. A user who comments the line out has the package
// back, where the ruleset writes the build file: `//...` names targets
// there, those commands fail on the repository of the dependency, and
// the file Bazel now reads is a change of the copy again.
// there, those commands fail on a repository that the file names and
// the main repository does not see, and the file Bazel now reads is a
// change of the copy again.
assert_eq!(every_target(&cell), [CONSUMER], "{test}");
assert!(analyses(&cell), "{test}: under the line of the scan");
cell.write(".bazelignore", DECLINED);
Expand Down
78 changes: 78 additions & 0 deletions crates/socket-patch-cli/tests/e2e_bazel_build/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,84 @@ fn vendored_mode_goes_on_outside_bazel_and_on_the_users_word() {
reverts(ALLOW);
}

/// Without a Bazel: a project that a release before the refusal vendored,
/// in a checkout with a Bazel workspace file that no Bazel builds it from.
/// Its lock names the copy of the patch of then, so the project uses
/// vendored mode already and is not declined: the scan in vendored mode
/// vendors a newer patch of the package in the place of the older one, and
/// the scan that names no mode, which such a pipeline runs, then finds the
/// project in sync.
#[test]
fn a_project_vendored_by_an_earlier_release_is_not_declined_a_newer_patch() {
let (flow, newer) = checkout(&[]);
let (cell, upstream) = (&flow.cell, &flow.upstream);
let older = Patched::new();
let then = registry(upstream);
grant(&then, upstream, &older, &older.package);
let earlier = socket_patch(cell, &then, API, &VENDORED);
assert_eq!(earlier.code, 0, "{}", earlier.dump);
cell.write("MODULE.bazel", "");
let named = |tree: &Tree| -> Value {
let lock: Value = serde_json::from_slice(&tree[&lock_of(API)]).expect("a lock");
lock["packages"][0]["dist"]["url"].clone()
};
assert_eq!(named(&cell.tree()), copy_of(&older).as_str());

let scan = socket_patch(cell, &flow.standin, API, &VENDORED);
let said = &scan.dump;
assert_eq!(scan.code, 0, "{said}");
assert_eq!(scan.envelope["status"], "success", "{said}");
let update = json!([{ "purl": PURL, "oldUuid": older.uuid, "newUuid": newer.uuid }]);
assert_eq!(scan.envelope["updates"], update, "{said}");
let vendored = cell.tree();
assert_eq!(named(&vendored), copy_of(&newer).as_str(), "{said}");
let source = |patch: &Patched| vendored.get(&format!("{API}/{}/{FILE}", copy_of(patch)));
let patched = newer.package.source.as_bytes();
assert_eq!(source(&newer).map(Vec::as_slice), Some(patched), "{said}");
assert_eq!(source(&older), None, "{said}");

let again = socket_patch(cell, &flow.standin, API, &["scan", "--yes"]);
assert_eq!(again.code, 0, "{}", again.dump);
assert_eq!(again.envelope["status"], "success", "{}", again.dump);
assert_eq!(changed(&vendored, &cell.tree()), NOTHING, "{}", again.dump);
}

/// Without a Bazel: a regular file named `workspace`, in lower case, at
/// the root of a checkout that has no Bazel file. On a disk that folds
/// case it is what a lookup of `WORKSPACE` finds, and vendored mode
/// declines the project; on a disk that does not, the project is vendored.
/// The test asks the disk which of the two it runs on and says so.
#[test]
fn a_file_named_workspace_in_lower_case_declines_where_the_disk_folds_case() {
let (flow, patch) = checkout(&["workspace"]);
let before = flow.cell.tree();
let folds = flow.cell.path("WORKSPACE").is_file();
if folds {
declines(&flow, API, &VENDORED, 1, "failed", &before);
} else {
vendors(&flow, API, &patch, &before, &[]);
}
let ran = ["does not fold case: vendored", "folds case: declined"][usize::from(folds)];
println!("a file named `workspace`: the disk {ran}");
}

/// Without a Bazel: with `SOCKET_NO_BAZEL=1` vendored mode is not declined
/// in a Bazel workspace. The scan vendors the package as it does in the
/// same checkout without the workspace file, to the byte.
#[test]
fn the_switch_makes_vendored_mode_the_one_of_a_checkout_without_bazel() {
let (flow, _patch) = checkout(&["MODULE.bazel"]);
let origin = flow.standin.origin();
let packagist = format!("{origin}/{PACKAGIST}");
let env = [
("SOCKET_PATCH_SERVER_URL", origin.as_str()),
("SOCKET_PACKAGIST_URL", packagist.as_str()),
];
let args = [&VENDORED[..], &["--cwd", API]].concat();
let declined = (flow.cell).off_is_without_bazel(&origin, &args, &env, &["MODULE.bazel"]);
assert_eq!(declined[0]["code"], 1, "{declined:#}");
}

/// Without a Bazel, a PHP or a Composer: the fixture's directories and the
/// legs of its workflow are those of its table. The fixture commits no
/// lock, no `vendor/` tree and no root `BUILD.bazel`: the run makes them.
Expand Down
26 changes: 26 additions & 0 deletions crates/socket-patch-cli/tests/e2e_bazel_build/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,32 @@ fn a_project_in_no_bazel_workspace_gets_no_bazelignore() {
}
}

/// Without a Bazel: with `SOCKET_NO_BAZEL=1` a run that keeps a copy of a
/// Go module in a Bazel workspace writes no `.bazelignore` and says
/// nothing of one: it is the run of the same checkout without its
/// `MODULE.bazel`, to the byte, in vendored and in agent mode. What an
/// earlier run wrote is still undone with the variable set: the revert of
/// a copy takes its pair, and the file that was made for it, away.
#[test]
fn the_switch_adds_no_bazelignore_line_and_still_takes_one_away() {
let (cell, with) = (checkout("", "MODULE.bazel"), Patching::new(&[]));
let modcache = with.modcache.path().to_str().expect("a UTF-8 path");
let (origin, env) = (with.standin.origin(), [("GOMODCACHE", modcache)]);
let before = cell.tree();
for mode in [Kept::Vendored, Kept::Agent] {
let beside = cell.off_is_without_bazel(&origin, &mode.scan(), &env, &["MODULE.bazel"]);
let ignored = beside[0]["files"][".bazelignore"].as_str();
assert_eq!(ignored, Some(mode.ignored("").as_str()), "{beside:#}");
}

with.run(&cell, &Kept::Vendored.scan());
assert!(cell.tree().contains_key(".bazelignore"));
let off = [env[0], ("SOCKET_NO_BAZEL", "1")];
let undo = cell.socket_patch(&origin, &["vendor", "--revert", "--yes"], &off);
assert_eq!(undo.code, 0, "{}", undo.dump);
assert_eq!(changed(&before, &cell.tree()), NOTHING, "{}", undo.dump);
}

/// Without a Bazel: a project that is in no git checkout looks no further
/// than its own directory. Below a Bazel root it is patched as anywhere
/// else, and gets no line and no warning: nothing above it is read, not
Expand Down
Loading
Loading