Skip to content

Commit 0708ad1

Browse files
[STYLE] apply changes found in review (- WIP pr #82 -)
1 parent c24e2ed commit 0708ad1

File tree

3 files changed

+87
-56
lines changed

3 files changed

+87
-56
lines changed

.github/workflows/Tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ jobs:
199199
- name: Install code-climate tools for ${{ matrix.python-version }}
200200
if: ${{ !cancelled() && runner.os != 'Windows' }}
201201
shell: bash
202-
run: ./tests/fetch_cc-test-reporter || true ;
202+
run: ./tests/fetch-test-reporter || true ;
203203
- name: Generate Coverage for py3.9 on ${{ matrix.os }}
204204
if: ${{ runner.python-version == '3.9' }}
205205
run: make -f Makefile test ;
@@ -304,7 +304,7 @@ jobs:
304304
pip install -r ./test-requirements.txt || true ;
305305
- name: Install code-climate tools for ${{ matrix.python-version }} on ${{ matrix.os }}
306306
if: ${{ !cancelled() && runner.os != 'Windows' }}
307-
run: if [ $OS == macos-latest ] ; then ./tests/fetch_cc-test-reporter || true ; fi ;
307+
run: if [ $OS == macos-latest ] ; then ./tests/fetch-test-reporter || true ; fi ;
308308
shell: bash
309309
- name: Pre-Clean
310310
id: clean-prep

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ else
9292
endif
9393

9494
ifndef CC_TOOL
95-
FETCH_CC_TOOL := tests/fetch_cc-test-reporter
95+
FETCH_CC_TOOL := tests/fetch-test-reporter
9696
CC_TOOL := ./cc-test-reporter
9797
CC_TOOL_ARGS := after-build --exit-code 0 -t coverage.py
9898
DS_TOOL := ./bin/deepsource
@@ -260,7 +260,7 @@ test-style: cleanup
260260
$(QUIET)tests/check_spelling 2>/dev/null || true
261261
$(QUIET)$(ECHO) "$@: Done."
262262

263-
cc-test-reporter: tests/fetch_cc-test-reporter
263+
cc-test-reporter: tests/fetch-test-reporter
264264
$(QUIET)$(FETCH_CC_TOOL) || DO_FAIL="exit 2" ;
265265
$(QUIET)$(WAIT) ;
266266
$(QUIET)$(DO_FAIL) ;

tests/fetch_cc-test-reporter renamed to tests/fetch-test-reporter

Lines changed: 83 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666

6767
# to check though:
68-
diff -q <(tail -n 269 "$0" | head -n 267 | shasum -a 384 -t -) <(tail -n 1 "$0") || exit 70 ;
68+
diff -q <(tail -n 300 "$0" | head -n 298 | shasum -a 384 -t -) <(tail -n 1 "$0") || exit 70 ;
6969

7070
ulimit -t 90
7171
PATH="/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:${PATH}"
@@ -76,17 +76,42 @@ DS_TAG="0.8.6" # circa 2025
7676
umask 127
7777

7878
LOCK_FILE="${TMPDIR:-/tmp}/org.pak.tests.scripts.code-climate.lock"
79-
EXIT_CODE=0
80-
81-
test -x "$(command -v grep)" || exit 126 ;
82-
test -x "$(command -v sed)" || exit 126 ;
83-
test -x "$(command -v curl)" || exit 126 ;
84-
test -x "$(command -v mkdir)" || exit 126 ;
85-
hash -p ./.github/tool_shlock_helper.sh shlock || exit 255 ;
86-
test -x "$(command -v shlock)" || exit 126 ;
87-
test -x "$(command -v gpgv)" || exit 126 ;
88-
test -x "$(command -v shasum)" || exit 126 ;
89-
test -x "$(command -v tar)" || exit 126 ;
79+
EXIT_CODE=1
80+
# Function to check if a command exists.
81+
82+
# USAGE:
83+
# ~$ check_command CMD
84+
# Arguments:
85+
# CMD (Required) -- Name of the command to check
86+
# Results:
87+
# exits 64 -- missing required argument
88+
# exits 126 -- check complete and has failed, can not find given command.
89+
# returns successful -- check complete and command found to be executable.
90+
function check_command() {
91+
test -z "$1" && { printf "%s\n" "Error: command name is required to check for existence." >&2 ; exit 64 ; } ;
92+
local cmd="$1" ;
93+
# shellcheck disable=SC2086
94+
test -x "$(command -v ${cmd})" || { printf "%s\n" "Error: Required command '$cmd' is not found." >&2 ; exit 126 ; } ;
95+
} # end check_command()
96+
# propagate/export function to sub-shells
97+
export -f check_command
98+
99+
# Set up CEP-5 shlock helper
100+
hash -p ./.github/tool_shlock_helper.sh shlock || { printf "%s\n" "Error: Failed to register shlock helper. CEP-5 locking will not work." >&2 ; exit 78 ; } ;
101+
check_command cat ;
102+
check_command chmod ;
103+
check_command curl ;
104+
check_command git ;
105+
check_command grep ;
106+
check_command gpg ;
107+
check_command mkdir ;
108+
check_command sed ; # but will try jq first
109+
check_command shasum ;
110+
check_command shlock ;
111+
check_command tar ;
112+
check_command uname ;
113+
114+
SCRIPT_FILE="tests/fetch-test-reporter"
90115

91116
# Detect the operating system
92117
case "$( command uname -s )" in
@@ -140,24 +165,25 @@ function cleanup() {
140165
unset CI_ENV_PREFIX 2>/dev/null || :
141166
unset CI_OS 2>/dev/null || :
142167
unset ARCH 2>/dev/null || :
168+
unset DS_TAG 2>/dev/null || :
143169
hash -d shlock 2>/dev/null > /dev/null || : ;
144170
}
145171

146172
if [[ ( $(shlock -f "${LOCK_FILE}" -p $$ ) -eq 0 ) ]] ; then
147-
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 129 ;' SIGHUP || EXIT_CODE=129
148-
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 143 ;' SIGTERM || EXIT_CODE=143
149-
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 131 ;' SIGQUIT || EXIT_CODE=131
150-
# SC2173 - https://github.com/koalaman/shellcheck/wiki/SC2173
151-
#trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 1 ;' SIGSTOP || EXIT_CODE=7
152-
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 130 ;' SIGINT || EXIT_CODE=130
153-
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || true || : ; wait ; exit 137 ;' SIGABRT || EXIT_CODE=137
154-
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit ${EXIT_CODE} ;' EXIT || EXIT_CODE=1
155-
trap 'cleanup ; wait ; exit ${EXIT_CODE} ;' EXIT || EXIT_CODE=1
173+
EXIT_CODE=0
174+
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 129 ;' SIGHUP || EXIT_CODE=129
175+
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 143 ;' SIGTERM || EXIT_CODE=143
176+
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 131 ;' SIGQUIT || EXIT_CODE=131
177+
# SC2173 - https://github.com/koalaman/shellcheck/wiki/SC2173
178+
#trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 1 ;' SIGSTOP || EXIT_CODE=7
179+
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 130 ;' SIGINT || EXIT_CODE=130
180+
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit 137 ;' SIGABRT || EXIT_CODE=137
181+
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ; exit ${EXIT_CODE} ;' EXIT || EXIT_CODE=1
182+
trap 'cleanup ; wait ; exit ${EXIT_CODE} ;' EXIT || EXIT_CODE=1
156183
else
157-
# shellcheck disable=SC2046
158-
printf "\t%s\n" "CodeClimate already in progress by "$(head "${LOCK_FILE}") >&2 ;
159-
false ;
160-
exit 126 ;
184+
# shellcheck disable=SC2046
185+
printf "\t%s\n" "Fetch Tool already in progress by "$(head "${LOCK_FILE}") >&2 ;
186+
exit 126 ;
161187
fi
162188

163189
# this is how test files are found:
@@ -187,7 +213,7 @@ fi
187213
#########################
188214
# actual Work starts here
189215
#########################
190-
curl -fLso ./test-reporter-latest-${CI_ENV_PREFIX} https://codeclimate.com/downloads/test-reporter/test-reporter-latest-${CI_ENV_PREFIX} ;
216+
curl -fLso ./test-reporter-latest-${CI_ENV_PREFIX} "https://codeclimate.com/downloads/test-reporter/test-reporter-latest-${CI_ENV_PREFIX}" ;
191217
for i in 1 256 512 ; do
192218
curl -fLso test-reporter-latest-${CI_ENV_PREFIX}.sha${i} "https://codeclimate.com/downloads/test-reporter/test-reporter-latest-${CI_ENV_PREFIX}.sha${i}" ; wait ;
193219
curl -fLso test-reporter-latest-${CI_ENV_PREFIX}.sha${i}.sig "https://codeclimate.com/downloads/test-reporter/test-reporter-latest-${CI_ENV_PREFIX}.sha${i}.sig" ; wait ;
@@ -196,15 +222,15 @@ for i in 1 256 512 ; do
196222
if [[ ${i} -eq 1 ]]; then
197223
printf "%s\n" "WARNING: SHA-1 is deprecated and should be avoided when possible. Consider using SHA-256 or SHA-512 for stronger integrity checks. (CWE-327: Use of a Broken or Risky Cryptographic Algorithm)"
198224
fi
199-
if [[ ( -r test-reporter-latest-${CI_OS:-linux}-${ARCH:-amd64}.sha${i}.sig ) ]] ; then
225+
if [[ ( -r test-reporter-latest-${CI_ENV_PREFIX}.sha${i}.sig ) ]] ; then
200226
# configure your CI evironment to trust the key at gpg --keyserver keys.openpgp.org --recv-keys 9BD9E2DD46DA965A537E5B0A5CBF320243B6FD85
201227
# FP: KEY FP 9BD9 E2DD 46DA 965A 537E 5B0A 5CBF 3202 43B6 FD85
202228
# OR...
203229
# Set CI=true to continue on missing keys
204230
gpg --verify test-reporter-latest-${CI_ENV_PREFIX}.sha${i}.sig test-reporter-latest-${CI_ENV_PREFIX}.sha${i} || ${CI} 2>/dev/null || ${CIRCLECI} 2>/dev/null || EXIT_CODE=126
205231
rm -vf test-reporter-latest-${CI_ENV_PREFIX}.sha${i}.sig 2>/dev/null ;
206232
fi
207-
shasum -a $i -c --ignore-missing test-reporter-latest-${CI_ENV_PREFIX}.sha${i} || EXIT_CODE=126
233+
shasum -a $i -c --ignore-missing test-reporter-latest-${CI_ENV_PREFIX}.sha${i} || EXIT_CODE=89
208234
rm -vf test-reporter-latest-${CI_ENV_PREFIX}.sha${i} 2>/dev/null ;
209235
fi
210236
done
@@ -215,7 +241,8 @@ if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
215241
fi
216242

217243
if [[ -z ${DEEPSOURCE_DSN} ]] && [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
218-
printf "%s\n" "No need for Unconfigured DeepSource." ;
244+
printf "::notice %s::%s\n" "file=${SCRIPT_FILE},line=${BASH_LINENO:-0},title=SKIP" \
245+
"No need for Unconfigured DeepSource." ;
219246
elif [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
220247
printf "%s\n" "Detected Config for DeepSource" ;
221248

@@ -224,8 +251,9 @@ elif [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
224251

225252
# Check if the response is valid
226253
if [[ "$response" == *"Not Found"* ]]; then
227-
printf "::debug::%s\n" "Repository not found or no releases available."
228-
EXIT_CODE=125
254+
printf "::warning %s::%s\n" "file=${SCRIPT_FILE},line=${BASH_LINENO:-0},title=${FUNCNAME:-$0}" \
255+
"Repository not found or no releases available." >&2 ;
256+
EXIT_CODE=69
229257
fi ;
230258

231259
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
@@ -241,16 +269,17 @@ elif [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
241269

242270
# Check if tag_name is empty
243271
if [[ ( -z "$tag_name" ) ]]; then
244-
printf "::debug::%s\n" "No tag name found in the response."
245-
EXIT_CODE=125
272+
printf "::warning %s::%s\n" "file=${SCRIPT_FILE},line=${BASH_LINENO:-0},title=${FUNCNAME:-$0}" \
273+
"No tag name found in the response." >&2 ;
274+
EXIT_CODE=65
246275
else
247276
printf "::debug::%s\n" "Found version ${tag_name} for DeepSource" ;
248277
DS_TAG="${tag_name#v}"
249278
fi ;
250279

251280
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
252281
printf "::debug::%s\n" "Creating ${BINDIR} directory for DeepSource" ;
253-
mkdir -m 755 "${BINDIR}" || EXIT_CODE=66 ;
282+
mkdir -v -m 755 "${BINDIR}" || EXIT_CODE=66 ;
254283
fi ;
255284

256285
curl -fLso "deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" "https://github.com/DeepSourceCorp/cli/releases/download/v${DS_TAG}/deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" || EXIT_CODE=125 ;
@@ -263,25 +292,29 @@ elif [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
263292

264293
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
265294
printf "::debug::%s\n" "Downloaded checksums.txt for DeepSource" ;
295+
if [[ ( -r checksums.txt ) ]] ; then
296+
shasum -a 256 -c --ignore-missing checksums.txt || EXIT_CODE=89
297+
rm -vf checksums.txt 2>/dev/null || : ;
298+
fi ;
299+
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
300+
printf "%s\n" "Validated deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz for DeepSource" ;
301+
fi ;
266302
fi ;
267-
268-
if [[ ( -r checksums.txt ) ]] ; then
269-
shasum -a 256 -c --ignore-missing checksums.txt || EXIT_CODE=126
270-
rm -f checksums.txt 2>/dev/null ;
271-
fi ;
303+
304+
printf "::group::%s\n" "Extracting deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" ;
272305

273306
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
274-
printf "::debug::%s\n" "Validated deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz for DeepSource" ;
275-
tar --no-same-owner -xzf "deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" -C "${BINDIR}" 2>/dev/null || : ;
276-
rm -f "deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" 2>/dev/null || EXIT_CODE=77 ;
277-
rm -fRd "${BINDIR}"/completions/ 2>/dev/null || : ;
307+
tar --no-same-owner -xzf "deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" -C "${BINDIR}" || : ;
308+
rm -vf "deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" 2>/dev/null || : ;
309+
rm -vfRd "${BINDIR}"/completions/ 2>/dev/null || : ;
310+
printf "::debug::%s\n" "Extracted deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" ;
278311
fi ;
279312

313+
printf "::endgroup::\n" ;
314+
280315
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
281316
# ref: https://docs.deepsource.com/docs/analyzers-test-coverage#setup-test-coverage
282317
printf "::debug::%s\n" "Extracted deepsource_${DS_TAG}_${CI_OS}_${ARCH}.tar.gz" ;
283-
#{ cat ./ds-cli.sh | sh - | grep -o "(:?version).{1}\s[0-9].[0-9].[0-9]" ;} ; wait ;
284-
#printf "::endgroup::\n" ;
285318
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
286319
printf "::debug::%s\n" "Checking for DeepSource CLI" ;
287320
if [[ ( -d "${BINDIR}" ) ]] ; then
@@ -301,21 +334,19 @@ elif [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
301334
else
302335
printf "::debug::..=> %s\n" "Missing ${BINDIR}/deepsource CLI tool" ;
303336
EXIT_CODE=66 ;
304-
printf "::debug::..=> %s\n" "Checking if ${BINDIR} is empty:" ;
305-
readonly BIN_FILES=$( ls -1 "${BINDIR}" 2>/dev/null ;)
306-
printf "::debug::..=> %s\n" "${BIN_FILES}" ;
307-
unset $BIN_FILES 2>/dev/null || : ;
308337
fi ;
309338
else
310339
printf "::debug::=> %s\n" "Missing ${BINDIR} directory." ;
311-
EXIT_CODE=125 ;
340+
EXIT_CODE=66 ;
312341
fi ;
313342
fi ;
314343
fi ;
315344

316345
if [[ ( ${EXIT_CODE} -eq 0 ) ]] ; then
346+
printf "::group::%s\n" "Checking DeepSource Config" ;
317347
# ref: https://docs.deepsource.com/docs/cli#commands
318-
./bin/deepsource config validate || EXIT_CODE=$? ;
348+
./bin/deepsource config validate || ${CI} 2>/dev/null || ${CIRCLECI} 2>/dev/null || EXIT_CODE=125 ;
349+
printf "::endgroup::\n" ;
319350
fi ;
320351
wait ;
321352
fi ;
@@ -333,4 +364,4 @@ cleanup 2>/dev/null || rm -f "${LOCK_FILE}" 2>/dev/null > /dev/null || : ; wait
333364
exit ${EXIT_CODE:-255} ;
334365

335366
# This file's code hash:
336-
f606b901a37e190d4d0a3e3f6945dda08598de00f182e212a3805c05e6110d5cde47fa492b549add4446124c14ed45f4 -
367+
64775bab23e56bbd695b87b099c23978d789791c79e94ae7c5cd126a99ca7fde949260d9ad26297027db34af2022f436 -

0 commit comments

Comments
 (0)