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
9 changes: 6 additions & 3 deletions crates/socket-patch-core/src/patch/gem_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ fn took(elapsed: Duration) -> String {
}

/// Run a build of `copy`'s extension (`run`, `from`: "its source gem with
/// the patch applied", "its patched gem"), saying when it starts and how it
/// the patch applied", "its upstream gem"), saying when it starts and how it
/// ended
/// ([`RubyEnv::progress`]). What the build was worth is the caller's to
/// say: one that exits cleanly may still have built nothing of the patch.
Expand Down Expand Up @@ -2546,10 +2546,13 @@ fn parse_installed(stdout: &str) -> Option<(String, PathBuf)> {
/// Nothing of the installed copy is changed. The Ruby is found as for a
/// rebuild ([`find_ruby`]): the one whose extension dir for the gem is the
/// installed one, so the staged build has the installed build's platform
/// and ABI. `Err` says why there is no staged install to use.
/// and ABI. `kind` says which gem `archive` is, for the build's progress
/// line: `patched`, or `upstream` when an unwind puts that one back
/// (#2152). `Err` says why there is no staged install to use.
pub(crate) async fn install_staged(
installed: &Path,
archive: &Path,
kind: &str,
stage_home: &Path,
env: &RubyEnv,
) -> Result<StagedInstall, String> {
Expand All @@ -2571,7 +2574,7 @@ pub(crate) async fn install_staged(
archive,
stage_home,
};
let stdout = run_build(&probe, run, &copy, env, "its patched gem").await?;
let stdout = run_build(&probe, run, &copy, env, &format!("its {kind} gem")).await?;
let (full_name, built) = parse_installed(&stdout)
.ok_or_else(|| format!("{}: unreadable install output", probe.ruby.display()))?;
let staged = StagedInstall {
Expand Down
104 changes: 104 additions & 0 deletions crates/socket-patch-core/src/patch/gem_refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ async fn refresh_installed(
crate::patch::gem_ext::install_staged(
&install.gem_dir,
&staged_archive,
kind,
&stage.join("home"),
ruby,
)
Expand Down Expand Up @@ -1129,4 +1130,107 @@ mod tests {
);
assert!(!home.join(STAGE_DIR).exists());
}

/// The progress line of a native gem's staged build names the gem
/// that is built (#2152): an unwind (`rollback`, `remove`) builds the
/// upstream gem, and the line said "from its patched gem" for it too.
#[cfg(unix)]
#[tokio::test]
async fn a_native_build_names_the_gem_it_builds() {
use std::os::unix::fs::PermissionsExt;
use std::sync::Mutex;
static SAID: Mutex<Vec<String>> = Mutex::new(Vec::new());
fn say(line: &str) {
SAID.lock().unwrap().push(line.to_string());
}

let tmp = tempfile::tempdir().unwrap();
// A gem home under a Ruby prefix, whose `bin/ruby` is the Ruby
// asked first. This one answers the extensions-dir question for
// the home and "installs" into the stage home what RubyGems would.
let prefix = tmp.path().join("prefix");
let home = prefix.join("lib/ruby/gems/3.4.0");
std::fs::create_dir_all(prefix.join("bin")).unwrap();
let fake = prefix.join("bin/ruby");
std::fs::write(
&fake,
"#!/bin/sh
if [ \"$3\" = extensions_dir ]; then
printf 'sp_extensions_dir\\t%s\\n' \"$4/extensions/p/3.4.0\"
elif [ \"$3\" = install ]; then
mkdir -p \"$5/gems/rack-2.2.3/lib\" \"$5/extensions/p/3.4.0/rack-2.2.3\"
printf 'BUILT\\n' > \"$5/gems/rack-2.2.3/lib/rack.rb\"
: > \"$5/extensions/p/3.4.0/rack-2.2.3/gem.build_complete\"
printf 'sp_installed\\track-2.2.3\\t%s\\n' \"$5/extensions/p/3.4.0/rack-2.2.3\"
else exit 1; fi
",
)
.unwrap();
std::fs::set_permissions(&fake, std::fs::Permissions::from_mode(0o755)).unwrap();
let proj = tmp.path().join("proj");
std::fs::create_dir_all(&proj).unwrap();

let native_meta = metadata("rack", "2.2.3", &["ext/rack/extconf.rb"]);
let old = gem_with(&native_meta, &[("lib/rack.rb", PATCHED_LIB, 0o644)]);
let new = gem_with(&native_meta, &[("lib/rack.rb", UPSTREAM_LIB, 0o644)]);
let gem_dir = install(&home, "PATCHED\n", &old);
std::fs::write(
home.join("specifications/rack-2.2.3.gemspec"),
"Gem::Specification.new do |s|\n s.name = \"rack\".freeze\n s.require_paths = \
[\"lib\".freeze]\n s.extensions = [\"ext/rack/extconf.rb\".freeze]\nend\n",
)
.unwrap();
std::fs::create_dir_all(home.join("extensions/p/3.4.0/rack-2.2.3")).unwrap();
let source = tmp.path().join("upstream.gem");
std::fs::write(&source, &new).unwrap();

let env = RubyEnv {
cwd: proj,
rebuild: true,
offline: true,
progress: Some(say),
};
// The unwind direction first, then the same build as a scan runs
// it, for the patched gem.
for kind in ["upstream", "patched"] {
let wanted = Wanted {
kind,
..wanted(&new, &[&source])
};
let targets = [Target::Installed(gem_dir.clone())];
std::fs::write(gem_dir.join("lib/rack.rb"), "INSTALLED\n").unwrap();
// A sibling test forking while the script was being written
// holds its write fd until it execs: ETXTBSY (os error 26).
let out = loop {
SAID.lock().unwrap().clear();
let out = refresh(&wanted, &targets, &Source::DiskOnly, &env).await;
match &out[0] {
Err(why) if why.contains("os error 26") => {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
_ => break out,
}
};
assert_eq!(out, [Ok(())], "{kind}");
assert_eq!(
std::fs::read(gem_dir.join("lib/rack.rb")).unwrap(),
b"BUILT\n",
"{kind}: the staged install replaces the copy"
);
let said = SAID.lock().unwrap().clone();
assert_eq!(said.len(), 2, "{kind}: {said:?}");
assert_eq!(
said[0],
format!(
"Rebuilding the native extension of rack-2.2.3 from its {kind} gem; this \
can take several minutes."
),
"{kind}"
);
assert!(
said[1].starts_with("The build of rack-2.2.3 finished after "),
"{kind}: {said:?}"
);
}
}
}
Loading