1
#!/bin/sh
2
#shellcheck disable=SC2004
3

4 0
set -eu
5

6
# shellcheck source=lib/libexec/translator.sh
7 0
. "${SHELLSPEC_LIB:-./lib}/libexec/translator.sh"
8 0
use escape_quote
9

10 0
delimiter="DELIMITER-$SHELLSPEC_UNIXTIME-$$"
11

12
trans() {
13
  # shellcheck disable=SC2145
14 0
  "trans_$@"
15
}
16

17
trans_block_example_group() {
18 0
  putsn "("
19 0
  [ "$skipped" ] && trans_skip ""
20 0
  putsn "shellspec_group_id $block_id $block_no"
21 0
  putsn "SHELLSPEC_LINENO_BEGIN=$lineno_begin"
22 0
  putsn "shellspec_marker \"$specfile\" $lineno"
23 0
  putsn "shellspec_block${block_no}() { "
24 0
  putsn "shellspec_filter '$enabled' '$focused' '$filter'"
25 0
  putsn "shellspec_example_group $1"
26 0
  putsn "}; shellspec_yield${block_no}() { :;"
27
}
28

29
trans_block_example() {
30 0
  putsn "("
31 0
  [ "$skipped" ] && trans_skip ""
32 0
  putsn "shellspec_example_id $block_id $example_no $block_no"
33 0
  putsn "SHELLSPEC_LINENO_BEGIN=$lineno_begin"
34 0
  putsn "shellspec_marker \"$specfile\" $lineno"
35 0
  putsn "shellspec_block${block_no}() { "
36 0
  putsn "shellspec_filter '$enabled' '$focused' '$filter'"
37 0
  putsn "shellspec_example_block"
38 0
  putsn "}; shellspec_example${block_no}() { "
39 0
  putsn "if [ \$# -eq 0 ]"
40 0
  putsn "then shellspec_example ${1:-@} --"
41 0
  putsn "else shellspec_example ${1:-@} -- \"\$@\""
42 0
  putsn "fi"
43 0
  putsn "}; shellspec_yield${block_no}() { :;"
44
}
45

46
trans_block_end() {
47 0
  set -- "${1:-}"
48 0
  putsn "}; SHELLSPEC_LINENO_END=$lineno_end"
49 0
  putsn "shellspec_filter '$enabled'"
50 0
  putsn "shellspec_block${block_no}) ${1# }"
51 0
  putsn "shellspec_marker \"$specfile\" $lineno"
52
}
53

54
trans_before_first_block() {
55 0
  putsn "shellspec_before_first_block"
56
}
57

58
trans_after_last_block() {
59 0
  putsn "shellspec_after_last_block"
60
}
61

62
trans_after_block() {
63 0
  putsn "shellspec_after_block"
64
}
65

66
trans_evaluation() {
67 0
  putsn "SHELLSPEC_LINENO=$lineno"
68 0
  putsn "if [ \$# -eq 0 ]"
69 0
  putsn "then shellspec_invoke_data"
70 0
  putsn "else shellspec_invoke_data \"\$@\""
71 0
  putsn "fi"
72 0
  putsn "shellspec_statement $1 $2"
73 0
  putsn "if [ -e \"\$SHELLSPEC_VARS_FILE\" ]; then"
74 0
  putsn "  . \"\$SHELLSPEC_VARS_FILE\""
75 0
  putsn "fi"
76
}
77

78
trans_expectation() {
79 0
  putsn "SHELLSPEC_LINENO=$lineno"
80 0
  putsn "shellspec_statement $1 $2"
81
}
82

83
trans_control() {
84 0
  putsn "SHELLSPEC_AUX_LINENO=$lineno"
85 0
  putsn "shellspec_$1 $2"
86
}
87

88
trans_pending() {
89 0
  putsn "SHELLSPEC_LINENO=$lineno"
90 0
  putsn "shellspec_pending ${1:-}"
91
}
92

93
trans_skip() {
94 0
  putsn "SHELLSPEC_LINENO=$lineno"
95 0
  putsn "shellspec_skip $skip_id ${1:-}"
96
}
97

98
trans_data_begin() {
99 0
  putsn "shellspec_data() {"
100
}
101

102
trans_data_text() {
103 0
  putsn "  shellspec_putsn $1"
104
}
105

106
trans_data_func() {
107 0
  putsn "  $1"
108
}
109

110
trans_data_file() {
111 0
  putsn "shellspec_cat $1"
112
}
113

114
trans_data_end() {
115 0
  putsn "}"
116 0
  putsn "SHELLSPEC_DATA=1"
117
}
118

119
trans_embedded_text_begin() {
120 0
  case $1 in
121 0
    expand) putsn "shellspec_cat <<DATA-$delimiter $2" ;;
122
    raw)    putsn "shellspec_cat <<'DATA-$delimiter' $2" ;;
123
  esac
124
}
125

126
trans_embedded_text_line() {
127
  putsn "$1"
128
}
129

130
trans_embedded_text_end() {
131
  putsn "DATA-$delimiter"
132
}
133

134
trans_out() {
135
  case $1 in
136
    logger)   putsn "shellspec_logger $2" ;;
137
    preserve) putsn "shellspec_preserve $2" ;;
138
    printf)   putsn "shellspec_printf $2" ;;
139
    puts)     putsn "shellspec_puts $2" ;;
140
    putsn)    putsn "shellspec_putsn $2" ;;
141
    sleep)    putsn "shellspec_sleep $2" ;;
142
  esac
143
}
144

145
trans_parameters_begin() {
146
  putsn "SHELLSPEC_PARAMETER_NO=$1"
147
  putsn "shellspec_parameters$1() { :;"
148
}
149

