| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Test osh usage "from the outside".
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   test/osh-usage.sh <function name>
 | 
| 7 | 
 | 
| 8 | set -o nounset
 | 
| 9 | set -o pipefail
 | 
| 10 | set -o errexit
 | 
| 11 | 
 | 
| 12 | source test/common.sh
 | 
| 13 | 
 | 
| 14 | DISABLED-test-oheap() {
 | 
| 15 |   # OHeap was disabled
 | 
| 16 |   if bin/osh -n --ast-format oheap -c 'echo hi'; then
 | 
| 17 |     die "Should have failed"
 | 
| 18 |   fi
 | 
| 19 |   echo OK
 | 
| 20 | }
 | 
| 21 | 
 | 
| 22 | test-ast-formats() {
 | 
| 23 |   bin/osh -n -c 'echo hi'
 | 
| 24 |   bin/osh -n --ast-format text -c 'echo hi'
 | 
| 25 |   bin/osh -n --ast-format abbrev-html -c 'echo hi'
 | 
| 26 |   bin/osh -n --ast-format html -c 'echo hi'
 | 
| 27 | 
 | 
| 28 |   # Removed with oheap
 | 
| 29 |   return
 | 
| 30 |   local ast_bin=_tmp/smoke-ast.bin 
 | 
| 31 |   bin/osh -n --ast-format oheap -c 'echo hi' > $ast_bin
 | 
| 32 |   ls -l $ast_bin
 | 
| 33 |   hexdump -C $ast_bin
 | 
| 34 | }
 | 
| 35 | 
 | 
| 36 | # Read from a file.
 | 
| 37 | test-osh-file() {
 | 
| 38 |   echo ===== Hello
 | 
| 39 |   cat >_tmp/smoke-prog.sh <<EOF
 | 
| 40 | echo hi
 | 
| 41 | 
 | 
| 42 | myfunc() {
 | 
| 43 |   echo "inside func"
 | 
| 44 | }
 | 
| 45 | myfunc 1 2 3
 | 
| 46 | 
 | 
| 47 | # TODO: Test vars don't persist.
 | 
| 48 | (echo "in subshell"; echo "another")
 | 
| 49 | 
 | 
| 50 | echo \$(echo ComSub)
 | 
| 51 | 
 | 
| 52 | EOF
 | 
| 53 |   $OSH _tmp/smoke-prog.sh
 | 
| 54 | 
 | 
| 55 |   echo ===== EMPTY
 | 
| 56 |   touch _tmp/empty.sh 
 | 
| 57 |   $OSH _tmp/empty.sh
 | 
| 58 | 
 | 
| 59 |   echo ===== NO TRAILING NEWLINE
 | 
| 60 |   echo -n 'echo hi' >_tmp/no-newline.sh
 | 
| 61 |   $OSH _tmp/no-newline.sh
 | 
| 62 | }
 | 
| 63 | 
 | 
| 64 | # Read from stdin.
 | 
| 65 | test-osh-stdin() {
 | 
| 66 |   $OSH < _tmp/smoke-prog.sh
 | 
| 67 | 
 | 
| 68 |   echo ===== EMPTY
 | 
| 69 |   $OSH < _tmp/empty.sh
 | 
| 70 | 
 | 
| 71 |   echo ===== NO TRAILING NEWLINE
 | 
| 72 |   $OSH < _tmp/no-newline.sh
 | 
| 73 | 
 | 
| 74 |   # Line continuation tests
 | 
| 75 |   $OSH <<EOF
 | 
| 76 | echo "
 | 
| 77 | hi
 | 
| 78 | "
 | 
| 79 | echo \\
 | 
| 80 | line continuation; echo two
 | 
| 81 | 
 | 
| 82 | cat <<EOF_INNER
 | 
| 83 | here doc
 | 
| 84 | EOF_INNER
 | 
| 85 | 
 | 
| 86 | echo \$(
 | 
| 87 | echo command sub
 | 
| 88 | )
 | 
| 89 | 
 | 
| 90 | myfunc() {
 | 
| 91 |   echo hi
 | 
| 92 | }
 | 
| 93 | 
 | 
| 94 | EOF
 | 
| 95 | }
 | 
| 96 | 
 | 
| 97 | test-osh-interactive() {
 | 
| 98 |   set +o errexit
 | 
| 99 |   echo 'echo hi' | $OSH -i
 | 
| 100 |   assert $? -eq 0
 | 
| 101 | 
 | 
| 102 |   echo 'exit' | $OSH -i
 | 
| 103 |   assert $? -eq 0
 | 
| 104 | 
 | 
| 105 |   # Parse failure
 | 
| 106 |   echo ';' | $OSH -i
 | 
| 107 |   assert $? -eq 2
 | 
| 108 | 
 | 
| 109 |   # Bug fix: this shouldn't try execute 'echo OIL OIL'
 | 
| 110 |   # The line lexer wasn't getting reset on parse failures.
 | 
| 111 |   echo ';echo OIL OIL' | $OSH -i
 | 
| 112 |   assert $? -eq 2
 | 
| 113 | 
 | 
| 114 |   # Bug fix: c_parser.Peek() in main_loop.InteractiveLoop can raise exceptions
 | 
| 115 |   echo 'v=`echo \"`' | $OSH -i
 | 
| 116 |   assert $? -eq 0
 | 
| 117 | }
 | 
