|
20 | 20 | package org.sonar.plugins.python; |
21 | 21 |
|
22 | 22 | import java.io.File; |
| 23 | +import java.io.FileNotFoundException; |
23 | 24 | import java.io.IOException; |
24 | 25 | import java.nio.charset.StandardCharsets; |
25 | 26 | import java.nio.file.Files; |
|
50 | 51 | import org.sonar.api.batch.sensor.internal.SensorContextTester; |
51 | 52 | import org.sonar.api.batch.sensor.issue.Issue; |
52 | 53 | import org.sonar.api.batch.sensor.issue.IssueLocation; |
| 54 | +import org.sonar.api.config.internal.MapSettings; |
53 | 55 | import org.sonar.api.internal.SonarRuntimeImpl; |
54 | 56 | import org.sonar.api.issue.NoSonarFilter; |
55 | 57 | import org.sonar.api.measures.CoreMetrics; |
|
67 | 69 | import org.sonar.python.checks.CheckList; |
68 | 70 |
|
69 | 71 | import static org.assertj.core.api.Assertions.assertThat; |
| 72 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
70 | 73 | import static org.mockito.Mockito.mock; |
71 | 74 | import static org.mockito.Mockito.spy; |
72 | 75 | import static org.mockito.Mockito.when; |
@@ -344,6 +347,20 @@ public void test_exception_does_not_fail_analysis() throws IOException { |
344 | 347 | assertThat(context.allIssues()).hasSize(2); |
345 | 348 | } |
346 | 349 |
|
| 350 | + @Test |
| 351 | + public void test_exception_should_fail_analysis_if_configured_so() throws IOException { |
| 352 | + DefaultInputFile inputFile = spy(createInputFile(FILE_1)); |
| 353 | + when(inputFile.contents()).thenThrow(FileNotFoundException.class); |
| 354 | + context.fileSystem().add(inputFile); |
| 355 | + |
| 356 | + activeRules = new ActiveRulesBuilder().build(); |
| 357 | + context.setSettings(new MapSettings().setProperty("sonar.internal.analysis.failFast", "true")); |
| 358 | + |
| 359 | + assertThatThrownBy(() -> sensor().execute(context)) |
| 360 | + .isInstanceOf(IllegalStateException.class) |
| 361 | + .hasCauseInstanceOf(FileNotFoundException.class); |
| 362 | + } |
| 363 | + |
347 | 364 | @Test |
348 | 365 | public void parse_error() { |
349 | 366 | inputFile("parse_error.py"); |
@@ -392,17 +409,21 @@ private PythonSensor sensor(@Nullable PythonCustomRuleRepository[] customRuleRep |
392 | 409 | } |
393 | 410 |
|
394 | 411 | private InputFile inputFile(String name) { |
395 | | - DefaultInputFile inputFile = TestInputFileBuilder.create("moduleKey", name) |
396 | | - .setModuleBaseDir(baseDir.toPath()) |
397 | | - .setCharset(StandardCharsets.UTF_8) |
398 | | - .setType(Type.MAIN) |
399 | | - .setLanguage(Python.KEY) |
400 | | - .initMetadata(TestUtils.fileContent(new File(baseDir, name), StandardCharsets.UTF_8)) |
401 | | - .build(); |
| 412 | + DefaultInputFile inputFile = createInputFile(name); |
402 | 413 | context.fileSystem().add(inputFile); |
403 | 414 | return inputFile; |
404 | 415 | } |
405 | 416 |
|
| 417 | + private DefaultInputFile createInputFile(String name) { |
| 418 | + return TestInputFileBuilder.create("moduleKey", name) |
| 419 | + .setModuleBaseDir(baseDir.toPath()) |
| 420 | + .setCharset(StandardCharsets.UTF_8) |
| 421 | + .setType(Type.MAIN) |
| 422 | + .setLanguage(Python.KEY) |
| 423 | + .initMetadata(TestUtils.fileContent(new File(baseDir, name), StandardCharsets.UTF_8)) |
| 424 | + .build(); |
| 425 | + } |
| 426 | + |
406 | 427 | private void verifyUsages(String componentKey, int line, int offset, TextRange... trs) { |
407 | 428 | Collection<TextRange> textRanges = context.referencesForSymbolAt(componentKey, line, offset); |
408 | 429 | assertThat(textRanges).containsExactly(trs); |
|
0 commit comments