diff --git a/javascript/extractor/test/com/semmle/js/extractor/test/AllTests.java b/javascript/extractor/test/com/semmle/js/extractor/test/AllTests.java index 7abf94bb57c9..7487e68efc01 100644 --- a/javascript/extractor/test/com/semmle/js/extractor/test/AllTests.java +++ b/javascript/extractor/test/com/semmle/js/extractor/test/AllTests.java @@ -1,5 +1,15 @@ package com.semmle.js.extractor.test; +import com.google.devtools.build.runfiles.Runfiles; +import com.semmle.util.process.Env; +import java.io.FileInputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @@ -18,4 +28,35 @@ RobustnessTests.class, NumericSeparatorTests.class }) -public class AllTests {} +public class AllTests { + + @BeforeClass + public static void setUp() throws Exception { + Runfiles.Preloaded runfiles = Runfiles.preload(); + String nodePath = runfiles.unmapped().rlocation(System.getenv("NODE_BIN")); + String tsWrapperZip = runfiles.unmapped().rlocation(System.getenv("TS_WRAPPER_ZIP")); + Path tempDir = Files.createTempDirectory("ts-wrapper"); + // extract the ts-wrapper.zip to tempDir: + try (ZipInputStream zis = new ZipInputStream(new FileInputStream(tsWrapperZip))) { + ZipEntry entry = zis.getNextEntry(); + while (entry != null) { + Path entryPath = tempDir.resolve(entry.getName()); + if (entry.isDirectory()) { + Files.createDirectories(entryPath); + } else { + Files.copy(zis, entryPath); + } + entry = zis.getNextEntry(); + } + } + Path tsWrapper = tempDir.resolve("javascript/tools/typescript-parser-wrapper/main.js"); + if (!Files.exists(tsWrapper)) { + throw new RuntimeException("Could not find ts-wrapper at " + tsWrapper); + } + Map envUpdate = new HashMap<>(); + envUpdate.put("SEMMLE_TYPESCRIPT_NODE_RUNTIME", nodePath); + envUpdate.put("SEMMLE_TYPESCRIPT_PARSER_WRAPPER", tsWrapper.toString()); + + Env.systemEnv().pushEnvironmentContext(envUpdate); + } +} diff --git a/javascript/extractor/test/com/semmle/js/extractor/test/BUILD.bazel b/javascript/extractor/test/com/semmle/js/extractor/test/BUILD.bazel index 57305838039b..4a7a7d8e08c1 100644 --- a/javascript/extractor/test/com/semmle/js/extractor/test/BUILD.bazel +++ b/javascript/extractor/test/com/semmle/js/extractor/test/BUILD.bazel @@ -1,33 +1,21 @@ java_test( - name = "test_jar", - srcs = glob(["**/*.java"]), - test_class = "com.semmle.js.extractor.test.AllTests", - deps = [ - "//javascript/extractor", - "//javascript/extractor:deps", - "@//resources/lib/java/DO_NOT_DISTRIBUTE:junit", - ], -) - -# We need to unzip the typescript wrapper, and provide node on the path. -# Therefore, we're wrapping the java_test inside a sh_test. -sh_test( name = "test", - size = "small", - srcs = ["run_tests.sh"], - args = [ - "$(execpath @nodejs//:node_bin)", - "$(JAVABASE)/bin/java", - "$(rootpath //javascript/extractor/lib/typescript)", - "$(rootpath test_jar_deploy.jar)", - ], + srcs = glob(["**/*.java"]), data = [ - ":test_jar_deploy.jar", "//javascript/extractor/lib/typescript", "//javascript/extractor/parser-tests", "//javascript/extractor/tests", - "@bazel_tools//tools/jdk:current_java_runtime", "@nodejs//:node_bin", ], - toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"], + test_class = "com.semmle.js.extractor.test.AllTests", + deps = [ + "//javascript/extractor", + "//javascript/extractor:deps", + "@//resources/lib/java/DO_NOT_DISTRIBUTE:junit", + "@bazel_tools//tools/java/runfiles", + ], + env = { + "NODE_BIN": "$(rlocationpath @nodejs//:node_bin)", + "TS_WRAPPER_ZIP": "$(rlocationpath //javascript/extractor/lib/typescript)", + }, ) diff --git a/javascript/extractor/test/com/semmle/js/extractor/test/run_tests.sh b/javascript/extractor/test/com/semmle/js/extractor/test/run_tests.sh deleted file mode 100755 index bf2087392227..000000000000 --- a/javascript/extractor/test/com/semmle/js/extractor/test/run_tests.sh +++ /dev/null @@ -1,31 +0,0 @@ -NODE=$1 -JAVA=$2 -PARSER_WRAPPER=$3 -TEST_JAR=$4 - -TEMP=$(mktemp -d) - -UNAME=$(uname -s) -echo $UNAME -# On Windows, the symlink set up by bazel that points at the test jar is a msys2/linux-style path -# The JVM can't resolve that, therefore copy the jar to the temp directory, and then set the -# windows path to it -if [[ "$UNAME" =~ _NT ]]; then - cp $TEST_JAR $TEMP/test.jar - TEST_JAR=$(cygpath -w $TEMP/test.jar) - echo "On Windows, new test jar: $TEST_JAR" -fi - -# unpack parser wrapper -unzip -q $PARSER_WRAPPER -d $TEMP/parser_wrapper -export SEMMLE_TYPESCRIPT_PARSER_WRAPPER=$TEMP/parser_wrapper/javascript/tools/typescript-parser-wrapper/main.js - -# setup node on path -NODE=$(realpath $NODE) -export PATH="$PATH:$(dirname $NODE)" - -$JAVA -Dbazel.test_suite=com.semmle.js.extractor.test.AllTests -jar $TEST_JAR -EXIT_CODE=$? - -rm -rf $TEMP -exit $EXIT_CODE