Skip to content

Commit 0fb2ec3

Browse files
author
Blaine Freestone
committed
add automated test for TASTy file determinism
!fixup add automated
1 parent 57af755 commit 0fb2ec3

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

tests/determinism/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_scala_annex//rules:scala.bzl", "scala_library")
2+
3+
# Sample library to test TASTy determinism
4+
scala_library(
5+
name = "tasty_test_lib",
6+
srcs = ["SampleClass.scala"],
7+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package determinism.test
2+
3+
class SampleClass {
4+
def method1(): String = "hello"
5+
6+
def method2(x: Int, y: String): Boolean = {
7+
x > 0 && y.nonEmpty
8+
}
9+
10+
private val field = 42
11+
12+
case class InnerCase(name: String, value: Int)
13+
14+
object InnerObject {
15+
def compute(): Int = field * 2
16+
}
17+
}

tests/determinism/test

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash -e
2+
. "$(dirname "$0")"/../common.sh
3+
4+
# Test that verifies TASTy files can be generated deterministically
5+
echo "Testing TASTy file generation with Scala 3..."
6+
7+
# Build the Scala 3 target, explicitly using Scala 3 toolchain
8+
echo "Building target with Scala 3 toolchain..."
9+
bazel build --@rules_scala_annex//rules/scala:scala-toolchain=test_zinc_3 --keep_going --remote_executor= --remote_cache= --disk_cache= :tasty_test_lib
10+
11+
# Get the generated jar file
12+
bazel_bin=$(bazel info bazel-bin)
13+
jar_file="$bazel_bin/determinism/tasty_test_lib.jar"
14+
15+
echo "Extracting TASTy files from build..."
16+
temp_dir=$(mktemp -d)
17+
18+
cleanup() {
19+
exit_code=$?
20+
for dir in "${temp_dirs[@]}"; do
21+
rm -r "$dir" 2>/dev/null || true
22+
done
23+
finish $exit_code
24+
}
25+
trap cleanup EXIT
26+
27+
# Extract all .tasty files from the jar
28+
unzip -j "$jar_file" "*.tasty" -d "$temp_dir" 2>/dev/null || {
29+
echo "No .tasty files found in jar, checking if they exist at all..."
30+
unzip -l "$jar_file" | grep -i tasty || {
31+
echo "ERROR: No TASTy files found in the generated jar"
32+
echo "Jar contents:"
33+
unzip -l "$jar_file"
34+
exit 1
35+
}
36+
}
37+
38+
# Verify TASTy files were generated
39+
tasty_files=$(find "$temp_dir" -name "*.tasty" | wc -l)
40+
if [ "$tasty_files" -eq 0 ]; then
41+
echo "ERROR: No TASTy files were extracted"
42+
exit 1
43+
fi
44+
45+
echo "SUCCESS: Found $tasty_files TASTy file(s) generated by Scala 3 compiler"
46+
47+
# Show what files were found
48+
echo "Generated TASTy files:"
49+
find "$temp_dir" -name "*.tasty" -exec basename {} \;
50+
51+
# Test determinism by rebuilding multiple times
52+
echo "Testing determinism by rebuilding 5 times..."
53+
54+
# Array to store temp directories for each build
55+
temp_dirs=("$temp_dir")
56+
57+
# Perform 4 additional builds (we already have one)
58+
for i in $(seq 2 5); do
59+
echo "Build $i/5..."
60+
bazel clean
61+
bazel build --@rules_scala_annex//rules/scala:scala-toolchain=test_zinc_3 --remote_executor= --remote_cache= --disk_cache= :tasty_test_lib
62+
63+
# Create temp directory for this build
64+
temp_dir_n=$(mktemp -d)
65+
temp_dirs+=("$temp_dir_n")
66+
67+
# Extract TASTy files from this build
68+
unzip -j "$jar_file" "*.tasty" -d "$temp_dir_n" 2>/dev/null || {
69+
echo "ERROR: Failed to extract TASTy files from build $i"
70+
exit 1
71+
}
72+
done
73+
74+
# Compare all builds against the first one
75+
echo "Comparing TASTy files across all 5 builds for determinism..."
76+
all_identical=true
77+
78+
for i in $(seq 2 5); do
79+
build_num=$((i-1))
80+
echo "Comparing build 1 with build $i..."
81+
diff_output=$(diff -r "${temp_dirs[0]}" "${temp_dirs[$build_num]}" 2>&1)
82+
if [ $? -ne 0 ]; then
83+
echo "ERROR: TASTy files differ between build 1 and build $i - not deterministic"
84+
echo "Differences found:"
85+
echo "$diff_output"
86+
all_identical=false
87+
fi
88+
done
89+
90+
if [ "$all_identical" = true ]; then
91+
echo "SUCCESS: TASTy files are identical across all 5 builds - build is deterministic!"
92+
else
93+
echo "ERROR: TASTy files differ between builds - not deterministic"
94+
exit 1
95+
fi

0 commit comments

Comments
 (0)