You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[MERGE #6128@koalaman] Allow SAFE_RUN to correctly run and check results
Merge pull request #6128 from koalaman:master
Hi, I came across [this shell function](https://github.com/microsoft/ChakraCore/blob/68d19b73e4c796540d2d44cc17ac2320c7576cd0/test/native-tests/test_native.sh#L30) in `test/native-tests/test_native.sh`:
```
SAFE_RUN() {
local SF_RETURN_VALUE=$($1 2>&1)
if [[ $? != 0 ]]; then
>&2 echo $SF_RETURN_VALUE
exit 1
fi
}
```
invoked as e.g.
```
SAFE_RUN `cd $TEST_PATH; ${CH_DIR} Platform.js > Makefile`
```
There are multiple issues that prevent it from living up to its name:
1. The code to run is executed by the backticks before the function ever starts:
```
$ test_run() { true; }
$ test_run `ls /doesnotexist`
ls: cannot access '/doesnotexist': No such file or directory
```
2. If the invocation is changed to pass the command directly, the invocation fails for metacharacters like `;` and `>`. Here they are both treated as literal text:
```
$ test_run() { local var=$($1 >&2); echo "$var"; }
$ test_run "echo Hello; ls > filelist"
Hello; ls > filelist
```
3. If passed a valid invocation, `local` [masks all exit codes](https://github.com/koalaman/shellcheck/wiki/SC2155) and returns success, even when the command fails:
```
$ test_run() { local foo=$($1); echo "$1 returned $?"; }
$ test_run true
true returned 0
$ test_run false
false returned 0
```
The suggested commits fixes all these issues.
I was unable to verify the fix locally as the tests appeared to hang when run overnight with and without these fixes. I'm not quite sure how they're used, but there is some possibility that this commit uncovers failures that were previously hidden.
0 commit comments