-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Analysis should not fail when highlighting has invalid text range (#4604
- Loading branch information
Showing
3 changed files
with
117 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...cript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.sonar.plugins.javascript.bridge; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||
import org.sonar.api.batch.sensor.internal.SensorContextTester; | ||
import org.sonar.api.issue.NoSonarFilter; | ||
import org.sonar.api.measures.FileLinesContext; | ||
import org.sonar.api.measures.FileLinesContextFactory; | ||
import org.sonar.api.testfixtures.log.LogTesterJUnit5; | ||
|
||
class AnalysisProcessorTest { | ||
|
||
@TempDir | ||
Path baseDir; | ||
|
||
@org.junit.jupiter.api.extension.RegisterExtension | ||
LogTesterJUnit5 logTester = new LogTesterJUnit5(); | ||
|
||
@Test | ||
void should_not_fail_when_invalid_range() { | ||
var fileLinesContextFactory = mock(FileLinesContextFactory.class); | ||
when(fileLinesContextFactory.createFor(any())).thenReturn(mock(FileLinesContext.class)); | ||
var processor = new AnalysisProcessor(mock(NoSonarFilter.class), fileLinesContextFactory); | ||
var context = SensorContextTester.create(baseDir); | ||
var file = TestInputFileBuilder | ||
.create("moduleKey", "file.js") | ||
.setContents("var x = 1;") | ||
.build(); | ||
var response = new BridgeServer.AnalysisResponse(); | ||
var highlight = new BridgeServer.Highlight(); | ||
highlight.location = new BridgeServer.Location(1, 2, 1, 1); // invalid range startCol > endCol | ||
response.highlights = new BridgeServer.Highlight[] { highlight }; | ||
processor.processResponse(context, mock(JsTsChecks.class), file, response); | ||
assertThat(logTester.logs()) | ||
.contains("Failed to save highlight in " + file.uri() + " at 1:2-1:1"); | ||
} | ||
|
||
@Test | ||
void should_not_fail_when_invalid_symbol() { | ||
var fileLinesContextFactory = mock(FileLinesContextFactory.class); | ||
when(fileLinesContextFactory.createFor(any())).thenReturn(mock(FileLinesContext.class)); | ||
var processor = new AnalysisProcessor(mock(NoSonarFilter.class), fileLinesContextFactory); | ||
var context = SensorContextTester.create(baseDir); | ||
var file = TestInputFileBuilder | ||
.create("moduleKey", "file.js") | ||
.setContents("var x = 1;") | ||
.build(); | ||
var response = new BridgeServer.AnalysisResponse(); | ||
var symbol = new BridgeServer.HighlightedSymbol(); | ||
symbol.declaration = new BridgeServer.Location(1, 2, 1, 1); // invalid range startCol > endCol | ||
response.highlightedSymbols = new BridgeServer.HighlightedSymbol[] { symbol }; | ||
processor.processResponse(context, mock(JsTsChecks.class), file, response); | ||
assertThat(logTester.logs()) | ||
.contains("Failed to create symbol declaration in " + file.uri() + " at 1:2-1:1"); | ||
|
||
context = SensorContextTester.create(baseDir); | ||
symbol.declaration = new BridgeServer.Location(1, 1, 1, 2); | ||
symbol.references = new BridgeServer.Location[] { new BridgeServer.Location(2, 2, 2, 1) }; | ||
processor.processResponse(context, mock(JsTsChecks.class), file, response); | ||
assertThat(logTester.logs()) | ||
.contains("Failed to create symbol reference in " + file.uri() + " at 2:2-2:1"); | ||
} | ||
} |