Skip to content

Commit be02512

Browse files
committed
Add a build system for the junit tests.
This is a bit more complicated than our usual setup, as we both need to unzip the typescript parser wrapper, and make node accessible on the path.
1 parent 52fcc5f commit be02512

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.semmle.js.extractor.FileExtractor.FileType;
3131
import com.semmle.js.extractor.VirtualSourceRoot;
3232
import com.semmle.util.data.StringUtil;
33+
import com.semmle.util.exception.Exceptions;
3334
import com.semmle.util.exception.UserError;
3435
import com.semmle.util.files.FileUtil;
3536
import com.semmle.util.files.FileUtil8;
@@ -443,8 +444,12 @@ public void excludeByClassificationBadPath() throws IOException {
443444

444445
/** Hide {@code p} on using {@link DosFileAttributeView} if available; otherwise do nothing. */
445446
private void hide(Path p) throws IOException {
447+
try {
446448
DosFileAttributeView attrs = Files.getFileAttributeView(p, DosFileAttributeView.class);
447449
if (attrs != null) attrs.setHidden(true);
450+
} catch (IOException e) {
451+
Exceptions.ignore(e, "On Linux within the bazel sandbox, we get a DosFileAttributeView that then throws an exception upon use");
452+
}
448453
}
449454

450455
@Test
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
java_test(
2+
name = "test_jar",
3+
srcs = glob(["**/*.java"]),
4+
test_class = "com.semmle.js.extractor.test.AllTests",
5+
deps = [
6+
"//javascript/extractor",
7+
"//javascript/extractor:deps",
8+
"@//resources/lib/java/DO_NOT_DISTRIBUTE:junit",
9+
],
10+
)
11+
12+
# We need to unzip the typescript wrapper, and provide node on the path.
13+
# Therefore, we're wrapping the java_test inside a sh_test.
14+
sh_test(
15+
name = "test",
16+
size = "small",
17+
srcs = ["run_tests.sh"],
18+
args = [
19+
"$(execpath @nodejs//:node_bin)",
20+
"$(JAVABASE)/bin/java",
21+
"$(rootpath //javascript/extractor/lib/typescript)",
22+
"$(rootpath test_jar_deploy.jar)",
23+
],
24+
data = [
25+
":test_jar_deploy.jar",
26+
"//javascript/extractor/lib/typescript",
27+
"//javascript/extractor/parser-tests",
28+
"//javascript/extractor/tests",
29+
"@bazel_tools//tools/jdk:current_java_runtime",
30+
"@nodejs//:node_bin",
31+
],
32+
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
33+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
NODE=$1
2+
JAVA=$2
3+
PARSER_WRAPPER=$3
4+
TEST_JAR=$4
5+
6+
TEMP=$(mktemp -d)
7+
8+
UNAME=$(uname -s)
9+
echo $UNAME
10+
# On Windows, the symlink set up by bazel that points at the test jar is a msys2/linux-style path
11+
# The JVM can't resolve that, therefore copy the jar to the temp directory, and then set the
12+
# windows path to it
13+
if [[ "$UNAME" =~ _NT ]]; then
14+
cp $TEST_JAR $TEMP/test.jar
15+
TEST_JAR=$(cygpath -w $TEMP/test.jar)
16+
echo "On Windows, new test jar: $TEST_JAR"
17+
fi
18+
19+
# unpack parser wrapper
20+
unzip -q $PARSER_WRAPPER -d $TEMP/parser_wrapper
21+
export SEMMLE_TYPESCRIPT_PARSER_WRAPPER=$TEMP/parser_wrapper/javascript/tools/typescript-parser-wrapper/main.js
22+
23+
# setup node on path
24+
NODE=$(realpath $NODE)
25+
export PATH="$PATH:$(dirname $NODE)"
26+
27+
$JAVA -Dbazel.test_suite=com.semmle.js.extractor.test.AllTests -jar $TEST_JAR
28+
EXIT_CODE=$?
29+
30+
rm -rf $TEMP
31+
exit $EXIT_CODE

0 commit comments

Comments
 (0)