Skip to content

Commit 1594a6c

Browse files
committed
SLCORE-281 Unable to locate node on Windows if multiple candidates
1 parent 432f344 commit 1594a6c

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

core/src/main/java/org/sonarsource/sonarlint/core/NodeJsHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private String runSimpleCommand(Command command) {
154154
msg.append("\nstderr: ").append(String.join("\n", stdErr));
155155
}
156156
LOG.debug(msg.toString());
157-
if (exitCode != 0 || stdOut.size() != 1) {
157+
if (exitCode != 0 || stdOut.isEmpty()) {
158158
return null;
159159
}
160160
return stdOut.get(0);

core/src/test/java/org/sonarsource/sonarlint/core/NodeJsHelperTests.java

+52-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Objects;
3030
import java.util.function.BiFunction;
3131
import java.util.function.Predicate;
32+
import java.util.stream.Stream;
3233
import javax.annotation.Nullable;
3334
import org.junit.jupiter.api.BeforeEach;
3435
import org.junit.jupiter.api.Test;
@@ -42,7 +43,6 @@
4243
import org.sonar.api.utils.command.StreamConsumer;
4344
import org.sonar.api.utils.log.LogTesterJUnit5;
4445
import org.sonar.api.utils.log.LoggerLevel;
45-
import org.sonarsource.sonarlint.core.NodeJsHelper;
4646
import org.sonarsource.sonarlint.core.client.api.common.Version;
4747

4848
import static org.assertj.core.api.Assertions.assertThat;
@@ -59,11 +59,11 @@ class NodeJsHelperTests {
5959
@RegisterExtension
6060
LogTesterJUnit5 logTester = new LogTesterJUnit5();
6161

62-
private System2 system2 = mock(System2.class);
62+
private final System2 system2 = mock(System2.class);
6363

6464
private CommandExecutor commandExecutor;
6565

66-
private Map<Predicate<Command>, BiFunction<StreamConsumer, StreamConsumer, Integer>> registeredCommandAnswers = new LinkedHashMap<>();
66+
private final Map<Predicate<Command>, BiFunction<StreamConsumer, StreamConsumer, Integer>> registeredCommandAnswers = new LinkedHashMap<>();
6767

6868
@BeforeEach
6969
public void prepare() {
@@ -143,7 +143,6 @@ void ignoreCommandExecutionError() throws IOException {
143143

144144
@Test
145145
void handleErrorDuringVersionCheck() throws IOException {
146-
147146
registerNodeVersionAnswer("wrong_version");
148147

149148
NodeJsHelper underTest = new NodeJsHelper(system2, null, commandExecutor);
@@ -200,6 +199,24 @@ void handleErrorDuringPathCheck() throws IOException {
200199
assertThat(underTest.getNodeJsVersion()).isNull();
201200
}
202201

202+
@Test
203+
void handleEmptyResponseDuringPathCheck() throws IOException {
204+
when(system2.isOsWindows()).thenReturn(true);
205+
206+
registerWhereAnswer();
207+
208+
NodeJsHelper underTest = new NodeJsHelper(system2, null, commandExecutor);
209+
underTest.detect(null);
210+
211+
assertThat(logTester.logs()).containsExactly(
212+
"Looking for node in the PATH",
213+
"Execute command 'where node'...",
214+
"Command 'where node' exited with 0",
215+
"Unable to locate node");
216+
assertThat(underTest.getNodeJsPath()).isNull();
217+
assertThat(underTest.getNodeJsVersion()).isNull();
218+
}
219+
203220
@Test
204221
void useWhereOnWindowsToResolveNodePath() throws IOException {
205222
when(system2.isOsWindows()).thenReturn(true);
@@ -223,6 +240,34 @@ void useWhereOnWindowsToResolveNodePath() throws IOException {
223240
assertThat(underTest.getNodeJsVersion()).isEqualTo(Version.create("10.5.4"));
224241
}
225242

243+
// SLCORE-281
244+
@Test
245+
void whereOnWindowsCanReturnMultipleCandidates() throws IOException {
246+
when(system2.isOsWindows()).thenReturn(true);
247+
248+
Path fake_node_path2 = Paths.get("foo2/node");
249+
250+
registerWhereAnswer(FAKE_NODE_PATH.toString(), fake_node_path2.toString());
251+
registerNodeVersionAnswer("v10.5.4");
252+
253+
NodeJsHelper underTest = new NodeJsHelper(system2, null, commandExecutor);
254+
underTest.detect(null);
255+
256+
assertThat(logTester.logs()).containsExactly(
257+
"Looking for node in the PATH",
258+
"Execute command 'where node'...",
259+
"Command 'where node' exited with 0\nstdout: "
260+
+ FAKE_NODE_PATH.toString() + "\n" + fake_node_path2
261+
.toString(),
262+
"Found node at " + FAKE_NODE_PATH.toString(),
263+
"Checking node version...",
264+
"Execute command '" + FAKE_NODE_PATH.toString() + " -v'...",
265+
"Command '" + FAKE_NODE_PATH.toString() + " -v' exited with 0\nstdout: v10.5.4",
266+
"Detected node version: 10.5.4");
267+
assertThat(underTest.getNodeJsPath()).isEqualTo(FAKE_NODE_PATH);
268+
assertThat(underTest.getNodeJsVersion()).isEqualTo(Version.create("10.5.4"));
269+
}
270+
226271
@Test
227272
void usePathHelperOnMacToResolveNodePath(@TempDir Path tempDir) throws IOException {
228273
when(system2.isOsMac()).thenReturn(true);
@@ -314,7 +359,7 @@ void logWhenUnableToGetNodeVersion() {
314359
}
315360

316361
private void registerNodeVersionAnswer(String version) {
317-
registeredCommandAnswers.put(c -> c.toString().endsWith("node -v"), (stdOut, stdErr) -> {
362+
registeredCommandAnswers.put(c -> c.toString().endsWith(FAKE_NODE_PATH.toString() + " -v"), (stdOut, stdErr) -> {
318363
stdOut.consumeLine(version);
319364
return 0;
320365
});
@@ -334,9 +379,9 @@ private void registerWhichAnswerIfPathIsSet(String whichOutput, @Nullable String
334379
});
335380
}
336381

337-
private void registerWhereAnswer(String whereOutput) {
382+
private void registerWhereAnswer(String... whereOutput) {
338383
registeredCommandAnswers.put(c -> c.toString().endsWith("where node"), (stdOut, stdErr) -> {
339-
stdOut.consumeLine(whereOutput);
384+
Stream.of(whereOutput).forEach(l -> stdOut.consumeLine(l));
340385
return 0;
341386
});
342387
}

0 commit comments

Comments
 (0)