| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Test the lossless invariant, which is useful for --tool ysh-ify and --tool
 | 
| 4 | # fmt.
 | 
| 5 | #
 | 
| 6 | # Usage:
 | 
| 7 | #   test/lossless.sh <function name>
 | 
| 8 | 
 | 
| 9 | set -o nounset
 | 
| 10 | set -o pipefail
 | 
| 11 | set -o errexit
 | 
| 12 | 
 | 
| 13 | source test/common.sh  # for run-other-suite-for-release
 | 
| 14 | source test/wild-runner.sh  # For MANIFEST, etc.
 | 
| 15 | 
 | 
| 16 | OSH=${OSH:-bin/osh}
 | 
| 17 | YSH=${YSH:-bin/ysh}
 | 
| 18 | 
 | 
| 19 | _compare() {
 | 
| 20 |   local path=$1
 | 
| 21 |   local sh=${2:-bin/osh}
 | 
| 22 | 
 | 
| 23 |   mkdir -p _tmp/lossless
 | 
| 24 |   $sh --tool lossless-cat $path > _tmp/lossless/left.txt
 | 
| 25 |   if diff -u $path _tmp/lossless/left.txt; then
 | 
| 26 | 	  echo "$path"
 | 
| 27 |   else
 | 
| 28 | 	  return 1
 | 
| 29 |   fi
 | 
| 30 | }
 | 
| 31 | 
 | 
| 32 | test-sh() {
 | 
| 33 |   for file in test/lossless/*.sh; do
 | 
| 34 |     echo "--- $file"
 | 
| 35 |     _compare $file
 | 
| 36 |   done
 | 
| 37 | }
 | 
| 38 | 
 | 
| 39 | test-ysh() {
 | 
| 40 |   for file in ysh/testdata/*.ysh test/lossless/*.ysh; do
 | 
| 41 |     echo "--- $file"
 | 
| 42 |     _compare $file $YSH
 | 
| 43 |   done
 | 
| 44 | }
 | 
| 45 | 
 | 
| 46 | test-ysh-strings() {
 | 
| 47 |   # TODO: extract test cases from test/ysh-every-string.sh
 | 
| 48 | 	# This includes multi-line strings
 | 
| 49 | 
 | 
| 50 |   echo 
 | 
| 51 | }
 | 
| 52 | 
 | 
| 53 | _compare-wild() {
 | 
| 54 |   local rel_path=$1
 | 
| 55 |   local abs_path=$2
 | 
| 56 | 
 | 
| 57 |   _compare $abs_path
 | 
| 58 | }
 | 
| 59 | 
 | 
| 60 | # Run on wild corpus.  I think this never passed?
 | 
| 61 | DISABLED-test-wild() {
 | 
| 62 |   wc -l $MANIFEST
 | 
| 63 |   cat $MANIFEST | xargs -n 2 -- $0 _compare-wild
 | 
| 64 | }
 | 
| 65 | 
 | 
| 66 | test-big-sh-files() {
 | 
| 67 |   local num_files=0
 | 
| 68 |   local num_passed=0
 | 
| 69 | 
 | 
| 70 |   local osh=bin/osh
 | 
| 71 | 
 | 
| 72 |   if false; then
 | 
| 73 |     local osh_cpp=_bin/cxx-asan/osh
 | 
| 74 |     ninja $osh_cpp
 | 
| 75 |     osh=$osh_cpp
 | 
| 76 |   fi
 | 
| 77 | 
 | 
| 78 |   for file in benchmarks/testdata/*; do
 | 
| 79 |     echo "--- $file"
 | 
| 80 |     echo
 | 
| 81 |     set +o errexit
 | 
| 82 |     time _compare $file $osh
 | 
| 83 |     local status=$?
 | 
| 84 |     set -o errexit
 | 
| 85 | 
 | 
| 86 |     if test $status = 0; then
 | 
| 87 |       num_passed=$((num_passed+1))
 | 
| 88 |     fi
 | 
| 89 |     num_files=$((num_files+1))
 | 
| 90 |   done
 | 
| 91 | 
 | 
| 92 |   echo
 | 
| 93 |   echo "$num_passed of $num_files files respect the lossless invariant"
 | 
| 94 | }
 | 
| 95 | 
 | 
| 96 | test-do-lossless-flag() {
 | 
| 97 | 
 | 
| 98 |   local sh_array='a[x+1]=1'
 | 
| 99 | 
 | 
| 100 |   # This gives you arithmetic parsing
 | 
| 101 |   $OSH -n -c "$sh_array"
 | 
| 102 |   # This gives you a sh_lhs.UnparsedIndex token!
 | 
| 103 |   $OSH --do-lossless -n -c "$sh_array"
 | 
| 104 | 
 | 
| 105 |   local backticks='`echo \`hostname\` zzz`'
 | 
| 106 | 
 | 
| 107 |   # This gives you NESTED Id.Left_Backtick and Id.Backtick_Right
 | 
| 108 |   $OSH -n -c "$backticks"
 | 
| 109 | 
 | 
| 110 |   # This gives (an erroneous?) Lit_EscapedChar
 | 
| 111 |   $OSH --do-lossless -n -c "$backticks"
 | 
| 112 | 
 | 
| 113 | 	local here=_tmp/lossless-here.sh 
 | 
| 114 |   cat >$here <<EOF
 | 
| 115 | cat <<-'HERE'
 | 
| 116 | 	one     # tabs stripped
 | 
| 117 | 		two   # 2 tabs
 | 
| 118 | 	three
 | 
| 119 | 	HERE
 | 
| 120 | EOF
 | 
| 121 | 
 | 
| 122 |   # TODO: May need to show difference with --tool tokens
 | 
| 123 |   $OSH -n $here
 | 
| 124 |   $OSH --do-lossless -n $here
 | 
| 125 | }
 | 
| 126 | 
 | 
| 127 | run-for-release() {
 | 
| 128 |   run-other-suite-for-release lossless run-test-funcs
 | 
| 129 | }
 | 
| 130 | 
 | 
| 131 | soil-run() {
 | 
| 132 |   run-test-funcs
 | 
| 133 | }
 | 
| 134 | 
 | 
| 135 | run-cpp() {
 | 
| 136 |   # Not automated now, but it speeds things up
 | 
| 137 | 
 | 
| 138 |   local variant=opt  # also asan, ubsan
 | 
| 139 | 
 | 
| 140 |   local osh=_bin/cxx-$variant/osh
 | 
| 141 |   local ysh=_bin/cxx-$variant/ysh
 | 
| 142 | 
 | 
| 143 |   ninja $osh $ysh
 | 
| 144 | 
 | 
| 145 |   OSH=$osh YSH=$ysh run-test-funcs
 | 
| 146 | }
 | 
| 147 | 
 | 
| 148 | "$@"
 |