History

feat: allow execution of scripts without 'bash'. (#650)

This works by trying to execute the file directly, and on failure, use 'bash' as interpreter. That way we are finally able to support a wider variety of fixture generators and make the crate more useful to a wieder audience.

Sebastian Thiel committed 3 years ago

Commit 15ecd841 parent 4eb842c1 file · +42 −27Browse files

Unified · Split

tests/tools/src/lib.rs+42−27
@@ -205,7 +205,7 @@ pub fn fixture_bytes(path: impl AsRef<Path>) -> Vec<u8> { · 204 unmodified lines
205 }205 }
206}206}
207 207
208/// Run the script file `script_name` using `bash`, like `make_repo.sh` to produce a read-only directory to which208/// Run the executable at `script_name`, like `make_repo.sh` or `my_setup.py` to produce a read-only directory to which
209/// the path is returned.209/// the path is returned.
210///210///
211/// Note that it persists and the script at `script_name` will only be executed once if it ran without error.211/// Note that it persists and the script at `script_name` will only be executed once if it ran without error.
@@ -366,32 +366,17 @@ fn scripted_fixture_read_only_with_args_inner( · 154 unmodified lines
366 );366 );
367 }367 }
368 let script_absolute_path = std::env::current_dir()?.join(script_path);368 let script_absolute_path = std::env::current_dir()?.join(script_path);
369 let output = std::process::Command::new("bash")369 let mut cmd = std::process::Command::new(&script_absolute_path);
370 .arg(script_absolute_path)370 let output = match configure_command(&mut cmd, &args, &script_result_directory).output() {
371 .args(args)371 Ok(out) => out,
372 .stdout(std::process::Stdio::piped())372 Err(err)
373 .stderr(std::process::Stdio::piped())373 if err.kind() == std::io::ErrorKind::PermissionDenied || err.raw_os_error() == Some(193) /* windows */ =>
374 .current_dir(&script_result_directory)374 {
375 .env_remove("GIT_DIR")375 cmd = std::process::Command::new("bash");
376 .env_remove("GIT_ASKPASS")376 configure_command(cmd.arg(script_absolute_path), &args, &script_result_directory).output()?
377 .env_remove("SSH_ASKPASS")377 }
378 .env("GIT_TERMINAL_PROMPT", "false")378 Err(err) => return Err(err.into()),
379 .env("GIT_AUTHOR_DATE", "2000-01-01 00:00:00 +0000")379 };
380 .env("GIT_AUTHOR_EMAIL", "author@example.com")
381 .env("GIT_AUTHOR_NAME", "author")
382 .env("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000")
383 .env("GIT_COMMITTER_EMAIL", "committer@example.com")
384 .env("GIT_COMMITTER_NAME", "committer")
385 .env("GIT_CONFIG_COUNT", "4")
386 .env("GIT_CONFIG_KEY_0", "commit.gpgsign")
387 .env("GIT_CONFIG_VALUE_0", "false")
388 .env("GIT_CONFIG_KEY_1", "tag.gpgsign")
389 .env("GIT_CONFIG_VALUE_1", "false")
390 .env("GIT_CONFIG_KEY_2", "init.defaultBranch")
391 .env("GIT_CONFIG_VALUE_2", "main")
392 .env("GIT_CONFIG_KEY_3", "protocol.file.allow")
393 .env("GIT_CONFIG_VALUE_3", "always")
394 .output()?;
395 if !output.status.success() {380 if !output.status.success() {
396 write_failure_marker(&failure_marker);381 write_failure_marker(&failure_marker);
397 }382 }
@@ -413,6 +398,36 @@ fn scripted_fixture_read_only_with_args_inner( · 15 unmodified lines
413 Ok(script_result_directory)398 Ok(script_result_directory)
414}399}
415 400
401fn configure_command<'a>(
402 cmd: &'a mut std::process::Command,
403 args: &[String],
404 script_result_directory: &Path,
405) -> &'a mut std::process::Command {
406 cmd.args(args)
407 .stdout(std::process::Stdio::piped())
408 .stderr(std::process::Stdio::piped())
409 .current_dir(script_result_directory)
410 .env_remove("GIT_DIR")
411 .env_remove("GIT_ASKPASS")
412 .env_remove("SSH_ASKPASS")
413 .env("GIT_TERMINAL_PROMPT", "false")
414 .env("GIT_AUTHOR_DATE", "2000-01-01 00:00:00 +0000")
415 .env("GIT_AUTHOR_EMAIL", "author@example.com")
416 .env("GIT_AUTHOR_NAME", "author")
417 .env("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000")
418 .env("GIT_COMMITTER_EMAIL", "committer@example.com")
419 .env("GIT_COMMITTER_NAME", "committer")
420 .env("GIT_CONFIG_COUNT", "4")
421 .env("GIT_CONFIG_KEY_0", "commit.gpgsign")
422 .env("GIT_CONFIG_VALUE_0", "false")
423 .env("GIT_CONFIG_KEY_1", "tag.gpgsign")
424 .env("GIT_CONFIG_VALUE_1", "false")
425 .env("GIT_CONFIG_KEY_2", "init.defaultBranch")
426 .env("GIT_CONFIG_VALUE_2", "main")
427 .env("GIT_CONFIG_KEY_3", "protocol.file.allow")
428 .env("GIT_CONFIG_VALUE_3", "always")
429}
430
416fn write_failure_marker(failure_marker: &Path) {431fn write_failure_marker(failure_marker: &Path) {
417 std::fs::write(failure_marker, []).ok();432 std::fs::write(failure_marker, []).ok();
418}433}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t