| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Run real shell code with osh and bash, and compare the results.
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   test/gold.sh <function name>
 | 
| 7 | 
 | 
| 8 | set -o nounset
 | 
| 9 | set -o pipefail
 | 
| 10 | set -o errexit
 | 
| 11 | shopt -s strict:all 2>/dev/null || true  # dogfood for OSH
 | 
| 12 | 
 | 
| 13 | source test/common.sh  # $OSH, run-test-funcs
 | 
| 14 | 
 | 
| 15 | readonly GOLD_DIR='test/gold'
 | 
| 16 | 
 | 
| 17 | # Runs an command (argv) the normal way (with its shebang) and then with
 | 
| 18 | # OSH, and compares the stdout and exit code.
 | 
| 19 | #
 | 
| 20 | # Also puts $PWD/bin on the front of $PATH, in order to read bin/readlink
 | 
| 21 | # and so forth.
 | 
| 22 | _compare() {
 | 
| 23 |   set +o errexit
 | 
| 24 | 
 | 
| 25 |   "$@" >_tmp/shebang.txt
 | 
| 26 |   local expected_status=$?
 | 
| 27 | 
 | 
| 28 |   export OILS_HIJACK_SHEBANG=$OSH
 | 
| 29 |   $OSH "$@" >_tmp/osh.txt
 | 
| 30 |   local osh_status=$?
 | 
| 31 | 
 | 
| 32 |   set -o errexit
 | 
| 33 | 
 | 
| 34 |   #md5sum _tmp/shebang.txt _tmp/osh.txt
 | 
| 35 | 
 | 
| 36 |   if ! diff -u _tmp/shebang.txt _tmp/osh.txt; then
 | 
| 37 |     echo FAIL
 | 
| 38 |     exit 1
 | 
| 39 |   fi
 | 
| 40 | 
 | 
| 41 |   if test $expected_status != $osh_status; then
 | 
| 42 |     echo "FAIL: Got status $osh_status but expected $expected_status"
 | 
| 43 |     echo "in test case: $@"
 | 
| 44 |     exit 1
 | 
| 45 |   fi
 | 
| 46 | 
 | 
| 47 |   return 0
 | 
| 48 | }
 | 
| 49 | 
 | 
| 50 | # Uses {core,osh}/*.py
 | 
| 51 | test-count() {
 | 
| 52 |   _compare metrics/source-code.sh for-translation
 | 
| 53 |   _compare metrics/source-code.sh overview
 | 
| 54 | }
 | 
| 55 | 
 | 
| 56 | # Uses $(cd $(dirname $0) && pwd)
 | 
| 57 | test-spec-file() {
 | 
| 58 |   _compare test/spec.sh builtin-special
 | 
| 59 | }
 | 
| 60 | 
 | 
| 61 | # Uses redirect of functions.
 | 
| 62 | test-html-summary() {
 | 
| 63 |   # BUG: in the devtools/release.sh process, there's nothing to summarize here
 | 
| 64 |   # because _tmp/spec is deleted.
 | 
| 65 |   _compare test/spec-runner.sh html-summary osh _tmp/spec/cpp
 | 
| 66 | }
 | 
| 67 | 
 | 
| 68 | test-gen-module-init() {
 | 
| 69 |   local modules='time datetime'
 | 
| 70 |   _compare build/ovm-actions.sh gen-module-init $modules
 | 
| 71 | }
 | 
| 72 | 
 | 
| 73 | # NOTE: zsh behaves differently under sh and bin/osh!  Looks like it is an
 | 
| 74 | # inherited file descriptor issue.
 | 
| 75 | #
 | 
| 76 | # A bin/osh app bundle also behaves differently.  Maybe because of the internal
 | 
| 77 | # environment variables.
 | 
| 78 | # FAILS
 | 
| 79 | 
 | 
| 80 | FAIL-test-startup-benchmark() {
 | 
| 81 |   _compare benchmarks/startup.sh compare-strace
 | 
| 82 | }
 | 
| 83 | 
 | 
| 84 | test-configure() { _compare ./configure; }
 | 
| 85 | test-configure-bug() { _compare $GOLD_DIR/configure-bug.sh; }
 | 
| 86 | test-nix() { _compare $GOLD_DIR/nix.sh isElfSimpleWithStdin; }
 | 
| 87 | test-and-or() { _compare $GOLD_DIR/and-or.sh test-simple; }
 | 
| 88 | 
 | 
| 89 | test-comments() { _compare $GOLD_DIR/comments.sh; }
 | 
| 90 | test-readonly_() { _compare $GOLD_DIR/readonly.sh; }
 | 
