@@ -100,6 +100,82 @@ fn skip_if_shell_missing(shell: &str) -> bool { |
100 | 100 | true |
101 | 101 | } |
102 | 102 |
|
| 103 | +/// The system bash-completion library, or `None` if it isn't installed. |
| 104 | +/// |
| 105 | +/// usage no longer embeds a copy of bash-completion, so the generated bash completion needs |
| 106 | +/// the real one loaded before it — `_init_completion` and `__ltrim_colon_completions` come from |
| 107 | +/// there. |
| 108 | +/// |
| 109 | +/// Candidates are the library itself, never the `profile.d/bash_completion.sh` snippet that |
| 110 | +/// Homebrew and others also install: that one is guarded on `$PS1` and so does nothing at all |
| 111 | +/// in a non-interactive shell, which is the only kind these tests run. Sourcing it would look |
| 112 | +/// like success and then fail the completion assertions. `BASH_COMPLETION_USER_DIR` is not |
| 113 | +/// consulted either — it holds per-command completions, not the library. |
| 114 | +/// |
| 115 | +/// `USAGE_TEST_BASH_COMPLETION` names a library directly, for testing against a copy that is |
| 116 | +/// not installed system-wide. |
| 117 | +fn bash_completion_candidates() -> Vec<PathBuf> { |
| 118 | + let mut candidates: Vec<PathBuf> = Vec::new(); |
| 119 | + if let Some(path) = env::var_os("USAGE_TEST_BASH_COMPLETION") { |
| 120 | + candidates.push(PathBuf::from(path)); |
| 121 | + } |
| 122 | + // Whichever Homebrew is installed: `/opt/homebrew` on Apple Silicon, `/usr/local` on Intel, |
| 123 | + // and anywhere else if the user relocated it. |
| 124 | + if let Some(prefix) = env::var_os("HOMEBREW_PREFIX") { |
| 125 | + candidates.push(PathBuf::from(prefix).join("share/bash-completion/bash_completion")); |
| 126 | + } |
| 127 | + candidates.extend( |
| 128 | + [ |
| 129 | + "/usr/share/bash-completion/bash_completion", |
| 130 | + "/usr/local/share/bash-completion/bash_completion", |
| 131 | + "/opt/homebrew/share/bash-completion/bash_completion", |
| 132 | + ] |
| 133 | + .into_iter() |
| 134 | + .map(PathBuf::from), |
| 135 | + ); |
| 136 | + candidates |
| 137 | +} |
| 138 | + |
| 139 | +/// Returns the first candidate that a non-interactive bash can actually load `_init_completion` |
| 140 | +/// from. |
| 141 | +/// |
| 142 | +/// Existing on disk is not the property the test needs — being sourceable into the shell the |
| 143 | +/// test spawns is. Probing for it means a wrapper, a version too old to define the function, or |
| 144 | +/// a path guess that turns out wrong is rejected here rather than surfacing as a puzzling |
| 145 | +/// completion assertion failure further down. |
| 146 | +fn system_bash_completion() -> Option<PathBuf> { |
| 147 | + bash_completion_candidates().into_iter().find(|candidate| { |
| 148 | + candidate.is_file() |
| 149 | + && Command::new(shell_program("bash")) |
| 150 | + .args(["-c", "source \"$1\" && declare -F _init_completion", "--"]) |
| 151 | + .arg(sh_path(candidate)) |
| 152 | + .output() |
| 153 | + .is_ok_and(|out| out.status.success()) |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +/// Returns `Some(path)` to the system bash-completion, or `None` if the test should be skipped. |
| 158 | +/// |
| 159 | +/// Panics under `CI` for the same reason [`skip_if_shell_missing`] does: bash-completion is |
| 160 | +/// installed by the workflow, so a run that quietly skipped this test would be reporting a |
| 161 | +/// green bash suite that never exercised a completion. |
| 162 | +/// |
| 163 | +/// What CI finds is `ubuntu-latest`'s package, which is 2.11 — deliberately the oldest version |
| 164 | +/// the generated script claims to support, so the claim is tested rather than asserted. |
| 165 | +fn bash_completion_or_skip() -> Option<PathBuf> { |
| 166 | + if let Some(path) = system_bash_completion() { |
| 167 | + return Some(path); |
| 168 | + } |
| 169 | + if env::var("CI").is_ok_and(|v| !v.is_empty()) { |
| 170 | + panic!( |
| 171 | + "no usable bash-completion but CI is set — refusing to skip. Tried: {:?}", |
| 172 | + bash_completion_candidates() |
| 173 | + ); |
| 174 | + } |
| 175 | + eprintln!("Skipping bash completion test - no usable bash-completion library found"); |
| 176 | + None |
| 177 | +} |
| 178 | + |
103 | 179 | fn shell_can_run_a_script(shell: &str) -> bool { |
104 | 180 | static NEXT: AtomicUsize = AtomicUsize::new(0); |
105 | 181 | let dir = env::temp_dir().join(format!( |
@@ -369,6 +445,9 @@ fn test_bash_completion_integration() { |
369 | 445 | if skip_if_shell_missing("bash") { |
370 | 446 | return; |
371 | 447 | } |
| 448 | + let Some(bash_completion) = bash_completion_or_skip() else { |
| 449 | + return; |
| 450 | + }; |
372 | 451 |
|
373 | 452 | // Build the usage binary |
374 | 453 | let usage_bin = build_usage_binary(); |
@@ -389,12 +468,10 @@ cmd "sub" help="Subcommand" { |
389 | 468 | let spec_kdl_file = temp_dir.join("testcli.kdl"); |
390 | 469 | fs::write(&spec_kdl_file, spec).unwrap(); |
391 | 470 |
|
392 | | - // Generate the completion with bash-completion library included |
393 | 471 | let output = Command::new(&usage_bin) |
394 | 472 | .args(["generate", "completion", "bash", "testcli"]) |
395 | 473 | .arg("-f") |
396 | 474 | .arg(spec_kdl_file.to_str().unwrap()) |
397 | | - .arg("--include-bash-completion-lib") |
398 | 475 | .output() |
399 | 476 | .expect("Failed to generate bash completion"); |
400 | 477 |
|
@@ -424,7 +501,8 @@ set +e |
424 | 501 | # Add usage binary to PATH |
425 | 502 | export PATH="{}:$PATH" |
426 | 503 |
|
427 | | -# Source our completion (which includes bash-completion library) |
| 504 | +# bash-completion first: the generated completion calls its functions. |
| 505 | +source {} |
428 | 506 | source {} |
429 | 507 |
|
430 | 508 | echo "LOAD_SUCCESS" |
|
519 | 597 | echo "COMPLETION_TEST_DONE" |
520 | 598 | "#, |
521 | 599 | path_var_entry("bash", usage_bin.parent().unwrap()), |
| 600 | + sh_path(&bash_completion), |
522 | 601 | sh_path(&comp_file), |
523 | 602 | sh_path(&temp_dir) |
524 | 603 | ); |
@@ -1576,6 +1655,64 @@ echo "GUARD_EXIT=$?" |
1576 | 1655 | let _ = fs::remove_dir_all(&temp_dir); |
1577 | 1656 | } |
1578 | 1657 |
|
| 1658 | +#[test] |
| 1659 | +fn test_bash_guard_reports_missing_bash_completion() { |
| 1660 | + if skip_if_shell_missing("bash") { |
| 1661 | + return; |
| 1662 | + } |
| 1663 | + |
| 1664 | + // usage no longer ships a copy of bash-completion, so "it isn't installed" is a |
| 1665 | + // reachable state for anyone sourcing a generated completion. It has to say so |
| 1666 | + // rather than fail as `_init_completion: command not found`. |
| 1667 | + let usage_bin = build_usage_binary(); |
| 1668 | + let temp_dir = env::temp_dir().join(format!("usage_bash_nobc_test_{}", std::process::id())); |
| 1669 | + fs::create_dir_all(&temp_dir).unwrap(); |
| 1670 | + |
| 1671 | + let output = Command::new(&usage_bin) |
| 1672 | + .args(["generate", "completion", "bash", "testcli"]) |
| 1673 | + .args(["--usage-cmd", "command usage --usage-spec"]) |
| 1674 | + .output() |
| 1675 | + .expect("Failed to generate bash completion"); |
| 1676 | + let comp_file = temp_dir.join("testcli.bash"); |
| 1677 | + fs::write(&comp_file, &output.stdout).unwrap(); |
| 1678 | + |
| 1679 | + // A non-interactive bash loads no bash-completion of its own, so sourcing only the |
| 1680 | + // generated script is exactly the state being tested. |
| 1681 | + let test_script = format!( |
| 1682 | + r#"#!/usr/bin/env bash |
| 1683 | +export PATH="{path}:$PATH" |
| 1684 | +source "{comp}" |
| 1685 | +_testcli testcli "" testcli 1 |
| 1686 | +echo "GUARD_EXIT=$?" |
| 1687 | +"#, |
| 1688 | + path = path_var_entry("bash", usage_bin.parent().unwrap()), |
| 1689 | + comp = sh_path(&comp_file), |
| 1690 | + ); |
| 1691 | + let script_file = temp_dir.join("test.sh"); |
| 1692 | + fs::write(&script_file, &test_script).unwrap(); |
| 1693 | + |
| 1694 | + let result = script_command("bash", &script_file) |
| 1695 | + .output() |
| 1696 | + .expect("Failed to run bash test"); |
| 1697 | + let stdout = String::from_utf8_lossy(&result.stdout); |
| 1698 | + let stderr = String::from_utf8_lossy(&result.stderr); |
| 1699 | + |
| 1700 | + assert!( |
| 1701 | + stdout.contains("GUARD_EXIT=1"), |
| 1702 | + "guard should return 1 when bash-completion is not loaded.\nstdout:\n{stdout}\nstderr:\n{stderr}" |
| 1703 | + ); |
| 1704 | + assert!( |
| 1705 | + stderr.contains("bash-completion is required"), |
| 1706 | + "guard should name bash-completion.\nstdout:\n{stdout}\nstderr:\n{stderr}" |
| 1707 | + ); |
| 1708 | + assert!( |
| 1709 | + !stderr.contains("_init_completion: command not found"), |
| 1710 | + "the guard should fire before bash reports a missing function.\nstderr:\n{stderr}" |
| 1711 | + ); |
| 1712 | + |
| 1713 | + let _ = fs::remove_dir_all(&temp_dir); |
| 1714 | +} |
| 1715 | + |
1579 | 1716 | #[test] |
1580 | 1717 | fn test_bash_self_completion_uses_executable_not_shadowing_function() { |
1581 | 1718 | if skip_if_shell_missing("bash") { |
@@ -1588,16 +1725,15 @@ fn test_bash_self_completion_uses_executable_not_shadowing_function() { |
1588 | 1725 |
|
1589 | 1726 | // The real usage binary is on $PATH *and* a `usage` function is defined, |
1590 | 1727 | // so the guard passes legitimately — only the spec call can go wrong. |
1591 | | - // bash-completion isn't loaded here; stub the three helpers so the spec |
| 1728 | + // bash-completion isn't loaded here; stub the helpers it provides so the spec |
1592 | 1729 | // write is the only thing under test. |
1593 | 1730 | let test_script = format!( |
1594 | 1731 | r#"#!/usr/bin/env bash |
1595 | 1732 | export PATH="{usage_dir}:$PATH" |
1596 | 1733 | export XDG_CACHE_HOME="{cache}" |
1597 | 1734 | usage() {{ echo "FUNCTION_MARKER"; }} |
1598 | | -_comp_initialize() {{ return 0; }} |
1599 | | -_comp_compgen() {{ return 0; }} |
1600 | | -_comp_ltrim_colon_completions() {{ return 0; }} |
| 1735 | +_init_completion() {{ return 0; }} |
| 1736 | +__ltrim_colon_completions() {{ return 0; }} |
1601 | 1737 | source "{asset}" |
1602 | 1738 | _usage usage "" usage 1 |
1603 | 1739 | echo "SPEC_BEGIN" |
|