|
| 1 | +//! Test for issue #875: Associative array key expansion (`${!assoc[@]}`) |
| 2 | +//! returns empty when used inside a process substitution nested inside a |
| 3 | +//! command substitution. |
| 4 | +
|
| 5 | +use bashkit::Bash; |
| 6 | + |
| 7 | +#[tokio::test] |
| 8 | +async fn assoc_keys_visible_in_nested_procsub_cmdsub() { |
| 9 | + let mut bash = Bash::new(); |
| 10 | + let result = bash |
| 11 | + .exec( |
| 12 | + r#" |
| 13 | +myfunc() { |
| 14 | + declare -A m=() |
| 15 | + m["a"]="1" |
| 16 | + m["b"]="2" |
| 17 | +
|
| 18 | + # Process substitution inside command substitution |
| 19 | + result="$(while IFS= read -r k; do echo "$k"; done < <(printf '%s\n' "${!m[@]}"))" |
| 20 | + echo "nested result: [$result]" |
| 21 | +} |
| 22 | +myfunc |
| 23 | +"#, |
| 24 | + ) |
| 25 | + .await |
| 26 | + .unwrap(); |
| 27 | + // Keys should be visible (order may vary since assoc arrays are unordered) |
| 28 | + assert!( |
| 29 | + result.stdout.contains("a") && result.stdout.contains("b"), |
| 30 | + "expected keys 'a' and 'b' in nested result, got: {}", |
| 31 | + result.stdout |
| 32 | + ); |
| 33 | + assert!( |
| 34 | + !result.stdout.contains("nested result: []"), |
| 35 | + "nested result should not be empty, got: {}", |
| 36 | + result.stdout |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +#[tokio::test] |
| 41 | +async fn assoc_keys_direct_expansion() { |
| 42 | + let mut bash = Bash::new(); |
| 43 | + let result = bash |
| 44 | + .exec( |
| 45 | + r#" |
| 46 | +myfunc() { |
| 47 | + declare -A m=() |
| 48 | + m["x"]="10" |
| 49 | + m["y"]="20" |
| 50 | + echo "direct keys: ${!m[@]}" |
| 51 | +} |
| 52 | +myfunc |
| 53 | +"#, |
| 54 | + ) |
| 55 | + .await |
| 56 | + .unwrap(); |
| 57 | + assert!( |
| 58 | + result.stdout.contains("x") && result.stdout.contains("y"), |
| 59 | + "expected keys 'x' and 'y', got: {}", |
| 60 | + result.stdout |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +#[tokio::test] |
| 65 | +async fn assoc_keys_in_procsub_alone() { |
| 66 | + let mut bash = Bash::new(); |
| 67 | + let result = bash |
| 68 | + .exec( |
| 69 | + r#" |
| 70 | +myfunc() { |
| 71 | + declare -A m=() |
| 72 | + m["p"]="1" |
| 73 | + m["q"]="2" |
| 74 | + while IFS= read -r k; do echo "procsub: $k"; done < <(printf '%s\n' "${!m[@]}") |
| 75 | +} |
| 76 | +myfunc |
| 77 | +"#, |
| 78 | + ) |
| 79 | + .await |
| 80 | + .unwrap(); |
| 81 | + assert!( |
| 82 | + result.stdout.contains("procsub: p") || result.stdout.contains("procsub: q"), |
| 83 | + "expected keys in procsub output, got: {}", |
| 84 | + result.stdout |
| 85 | + ); |
| 86 | +} |
0 commit comments