| 91 | test-export-case() { _compare $GOLD_DIR/export.sh; }
 | 
| 92 | test-glob() { _compare $GOLD_DIR/glob.sh; }
 | 
| 93 | test-no-op() { _compare metrics/source-code.sh; }
 | 
| 94 | test-complex-here-docs() { _compare $GOLD_DIR/complex-here-docs.sh; }
 | 
| 95 | 
 | 
| 96 | # FAILS
 | 
| 97 | FAIL-test-big-here-doc() { _compare $GOLD_DIR/big-here-doc.sh; }
 | 
| 98 | 
 | 
| 99 | test-case-in-subshell() { _compare $GOLD_DIR/case-in-subshell.sh; }
 | 
| 100 | test-command-sub() { _compare $GOLD_DIR/command-sub.sh; }
 | 
| 101 | test-command-sub-2() { _compare $GOLD_DIR/command-sub-2.sh; }
 | 
| 102 | 
 | 
| 103 | char-class() { _compare $GOLD_DIR/char-class.sh demo; }
 | 
| 104 | strip-op-char-class() { _compare $GOLD_DIR/strip-op-char-class.sh; }
 | 
| 105 | 
 | 
| 106 | # Similar tests for backslash escaping.
 | 
| 107 | FAIL-test-echo-e() { _compare $GOLD_DIR/echo-e.sh; }
 | 
| 108 | 
 | 
| 109 | test-dollar-sq() { _compare $GOLD_DIR/dollar-sq.sh; }
 | 
| 110 | test-word-eval() { _compare $GOLD_DIR/word-eval.sh; }
 | 
| 111 | 
 | 
| 112 | test-abuild() {
 | 
| 113 |   _compare $GOLD_DIR/abuild.sh is_function is_function
 | 
| 114 | }
 | 
| 115 | 
 | 
| 116 | # Needs declare -p
 | 
| 117 | FAIL-test-declare() { _compare $GOLD_DIR/declare.sh demo; }
 | 
| 118 | 
 | 
| 119 | # Needs declare -p
 | 
| 120 | FAIL-test-scope() { _compare $GOLD_DIR/scope.sh; }
 | 
| 121 | 
 | 
| 122 | FAIL-test-readlink() { $GOLD_DIR/readlink.sh compare; }
 | 
| 123 | 
 | 
| 124 | test-errexit() { _compare $GOLD_DIR/errexit.sh all; }
 | 
| 125 | 
 | 
| 126 | # Hm this isn't tickling the bug?
 | 
| 127 | test-errexit-confusion() {
 | 
| 128 |   _compare $GOLD_DIR/errexit-confusion.sh run-for-release-OLD
 | 
| 129 |   _compare $GOLD_DIR/errexit-confusion.sh run-for-release-FIXED
 | 
| 130 | }
 | 
| 131 | 
 | 
| 132 | test-parse-help() {
 | 
| 133 |   local dir=benchmarks/parse-help
 | 
| 134 | 
 | 
| 135 |   # This is not hermetic since it calls 'ls'
 | 
| 136 |   _compare $dir/excerpt.sh _parse_help ls
 | 
| 137 | }
 | 
| 138 | 
 | 
| 139 | test-autoconf-backtick() {
 | 
| 140 |   # https://github.com/oilshell/oil/issues/1449
 | 
| 141 |   _compare $GOLD_DIR/autoconf-backtick.sh
 | 
| 142 | }
 | 
| 143 | 
 | 
| 144 | # Gah, bash gets this from compile-time configuration generated with autoconf,
 | 
| 145 | # not uname().  It looks like 'linux-gnu' on Ubuntu.  In Alpine, it's
 | 
| 146 | # 'linux-musl'.
 | 
| 147 | _ostype() {
 | 
| 148 |   echo $OSTYPE
 | 
| 149 | }
 | 
| 150 | 
 | 
| 151 | FAIL-test-ostype() {
 | 
| 152 |   _compare $0 _ostype
 | 
| 153 | }
 | 
| 154 | 
 | 
| 155 | # TODO:
 | 
| 156 | # - Run the failing tests, and add an --allowed-failures mechanism
 | 
| 157 | # - and a timeout for big-here-doc
 | 
| 158 | 
 | 
| 159 | # TODO: Turn it into a table?
 | 
| 160 | run-for-release() {
 | 
| 161 |   run-other-suite-for-release gold run-test-funcs
 | 
| 162 | }
 | 
| 163 | 
 | 
| 164 | soil-run() {
 | 
| 165 |   run-test-funcs
 | 
| 166 | }
 | 
| 167 | 
 | 
| 168 | "$@"
 |