Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ dev.Dockerfile
/bin/
!/bin/run.sh
!/bin/prepare.sh
!/bin/warm-up.sh
!/bin/record-prefetch-list.sh
test/
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,24 @@ ENV YARN_ENABLE_HARDENED_MODE=0
# Let's check to be certain
RUN corepack yarn --version

# Node saves the compiled code of each module it loads in this folder, and
# reuses it in later runs. bin/warm-up.sh runs the test runner once to fill
# it, so each run skips compiling TypeScript, tstyche and Jest.
ENV NODE_COMPILE_CACHE=/opt/test-runner/.node-compile-cache
RUN set -ex; \
bin/warm-up.sh; \
# remove Jest's cache from the warm-up, so runs start with an empty one
rm -rf /tmp/jest_*

ENTRYPOINT [ "/opt/test-runner/bin/run.sh" ]

# Record the files read by a run. A separate stage keeps strace out of the
# final image.
FROM runner AS prefetch-list
RUN set -ex; \
apt-get update; \
apt-get install strace -y; \
bin/record-prefetch-list.sh /tmp/prefetch-list

FROM runner
COPY --from=prefetch-list /tmp/prefetch-list /opt/test-runner/.prefetch-list
35 changes: 35 additions & 0 deletions bin/record-prefetch-list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

# Synopsis:
# Record the files read by one run of the test runner, while building the
# Docker image. run.sh reads these files into the page cache at the start of
# each run (see run.sh).

# Arguments:
# $1: path of the list to write

set -euo pipefail

ROOT="$(realpath "$(dirname "$0")/..")"
TRACE="$(mktemp)"

# Log every file opened by the warm-up run and by every process it starts
strace -f -qq -e trace=openat,execve -e signal=none -o "${TRACE}" "${ROOT}/bin/warm-up.sh"

# Keep the files that opened successfully, in the order they were first
# opened. The warm-up exercise reads packages through its node_modules link,
# so those paths are rewritten to the real folder. Files in the exercise
# itself, and in /tmp, /proc, /dev and /sys, are left out.
grep -v ' = -1 ' "${TRACE}" \
| grep -oE '(openat\(AT_FDCWD, |execve\()"[^"]+"' \
| sed -E 's/^[^"]*"//; s/"$//' \
| sed -E "s#^/tmp/[^/]+/warm-up/node_modules/#${ROOT}/node_modules/#" \
| grep -vE '^/(tmp|proc|dev|sys)/' \
| awk '!seen[$0]++' \
| while read -r file; do
if test -f "${file}"; then echo "${file}"; fi
done > "$1"

rm -f "${TRACE}"

echo "Recorded $(wc -l < "$1") files in $1"
96 changes: 28 additions & 68 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ OUTPUT="${OUTPUT%/}/"
set -euo pipefail

ROOT="$(realpath $(dirname "$0")/..)"

# Read the files a run needs into the page cache, 8 at a time, in the
# background. The Docker image records the list at build time. On a machine
# where the image's files are not in the page cache yet, Node read them one
# at a time as it needed them, which took several seconds per run.
if test -f "${ROOT}/.prefetch-list"; then
( nice -n 19 xargs -P 8 -n 50 -a "${ROOT}/.prefetch-list" cat > /dev/null 2>&1 & )
fi
REPORTER="$ROOT/dist/reporter.js"
SETUP="$ROOT/dist/jest/setup.js"
CONFIG="$ROOT/jest.runner.config.js"
Expand Down Expand Up @@ -466,7 +474,9 @@ if test -d "${OUTPUT}__typetests__/"; then
else
has_type_test=true
echo "✔️ type tests discovered."
cd "${OUTPUT}" && run_tool tstyche --listFiles
# List the files found above. Asking tstyche to list them (--listFiles)
# started it an extra time.
echo "${type_tests}"

echo ""
echo "⚙️ tstyche"
Expand Down Expand Up @@ -542,73 +552,9 @@ echo " ➤ Step 3/3: Execution (tests: does the solution work?) "
echo "╚═════════════════════════════════════════════════════════════╝"
echo ""

jest_tests=$(cd "${OUTPUT}" && run_tool jest --listTests --passWithNoTests) || false

if [ -z "${jest_tests}" ]; then
echo "✔️ no jest tests (*.test.ts) discovered."
# TODO: check the config file to see if jest tests were expected or not
# flag.tests.jest

if [ "$has_type_test" = true ]; then
echo ""
echo "✅ did run type tests, so this is fine."

# TODO: use results from tstyche
runner_result="The type tests ran correctly. We are working on showing the individual tests results but for now, everything is fine!"
echo "{ \"version\": 1, \"status\": \"pass\", \"message\": \"${runner_result}\" }" > $result_file
else
echo "❌ neither type tests, nor execution tests ran"
runner_result="The submitted code was not subjected to any type or execution tests. It did compile correctly, but something is wrong because at least one test was expected."
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"${runner_result}\" }" > $result_file
sed -Ei ':a;N;$!ba;s/\r{0,1}\n/\\n/g' $result_file
fi

echo ""
echo "If the solution previously contained configuration files, "
echo "they were disabled and now need to be restored."
echo ""

