29
29
import java .util .Objects ;
30
30
import java .util .function .BiFunction ;
31
31
import java .util .function .Predicate ;
32
+ import java .util .stream .Stream ;
32
33
import javax .annotation .Nullable ;
33
34
import org .junit .jupiter .api .BeforeEach ;
34
35
import org .junit .jupiter .api .Test ;
42
43
import org .sonar .api .utils .command .StreamConsumer ;
43
44
import org .sonar .api .utils .log .LogTesterJUnit5 ;
44
45
import org .sonar .api .utils .log .LoggerLevel ;
45
- import org .sonarsource .sonarlint .core .NodeJsHelper ;
46
46
import org .sonarsource .sonarlint .core .client .api .common .Version ;
47
47
48
48
import static org .assertj .core .api .Assertions .assertThat ;
@@ -59,11 +59,11 @@ class NodeJsHelperTests {
59
59
@ RegisterExtension
60
60
LogTesterJUnit5 logTester = new LogTesterJUnit5 ();
61
61
62
- private System2 system2 = mock (System2 .class );
62
+ private final System2 system2 = mock (System2 .class );
63
63
64
64
private CommandExecutor commandExecutor ;
65
65
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 <>();
67
67
68
68
@ BeforeEach
69
69
public void prepare () {
@@ -143,7 +143,6 @@ void ignoreCommandExecutionError() throws IOException {
143
143
144
144
@ Test
145
145
void handleErrorDuringVersionCheck () throws IOException {
146
-
147
146
registerNodeVersionAnswer ("wrong_version" );
148
147
149
148
NodeJsHelper underTest = new NodeJsHelper (system2 , null , commandExecutor );
@@ -200,6 +199,24 @@ void handleErrorDuringPathCheck() throws IOException {
200
199
assertThat (underTest .getNodeJsVersion ()).isNull ();
201
200
}
202
201
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
+
203
220
@ Test
204
221
void useWhereOnWindowsToResolveNodePath () throws IOException {
205
222
when (system2 .isOsWindows ()).thenReturn (true );
@@ -223,6 +240,34 @@ void useWhereOnWindowsToResolveNodePath() throws IOException {
223
240
assertThat (underTest .getNodeJsVersion ()).isEqualTo (Version .create ("10.5.4" ));
224
241
}
225
242
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\n stdout: "
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\n stdout: 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
+
226
271
@ Test
227
272
void usePathHelperOnMacToResolveNodePath (@ TempDir Path tempDir ) throws IOException {
228
273
when (system2 .isOsMac ()).thenReturn (true );
@@ -314,7 +359,7 @@ void logWhenUnableToGetNodeVersion() {
314
359
}
315
360
316
361
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 ) -> {
318
363
stdOut .consumeLine (version );
319
364
return 0 ;
320
365
});
@@ -334,9 +379,9 @@ private void registerWhichAnswerIfPathIsSet(String whichOutput, @Nullable String
334
379
});
335
380
}
336
381
337
- private void registerWhereAnswer (String whereOutput ) {
382
+ private void registerWhereAnswer (String ... whereOutput ) {
338
383
registeredCommandAnswers .put (c -> c .toString ().endsWith ("where node" ), (stdOut , stdErr ) -> {
339
- stdOut .consumeLine (whereOutput );
384
+ Stream . of ( whereOutput ). forEach ( l -> stdOut .consumeLine (l ) );
340
385
return 0 ;
341
386
});
342
387
}
0 commit comments