Repository: reubeno/brush
Pull request #378 (2025-02-03 05:22:02)

chore: fix rng warnings
There are new warnings from deprecated rng functions; this fixes those warnings and adds some *extremely minimal* coverage of `$RANDOM` and `$SRANDOM` in the compat tests.

github-actions[bot]: # Performance Benchmark Report
| Benchmark name                         | Baseline (μs) |  Test/PR (μs) |    Delta (μs) | Delta %         |
| -------------------------------------- | ------------- | ------------- | ------------- | --------------- |
| `clone_shell_object                  ` | `   18.02 μs` | `   18.22 μs` | `    0.21 μs` | `🟠 +1.14%    ` |
| `eval_arithmetic                     ` | `    0.17 μs` | `    0.17 μs` | `    0.00 μs` | `⚪  Unchanged` |
| `expand_one_string                   ` | `    1.83 μs` | `    1.83 μs` | `   -0.00 μs` | `⚪  Unchanged` |
| `for_loop                            ` | `   22.01 μs` | `   22.16 μs` | `    0.15 μs` | `⚪  Unchanged` |
| `function_call                       ` | `    2.33 μs` | `    2.36 μs` | `    0.03 μs` | `⚪  Unchanged` |
| `instantiate_shell                   ` | `   57.16 μs` | `   57.47 μs` | `    0.31 μs` | `⚪  Unchanged` |
| `instantiate_shell_with_init_scripts ` | `21543.47 μs` | `21776.64 μs` | `  233.17 μs` | `⚪  Unchanged` |
| `parse_bash_completion               ` | ` 1663.70 μs` | ` 1666.44 μs` | `    2.75 μs` | `⚪  Unchanged` |
| `parse_sample_script                 ` | `    1.80 μs` | `    1.76 μs` | `   -0.04 μs` | `🟢 -2.22%    ` |
| `run_echo_builtin_command            ` | `   15.29 μs` | `   15.23 μs` | `   -0.07 μs` | `⚪  Unchanged` |
| `run_one_external_command            ` | ` 2318.89 μs` | ` 2260.51 μs` | `  -58.38 μs` | `🟢 -2.52%    ` |
| `tokenize_sample_script              ` | `    2.86 μs` | `    2.79 μs` | `   -0.07 μs` | `⚪  Unchanged` |

# Code Coverage Report: Only Changed Files listed

| Package | Base Coverage | New Coverage | Difference |
| ------- | ------------- | ------------ | ---------- |
| brush-core/src/builtins/mapfile.rs | 🟢 76.6% | 🟢 76.84% | 🟢 0.24% |
| brush-core/src/builtins/read.rs | 🟠 66.87% | 🟠 66.47% | 🔴 -0.4% |
| brush-core/src/commands.rs | 🟢 87.21% | 🟢 86.97% | 🔴 -0.24% |
| brush-core/src/interp.rs | 🟢 91.58% | 🟢 91.68% | 🟢 0.1% |
| brush-core/src/jobs.rs | 🔴 37.07% | 🔴 47.41% | 🟢 10.34% |
| **Overall Coverage** | **🟢 77.89%** | **🟢 78.04%** | **🟢 0.15%** |

_Minimum allowed coverage is_ `70%`_, this run produced_ `78.04%`
<!-- Sticky Pull Request Comment -->
github-actions[bot]: ## Test Results
    2 files      9 suites   1m 39s ⏱️
  671 tests   671 ✅ 0 💤 0 ❌
1 328 runs  1 328 ✅ 0 💤 0 ❌

Results for commit f7ad9005.

[test-results]:data:application/gzip;base64,H4sIAFZVoGcC/13MyQ6CMBSF4VchXbvoCNSXMZcOSSNQ02FFfHfbBhRcfv9Jzoasm01E947eOhSzSw2yQOcAyfm1srosqW79QA49YlbqPz3dqyT8DRbcfAkmBB/2EvJaPwmj467j85J+n82ny+bzo/LL4lIBsgNoibHgRnIp8GS5NmZkwKlmoBWVMPVCEEDvD85Gcl8GAQAA


Patch:
diff --git a/brush-core/src/shell.rs b/brush-core/src/shell.rs
index 8cdac2c..21ce252 100644
--- a/brush-core/src/shell.rs
+++ b/brush-core/src/shell.rs
@@ -1564,15 +1564,15 @@ fn repeated_char_str(c: char, count: usize) -> String {
 }
 
 fn get_random_value(_shell: &Shell) -> ShellValue {
-    let mut rng = rand::thread_rng();
-    let num = rng.gen_range(0..32768);
+    let mut rng = rand::rng();
+    let num = rng.random_range(0..32768);
     let str = num.to_string();
     str.into()
 }
 
 fn get_srandom_value(_shell: &Shell) -> ShellValue {
-    let mut rng = rand::thread_rng();
-    let num: u32 = rng.gen();
+    let mut rng = rand::rng();
+    let num: u32 = rng.random();
     let str = num.to_string();
     str.into()
 }


Test patch:
diff --git a/brush-shell/tests/cases/well_known_vars.yaml b/brush-shell/tests/cases/well_known_vars.yaml
index cdd397b..9e9e568 100644
--- a/brush-shell/tests/cases/well_known_vars.yaml
+++ b/brush-shell/tests/cases/well_known_vars.yaml
@@ -126,6 +126,15 @@ cases:
         echo "LINENO: $LINENO"
       done
 
+  - name: "RANDOM"
+    stdin: |
+      first=${RANDOM}
+      second=${RANDOM}
+
+      [[ $first != $second ]] && echo "Confirmed RANDOM at least isn't static"
+      [[ $first -ge 0 && $first -lt 32768 ]] && echo "RANDOM value is within expected range"
+      [[ $second -ge 0 && $second -lt 32768 ]] && echo "RANDOM value is within expected range"
+
   - name: "SHELLOPTS"
     stdin: |
       echo "SHELLOPTS: $SHELLOPTS"
@@ -135,3 +144,10 @@ cases:
       echo "SHLVL: $SHLVL"
       bash -c 'echo "bash SHLVL: $SHLVL"'
       $0 -c 'echo "nested SHLVL: $SHLVL"'
+
+  - name: "SRANDOM"
+    stdin: |
+      first=${SRANDOM}
+      second=${SRANDOM}
+
+      [[ $first != $second ]] && echo "Confirmed SRANDOM at least isn't static"