OILS / test / gold.sh View on Github | oilshell.org

168 lines, 91 significant
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
8set -o nounset
9set -o pipefail
10set -o errexit
11shopt -s strict:all 2>/dev/null || true # dogfood for OSH
12
13source test/common.sh # $OSH, run-test-funcs
14
15readonly 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
51test-count() {
52 _compare metrics/source-code.sh for-translation
53 _compare metrics/source-code.sh overview
54}
55
56# Uses $(cd $(dirname $0) && pwd)
57test-spec-file() {
58 _compare test/spec.sh builtin-special
59}
60
61# Uses redirect of functions.
62test-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
68test-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
80FAIL-test-startup-benchmark() {
81 _compare benchmarks/startup.sh compare-strace
82}
83
84test-configure() { _compare ./configure; }
85test-configure-bug() { _compare $GOLD_DIR/configure-bug.sh; }
86test-nix() { _compare $GOLD_DIR/nix.sh isElfSimpleWithStdin; }
87test-and-or() { _compare $GOLD_DIR/and-or.sh test-simple; }
88
89test-comments() { _compare $GOLD_DIR/comments.sh; }
90test-readonly_() { _compare $GOLD_DIR/readonly.sh; }
91test-export-case() { _compare $GOLD_DIR/export.sh; }
92test-glob() { _compare $GOLD_DIR/glob.sh; }
93test-no-op() { _compare metrics/source-code.sh; }
94test-complex-here-docs() { _compare $GOLD_DIR/complex-here-docs.sh; }
95
96# FAILS
97FAIL-test-big-here-doc() { _compare $GOLD_DIR/big-here-doc.sh; }
98
99test-case-in-subshell() { _compare $GOLD_DIR/case-in-subshell.sh; }
100test-command-sub() { _compare $GOLD_DIR/command-sub.sh; }
101test-command-sub-2() { _compare $GOLD_DIR/command-sub-2.sh; }
102
103char-class() { _compare $GOLD_DIR/char-class.sh demo; }
104strip-op-char-class() { _compare $GOLD_DIR/strip-op-char-class.sh; }
105
106# Similar tests for backslash escaping.
107FAIL-test-echo-e() { _compare $GOLD_DIR/echo-e.sh; }
108
109test-dollar-sq() { _compare $GOLD_DIR/dollar-sq.sh; }
110test-word-eval() { _compare $GOLD_DIR/word-eval.sh; }
111
112test-abuild() {
113 _compare $GOLD_DIR/abuild.sh is_function is_function
114}
115
116# Needs declare -p
117FAIL-test-declare() { _compare $GOLD_DIR/declare.sh demo; }
118
119# Needs declare -p
120FAIL-test-scope() { _compare $GOLD_DIR/scope.sh; }
121
122FAIL-test-readlink() { $GOLD_DIR/readlink.sh compare; }
123
124test-errexit() { _compare $GOLD_DIR/errexit.sh all; }
125
126# Hm this isn't tickling the bug?
127test-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
132test-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
139test-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
151FAIL-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?
160run-for-release() {
161 run-other-suite-for-release gold run-test-funcs
162}
163
164soil-run() {
165 run-test-funcs
166}
167
168"$@"