Merge pull request #15627 from github/criemen/java-test

Move the JS java tests to be a proper `java_test` target.
This commit is contained in:
Cornelius Riemenschneider
2024-02-16 11:15:18 +01:00
committed by GitHub
3 changed files with 54 additions and 56 deletions

View File

@@ -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 @@ import org.junit.runners.Suite.SuiteClasses;
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<String, String> envUpdate = new HashMap<>();
envUpdate.put("SEMMLE_TYPESCRIPT_NODE_RUNTIME", nodePath);
envUpdate.put("SEMMLE_TYPESCRIPT_PARSER_WRAPPER", tsWrapper.toString());
Env.systemEnv().pushEnvironmentContext(envUpdate);
}
}

View File

@@ -1,33 +1,21 @@
java_test(
name = "test_jar",
name = "test",
srcs = glob(["**/*.java"]),
data = [
"//javascript/extractor/lib/typescript",
"//javascript/extractor/parser-tests",
"//javascript/extractor/tests",
"@nodejs//:node_bin",
],
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",
],
)
# 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)",
],
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"],
env = {
"NODE_BIN": "$(rlocationpath @nodejs//:node_bin)",
"TS_WRAPPER_ZIP": "$(rlocationpath //javascript/extractor/lib/typescript)",
},
)

View File

@@ -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