150
trans_parameters() {
151
  putsn "shellspec_parameterized_example $1"
152
}
153

154
trans_parameters_end() {
155
  putsn "}"
156
}
157

158
trans_mock_begin() {
159
  putsn "shellspec_unsetf ${1%% *} ||:"
160
  putsn "shellspec_after_mock shellspec_unmock_${mock_no}"
161
  putsn "shellspec_unmock_${mock_no}() {"
162
  putsn "  shellspec_unmock $1"
163
  putsn "}"
164
  putsn "<<'MOCK-$delimiter' shellspec_mock $1"
165
}
166

167
trans_mock_end() {
168
  putsn "MOCK-$delimiter"
169
}
170

171
trans_constant() {
172
  ( eval "putsn $1=\\'$2\\'" ) ||:
173
}
174

175
trans_include() {
176
  putsn "if shellspec_unless SKIP; then"
177
  putsn "  __SOURCED__=${1# }"
178
  putsn "  shellspec_coverage_start"
179
  putsn "  . $1"
180
  putsn "  shellspec_coverage_stop"
181
  putsn "  unset __SOURCED__ ||:"
182
  putsn "fi"
183
}
184

185
trans_line() {
186
  putsn "$1"
187
}
188

189
trans_with_function() {
190
  putsn "$1 { "
191
}
192

193
syntax_error() {
194
  set -- "Syntax error: $1 in $specfile line $lineno" "${2:-}"
195
  putsn "shellspec_abort $SHELLSPEC_ERROR_EXIT_CODE \"$1\" \"$2\" 2>&3"
196
}
197

198
metadata=1 finished=1 coverage='' fd='' spec_no=1 progress=''
199

200
for param in "$@"; do
201
  case $param in
202
    --no-metadata) metadata='' ;;
203
    --no-finished) finished='' ;;
204
    --coverage   ) coverage=1 ;;
205
    --fd=*       ) fd=${param#*=} ;;
206
    --spec-no=*  ) spec_no=${param#*=} ;;
207
    --progress   ) progress=1 ;;
208
    *) set -- "$@" "$param" ;;
209
  esac
210
  shift
211
done
212

213
filter=1
214
[ "$SHELLSPEC_FOCUS_FILTER" ] && filter=''
215
[ "$SHELLSPEC_TAG_FILTER" ] && filter=''
216
[ "$SHELLSPEC_EXAMPLE_FILTER" ] && filter=''
217

218
putsn "#!/bin/sh"
219
putsn "export SHELLSPEC_PATH=\"\${SHELLSPEC_PATH:-\$PATH}\""
220
putsn "SHELLSPEC_SPECFILE=''"
221
putsn "SHELLSPEC_DATA=''"
222
putsn "SHELLSPEC_WORKDIR=\"\$SHELLSPEC_TMPBASE\""
223
putsn "SHELLSPEC_MOCK_BINDIR=\"\$SHELLSPEC_WORKDIR/$spec_no\""
224
putsn "SHELLSPEC_STDIO_FILE_BASE=\"\$SHELLSPEC_WORKDIR\""
225
puts "PATH=\"\$SHELLSPEC_MOCK_BINDIR:\$SHELLSPEC_SUPPORT_BINDIR"
226
if [ "$SHELLSPEC_SANDBOX" ]; then
227
  putsn "\${SHELLSPEC_SANDBOX_PATH:+:}\$SHELLSPEC_SANDBOX_PATH\""
228
  putsn "readonly PATH"
229
else
230
  putsn "\${PATH:+:}\$PATH\""
231
fi
232
putsn "[ \"\$SHELLSPEC_DEBUG_TRAP\" ] && trap - DEBUG"
233
putsn "shellspec_coverage_setup() { shellspec_coverage_disabled; }"
234
[ "$coverage" ] && putsn ". \"\${SHELLSPEC_COVERAGE_SETUP:-/dev/null}\""
235
[ "$fd" ] && putsn "exec 1>&$fd"
236
putsn ". \"\$SHELLSPEC_LIB/bootstrap.sh\""
237
putsn "shellspec_coverage_setup \"\$SHELLSPEC_SHELL_TYPE\""
238
putsn "shellspec_metadata $metadata"
239

240
specfile_count=0
241
progress() { :; }
242
if [ "$progress" ]; then
243
  count_specfile() { specfile_count=$(($specfile_count + 1)); }
244
  eval find_specfiles count_specfile ${1+'"$@"'}
245
  progress() { puts "$@" > "$SHELLSPEC_DEV_TTY"; }
246
fi
247

248
specfile() {
249
  [ -e "$2" ] || return 0
250
  progress "${CR}Translate[$spec_no/$specfile_count]: $2${ESC}[K"
251
  (
252
    specfile=$2 ranges=${3:-} run_all=''
253
    escape_quote specfile
254
    [ "$ranges" ] && enabled='' || enabled=1
255
    [ "$enabled" ] && [ "$filter" ] && run_all=1
256

257
    putsn "shellspec_marker '$specfile' ---"
258
    putsn "(shellspec_begin '$specfile' '$spec_no'"
259
    putsn "shellspec_perform '$enabled' '$filter'"
260
    initialize
261
    putsn "shellspec_marker '$specfile' BOF"
262
    translate < "$2"
263
    putsn "shellspec_marker '$specfile' EOF"
264
    finalize
265
    putsn "shellspec_end ${run_all:+$(($example_no - 1))})"
266
  )
267
  wait # Workaround for ksh88. Segmentation fault when processing many files.
268
  spec_no=$(($spec_no + 1))
269
}
270
eval find_specfiles specfile ${1+'"$@"'}
271
progress "${CR}${ESC}[2K"
272

273
putsn "shellspec_finished $finished"

Read our documentation on viewing source code .

Loading