# Restore configuration files
if test -f "${OUTPUT}babel.config.js.💥.bak"; then
echo "✔️ restoring babel.config.js in output"
unlink "${OUTPUT}babel.config.js"
mv "${OUTPUT}babel.config.js.💥.bak" "${OUTPUT}babel.config.js" || true
fi;

if test -f "${OUTPUT}package.json.💥.bak"; then
echo "✔️ restoring package.json in output"
if test -f "${OUTPUT}package.json"; then
unlink "${OUTPUT}package.json"
fi
mv "${OUTPUT}package.json.💥.bak" "${OUTPUT}package.json" || true
fi;

if test -f "${OUTPUT}tsconfig.json.💥.bak"; then
echo "✔️ restoring tsconfig.json in output"
mv "${OUTPUT}tsconfig.json.💥.bak" "${OUTPUT}tsconfig.json" || true
fi;

if test -f "${OUTPUT}yarn.lock.💥.bak"; then
echo "✔️ restoring yarn.lock in output"
if test -f "${OUTPUT}yarn.lock"; then
unlink "${OUTPUT}yarn.lock"
fi
mv "${OUTPUT}yarn.lock.💥.bak" "${OUTPUT}yarn.lock" || true
fi;

echo ""
echo "---------------------------------------------------------------"
echo "The results of this run have been written to 'results.json'."
echo "👁️ ${result_file}"

# Test runner didn't fail!
exit 0
fi

echo "✔️ jest tests (*.test.ts) discovered."
echo $jest_tests

# Run tests
# Run tests. If Jest finds no test files, the reporter writes the "no tests"
# error to results.json. Checking for that here would mean starting Jest a
# second time (with --listTests) just to list the test files.
echo ""
echo "⚙️ jest <...>"
echo ""
Expand All @@ -632,6 +578,20 @@ test_exit=$?

if [ $test_exit -eq 1 ]; then
echo "❌ not all tests (*.test.ts) passed."
elif grep -q "not subjected to any type or execution tests" "${result_file}"; then
# Jest found no test files, and the reporter wrote the "no tests" error.
echo "✔️ no jest tests (*.test.ts) discovered."

if [ "$has_type_test" = true ]; then
echo ""
echo "✅ did run type tests, so this is fine."

# TODO: use results from tstyche
runner_result="The type tests ran correctly. We are working on showing the individual tests results but for now, everything is fine!"
echo "{ \"version\": 1, \"status\": \"pass\", \"message\": \"${runner_result}\" }" > $result_file
else
echo "❌ neither type tests, nor execution tests ran"
fi
else
echo "✅ all tests (*.test.ts) passed."
fi;
Expand Down
56 changes: 56 additions & 0 deletions bin/warm-up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash

# Synopsis:
# Run the test runner once on a small exercise while building the Docker
# image, with NODE_COMPILE_CACHE set (see the Dockerfile). Node saves the
# compiled code of every module it loads (TypeScript, tstyche, Jest and its
# dependencies) in that folder. Each run then loads the compiled code instead
# of compiling about 20 MB of JavaScript again.

set -euo pipefail

ROOT="$(realpath "$(dirname "$0")/..")"
WARM_UP_DIR="$(mktemp -d)/warm-up/"

mkdir -p "${WARM_UP_DIR}.meta" "${WARM_UP_DIR}__typetests__"

cat > "${WARM_UP_DIR}.meta/config.json" <<'JSON'
{
"files": {
"solution": ["warm-up.ts"],
"test": ["__typetests__/warm-up.tst.ts", "warm-up.test.ts"]
}
}
JSON

cat > "${WARM_UP_DIR}warm-up.ts" <<'TS'
export function answer(): number {
return 42
}
TS

cat > "${WARM_UP_DIR}warm-up.test.ts" <<'TS'
import { answer } from './warm-up'
describe('answer', () => {
it('is 42', () => {
expect(answer()).toBe(42)
})
})
TS

cat > "${WARM_UP_DIR}__typetests__/warm-up.tst.ts" <<'TS'
import { expect, test } from 'tstyche'
import { answer } from '../warm-up.ts'
test('answer returns a number', () => {
expect(answer()).type.toBe<number>()
})
TS

"${ROOT}/bin/run.sh" warm-up "${WARM_UP_DIR}" "${WARM_UP_DIR}" > /dev/null

# Check the warm-up run passed, so a broken runner fails the image build
grep -q '"status": "pass"' "${WARM_UP_DIR}results.json"

rm -rf "$(dirname "${WARM_UP_DIR}")"
20 changes: 20 additions & 0 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ export class Output {
}

public finish(aggregatedResults: AggregatedResult): void {
// If Jest found no test files, report that no tests ran. Checking here
// saves run.sh from starting Jest a second time to list the test files.
// run.sh replaces this with a pass if the type tests ran.
if (!this.results.status && aggregatedResults.numTotalTestSuites === 0) {
const artifact = JSON.stringify(
{
version: 1,
status: 'error',
message:
'The submitted code was not subjected to any type or execution ' +
'tests. It did compile correctly, but something is wrong because ' +
'at least one test was expected.',
},
undefined,
2
)
fs.writeFileSync(this.outputFile, artifact)
return
}

if (!this.results.status) {
this.results.status =
aggregatedResults.numRuntimeErrorTestSuites === 0 &&
Expand Down
Loading