From 1311c47474c1362890e235158deaf202f36585c4 Mon Sep 17 00:00:00 2001 From: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:56:14 +0200 Subject: [PATCH] JS-232 Introduce the analyzer property sonar.scanner.skipNodeProvisioning (#4820) --- .../javascript/it/plugin/EmbeddedNodeTest.java | 2 +- .../nodejs/NodeCommandBuilderImpl.java | 9 +++++++-- .../javascript/nodejs/NodeCommandTest.java | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/EmbeddedNodeTest.java b/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/EmbeddedNodeTest.java index fd9cac4ebda..6770c512d81 100644 --- a/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/EmbeddedNodeTest.java +++ b/its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/EmbeddedNodeTest.java @@ -65,7 +65,7 @@ void embedded_node() { var buildResult = orchestrator.executeBuild(build); assertThat(buildResult.isSuccess()).isTrue(); - assertThat(buildResult.getLogs()).contains("INFO: Using embedded Node.js runtime"); + assertThat(buildResult.getLogs()).contains("INFO: Using embedded Node.js runtime."); assertThat(buildResult.getLogsLines(l -> l.startsWith("ERROR"))).isEmpty(); orchestrator.stop(); } diff --git a/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/nodejs/NodeCommandBuilderImpl.java b/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/nodejs/NodeCommandBuilderImpl.java index 227b4a5d351..7294cf905e4 100644 --- a/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/nodejs/NodeCommandBuilderImpl.java +++ b/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/nodejs/NodeCommandBuilderImpl.java @@ -50,6 +50,7 @@ public class NodeCommandBuilderImpl implements NodeCommandBuilder { public static final String NODE_EXECUTABLE_PROPERTY = "sonar.nodejs.executable"; private static final String NODE_FORCE_HOST_PROPERTY = "sonar.nodejs.forceHost"; + private static final String SKIP_NODE_PROVISIONING_PROPERTY = "sonar.scanner.skipNodeProvisioning"; private static final Pattern NODEJS_VERSION_PATTERN = Pattern.compile( "v?(\\d+)\\.(\\d+)\\.(\\d+)" @@ -247,7 +248,7 @@ private String retrieveNodeExecutable(Configuration configuration) private String locateNode(boolean isForceHost) throws IOException { var defaultNode = NODE_EXECUTABLE_DEFAULT; if (embeddedNode.isAvailable() && !isForceHost) { - LOG.info("Using embedded Node.js runtime"); + LOG.info("Using embedded Node.js runtime."); defaultNode = embeddedNode.binary().toString(); } else if (processWrapper.isMac()) { defaultNode = locateNodeOnMac(); @@ -255,12 +256,16 @@ private String locateNode(boolean isForceHost) throws IOException { defaultNode = locateNodeOnWindows(); } + if (isForceHost) { + LOG.info("Forcing to use Node.js from the host."); + } + LOG.info("Using Node.js executable: '{}'.", defaultNode); return defaultNode; } private static boolean isForceHost(Configuration configuration) { - return configuration.getBoolean(NODE_FORCE_HOST_PROPERTY).orElse(false); + return configuration.getBoolean(NODE_FORCE_HOST_PROPERTY).orElse(configuration.getBoolean(SKIP_NODE_PROVISIONING_PROPERTY).orElse(false)); } private String locateNodeOnMac() throws IOException { diff --git a/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/nodejs/NodeCommandTest.java b/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/nodejs/NodeCommandTest.java index 635fbf7f9ab..d63e3737fbb 100644 --- a/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/nodejs/NodeCommandTest.java +++ b/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/nodejs/NodeCommandTest.java @@ -436,6 +436,23 @@ void test_embedded_runtime_with_forceHost_for_macos() throws Exception { .endsWith("src/test/resources/package/node_modules/run-node/run-node"); } + @Test + void test_skipping_nodejs_provisioning_property() throws Exception { + var skipNodePropvisioning = "sonar.scanner.skipNodeProvisioning"; + var settings = new MapSettings(); + settings.setProperty(skipNodePropvisioning, true); + + builder() + .configuration(settings.asConfig()) + .script("script.js") + .pathResolver(getPathResolver()) + .build(); + + assertThat(logTester.logs(Level.INFO)) + .doesNotContain("Using embedded Node.js runtime") + .contains("Forcing to use Node.js from the host."); + } + private static String resourceScript(String script) throws URISyntaxException { return new File(NodeCommandTest.class.getResource("/" + script).toURI()).getAbsolutePath(); }