Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from SonarSource:master #216

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,31 +234,6 @@ public String getFixRecommendations() {
@JsonNotification("sonarlint/showIssueOrHotspot")
void showIssueOrHotspot(ShowAllLocationsCommand.Param params);

@JsonRequest("sonarlint/isIgnoredByScm")
CompletableFuture<Boolean> isIgnoredByScm(String fileUri);

class ShouldAnalyseFileCheckResult {
boolean shouldBeAnalysed;
String reason;

public ShouldAnalyseFileCheckResult(boolean shouldBeAnalysed, @Nullable String reason) {
this.shouldBeAnalysed = shouldBeAnalysed;
this.reason = reason;
}

public boolean isShouldBeAnalysed() {
return shouldBeAnalysed;
}

@CheckForNull
public String getReason() {
return reason;
}
}

@JsonRequest("sonarlint/shouldAnalyseFile")
CompletableFuture<ShouldAnalyseFileCheckResult> shouldAnalyseFile(SonarLintExtendedLanguageServer.UriParams fileUri);

class FileUrisParams {
Collection<String> fileUris;

Expand Down Expand Up @@ -796,6 +771,9 @@ public String getReason() {
@JsonNotification("sonarlint/readyForTests")
void readyForTests();

@JsonRequest("sonarlint/isOpenInEditor")
CompletableFuture<Boolean> isOpenInEditor(String fileUri);

class SslCertificateConfirmationParams {

@Expose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,28 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
@Override
public void didOpen(DidOpenTextDocumentParams params) {
var uri = create(params.getTextDocument().getUri());
if (openNotebooksCache.isNotebook(uri)) {
lsLogOutput.debug(String.format("Skipping text document analysis of notebook \"%s\"", uri));
return;
}
var file = openFilesCache.didOpen(uri, params.getTextDocument().getLanguageId(), params.getTextDocument().getText(), params.getTextDocument().getVersion());
CompletableFutures.computeAsync(cancelChecker -> {
String configScopeId;
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file);
var maybeWorkspaceFolder = workspaceFoldersManager.findFolderForFile(uri);
if (maybeWorkspaceFolder.isPresent()) {
configScopeId = maybeWorkspaceFolder.get().getUri().toString();
client.isOpenInEditor(uri.toString()).thenAccept(isOpen -> {
if (Boolean.TRUE.equals(isOpen)) {
if (openNotebooksCache.isNotebook(uri)) {
lsLogOutput.debug(String.format("Skipping text document analysis of notebook \"%s\"", uri));
return;
}
var file = openFilesCache.didOpen(uri, params.getTextDocument().getLanguageId(), params.getTextDocument().getText(), params.getTextDocument().getVersion());
CompletableFutures.computeAsync(cancelChecker -> {
String configScopeId;
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file);
var maybeWorkspaceFolder = workspaceFoldersManager.findFolderForFile(uri);
if (maybeWorkspaceFolder.isPresent()) {
configScopeId = maybeWorkspaceFolder.get().getUri().toString();
} else {
configScopeId = ROOT_CONFIGURATION_SCOPE;
}
backendServiceFacade.getBackendService().didOpenFile(configScopeId, uri);
return null;
});
} else {
configScopeId = ROOT_CONFIGURATION_SCOPE;
lsLogOutput.debug(String.format("Skipping analysis of file not open in the editor: \"%s\"", uri));
}
backendServiceFacade.getBackendService().didOpenFile(configScopeId, uri);
return null;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ private static RuleKey safeParseRuleKey(String key) {
}
}

@CheckForNull
private static String safeParseLevel(Map<String, Object> config) {
Object levelValue = config.get("level");
return levelValue instanceof String actualValue ? actualValue : null;
return levelValue instanceof String actualValue ? actualValue : "on";
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ protected static class FakeLanguageClient implements SonarLintExtendedLanguageCl
ShowRuleDescriptionParams ruleDesc;
boolean isIgnoredByScm = false;
boolean shouldAnalyseFile = true;
boolean isOpenInEditor = true;
final AtomicInteger needCompilationDatabaseCalls = new AtomicInteger();
final Set<String> openedLinks = new HashSet<>();
final Set<MessageParams> shownMessages = new HashSet<>();
Expand Down Expand Up @@ -382,6 +383,7 @@ void clear() {
shouldAnalyseFile = true;
scopeReadyForAnalysis.clear();
suggestedBindings = null;
isOpenInEditor = true;
}

@Override
Expand Down Expand Up @@ -486,6 +488,11 @@ public void readyForTests() {
readyForTestsLatch.countDown();
}

@Override
public CompletableFuture<Boolean> isOpenInEditor(String fileUri) {
return CompletableFuture.completedFuture(isOpenInEditor);
}

@Override
public CompletableFuture<Boolean> askSslCertificateConfirmation(SslCertificateConfirmationParams params) {
return null;
Expand Down Expand Up @@ -558,17 +565,6 @@ public void showIssue(ShowAllLocationsCommand.Param issue) {
public void showIssueOrHotspot(ShowAllLocationsCommand.Param params) {
}

@Override
public CompletableFuture<Boolean> isIgnoredByScm(String fileUri) {
return CompletableFutures.computeAsync(cancelToken -> isIgnoredByScm);
}

@Override
public CompletableFuture<ShouldAnalyseFileCheckResult> shouldAnalyseFile(SonarLintExtendedLanguageServer.UriParams fileUri) {
return CompletableFutures.computeAsync(cancelToken -> new ShouldAnalyseFileCheckResult(shouldAnalyseFile, "reason"));

}

@Override
public CompletableFuture<FileUrisResult> filterOutExcludedFiles(FileUrisParams params) {
return CompletableFutures.computeAsync(cancelToken -> new FileUrisResult(params.getFileUris()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,6 @@ void analyzeSimplePythonFileOnOpen() throws Exception {
tuple(1, 2, 1, 7, "python:PrintStatementUsage", "sonarqube", "Replace print statement by built-in function.", DiagnosticSeverity.Warning)));
}

@Test
@Disabled("We lost the ability to exclude preview files")
void doNotAnalyzePythonFileOnPreview() throws Exception {
setShowVerboseLogs(client.globalSettings, true);
notifyConfigurationChangeOnClient();

var uri = getUri("analyzeSimplePythonFileOnOpen.py", analysisDir);

client.shouldAnalyseFile = false;
didOpen(uri, "python", "def foo():\n print 'toto'\n");

awaitUntilAsserted(() -> assertThat(client.logs)
.extracting(withoutTimestamp())
.contains("[Info] reason \"" + uri + "\""));
assertThat(client.getDiagnostics(uri)).isEmpty();
}

@Test
void analyzeSimplePythonFileWithCustomRuleConfig() throws Exception {
var uri = getUri("analyzeSimplePyFileWithCustomRuleConfig.py", analysisDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ void analysisShouldUseFolderSettings() throws Exception {
assertThat(client.getDiagnostics(uriOutsideFolder)).isEmpty();
}

@Test
void doNotAnalyzePythonFileOnPreview() {
setShowVerboseLogs(client.globalSettings, true);
notifyConfigurationChangeOnClient();
client.isOpenInEditor = false;

var uri = folder1BaseDir.resolve("doNotAnalyzePythonFileOnPreview.py").toUri().toString();

didOpen(uri, "python", "def foo():\n print 'toto'\n");

awaitUntilAsserted(() -> assertThat(client.logs)
.extracting(withoutTimestamp())
.contains(String.format("[Debug] Skipping analysis of file not open in the editor: \"%s\"", uri)));
assertThat(client.getDiagnostics(uri)).isEmpty();
}

@Test
void shouldBatchAnalysisFromTheSameFolder() {
var file1InFolder = folder1BaseDir.resolve("file1.py").toUri().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,57 @@ class RulesConfigurationTests {

@Test
void configuredRuleShouldHaveEqualsAndHashCode() {
var rule1 = parseRule("{\n" +
" \"xoo:rule1\": {\n" +
" \"level\": \"on\",\n" +
" \"parameters\": {\n" +
" \"param1\": \"value1\"\n" +
" }\n" +
" }\n" +
"}");
var sameAsRule1 = parseRule("{\n" +
" \"xoo:rule1\": {\n" +
" \"level\": \"on\",\n" +
" \"parameters\": {\n" +
" \"param1\": \"value1\"\n" +
" }\n" +
" }\n" +
"}");
var keyDiffers = parseRule("{\n" +
" \"xoo:rule2\": {\n" +
" \"level\": \"on\",\n" +
" \"parameters\": {\n" +
" \"param1\": \"value1\"\n" +
" }\n" +
" }\n" +
"}");
var levelDiffers = parseRule("{\n" +
" \"xoo:rule1\": {\n" +
" \"level\": \"off\",\n" +
" \"parameters\": {\n" +
" \"param1\": \"value1\"\n" +
" }\n" +
" }\n" +
"}");
var noParams = parseRule("{\n" +
" \"xoo:rule1\": {\n" +
" \"level\": \"on\"\n" +
" }\n" +
"}");
var paramsDiffer = parseRule("{\n" +
" \"xoo:rule1\": {\n" +
" \"level\": \"on\",\n" +
" \"parameters\": {\n" +
" \"param1\": \"value2\"\n" +
" }\n" +
" }\n" +
"}");
var rule1 = parseRule("""
{
"xoo:rule1": {
"level": "on",
"parameters": {
"param1": "value1"
}
}
}""");
var sameAsRule1 = parseRule("""
{
"xoo:rule1": {
"level": "on",
"parameters": {
"param1": "value1"
}
}
}""");
var keyDiffers = parseRule("""
{
"xoo:rule2": {
"level": "on",
"parameters": {
"param1": "value1"
}
}
}""");
var levelDiffers = parseRule("""
{
"xoo:rule1": {
"level": "off",
"parameters": {
"param1": "value1"
}
}
}""");
var noParams = parseRule("""
{
"xoo:rule1": {
"level": "on"
}
}""");
var paramsDiffer = parseRule("""
{
"xoo:rule1": {
"level": "on",
"parameters": {
"param1": "value2"
}
}
}""");

assertThat(rule1)
.hasSameHashCodeAs(sameAsRule1)
Expand All @@ -86,6 +92,50 @@ void configuredRuleShouldHaveEqualsAndHashCode() {
.isNotEqualTo(paramsDiffer);
}

@Test
void configuredRule_should_be_on_when_on() {
var rule1 = parseRule("""
{
"xoo:rule1": {
"level": "on",
"parameters": {
"param1": "value1"
}
}
}""");

assertThat(rule1.level).isEqualTo("on");
}

@Test
void configuredRule_should_be_off_when_off() {
var rule1 = parseRule("""
{
"xoo:rule1": {
"level": "off",
"parameters": {
"param1": "value1"
}
}
}""");

assertThat(rule1.level).isEqualTo("off");
}

@Test
void configuredRule_should_be_on_when_not_specified() {
var rule1 = parseRule("""
{
"xoo:rule1": {
"parameters": {
"param1": "value1"
}
}
}""");

assertThat(rule1.level).isEqualTo("on");
}

private static RulesConfiguration.ConfiguredRule parseRule(String ruleJson) {
return new RulesConfiguration.ConfiguredRule(new Gson().<Map<String, Object>>fromJson(ruleJson, Map.class).entrySet().iterator().next());
}
Expand Down