|
| 1 | +package com.semmle.js.extractor.test; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import com.semmle.js.extractor.Main; |
| 6 | +import com.semmle.ts.extractor.TypeScriptParser; |
| 7 | +import com.semmle.util.extraction.ExtractorOutputConfig; |
| 8 | +import com.semmle.util.process.Env; |
| 9 | +import com.semmle.util.srcarchive.DummySourceArchive; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.rules.TemporaryFolder; |
| 22 | + |
| 23 | +public class TypeScriptParserTests { |
| 24 | + @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setup() { |
| 28 | + Env.systemEnv() |
| 29 | + .pushEnvironmentContextExcluding( |
| 30 | + List.of( |
| 31 | + TypeScriptParser.TYPESCRIPT_NODE_RUNTIME_EXTRA_ARGS_VAR, |
| 32 | + TypeScriptParser.TYPESCRIPT_NODE_FLAGS, |
| 33 | + "SEMMLE_TYPESCRIPT_RAM", |
| 34 | + "SEMMLE_TYPESCRIPT_RAM_RESERVE", |
| 35 | + "LGTM_TYPESCRIPT_RAM", |
| 36 | + "LGTM_TYPESCRIPT_RAM_RESERVE", |
| 37 | + "ODASA_DB")); |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + public void teardown() { |
| 42 | + Env.systemEnv().popEnvironmentContext(); |
| 43 | + } |
| 44 | + |
| 45 | + private List<String> startParser(Map<String, String> environment) throws Exception { |
| 46 | + Path wrapper = temporaryFolder.newFolder().toPath().resolve("wrapper.cjs"); |
| 47 | + Files.writeString( |
| 48 | + wrapper, |
| 49 | + """ |
| 50 | + const fs = require('fs'); |
| 51 | + const readline = require('readline'); |
| 52 | + fs.writeFileSync(__filename + '.args', JSON.stringify(process.execArgv)); |
| 53 | + readline.createInterface({ input: process.stdin }).on('line', line => { |
| 54 | + const request = JSON.parse(line); |
| 55 | + const response = request.command === 'get-metadata' |
| 56 | + ? { type: 'metadata', nodeFlags: {}, syntaxKinds: {} } |
| 57 | + : { type: 'ok' }; |
| 58 | + console.log(JSON.stringify(response)); |
| 59 | + }); |
| 60 | + """); |
| 61 | + Map<String, String> env = new LinkedHashMap<>(environment); |
| 62 | + env.put(TypeScriptParser.PARSER_WRAPPER_PATH_ENV_VAR, wrapper.toString()); |
| 63 | + Env.systemEnv().pushEnvironmentContext(env); |
| 64 | + TypeScriptParser parser = new TypeScriptParser(); |
| 65 | + try { |
| 66 | + parser.prepareFiles(List.of()); |
| 67 | + List<String> arguments = new ArrayList<>(); |
| 68 | + for (JsonElement argument : |
| 69 | + new JsonParser().parse(Files.readString(Path.of(wrapper + ".args"))).getAsJsonArray()) { |
| 70 | + arguments.add(argument.getAsString()); |
| 71 | + } |
| 72 | + return arguments; |
| 73 | + } finally { |
| 74 | + parser.killProcess(); |
| 75 | + Env.systemEnv().popEnvironmentContext(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test(timeout = 30000) |
| 80 | + public void noAutomaticHeapLimit() throws Exception { |
| 81 | + Assert.assertEquals(List.of(), startParser(Map.of())); |
| 82 | + } |
| 83 | + |
| 84 | + @Test(timeout = 30000) |
| 85 | + public void ignoresLegacyMemoryEnvironment() throws Exception { |
| 86 | + for (String prefix : List.of("SEMMLE_", "LGTM_")) { |
| 87 | + Assert.assertEquals( |
| 88 | + List.of(), |
| 89 | + startParser( |
| 90 | + Map.of(prefix + "TYPESCRIPT_RAM", "128", prefix + "TYPESCRIPT_RAM_RESERVE", "32"))); |
| 91 | + for (String suffix : List.of("TYPESCRIPT_RAM", "TYPESCRIPT_RAM_RESERVE")) { |
| 92 | + Assert.assertEquals(List.of(), startParser(Map.of(prefix + suffix, "not-a-memory-size"))); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test(timeout = 30000) |
| 98 | + public void preservesExplicitNodeArguments() throws Exception { |
| 99 | + Assert.assertEquals( |
| 100 | + List.of("--no-warnings", "--max-old-space-size=128"), |
| 101 | + startParser( |
| 102 | + Map.of( |
| 103 | + TypeScriptParser.TYPESCRIPT_NODE_RUNTIME_EXTRA_ARGS_VAR, |
| 104 | + "--no-warnings --max-old-space-size=128"))); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void toleratesLegacyMemoryOption() throws Exception { |
| 109 | + Path emptyDirectory = temporaryFolder.newFolder().toPath(); |
| 110 | + for (String value : List.of("128", "not-a-memory-size")) { |
| 111 | + new Main(new ExtractorOutputConfig(null, new DummySourceArchive())) |
| 112 | + .run(new String[] {"--quiet", "--typescript-ram", value, emptyDirectory.toString()}); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments