Skip to content

Commit

Permalink
Rework the test harness build. (#197)
Browse files Browse the repository at this point in the history
This PR reworks how the test harness gets built, to use the same
mechanism as the main build and linter.
  • Loading branch information
danfuzz authored Nov 2, 2023
2 parents 967bc71 + 463e36e commit 9c69061
Show file tree
Hide file tree
Showing 8 changed files with 3,687 additions and 74 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Other notable changes:
* Updated to latest ESLint. Adjusted comments and code accordingly.
* Un-janked how the linter gets built. It's now built using the same mechanism
as the main application build.
* Testing: As with the linter, un-janked how it gets built.

### v0.5.17 -- 2023-10-30

Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/bashy-node/node-project/build-main-module
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function suspect-file-check {

suspectFiles="$(
lib ls-files --output=array --cd="${dir}" \
--exclude='\.(cjs|js|json|html|md|mjs|pyc|ts|yml)$'
--exclude='\.(cjs|js|json|html|md|mjs|pyc|ts|txt|yml)$'
)" \
|| return "$?"

Expand Down
146 changes: 74 additions & 72 deletions scripts/lib/lactoserv/run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,76 @@ process-args "$@" || exit "$?"
# Helper functions
#

# Builds the tool.
function build-tool {
local name="$1"
# Performs a build if necessary / requested.
function build-if-necessary {
local action="$1"

rm -rf node_modules "${name}" *.json
local buildCmd=(lib build --out="${outDir}")
local doBuild=0

jval >package.json \
dependencies:json="${TOOL_DEPENDENCIES}" \
'{ dependencies: $dependencies }'
case "${action}" in
build)
doBuild=1
;;
clean)
buildCmd+=(--clean)
doBuild=1
;;
run)
if [[ ! -x "${outDir}/lactoserv/lib/code/node_modules" ]]; then
doBuild=1
fi
;;
esac

if (( doBuild )); then
progress-msg 'Building...'
"${buildCmd[@]}" || return "$?"
progress-msg
progress-msg 'Running...'
progress-msg
fi

npm install \
}

# Finds a hopefully-unique file at the top level of the project whose name has
# the right prefix. Prints it.
function config-file-path {
local configFile

configFile=($(
lib ls-files --output=lines \
--cd="${srcDir}" --full-paths --depth=1 --include='^jest\.config\.'
)) \
|| return "$?"

{
echo '#!/bin/bash'
echo 'CMD_DIR="$(dirname "$(readlink -f "$0")")"'
echo 'node "${CMD_DIR}/node_modules/.bin/jest" "$@"'
} \
>"${name}"
chmod 755 "${name}"
if (( ${#configFile[@]} != 1 )); then
if (( ${#configFile[@]} == 0 )); then
error-msg 'Did not find a Jest config file in the project!'
else
error-msg 'Did not find a _unique_ Jest config file!'
fi
return 1
fi

echo "${configFile[0]}"
}

# Builds the tester if necessary, and then prints the path to it main binary.
function tester-path {
local toolModule=tester
local toolDir="${outDir}/${toolModule}"
local toolPath="${toolDir}/bin/tester"

# Build the tester, if necessary.
if [[ ! -x ${toolPath} ]]; then
lib 1>&2 node-project build-main-module \
--allow-platform-specific-files \
--out="${outDir}" --modules-dir="${srcDir}" "${toolModule}" \
|| return "$?"
fi

echo "${toolPath}"
}


Expand All @@ -79,49 +129,14 @@ srcDir="$(base-dir)/src"
outDir="$(lib buildy out-dir --out="${outDir}")" \
|| exit "$?"

# Build the system if necessary.

buildCmd=(lib build --out="${outDir}")
doBuild=0

case "${action}" in
build)
doBuild=1
;;
clean)
buildCmd+=(--clean)
doBuild=1
;;
run)
if [[ ! -x "${outDir}/lactoserv/lib/code/node_modules" ]]; then
doBuild=1
fi
;;
esac

if (( doBuild )); then
progress-msg 'Building...'
"${buildCmd[@]}" || exit "$?"
progress-msg
progress-msg 'Running...'
progress-msg
fi

# Build the tool if necessary.
configFile="$(config-file-path)" \
|| exit "$?"

toolDir="${outDir}/tester"
toolBinName='jest'
toolBinary="${toolDir}/${toolBinName}"
toolPath="$(tester-path)" \
|| exit "$?"

if [[ ! -x ${toolBinary} ]]; then
rm -rf "${toolDir}"
mkdir -p "${toolDir}"
(
cd "${toolDir}"
build-tool "${toolBinName}"
) \
|| exit "$?"
fi
build-if-necessary "${action}" \
|| exit "$?"

# Find the tests, and generally set up the testing directory.

Expand All @@ -146,27 +161,14 @@ while IFS='' read -r -d $'\0' dir; do
cp -r "${dir}" "${testsDir}/${moduleName}"
done < <(jget --output=raw0 "${dirsWithTests}" '.[]')

# Find a hopefully-unique file at the top level whose name has the right prefix.
configFile=($(
lib ls-files --output=lines \
--cd="${srcDir}" --full-paths --depth=1 --include='^jest\.config\.'
)) \
|| exit "$?"

if (( ${#configFile[@]} != 1 )); then
if (( ${#configFile[@]} == 0 )); then
error-msg 'No Jest config file!'
else
error-msg 'No unique Jest config file!'
fi
exit 1
fi
# Copy the config file into place, where it will be found when the test harness
# is run.
cp "${configFile}" "${testsDir}"
configFile="${testsDir}/$(basename "${configFile}")"

# Run the tool!

"${toolBinary}" --config="${configFile}" "${args[@]}"
"${toolPath}" --config="${configFile}" "${args[@]}"
status="$?"

info-msg
Expand Down
2 changes: 1 addition & 1 deletion src/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default {

// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: [
"../tester/node_modules/jest-extended/all"
"../tester/lib/node_modules/jest-extended/all"
],

// The number of seconds after which a test is considered as slow and reported as such in the results.
Expand Down
10 changes: 10 additions & 0 deletions src/main-tester/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@this/main-lint
====================

This is the main module for the linter.

- - - - - - - - - -
```
Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
SPDX-License-Identifier: Apache-2.0
```
14 changes: 14 additions & 0 deletions src/main-tester/bin/tester
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
# SPDX-License-Identifier: Apache-2.0

# Figure out the symlink-resolved program name and directory.
cmdName="$(readlink -f "$0")" || exit "$?"
cmdDir="${cmdName%/*}"
cmdName="${cmdName##*/}"
baseDir="${cmdDir%/*}"

# Call through to the `jest` that got built by the main Jest dependency.
exec "${baseDir}/lib/node_modules/.bin/jest" \
"$@"
Loading

0 comments on commit 9c69061

Please sign in to comment.