forked from brokenpip3/setup-bats-libs
-
Notifications
You must be signed in to change notification settings - Fork 7
/
action.yaml
383 lines (354 loc) · 14.4 KB
/
action.yaml
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
name: Setup Bats and Bats libraries
description: A GitHub Action for installing Bats and Bats-libs(support, assert, detik, file)
author: bats-core
branding:
color: yellow
icon: command
inputs:
# Bats binary
bats-install:
description: "Bats installation, default to true"
required: false
default: true
bats-version:
description: "Bats version, default to latest (1.11.0 atm)"
required: false
# support
support-install:
description: "Bats-support installation, default to true"
required: false
default: true
support-version:
description: "Bats-support version, default to latest"
required: false
default: "0.3.0"
support-path:
description: "Bats-support path, default to /usr/lib/bats-support"
required: false
default: "/usr/lib/bats-support"
support-clean:
description: "Bats-support: clean temp files"
required: false
default: true
# assert
assert-install:
description: "Bats-assert installation, default to true"
required: false
default: true
assert-version:
description: "Bats-assert version, default to latest"
required: false
default: "2.1.0"
assert-path:
description: "Bats-assert path, default to /usr/lib/bats-assert"
required: false
default: "/usr/lib/bats-assert"
assert-clean:
description: "Bats-assert: clean temp files"
required: false
default: true
# detik
detik-install:
description: "Bats-detik installation, default to true"
required: false
default: true
detik-version:
description: "Bats-detik version, default to latest"
required: false
default: "1.3.2"
detik-path:
description: "Bats-detik path, default to /usr/lib/bats-detik"
required: false
default: "/usr/lib/bats-detik"
detik-clean:
description: "Bats-detik: clean temp files"
required: false
default: true
# file
file-install:
description: "Bats-file installation, default to true"
required: false
default: true
file-version:
description: "Bats-file version, default to latest"
required: false
default: "0.4.0"
file-path:
description: "Bats-file path, default to /usr/lib/bats-file"
required: false
default: "/usr/lib/bats-file"
file-clean:
description: "Bats-file: clean temp files"
required: false
default: true
outputs:
bats-installed:
description: "True/False if bats has been installed"
value: ${{ (steps.bats-install.outputs.bats-installed != '') }}
support-installed:
description: "True/False if bats-support has been installed"
value: ${{ (steps.support-install.outputs.support-installed != '') }}
assert-installed:
description: "True/False if bats-assert has been installed"
value: ${{ (steps.assert-install.outputs.assert-installed != '') }}
detik-installed:
description: "True/False if bats-detik has been installed"
value: ${{ (steps.detik-install.outputs.detik-installed != '') }}
file-installed:
description: "True/False if bats-file has been installed"
value: ${{ (steps.file-install.outputs.file-installed != '') }}
lib-path:
description: "Bats lib path to use to load the libraries"
value: ${{ steps.libpath.outputs.libpath }}
tmp-path:
description: "Temporary path with each library tests"
value: ${{ steps.set-paths.outputs.tmp-path }}
runs:
using: composite
steps:
# This action would be much easier if only matrix steps will be supported in a composite action
- name: "Set cache for Bats"
uses: actions/cache@v4
if: inputs.bats-install == 'true'
id: bats-cache
with:
path: |
~/.local/share/bats
key: ${{ runner.os }}-${{ runner.arch }}-bats-${{ inputs.bats-version }}
- name: "Set PATH in case of cache-hit"
if: inputs.bats-install == 'true' && steps.bats-cache.outputs.cache-hit == 'true'
id: bats-cache-path
shell: bash
run: |
DESTDIR="$HOME/.local/share/bats"
echo "${DESTDIR}/bin" >> "$GITHUB_PATH"
- name: "Download and install Bats"
if: inputs.bats-install == 'true' && steps.bats-cache.outputs.cache-hit != 'true'
id: bats-install
shell: bash
run: |
# In $HOME to avoid sudo requirements
VERSION=${{ inputs.bats-version }}
DESTDIR="$HOME/.local/share/bats"
TEMPDIR="/tmp/bats"
URL="https://github.com/bats-core/bats-core/"
# From https://github.com/fluxcd/flux2/blob/44d69d6fc0c353e79c1bad021a4aca135033bce8/action/action.yml#L35
if [[ -z "$VERSION" ]] || [[ "$VERSION" = "latest" ]]; then
VERSION=$(curl -fsSL --retry 4 --retry-connrefused https://api.github.com/repos/bats-core/bats-core/releases/latest | grep tag_name | cut -d '"' -f 4)
fi
[[ $VERSION == v* ]] && VERSION="${VERSION:1}"
mkdir -p ${TEMPDIR}
mkdir -p ${DESTDIR}
curl -sL --retry 4 --retry-connrefused ${URL}/archive/refs/tags/v${VERSION}.tar.gz | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
./install.sh ${DESTDIR}
echo "Bats v${VERSION} installed in ${DESTDIR}"
echo "${DESTDIR}/bin" >> "$GITHUB_PATH"
echo "bats-installed=true" >> $GITHUB_OUTPUT
rm -rf ${TEMPDIR} || exit 0
- name: Set paths and commands for libraries
shell: bash
id: set-paths
run: |
if [[ "$RUNNER_OS" == "macOS" ]]; then
DEFAULT_BASE_DIR="/usr/local/lib"
[[ "${DEFAULT_BASE_DIR}" == "$HOME"* ]] && CMD="" || CMD="sudo"
TEMPDIR="/tmp"
elif [[ "$RUNNER_OS" == "Windows" ]]; then
DEFAULT_BASE_DIR="/c/Users/runneradmin"
CMD=""
TEMPDIR="$HOME/AppData/Local/Temp"
else
DEFAULT_BASE_DIR="/usr/lib"
[[ "${DEFAULT_BASE_DIR}" == "$HOME"* ]] && CMD="" || CMD="sudo"
TEMPDIR="/tmp"
fi
echo "_LIB_PATH=$DEFAULT_BASE_DIR" >> /tmp/bats-lib-path
echo "DEFAULT_BASE_DIR=$DEFAULT_BASE_DIR" >> $GITHUB_ENV
echo "CMD=$CMD" >> $GITHUB_ENV
echo "TEMPDIR=$TEMPDIR" >> $GITHUB_ENV
echo "tmp-path=$TEMPDIR" >> $GITHUB_OUTPUT
- name: "Calculate DESTDIR for Bats-support"
if: inputs.support-install == 'true'
id: calculate-support-destdir
shell: bash
run: |
if [ -z "${{ inputs.support-path }}" ] || [ "${{ inputs.support-path }}" == "/usr/lib/bats-support" ]; then
echo "SUPPORT_DESTDIR=${DEFAULT_BASE_DIR}/bats-support" >> $GITHUB_ENV
else
echo "SUPPORT_DESTDIR=${{ inputs.support-path }}" >> $GITHUB_ENV
fi
- name: "Set cache for Bats-support"
if: inputs.support-install == 'true'
uses: actions/cache@v4
id: support-cache
with:
path: ${{ env.SUPPORT_DESTDIR }}
key: ${{ runner.os }}-${{ runner.arch }}-bats-support-${{ inputs.support-version }}
- name: "Download and install Bats-support"
if: inputs.support-install == 'true' && steps.support-cache.outputs.cache-hit != 'true'
id: support-install
shell: bash
run: |
VERSION=${{ inputs.support-version }}
url="https://github.com/bats-core/bats-support/archive/refs/tags/v${VERSION}.tar.gz"
TEMPDIR="${TEMPDIR}/bats-support"
DESTDIR=${SUPPORT_DESTDIR}
mkdir -p ${TEMPDIR}
${CMD} mkdir -p ${DESTDIR}/src/
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
# Archlinux style, except that we are not in a fakeroot env
${CMD} install -m755 load.bash ${DESTDIR}/load.bash
for fn in src/*.bash; do
${CMD} install -m755 $fn \
${DESTDIR}/src/$(basename $fn)
done
echo "Bats Support v$VERSION installed in $DESTDIR"
echo "support-installed=true" >> $GITHUB_OUTPUT
[[ "${{ inputs.support-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
- name: "Calculate DESTDIR for Bats-assert"
if: inputs.assert-install == 'true'
id: calculate-assert-destdir
shell: bash
run: |
if [ -z "${{ inputs.assert-path }}" ] || [ "${{ inputs.assert-path }}" == "/usr/lib/bats-assert" ]; then
echo "ASSERT_DESTDIR=${DEFAULT_BASE_DIR}/bats-assert" >> $GITHUB_ENV
else
echo "ASSERT_DESTDIR=${{ inputs.assert-path }}" >> $GITHUB_ENV
fi
- name: "Set cache for Bats-assert"
if: inputs.assert-install == 'true'
uses: actions/cache@v4
id: assert-cache
with:
path: ${{ env.ASSERT_DESTDIR }}
key: ${{ runner.os }}-${{ runner.arch }}-bats-assert-${{ inputs.assert-version }}
- name: "Download and install Bats-assert"
if: inputs.assert-install == 'true' && steps.assert-cache.outputs.cache-hit != 'true'
id: assert-install
shell: bash
run: |
VERSION=${{ inputs.assert-version }}
url="https://github.com/bats-core/bats-assert/archive/refs/tags/v${VERSION}.tar.gz"
TEMPDIR="${TEMPDIR}/bats-assert"
DESTDIR=${ASSERT_DESTDIR}
mkdir -p ${TEMPDIR}
${CMD} mkdir -p ${DESTDIR}/src/
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
# Archlinux style, except that we are not in a fakeroot env
${CMD} install -m755 load.bash ${DESTDIR}/load.bash
for fn in src/*.bash; do
${CMD} install -m755 $fn \
${DESTDIR}/src/$(basename $fn)
done
echo "Bats Assert v$VERSION installed in $DESTDIR"
echo "assert-installed=true" >> "$GITHUB_OUTPUT"
[[ "${{ inputs.assert-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
- name: "Calculate DESTDIR for Bats-detik"
if: inputs.detik-install == 'true'
id: calculate-detik-destdir
shell: bash
run: |
if [ -z "${{ inputs.detik-path }}" ] || [ "${{ inputs.detik-path }}" == "/usr/lib/bats-detik" ]; then
echo "DETIK_DESTDIR=${DEFAULT_BASE_DIR}/bats-detik" >> $GITHUB_ENV
else
echo "DETIK_DESTDIR=${{ inputs.detik-path }}" >> $GITHUB_ENV
fi
- name: "Set cache for Bats-detik"
if: inputs.detik-install == 'true'
uses: actions/cache@v4
id: detik-cache
with:
path: ${{ env.DETIK_DESTDIR }}
key: ${{ runner.os }}-${{ runner.arch }}-bats-detik-${{ inputs.detik-version }}
- name: "Download and install Bats-detik"
if: inputs.detik-install == 'true' && steps.detik-cache.outputs.cache-hit != 'true'
id: detik-install
shell: bash
run: |
VERSION=${{ inputs.detik-version }}
url="https://github.com/bats-core/bats-detik/archive/refs/tags/v${VERSION}.tar.gz"
TEMPDIR="${TEMPDIR}/bats-detik"
DESTDIR="${DETIK_DESTDIR}"
mkdir -p ${TEMPDIR}
${CMD} mkdir -p ${DESTDIR}/src/
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
# Archlinux style, except that we are not in a fakeroot env so we need to use sudo
for fn in lib/*.bash; do
${CMD} install -m755 $fn \
${DESTDIR}/$(basename $fn)
done
echo "Bats Detik v$VERSION installed in $DESTDIR"
echo "detik-installed=true" >> $GITHUB_OUTPUT
[[ "${{ inputs.detik-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
- name: "Calculate DESTDIR for Bats-file"
if: inputs.file-install == 'true'
id: calculate-file-destdir
shell: bash
run: |
if [ -z "${{ inputs.file-path }}" ] || [ "${{ inputs.file-path }}" == "/usr/lib/bats-file" ]; then
echo "FILE_DESTDIR=${DEFAULT_BASE_DIR}/bats-file" >> $GITHUB_ENV
else
echo "FILE_DESTDIR=${{ inputs.file-path }}" >> $GITHUB_ENV
fi
- name: "Set cache for Bats-file"
if: inputs.file-install == 'true'
uses: actions/cache@v4
id: file-cache
with:
path: ${{ env.FILE_DESTDIR }}
key: ${{ runner.os }}-${{ runner.arch }}-bats-file-${{ inputs.file-version }}
- name: "Download and install Bats-file"
if: inputs.file-install == 'true' && steps.file-cache.outputs.cache-hit != 'true'
id: file-install
shell: bash
run: |
VERSION=${{ inputs.file-version }}
url="https://github.com/bats-core/bats-file/archive/refs/tags/v${VERSION}.tar.gz"
TEMPDIR="${TEMPDIR}/bats-file"
DESTDIR="${FILE_DESTDIR}"
mkdir -p ${TEMPDIR}
${CMD} mkdir -p ${DESTDIR}/src/
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
# Archlinux style, except that we are not in a fakeroot env
${CMD} install -m755 load.bash ${DESTDIR}/load.bash
for fn in src/*.bash; do
${CMD} install -m755 $fn \
${DESTDIR}/src/$(basename $fn)
done
echo "Bats File v$VERSION installed in $DESTDIR"
echo "file-installed=true" >> $GITHUB_OUTPUT
[[ "${{ inputs.file-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
- name: "build BATS_LIB_PATH"
id: libpath
shell: bash
run: |
# In case of windows if the path passed
# as github workspace could be something like
# D:\a\bats-action\bats-action/tests
# so we need to translate D: into /d/
# and replace the wrong slash
winfix() {
local winpath="$1"
echo "$winpath" | sed 's|\\|/|g' | sed 's|^\([A-Za-z]\):|/\L\1|'
}
if [[ "$RUNNER_OS" == "Windows" ]]; then
S="$(winfix ${SUPPORT_DESTDIR%/*})"
A="$(winfix ${ASSERT_DESTDIR%/*})"
D="$(winfix ${DETIK_DESTDIR%/*})"
F="$(winfix ${FILE_DESTDIR%/*})"
LIB_PATH="${S}:${A}:${D}:${F}"
else
LIB_PATH="${SUPPORT_DESTDIR%/*}:${ASSERT_DESTDIR%/*}:${DETIK_DESTDIR%/*}:${FILE_DESTDIR%/*}"
fi
echo "libpath=$LIB_PATH" >> $GITHUB_OUTPUT
- name: "Print info"
id: info
shell: bash
run: |
echo "Bats installed: ${{ (steps.bats-install.outputs.bats-installed != '') }}"
echo "Support installed: ${{ (steps.support-install.outputs.support-installed != '') }}"
echo "Assert installed: ${{ (steps.assert-install.outputs.assert-installed != '') }}"
echo "Detik installed: ${{ (steps.detik-install.outputs.detik-installed != '') }}"
echo "File installed: ${{ (steps.file-install.outputs.file-installed != '') }}"
echo "Bats lib to use in BATS_LIB_PATH env ${{ steps.libpath.outputs.libpath }}"
echo "Tmp path: ${{ steps.set-paths.outputs.tmp-path }}"