diff --git a/tests/e2e.rs b/tests/e2e.rs index 3984dd4..0f7982a 100644 --- a/tests/e2e.rs +++ b/tests/e2e.rs @@ -877,11 +877,10 @@ async fn want_by_sha_tolerates_object_check_failure() { // A git shim that passes everything through except `cat-file`, which fails. // It drains stdin first: exiting unread races the query write into EPIPE. let shim = work.path().join("git-shim"); - std::fs::write( + helpers::write_test_script( &shim, "#!/bin/sh\nif [ \"$1\" = cat-file ]; then cat >/dev/null; echo 'fatal: simulated cat-file failure' >&2; exit 1; fi\nexec git \"$@\"\n", - ) - .unwrap(); + ); std::fs::set_permissions(&shim, std::fs::Permissions::from_mode(0o755)).unwrap(); let cache = tempfile::tempdir().unwrap(); @@ -1467,7 +1466,7 @@ async fn check_upload_pack_disconnect(reset_connection: bool) { let bin = tempfile::tempdir().unwrap(); let pid_file = bin.path().join("grandchild.pid"); let fake_git = bin.path().join("fake-git"); - std::fs::write( + helpers::write_test_script( &fake_git, format!( "#!/bin/sh\n\ @@ -1478,8 +1477,7 @@ async fn check_upload_pack_disconnect(reset_connection: bool) { exec git \"$@\"\n", pid_file.display() ), - ) - .unwrap(); + ); std::fs::set_permissions(&fake_git, std::fs::Permissions::from_mode(0o755)).unwrap(); let proxy = start_proxy_with_git(up.path(), "8m", fake_git.to_str().unwrap()).await; @@ -1530,7 +1528,7 @@ async fn upload_pack_cap_holds_a_slot_until_the_process_exits() { let dir = tempfile::tempdir().unwrap(); let fake_git = dir.path().join("fake-git"); - std::fs::write(&fake_git, "#!/bin/sh\ncat >/dev/null\nexec sleep 30\n").unwrap(); + helpers::write_test_script(&fake_git, "#!/bin/sh\ncat >/dev/null\nexec sleep 30\n"); std::fs::set_permissions(&fake_git, std::fs::Permissions::from_mode(0o755)).unwrap(); let cache = GitCache::new( GitConfig { @@ -1595,14 +1593,13 @@ async fn upload_pack_runs_with_pack_options() { let dir = tempfile::tempdir().unwrap(); let argv_log = dir.path().join("argv.log"); let fake_git = dir.path().join("fake-git"); - std::fs::write( + helpers::write_test_script( &fake_git, format!( "#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{}'\nexec git \"$@\"\n", argv_log.display() ), - ) - .unwrap(); + ); std::fs::set_permissions(&fake_git, std::fs::Permissions::from_mode(0o755)).unwrap(); let cache = GitCache::new( GitConfig { diff --git a/tests/helpers.rs b/tests/helpers.rs index 4d451ac..a67ad9f 100644 --- a/tests/helpers.rs +++ b/tests/helpers.rs @@ -7,6 +7,7 @@ // rest is the sharing, not a real gap. #![allow(dead_code)] +use std::path::Path; use std::sync::Arc; use std::time::Duration; @@ -168,3 +169,19 @@ pub fn github_auth_config(base: &str, ttl_secs: u64) -> AuthConfig { upstream_base: base.to_string(), } } + +/// Write an executable-to-be script through a short-lived `sh`, so this process +/// never holds a writable descriptor for it. The tests run on parallel threads: +/// a child forked by another one while the descriptor is open inherits it, and +/// a later `exec` of the script then fails with "Text file busy". +pub fn write_test_script(path: &Path, script: impl AsRef) { + assert!( + std::process::Command::new("sh") + .args(["-c", "printf '%s' \"$2\" > \"$1\"", "sh"]) + .arg(path) + .arg(script.as_ref()) + .status() + .unwrap() + .success() + ); +} diff --git a/tests/lifecycle.rs b/tests/lifecycle.rs index 7a9499e..c5c9406 100644 --- a/tests/lifecycle.rs +++ b/tests/lifecycle.rs @@ -10,6 +10,8 @@ use rustix::process::{Pid, Signal, kill_process}; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; +mod helpers; + #[tokio::test] async fn signals_cancel_upstream_before_http_drain() { for signal in [Signal::TERM, Signal::INT] { @@ -23,11 +25,10 @@ async fn signals_cancel_upstream_before_http_drain() { .success() ); let wrapper = root.path().join("git-wrapper"); - std::fs::write( + helpers::write_test_script( &wrapper, "#!/bin/sh\nsleep 30 &\necho \"$$ $!\" > \"$READY_FIFO\"\nwait\n", - ) - .unwrap(); + ); std::fs::set_permissions(&wrapper, std::fs::Permissions::from_mode(0o755)).unwrap(); let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let address = listener.local_addr().unwrap(); diff --git a/tests/retry.rs b/tests/retry.rs index 23c14c5..6b22820 100644 --- a/tests/retry.rs +++ b/tests/retry.rs @@ -14,6 +14,8 @@ use git_cache_proxy::git::{CacheOutcome, GitCache, GitConfig}; use git_cache_proxy::metrics::Metrics; use git_cache_proxy::repo::{RepoRef, resolve}; +mod helpers; + #[tokio::test] async fn clone_retries_tls_eof_and_removes_partial_staging() { let f = Fixture::new(); @@ -259,7 +261,7 @@ impl Fixture { std::fs::create_dir(&upstream).unwrap(); git(&upstream, &["init", "--bare", "-q", "repo.git"]); let binary = root.path().join("git-wrapper"); - std::fs::write( + helpers::write_test_script( &binary, r#"#!/bin/sh set -eu @@ -287,8 +289,7 @@ if test "$count" -le "$limit"; then fi exec git "$@" "#, - ) - .unwrap(); + ); std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755)).unwrap(); let repo = resolve( "repo.git",