Files
JUnit_Tutorials/repeated/scripts/run-all.sh
T

122 lines
4.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Regenerates every file in docs/output/.
#
# docs/output/00, 01, 02, 04, 05 come straight from `mvn test` -- the four committed test
# classes write them via Capture.write() as a side effect of their own assertions.
#
# docs/output/03, 06, 07 come from three scratch classes that are deliberately NOT part of the
# committed suite (a bogus display-name placeholder, and two deterministic "flaky test" demos).
# This script recreates them in a throwaway directory, compiles and runs them against the
# JUnit Platform Console Standalone launcher, captures the real output, and cleans up -- so the
# committed suite stays green at 14/14 while every number in docs/output/ still traces back to a
# real run.
#
# Requires: JDK 17+ on PATH (JUnit 6 requires Java 17; this module was built on JDK 25), network
# access to download the console-standalone jar on first run.
set -euo pipefail
cd "$(dirname "$0")/.."
JUNIT_VERSION="6.1.3"
JAR_DIR="$(mktemp -d)"
STANDALONE_JAR="${JAR_DIR}/junit-platform-console-standalone-${JUNIT_VERSION}.jar"
trap 'rm -rf "${JAR_DIR}"' EXIT
echo "== 1/3: committed suite (mvn test) =="
mvn -q test
mvn test > "${JAR_DIR}/full-run.txt" 2>&1 || true
grep -v '^Picked up JAVA_TOOL_OPTIONS' "${JAR_DIR}/full-run.txt" | tail -20 > docs/output/00-full-test-run.txt
sed -i '1i# mvn test -- full committed suite (ReliabilityTest, CustomNameTest, LifecycleTest, MetadataTest)\n' docs/output/00-full-test-run.txt
echo "== 2/3: downloading junit-platform-console-standalone ${JUNIT_VERSION} =="
curl -fsSL -o "${STANDALONE_JAR}" \
"https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/${JUNIT_VERSION}/junit-platform-console-standalone-${JUNIT_VERSION}.jar"
echo "== 3/3: scratch classes (written, run, captured, discarded) =="
SCRATCH_SRC="${JAR_DIR}/scratch-src"
SCRATCH_CLASSES="${JAR_DIR}/scratch-classes"
PKG_DIR="${SCRATCH_SRC}/com/ankurm/tutorials/junit/repeated"
mkdir -p "${PKG_DIR}" "${SCRATCH_CLASSES}"
cat > "${PKG_DIR}/ScratchPlaceholderTest.java" <<'EOF'
package com.ankurm.tutorials.junit.repeated;
import org.junit.jupiter.api.RepeatedTest;
class ScratchPlaceholderTest {
@RepeatedTest(value = 2, name = "{shortDisplayname} test")
void bogusPlaceholderTest() {
}
}
EOF
cat > "${PKG_DIR}/FlakyDetectionDemoTest.java" <<'EOF'
package com.ankurm.tutorials.junit.repeated;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import static org.junit.jupiter.api.Assertions.fail;
class FlakyDetectionDemoTest {
@RepeatedTest(10)
void intermittentRaceCondition(RepetitionInfo info) {
int current = info.getCurrentRepetition();
if (current == 4 || current == 8) {
fail("simulated race condition reproduced on repetition " + current);
}
}
}
EOF
cat > "${PKG_DIR}/FailureThresholdStopsRemainingDemoTest.java" <<'EOF'
package com.ankurm.tutorials.junit.repeated;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import static org.junit.jupiter.api.Assertions.fail;
class FailureThresholdStopsRemainingDemoTest {
@RepeatedTest(value = 10, failureThreshold = 2)
void intermittentRaceCondition(RepetitionInfo info) {
int current = info.getCurrentRepetition();
if (current == 4 || current == 8) {
fail("simulated race condition reproduced on repetition " + current);
}
}
}
EOF
javac -cp "${STANDALONE_JAR}" -d "${SCRATCH_CLASSES}" "${PKG_DIR}"/*.java
run_scratch() {
# These scratch runs are EXPECTED to report test failures (that's the point of 06 and 07),
# so capture output via command substitution with `|| true` rather than a raw pipe -- under
# `set -o pipefail` a piped non-zero exit from `java` would otherwise abort this script.
local class_name="$1" out_file="$2" header="$3" raw_out
raw_out="$(java -jar "${STANDALONE_JAR}" execute \
-cp "${SCRATCH_CLASSES}" \
--select-class="com.ankurm.tutorials.junit.repeated.${class_name}" \
--details=tree --disable-ansi-colors 2>&1 || true)"
{
echo "${header}"
echo
printf '%s\n' "${raw_out}" | grep -v '^Picked up JAVA_TOOL_OPTIONS'
} > "docs/output/${out_file}"
}
run_scratch "ScratchPlaceholderTest" "03-bogus-placeholder-scratch.txt" \
"# Scratch run: {shortDisplayname} is not a real placeholder (ScratchPlaceholderTest, removed before commit)"
run_scratch "FlakyDetectionDemoTest" "06-flaky-detected.txt" \
"# Scratch run: @RepeatedTest with no failureThreshold -- all 10 repetitions still run even though 2 fail (FlakyDetectionDemoTest, removed before commit)"
run_scratch "FailureThresholdStopsRemainingDemoTest" "07-failure-threshold-skips-remaining.txt" \
"# Scratch run: @RepeatedTest(failureThreshold = 2) -- after the 2nd failure (repetition 8), repetitions 9 and 10 are SKIPPED, not invoked (FailureThresholdStopsRemainingDemoTest, removed before commit)"
echo "Done -- docs/output/ regenerated."