Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 2005b9f

Browse files
authored
feat: remove sudo if inside home directory, add cache for bats-libs, add outputs (#9)
* remove sudo if inside home directory * fix: needs in local home action * fix: set the right libraries path * add outputs, support if cache in tests * add cache for all the libraries * add version to cache key * lowercase true * add debug * add cache explanation in README * debug output run only if in debug mode
1 parent 14f931e commit 2005b9f

File tree

3 files changed

+221
-23
lines changed

3 files changed

+221
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: "Test local action inside $HOME"
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
release:
6+
types: [published]
7+
push:
8+
branches: ["main"]
9+
tags: ["*"]
10+
11+
jobs:
12+
local_test_home:
13+
runs-on: ubuntu-latest
14+
env:
15+
BATS_LIB_PATH: "${{ github.workspace }}/tests"
16+
TERM: xterm
17+
name: local default
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
- name: Setup Bats and Bats libs
22+
id: setup-bats-libs
23+
uses: ./
24+
with:
25+
support-clean: "false"
26+
support-path: "${{ github.workspace }}/tests/bats-support"
27+
assert-clean: "false"
28+
assert-path: "${{ github.workspace }}/tests/bats-assert"
29+
detik-clean: "false"
30+
detik-path: "${{ github.workspace }}/tests/bats-detik"
31+
file-clean: "false"
32+
file-path: "${{ github.workspace }}/tests/bats-file"
33+
- name: Execute test to check Bats-support
34+
if: steps.setup-bats-libs.outputs.support-installed == 'true'
35+
run: |
36+
cd /tmp/bats-support/
37+
bats test
38+
- name: Execute test to check Bats-assert
39+
if: steps.setup-bats-libs.outputs.assert-installed == 'true'
40+
run: |
41+
ls -l $BATS_LIB_PATH/
42+
cd /tmp/bats-assert/
43+
bats test
44+
- name: Execute test to check Bats-detik
45+
if: steps.setup-bats-libs.outputs.detik-installed == 'true'
46+
run: |
47+
cd /tmp/bats-detik/
48+
bats tests
49+
- name: Execute test to check Bats-file
50+
if: steps.setup-bats-libs.outputs.file-installed == 'true'
51+
# Currently the bats-file tests are broken
52+
# in gh runner env
53+
continue-on-error: true
54+
run: |
55+
cd /tmp/bats-file/
56+
bats test
57+
- name: Execute example tests
58+
run:
59+
bats -T -p tests
60+
local_test_home_trigger_cache:
61+
needs: [local_test_home]
62+
runs-on: ubuntu-latest
63+
env:
64+
BATS_LIB_PATH: "${{ github.workspace }}/tests"
65+
TERM: xterm
66+
name: local default cache
67+
steps:
68+
- name: Checkout
69+
uses: actions/checkout@v3
70+
- name: Setup Bats and Bats libs
71+
id: setup-bats-libs
72+
uses: ./
73+
with:
74+
support-clean: "false"
75+
support-path: "${{ github.workspace }}/tests/bats-support"
76+
assert-clean: "false"
77+
assert-path: "${{ github.workspace }}/tests/bats-assert"
78+
detik-clean: "false"
79+
detik-path: "${{ github.workspace }}/tests/bats-detik"
80+
file-clean: "false"
81+
file-path: "${{ github.workspace }}/tests/bats-file"
82+
- name: Execute test to check Bats-support
83+
if: steps.setup-bats-libs.outputs.support-installed == 'true'
84+
run: |
85+
cd /tmp/bats-support/
86+
bats test
87+
- name: Execute test to check Bats-assert
88+
if: steps.setup-bats-libs.outputs.assert-installed == 'true'
89+
run: |
90+
ls -l $BATS_LIB_PATH/
91+
cd /tmp/bats-assert/
92+
bats test
93+
- name: Execute test to check Bats-detik
94+
if: steps.setup-bats-libs.outputs.detik-installed == 'true'
95+
run: |
96+
cd /tmp/bats-detik/
97+
bats tests
98+
- name: Execute test to check Bats-file
99+
if: steps.setup-bats-libs.outputs.file-installed == 'true'
100+
# Currently the bats-file tests are broken
101+
# in gh runner env
102+
continue-on-error: true
103+
run: |
104+
cd /tmp/bats-file/
105+
bats test
106+
- name: Execute example tests
107+
run:
108+
bats -T -p tests

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ For example, if you want to install `bats-support` in the `./test/bats-support`
4040
support-path: ${{ github.workspace }}/test/bats-support
4141
```
4242

43+
## About Caching
44+
45+
The caching mechanism for the `bats binary` is always available. However, the caching for the `bats libraries` is dependent on the location of each library path. If a library is located within the $HOME directory, caching is supported. Conversely, if a library is located outside the $HOME directory (which is the default location per each library), caching is not supported. This is due to a known limitation with sudo and the cache action, as detailed in this GitHub issue: https://github.com/actions/toolkit/issues/946.
46+
4347
## Inputs
4448

4549
| Key | Default | Required | Description |
@@ -63,6 +67,16 @@ For example, if you want to install `bats-support` in the `./test/bats-support`
6367
| file-path | `/usr/lib/bats-file` | false | Bats-file path |
6468
| file-clean | `true` | false | Bats-file: clean temp files |
6569

70+
## Outputs
71+
72+
| Key | Description |
73+
|------------------|------------------------------------------------|
74+
| bats-installed | True/False if bats has been installed |
75+
| support-installed| True/False if bats-support has been installed |
76+
| assert-installed | True/False if bats-assert has been installed |
77+
| detik-installed | True/False if bats-detik has been installed |
78+
| file-installed | True/False if bats-file has been installed |
79+
6680
## TODO
6781

6882
* [X] Add more tests
@@ -73,6 +87,6 @@ For example, if you want to install `bats-support` in the `./test/bats-support`
7387

7488
* [X] Better Readme
7589

76-
* [ ] Remove sudo in case of bats libs installed in $HOME
90+
* [x] Remove sudo in case of bats libs installed in $HOME
7791

78-
* [ ] Add cache for bats libs
92+
* [x] Add cache for bats libs

action.yaml

+97-21
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ inputs:
8181
description: "Bats-file: clean temp files"
8282
required: false
8383
default: true
84+
outputs:
85+
bats-installed:
86+
description: "True/False if bats has been installed"
87+
value: ${{ (steps.bats-install.outputs.bats-installed != '') }}
88+
support-installed:
89+
description: "True/False if bats-support has been installed"
90+
value: ${{ (steps.support-install.outputs.support-installed != '') }}
91+
assert-installed:
92+
description: "True/False if bats-assert has been installed"
93+
value: ${{ (steps.assert-install.outputs.assert-installed != '') }}
94+
detik-installed:
95+
description: "True/False if bats-detik has been installed"
96+
value: ${{ (steps.detik-install.outputs.detik-installed != '') }}
97+
file-installed:
98+
description: "True/False if bats-file has been installed"
99+
value: ${{ (steps.file-install.outputs.file-installed != '') }}
100+
84101
runs:
85102
using: composite
86103
steps:
@@ -93,12 +110,14 @@ runs:
93110
path: |
94111
~/.local/share/bats
95112
~/.local/bin/bats
96-
key: ${{ runner.os }}-bats
113+
key: ${{ runner.os }}-bats-${{ inputs.bats-version }}
114+
97115
- name: "Download and install Bats"
98116
if: inputs.bats-install == 'true' && steps.bats-cache.outputs.cache-hit != 'true'
117+
id: bats-install
99118
shell: bash
100119
run: |
101-
# In $HOME to avoid sudo requirements that eventually will be removed for the library as well
120+
# In $HOME to avoid sudo requirements
102121
VERSION=${{ inputs.bats-version }}
103122
DESTDIR="$HOME/.local/bin"
104123
LIBDIR="$HOME/.local/share/bats"
@@ -131,12 +150,21 @@ runs:
131150
install -Dm755 lib/bats-core/* -t "${LIBDIR}"
132151
133152
echo "Bats v$VERSION installed in $DESTDIR"
153+
echo "$DESTDIR" >> "$GITHUB_PATH"
154+
echo "bats-installed=true" >> $GITHUB_OUTPUT
134155
rm -rf ${TEMPDIR} || exit 0
135156
136-
echo "$DESTDIR" >> "$GITHUB_PATH"
157+
- name: "Set cache for Bats-support"
158+
uses: actions/cache@v3
159+
if: inputs.support-install == 'true'
160+
id: support-cache
161+
with:
162+
path: ${{ inputs.support-path }}
163+
key: ${{ runner.os }}-bats-support-${{ inputs.support-version }}
137164

138165
- name: "Download and install Bats-support"
139-
if: inputs.support-install == 'true'
166+
if: inputs.support-install == 'true' && steps.support-cache.outputs.cache-hit != 'true'
167+
id: support-install
140168
shell: bash
141169
run: |
142170
VERSION=${{ inputs.support-version }}
@@ -145,19 +173,31 @@ runs:
145173
url="https://github.com/bats-core/bats-support/archive/refs/tags/v${VERSION}.tar.gz"
146174
147175
mkdir -p ${TEMPDIR}
148-
sudo mkdir -p ${DESTDIR}/src/
176+
[[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo"
177+
${CMD} mkdir -p ${DESTDIR}/src/
149178
curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
150-
# Archlinux style, except that we are not in a fakeroot env so we need to use sudo
151-
sudo install -Dm755 load.bash ${DESTDIR}/load.bash
179+
# Archlinux style, except that we are not in a fakeroot env
180+
${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash
152181
for fn in src/*.bash; do
153-
sudo install -Dm755 $fn \
182+
${CMD} install -Dm755 $fn \
154183
${DESTDIR}/src/$(basename $fn)
155184
done
156185
echo "Bats Support v$VERSION installed in $DESTDIR"
186+
echo "support-installed=true" >> $GITHUB_OUTPUT
157187
# Cleanup bats-support if required
158188
[[ "${{ inputs.support-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0
159-
- name: "Download and install Bats-assert"
189+
190+
- name: "Set cache for Bats-assert"
191+
uses: actions/cache@v3
160192
if: inputs.assert-install == 'true'
193+
id: assert-cache
194+
with:
195+
path: ${{ inputs.assert-path }}
196+
key: ${{ runner.os }}-bats-assert-${{ inputs.assert-version }}
197+
198+
- name: "Download and install Bats-assert"
199+
if: inputs.assert-install == 'true' && steps.assert-cache.outputs.cache-hit != 'true'
200+
id: assert-install
161201
shell: bash
162202
run: |
163203
VERSION=${{ inputs.assert-version }}
@@ -166,19 +206,31 @@ runs:
166206
url="https://github.com/bats-core/bats-assert/archive/refs/tags/v${VERSION}.tar.gz"
167207
168208
mkdir -p ${TEMPDIR}
169-
sudo mkdir -p ${DESTDIR}/src/
209+
[[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo"
210+
${CMD} mkdir -p ${DESTDIR}/src/
170211
curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
171-
# Archlinux style, except that we are not in a fakeroot env so we need to use sudo
172-
sudo install -Dm755 load.bash ${DESTDIR}/load.bash
212+
# Archlinux style, except that we are not in a fakeroot env
213+
${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash
173214
for fn in src/*.bash; do
174-
sudo install -Dm755 $fn \
215+
${CMD} install -Dm755 $fn \
175216
${DESTDIR}/src/$(basename $fn)
176217
done
177218
echo "Bats Assert v$VERSION installed in $DESTDIR"
219+
echo "assert-installed=true" >> "$GITHUB_OUTPUT"
178220
# Cleanup bats-support if required
179221
[[ "${{ inputs.assert-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0
180-
- name: "Download and install Bats-detik"
222+
223+
- name: "Set cache for Bats-detik"
224+
uses: actions/cache@v3
181225
if: inputs.detik-install == 'true'
226+
id: detik-cache
227+
with:
228+
path: ${{ inputs.detik-path }}
229+
key: ${{ runner.os }}-bats-detik-${{ inputs.detik-version }}
230+
231+
- name: "Download and install Bats-detik"
232+
if: inputs.detik-install == 'true' && steps.detik-cache.outputs.cache-hit != 'true'
233+
id: detik-install
182234
shell: bash
183235
run: |
184236
VERSION=${{ inputs.detik-version }}
@@ -187,18 +239,30 @@ runs:
187239
url="https://github.com/bats-core/bats-detik/archive/refs/tags/v${VERSION}.tar.gz"
188240
189241
mkdir -p ${TEMPDIR}
190-
sudo mkdir -p ${DESTDIR}/src/
242+
[[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo"
243+
${CMD} mkdir -p ${DESTDIR}/src/
191244
curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
192245
# Archlinux style, except that we are not in a fakeroot env so we need to use sudo
193246
for fn in lib/*.bash; do
194-
sudo install -Dm755 $fn \
247+
${CMD} install -Dm755 $fn \
195248
${DESTDIR}/$(basename $fn)
196249
done
197250
echo "Bats Detik v$VERSION installed in $DESTDIR"
251+
echo "detik-installed=true" >> "$GITHUB_OUTPUT"
198252
# Cleanup bats-support if required
199253
[[ "${{ inputs.detik-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0
200-
- name: "Download and install Bats-file"
254+
255+
- name: "Set cache for Bats-file"
256+
uses: actions/cache@v3
201257
if: inputs.file-install == 'true'
258+
id: file-cache
259+
with:
260+
path: ${{ inputs.file-path }}
261+
key: ${{ runner.os }}-bats-file-${{ inputs.file-version }}
262+
263+
- name: "Download and install Bats-file"
264+
if: inputs.file-install == 'true' && steps.file-cache.outputs.cache-hit != 'true'
265+
id: file-install
202266
shell: bash
203267
run: |
204268
VERSION=${{ inputs.file-version }}
@@ -207,14 +271,26 @@ runs:
207271
url="https://github.com/bats-core/bats-file/archive/refs/tags/v${VERSION}.tar.gz"
208272
209273
mkdir -p ${TEMPDIR}
210-
sudo mkdir -p ${DESTDIR}/src/
274+
[[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo"
275+
${CMD} mkdir -p ${DESTDIR}/src/
211276
curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
212-
# Archlinux style, except that we are not in a fakeroot env so we need to use sudo
213-
sudo install -Dm755 load.bash ${DESTDIR}/load.bash
277+
# Archlinux style, except that we are not in a fakeroot env
278+
${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash
214279
for fn in src/*.bash; do
215-
sudo install -Dm755 $fn \
280+
${CMD} install -Dm755 $fn \
216281
${DESTDIR}/src/$(basename $fn)
217282
done
218283
echo "Bats File v$VERSION installed in $DESTDIR"
284+
echo "file-installed=true" >> "$GITHUB_OUTPUT"
219285
# Cleanup bats-support if required
220286
[[ "${{ inputs.file-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0
287+
288+
- name: "Debug print if installed"
289+
if: runner.debug == '1'
290+
shell: bash
291+
run: |
292+
echo "Bats installed: ${{ (steps.bats-install.outputs.bats-installed != '') }}"
293+
echo "Support installed: ${{ (steps.support-install.outputs.support-installed != '') }}"
294+
echo "Assert installed: ${{ (steps.assert-install.outputs.assert-installed != '') }}"
295+
echo "Detik installed: ${{ (steps.detik-install.outputs.detik-installed != '') }}"
296+
echo "File installed: ${{ (steps.file-install.outputs.file-installed != '') }}"

0 commit comments

Comments
 (0)