Skip to content

Commit 4b0728b

Browse files
brunoborgesCopilot
andauthored
Extract repeated directory-check assertions into check-dir.sh helper (#1127)
* Extract repeated directory-check assertions into check-dir.sh helper The e2e-cache.yml workflow repeated the same inline shell block many times to assert a cache directory exists (and list it), plus inverse checks that a directory does NOT exist (the gradle2/maven2/sbt2 cache-miss jobs). Add `__tests__/check-dir.sh` (POSIX sh, executable) with a `check-dir.sh <dir> [present|absent]` interface and replace every inline check with a call to it, passing already-expanded $HOME paths to avoid tilde-expansion pitfalls. The sbt jobs override working-directory, so they call the helper via $GITHUB_WORKSPACE. Per-OS Coursier conditionals and all build steps are left unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Address review: argc guard in check-dir.sh, fix sbt ubuntu matrix conditionals - check-dir.sh: with set -u, invoking without a <dir> argument failed with an opaque "parameter not set" error. Add an explicit argc check that prints a usage message and exits 2 (distinct from the assertion failure code 1). - e2e-cache.yml: the sbt-save and sbt-restore jobs run on an ubuntu-22.04 matrix entry, but their coursier-cache steps were guarded by 'if: matrix.os == "ubuntu-latest"', so those checks never executed on Ubuntu. Align the conditionals with the matrix (ubuntu-22.04), matching the newer sbt1/sbt2 jobs. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 7d48588 commit 4b0728b

2 files changed

Lines changed: 66 additions & 133 deletions

File tree

.github/workflows/e2e-cache.yml

Lines changed: 27 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ jobs:
4242
# https://github.com/actions/cache/issues/454#issuecomment-840493935
4343
run: |
4444
gradle downloadDependencies --no-daemon -p __tests__/cache/gradle1
45-
if [ ! -d ~/.gradle/caches ]; then
46-
echo "::error::The ~/.gradle/caches directory does not exist unexpectedly"
47-
exit 1
48-
fi
45+
bash __tests__/check-dir.sh "$HOME/.gradle/caches"
4946
gradle-restore:
5047
runs-on: ${{ matrix.os }}
5148
strategy:
@@ -66,12 +63,7 @@ jobs:
6663
java-version: '11'
6764
cache: gradle
6865
- name: Confirm that ~/.gradle/caches directory has been made
69-
run: |
70-
if [ ! -d ~/.gradle/caches ]; then
71-
echo "::error::The ~/.gradle/caches directory does not exist unexpectedly"
72-
exit 1
73-
fi
74-
ls ~/.gradle/caches/
66+
run: bash __tests__/check-dir.sh "$HOME/.gradle/caches"
7567
maven-save:
7668
runs-on: ${{ matrix.os }}
7769
strategy:
@@ -93,10 +85,7 @@ jobs:
9385
- name: Create files to cache
9486
run: |
9587
mvn verify -f __tests__/cache/maven/pom.xml
96-
if [ ! -d ~/.m2/repository ]; then
97-
echo "::error::The ~/.m2/repository directory does not exist unexpectedly"
98-
exit 1
99-
fi
88+
bash __tests__/check-dir.sh "$HOME/.m2/repository"
10089
maven-restore:
10190
runs-on: ${{ matrix.os }}
10291
strategy:
@@ -117,12 +106,7 @@ jobs:
117106
java-version: '11'
118107
cache: maven
119108
- name: Confirm that ~/.m2/repository directory has been made
120-
run: |
121-
if [ ! -d ~/.m2/repository ]; then
122-
echo "::error::The ~/.m2/repository directory does not exist unexpectedly"
123-
exit 1
124-
fi
125-
ls ~/.m2/repository
109+
run: bash __tests__/check-dir.sh "$HOME/.m2/repository"
126110
sbt-save:
127111
runs-on: ${{ matrix.os }}
128112
defaults:
@@ -155,25 +139,13 @@ jobs:
155139

156140
- name: Check files to cache on macos-latest
157141
if: matrix.os == 'macos-15-intel'
158-
run: |
159-
if [ ! -d ~/Library/Caches/Coursier ]; then
160-
echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
161-
exit 1
162-
fi
142+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
163143
- name: Check files to cache on windows-latest
164144
if: matrix.os == 'windows-latest'
165-
run: |
166-
if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
167-
echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
168-
exit 1
169-
fi
145+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
170146
- name: Check files to cache on ubuntu-latest
171-
if: matrix.os == 'ubuntu-latest'
172-
run: |
173-
if [ ! -d ~/.cache/coursier ]; then
174-
echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
175-
exit 1
176-
fi
147+
if: matrix.os == 'ubuntu-22.04'
148+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
177149
sbt-restore:
178150
runs-on: ${{ matrix.os }}
179151
defaults:
@@ -200,28 +172,13 @@ jobs:
200172

201173
- name: Confirm that ~/Library/Caches/Coursier directory has been made
202174
if: matrix.os == 'macos-15-intel'
203-
run: |
204-
if [ ! -d ~/Library/Caches/Coursier ]; then
205-
echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
206-
exit 1
207-
fi
208-
ls ~/Library/Caches/Coursier
175+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
209176
- name: Confirm that ~/AppData/Local/Coursier/Cache directory has been made
210177
if: matrix.os == 'windows-latest'
211-
run: |
212-
if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
213-
echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
214-
exit 1
215-
fi
216-
ls ~/AppData/Local/Coursier/Cache
178+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
217179
- name: Confirm that ~/.cache/coursier directory has been made
218-
if: matrix.os == 'ubuntu-latest'
219-
run: |
220-
if [ ! -d ~/.cache/coursier ]; then
221-
echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
222-
exit 1
223-
fi
224-
ls ~/.cache/coursier
180+
if: matrix.os == 'ubuntu-22.04'
181+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
225182
gradle1-save:
226183
runs-on: ${{ matrix.os }}
227184
strategy:
@@ -246,10 +203,7 @@ jobs:
246203
# https://github.com/actions/cache/issues/454#issuecomment-840493935
247204
run: |
248205
gradle downloadDependencies --no-daemon -p __tests__/cache/gradle1
249-
if [ ! -d ~/.gradle/caches ]; then
250-
echo "::error::The ~/.gradle/caches directory does not exist unexpectedly"
251-
exit 1
252-
fi
206+
bash __tests__/check-dir.sh "$HOME/.gradle/caches"
253207
gradle1-restore:
254208
runs-on: ${{ matrix.os }}
255209
strategy:
@@ -271,12 +225,7 @@ jobs:
271225
cache: gradle
272226
cache-dependency-path: __tests__/cache/gradle1/*.gradle*
273227
- name: Confirm that ~/.gradle/caches directory has been made
274-
run: |
275-
if [ ! -d ~/.gradle/caches ]; then
276-
echo "::error::The ~/.gradle/caches directory does not exist unexpectedly"
277-
exit 1
278-
fi
279-
ls ~/.gradle/caches/
228+
run: bash __tests__/check-dir.sh "$HOME/.gradle/caches"
280229
gradle2-restore:
281230
runs-on: ${{ matrix.os }}
282231
strategy:
@@ -298,11 +247,7 @@ jobs:
298247
cache: gradle
299248
cache-dependency-path: __tests__/cache/gradle2/*.gradle*
300249
- name: Confirm that ~/.gradle/caches directory has not been made
301-
run: |
302-
if [ -d ~/.gradle/caches ]; then
303-
echo "::error::The ~/.gradle/caches directory exists unexpectedly"
304-
exit 1
305-
fi
250+
run: bash __tests__/check-dir.sh "$HOME/.gradle/caches" absent
306251
maven1-save:
307252
runs-on: ${{ matrix.os }}
308253
strategy:
@@ -325,10 +270,7 @@ jobs:
325270
- name: Create files to cache
326271
run: |
327272
mvn verify -f __tests__/cache/maven/pom.xml
328-
if [ ! -d ~/.m2/repository ]; then
329-
echo "::error::The ~/.m2/repository directory does not exist unexpectedly"
330-
exit 1
331-
fi
273+
bash __tests__/check-dir.sh "$HOME/.m2/repository"
332274
maven1-restore:
333275
runs-on: ${{ matrix.os }}
334276
strategy:
@@ -350,12 +292,7 @@ jobs:
350292
cache: maven
351293
cache-dependency-path: __tests__/cache/maven/pom.xml
352294
- name: Confirm that ~/.m2/repository directory has been made
353-
run: |
354-
if [ ! -d ~/.m2/repository ]; then
355-
echo "::error::The ~/.m2/repository directory does not exist unexpectedly"
356-
exit 1
357-
fi
358-
ls ~/.m2/repository
295+
run: bash __tests__/check-dir.sh "$HOME/.m2/repository"
359296
maven2-restore:
360297
runs-on: ${{ matrix.os }}
361298
strategy:
@@ -377,11 +314,7 @@ jobs:
377314
cache: maven
378315
cache-dependency-path: __tests__/cache/maven2/pom.xml
379316
- name: Confirm that ~/.m2/repository directory has not been made
380-
run: |
381-
if [ -d ~/.m2/repository ]; then
382-
echo "::error::The ~/.m2/repository directory exists unexpectedly"
383-
exit 1
384-
fi
317+
run: bash __tests__/check-dir.sh "$HOME/.m2/repository" absent
385318
sbt1-save:
386319
runs-on: ${{ matrix.os }}
387320
defaults:
@@ -415,25 +348,13 @@ jobs:
415348

416349
- name: Check files to cache on macos-latest
417350
if: matrix.os == 'macos-15-intel'
418-
run: |
419-
if [ ! -d ~/Library/Caches/Coursier ]; then
420-
echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
421-
exit 1
422-
fi
351+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
423352
- name: Check files to cache on windows-latest
424353
if: matrix.os == 'windows-latest'
425-
run: |
426-
if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
427-
echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
428-
exit 1
429-
fi
354+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
430355
- name: Check files to cache on ubuntu-latest
431356
if: matrix.os == 'ubuntu-22.04'
432-
run: |
433-
if [ ! -d ~/.cache/coursier ]; then
434-
echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
435-
exit 1
436-
fi
357+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
437358
sbt1-restore:
438359
runs-on: ${{ matrix.os }}
439360
defaults:
@@ -461,28 +382,13 @@ jobs:
461382

462383
- name: Confirm that ~/Library/Caches/Coursier directory has been made
463384
if: matrix.os == 'macos-15-intel'
464-
run: |
465-
if [ ! -d ~/Library/Caches/Coursier ]; then
466-
echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
467-
exit 1
468-
fi
469-
ls ~/Library/Caches/Coursier
385+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
470386
- name: Confirm that ~/AppData/Local/Coursier/Cache directory has been made
471387
if: matrix.os == 'windows-latest'
472-
run: |
473-
if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
474-
echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
475-
exit 1
476-
fi
477-
ls ~/AppData/Local/Coursier/Cache
388+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
478389
- name: Confirm that ~/.cache/coursier directory has been made
479390
if: matrix.os == 'ubuntu-22.04'
480-
run: |
481-
if [ ! -d ~/.cache/coursier ]; then
482-
echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
483-
exit 1
484-
fi
485-
ls ~/.cache/coursier
391+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
486392
sbt2-restore:
487393
runs-on: ${{ matrix.os }}
488394
defaults:
@@ -510,22 +416,10 @@ jobs:
510416

511417
- name: Confirm that ~/Library/Caches/Coursier directory has not been made
512418
if: matrix.os == 'macos-15-intel'
513-
run: |
514-
if [ -d ~/Library/Caches/Coursier ]; then
515-
echo "::error::The ~/Library/Caches/Coursier directory exists unexpectedly"
516-
exit 1
517-
fi
419+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier" absent
518420
- name: Confirm that ~/AppData/Local/Coursier/Cache directory has not been made
519421
if: matrix.os == 'windows-latest'
520-
run: |
521-
if [ -d ~/AppData/Local/Coursier/Cache ]; then
522-
echo "::error::The ~/AppData/Local/Coursier/Cache directory exists unexpectedly"
523-
exit 1
524-
fi
422+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache" absent
525423
- name: Confirm that ~/.cache/coursier directory has not been made
526424
if: matrix.os == 'ubuntu-22.04'
527-
run: |
528-
if [ -d ~/.cache/coursier ]; then
529-
echo "::error::The ~/.cache/coursier directory exists unexpectedly"
530-
exit 1
531-
fi
425+
run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier" absent

__tests__/check-dir.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
# Assert whether a directory exists, for use in the e2e cache workflows.
3+
#
4+
# Usage: check-dir.sh <dir> [present|absent]
5+
#
6+
# present (default): fail if <dir> does NOT exist, otherwise list its contents.
7+
# absent: fail if <dir> DOES exist.
8+
#
9+
# Call with already-expanded paths (e.g. "$HOME/.gradle/caches") to avoid
10+
# tilde-expansion pitfalls.
11+
set -eu
12+
13+
if [ "$#" -lt 1 ]; then
14+
echo "Usage: check-dir.sh <dir> [present|absent]" >&2
15+
exit 2
16+
fi
17+
18+
dir=$1
19+
mode=${2:-present}
20+
21+
case "$mode" in
22+
present)
23+
if [ ! -d "$dir" ]; then
24+
echo "::error::The $dir directory does not exist unexpectedly"
25+
exit 1
26+
fi
27+
ls "$dir"
28+
;;
29+
absent)
30+
if [ -d "$dir" ]; then
31+
echo "::error::The $dir directory exists unexpectedly"
32+
exit 1
33+
fi
34+
;;
35+
*)
36+
echo "::error::Unknown mode '$mode' (expected 'present' or 'absent')"
37+
exit 1
38+
;;
39+
esac

0 commit comments

Comments
 (0)