| 118 | 
 | 
| 119 | test-help() {
 | 
| 120 |   set +o errexit
 | 
| 121 | 
 | 
| 122 |   # TODO: Test the oil.ovm binary as well as bin/oil.py.
 | 
| 123 |   export PYTHONPATH='.:vendor/'  # TODO: Put this in one place.
 | 
| 124 | 
 | 
| 125 |   # Bundle usage.
 | 
| 126 |   bin/oil.py --help
 | 
| 127 |   assert $? -eq 0
 | 
| 128 | 
 | 
| 129 |   # Pass applet as first name.
 | 
| 130 |   bin/oil.py osh --help
 | 
| 131 |   assert $? -eq 0
 | 
| 132 | 
 | 
| 133 |   bin/oil.py oil --help
 | 
| 134 |   assert $? -eq 0
 | 
| 135 | 
 | 
| 136 |   # Symlinks.
 | 
| 137 |   bin/osh --help
 | 
| 138 |   assert $? -eq 0
 | 
| 139 | 
 | 
| 140 |   bin/oil --help
 | 
| 141 |   assert $? -eq 0
 | 
| 142 | }
 | 
| 143 | 
 | 
| 144 | test-exit-builtin-interactive() {
 | 
| 145 |   set +o errexit
 | 
| 146 |   echo 'echo one; exit 42; echo two' | bin/osh -i
 | 
| 147 |   assert $? -eq 42
 | 
| 148 | }
 | 
| 149 | 
 | 
| 150 | test-rc-file() {
 | 
| 151 |   set +o errexit
 | 
| 152 | 
 | 
| 153 |   local rc=_tmp/testrc
 | 
| 154 |   echo 'PS1="TESTRC$ "' > $rc
 | 
| 155 | 
 | 
| 156 |   bin/osh -i --rcfile $rc < /dev/null
 | 
| 157 |   assert $? -eq 0
 | 
| 158 | 
 | 
| 159 |   bin/osh -i --rcfile /dev/null < /dev/null
 | 
| 160 |   assert $? -eq 0
 | 
| 161 | 
 | 
| 162 |   # oshrc is optional
 | 
| 163 |   # TODO: Could warn about nonexistent explicit --rcfile?
 | 
| 164 |   bin/osh -i --rcfile nonexistent__ < /dev/null
 | 
| 165 |   assert $? -eq 0
 | 
| 166 | }
 | 
| 167 | 
 | 
| 168 | test-noexec-fails-properly() {
 | 
| 169 |   set +o errexit
 | 
| 170 |   local tmp=_tmp/osh-usage-noexec.txt
 | 
| 171 |   bin/osh -n -c 'echo; echo; |' > $tmp
 | 
| 172 |   assert $? -eq 2
 | 
| 173 |   read < $tmp
 | 
| 174 |   assert $? -eq 1  # shouldn't have read any lines!
 | 
| 175 |   echo "$tmp appears empty, as expected"
 | 
| 176 | }
 | 
| 177 | 
 | 
| 178 | test-version() {
 | 
| 179 |   set +o errexit
 | 
| 180 |   bin/osh --version
 | 
| 181 |   assert $? -eq 0
 | 
| 182 | }
 | 
| 183 | 
 | 
| 184 | DISABLED-test-symlink() {
 | 
| 185 |   local tmp=_tmp/osh-usage
 | 
| 186 |   mkdir -p $tmp
 | 
| 187 | 
 | 
| 188 |   local repo_root=$PWD
 | 
| 189 | 
 | 
| 190 |   # requires 'make'
 | 
| 191 |   local bundle=$PWD/_bin/oil.ovm 
 | 
| 192 |   #local bundle=$repo_root/bin/oil.py
 | 
| 193 | 
 | 
| 194 |   ln -s -f -v $bundle $tmp/osh
 | 
| 195 |   ln -s -f -v $bundle $tmp/bash
 | 
| 196 | 
 | 
| 197 |   cd $tmp
 | 
| 198 | 
 | 
| 199 |   ./osh -c 'echo $OIL_VERSION'
 | 
| 200 |   assert $? -eq 0
 | 
| 201 |   ./bash -c 'echo $OIL_VERSION'
 | 
| 202 |   assert $? -eq 0
 | 
| 203 | }
 | 
| 204 | 
 | 
| 205 | run-for-release() {
 | 
| 206 |   run-other-suite-for-release osh-usage run-test-funcs
 | 
| 207 | }
 | 
| 208 | 
 | 
| 209 | soil-run() {
 | 
| 210 |   run-test-funcs
 | 
| 211 | }
 | 
| 212 | 
 | 
| 213 | "$@"
 |