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