Skip to content

Commit

Permalink
Optimize test and add Remote test with sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
philippefichet committed Dec 21, 2023
1 parent 952bfa2 commit 106bb3a
Show file tree
Hide file tree
Showing 22 changed files with 411 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
java-version: 11
distribution: temurin
- name: Build with Maven
run: mvn clean package source:jar javadoc:jar verify spotless:check
env:
SONARLINT4NETBEANS_TEST_REMOTE_PROJECT_MAVEN1_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -D clean package source:jar javadoc:jar verify spotless:check
timeout-minutes: 10
- name: Test Report
uses: dorny/test-reporter@v1
Expand Down
13 changes: 8 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
<name>sonarlint4netbeans.test.skip.javascript</name>
<value>${sonarlint4netbeans.test.skip.javascript}</value>
</property>
<!-- For test with real sonarcloud calls -->
<property>
<name>sonarlint4netbeans.test.remote.project-maven1.token</name>
<value>${sonarlint4netbeans.test.remote.project-maven1.token}</value>
</property>
</systemProperties>
<!-- https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#surefire-extensions-and-reports-configuration-for-displayname -->
<!-- Surefire Extensions and Reports Configuration for @DisplayName -->
Expand Down Expand Up @@ -135,11 +140,6 @@
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down Expand Up @@ -516,5 +516,8 @@
<surefire.reportFormat>plain</surefire.reportFormat>
<!-- to use friends org.netbeans.modules.git -->
<maven.nbm.verify>warn</maven.nbm.verify>
<sonarlint4netbeans.test.remote.project-maven1.token>
${env.SONARLINT4NETBEANS_TEST_REMOTE_PROJECT_MAVEN1_TOKEN}
</sonarlint4netbeans.test.remote.project-maven1.token>
</properties>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@
* @author FICHET Philippe &lt;philippe.fichet@laposte.net&gt;
*/
public interface SonarLintDataManager {
/**
* Retrieve project preferences data for remote configuration (sonarcloud/sonarqube)
* @param project project to retrieve preference
* @return project preferences data for remote configuration (sonarcloud/sonarqube) or global settings preference if project is null
*/
public Preferences getRemoteConfigurationPreferences(Project project);

/**
* Retrieve project preferences data
* @param project project to retrieve preference
* @return project preferences data or global settings preference if project if null
* @return project preferences data or global settings preference if project is null
*/
public Preferences getPreferences(Project project);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.github.philippefichet.sonarlint4netbeans;

import com.github.philippefichet.sonarlint4netbeans.project.SonarLintProjectPreferenceScope;
import com.github.philippefichet.sonarlint4netbeans.remote.configuration.SonarLintRemoteProjectConfiguration;
import java.awt.Image;
import java.io.File;
import java.nio.charset.Charset;
Expand All @@ -43,6 +44,14 @@
*/
public class SonarLintDataManagerImpl implements SonarLintDataManager {
private static final String PREFERENCE_SCOPE_KEY = "sonarlint-preference-scope";
@Override
public Preferences getRemoteConfigurationPreferences(Project project) {
if (project == null) {
return getGlobalSettingsPreferences();
}
return ProjectUtils.getPreferences(project, SonarLintRemoteProjectConfiguration.class, true);
}

@Override
public Preferences getPreferences(Project project) {
if (project == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ public final class SonarLintEngineImpl implements SonarLintEngine {
private final List<Consumer<SonarLintEngine>> configurationChanged = Collections.synchronizedList(new ArrayList<>());
private final Map<String, Path> pluginPaths = new HashMap<>();
private final Lookup lookup = Lookup.getDefault();
private final Language[] languages;

/**
* Default constructor for lookup
*/
public SonarLintEngineImpl() {
this(Language.values());
}

public SonarLintEngineImpl(Language[] languages) {
this.languages = languages;
SonarLintDataManager sonarLintDataManager = getSonarLintDataManager();
pluginPaths.put("java", sonarLintDataManager.getInstalledFile("sonar/plugins/sonar-java-plugin-" + SONAR_JAVA_PLUGIN_VERSION + ".jar").toPath());
pluginPaths.put("javascript", sonarLintDataManager.getInstalledFile("sonar/plugins/sonar-javascript-plugin-" + SONAR_JAVASCRIPT_PLUGIN_VERSION + ".jar").toPath());
Expand Down Expand Up @@ -160,7 +169,7 @@ private void createInternalEngine(StandaloneSonarLintEngineImpl oldStandaloneSon

List<Path> allPluginPaths = new ArrayList<>(allPlugins.values());
StandaloneGlobalConfiguration.Builder configBuilder = StandaloneGlobalConfiguration.builder()
.addEnabledLanguages(Language.values())
.addEnabledLanguages(languages)
.addPlugins(allPluginPaths.toArray(new Path[allPluginPaths.size()]));
Optional<String> nodeJSPathOptional = getNodeJSPath();
Optional<Version> nodeJSVersionOptional = getNodeJSVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ public final class SonarLintRemoteEngine {
private static final String RUNTIME_NODE_JS_VERSION_PREFERENCE = "nodejs.version";
private final Clock clock = Clock.systemUTC();
private final Lookup lookup = Lookup.getDefault();
private final Language[] languages;

private final Map<String, ConnectedSonarLintEngineImpl> connectedSonarLintEngineImpls = Collections.synchronizedMap(new HashMap<>());

/**
* Default constructor for Lookup
*/
public SonarLintRemoteEngine() {
this.languages = Language.values();
}


/**
* Constructor testing, reduce downloads (plugin, rules, ...)
*/
public SonarLintRemoteEngine(Language[] languages) {
this.languages = languages;
}

private String toKey(SonarLintRemoteProjectConfiguration sonarLintRemoteProjectConfiguration) {
return sonarLintRemoteProjectConfiguration.getProjectKey() + "-" +
sonarLintRemoteProjectConfiguration.getOrganization() + "-" +
Expand Down Expand Up @@ -139,13 +148,13 @@ private ConnectedSonarLintEngineImpl getConnectedSonarLintEngineImpl(SonarLintRe
String sonarLintHome = System.getProperty("user.home") + File.separator + ".sonarlint4netbeans";
return connectedSonarLintEngineImpls.computeIfAbsent(
sonarLintRemoteProjectConfiguration.getConnectionId(),
c ->
(String connectionId) ->
new ConnectedSonarLintEngineImpl(
createBuilder()
.setConnectionId(c)
.setConnectionId(connectionId)
.setStorageRoot(Paths.get(sonarLintHome, "storage"))
.setWorkDir(Paths.get(sonarLintHome, "work"))
.addEnabledLanguages(Language.values())
.addEnabledLanguages(languages)
.build()
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
*/
package com.github.philippefichet.sonarlint4netbeans.remote.configuration;

import com.github.philippefichet.sonarlint4netbeans.SonarLintDataManager;
import java.io.File;
import java.util.Map;
import java.util.Optional;
import java.util.prefs.Preferences;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.libs.git.GitBranch;
import org.netbeans.libs.git.GitException;
import org.netbeans.libs.git.GitRepository;
import org.netbeans.libs.git.progress.ProgressMonitor;
import org.openide.filesystems.FileUtil;
import org.openide.util.Lookup;

/**
*
Expand All @@ -54,7 +55,8 @@ public SonarLintRemoteProjectConfiguration(Project project, String connectionId,
}

public static SonarLintRemoteProjectConfiguration fromProject(Project project) {
Preferences preferences = ProjectUtils.getPreferences(project, SonarLintRemoteProjectConfiguration.class, true);
SonarLintDataManager sonarLintDataManager = Lookup.getDefault().lookup(SonarLintDataManager.class);
Preferences preferences = sonarLintDataManager.getRemoteConfigurationPreferences(project);
return new SonarLintRemoteProjectConfiguration(
project,
preferences.get(PROP_CONNECTION_ID, null),
Expand All @@ -66,6 +68,7 @@ public static SonarLintRemoteProjectConfiguration fromProject(Project project) {

public static SonarLintRemoteProjectConfiguration fromProject(Project project, String connectionId, String projectKey, String organization) {
File projectDir = FileUtil.toFile(project.getProjectDirectory());
// TODO
GitRepository instance = GitRepository.getInstance(projectDir);
Map<String, GitBranch> branches = null;
try {
Expand All @@ -82,7 +85,8 @@ public static SonarLintRemoteProjectConfiguration fromProject(Project project, S
}

public static void save(Project project, String sonarLintRemoteConnectionId, String projectKey, String organization) {
Preferences preferences = ProjectUtils.getPreferences(project, SonarLintRemoteProjectConfiguration.class, true);
SonarLintDataManager sonarLintDataManager = Lookup.getDefault().lookup(SonarLintDataManager.class);
Preferences preferences = sonarLintDataManager.getRemoteConfigurationPreferences(project);
preferences.put(PROP_CONNECTION_ID, sonarLintRemoteConnectionId);
preferences.put(PROP_PROJECT_KEY, projectKey);
preferences.put(PROP_ORGANIZATION, organization);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileUtil;

/**
*
Expand All @@ -53,6 +54,24 @@ public SonarLintDataManagerMockedBuilder createPreferences(Project project)
{
Mockito.when(sonarLintDataManagerMocked.getPreferences(project))
.thenReturn(new SonarLintPreferencesForTesting());
Mockito.when(sonarLintDataManagerMocked.getRemoteConfigurationPreferences(project))
.thenReturn(new SonarLintPreferencesForTesting());
return this;
}

public SonarLintDataManagerMockedBuilder createPreferences(Project project, SonarLintProjectPreferenceScope scope)
{
createPreferences(project);
preferencesScope(project, scope);
return this;
}

public SonarLintDataManagerMockedBuilder addFileToProject(Project project, File file)
{
Mockito.when(sonarLintDataManagerMocked.getProject(file))
.thenReturn(Optional.of(project));
Mockito.when(sonarLintDataManagerMocked.getProject(FileUtil.toFileObject(file)))
.thenReturn(Optional.of(project));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue;
import org.sonarsource.sonarlint.core.commons.IssueSeverity;
import org.sonarsource.sonarlint.core.commons.Language;
import org.sonarsource.sonarlint.core.commons.RuleType;

/**
Expand All @@ -54,6 +55,7 @@ static Arguments[] parametersForAnalyze() throws IOException
Arguments.of(
SonarLintEngineTestConfiguration.builder()
.requirePlugin("java")
.enabledLanguages(Language.JAVA)
.description("SonarLintFileDemo.java with rule java:S115 but without java:S122 and java:S1118 to check CODE_SMELL CRITICAL")
.includeRules("java:S115")
.excludeRules("java:S1220", "java:S1118")
Expand All @@ -75,6 +77,7 @@ static Arguments[] parametersForAnalyze() throws IOException
Arguments.of(
SonarLintEngineTestConfiguration.builder()
.requirePlugin("java")
.enabledLanguages(Language.JAVA)
.description("NewClass.java with rule java:S1133 but without java:S1186, java:S1598, java:S100, java:S1134, java:S2168 and java:S115 to check CODE_SMELL INFO")
.includeRules("java:S1133")
.excludeRules("java:S1186", "java:S1598", "java:S100", "java:S1134", "java:S2168", "java:S115")
Expand All @@ -96,6 +99,7 @@ static Arguments[] parametersForAnalyze() throws IOException
Arguments.of(
SonarLintEngineTestConfiguration.builder()
.requirePlugin("java")
.enabledLanguages(Language.JAVA)
.description("SonarLintFileDemo.java with rule java:S115 but without java:S1220 and S1118 rule with default parameters")
.includeRules("java:S115")
.excludeRules("java:S1220", "java:S1118")
Expand All @@ -117,6 +121,7 @@ static Arguments[] parametersForAnalyze() throws IOException
Arguments.of(
SonarLintEngineTestConfiguration.builder()
.requirePlugin("java")
.enabledLanguages(Language.JAVA)
.description("SonarLintFileDemo.java with rule java:S115 but without java:S1220 and S1118 rule with custom parameters")
.includeRules("java:S115")
.excludeRules("java:S1220", "java:S1118")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue;
import org.sonarsource.sonarlint.core.commons.IssueSeverity;
import org.sonarsource.sonarlint.core.commons.Language;
import org.sonarsource.sonarlint.core.commons.RuleType;

/**
Expand Down Expand Up @@ -63,6 +64,7 @@ public static Arguments[] parametersForAnalyze() throws IOException
SonarLintEngineTestConfiguration.builder()
.description("sonarlint-example.js with rule javascript:S108 to check javascript plugin that require nodejs")
.requirePlugin("javascript")
.enabledLanguages(Language.JS)
.requireNodeJS()
.excludeRules("javascript:S3504", "javascript:S3827", "javascript:S3504")
.includeRules("javascript:S108")
Expand All @@ -85,6 +87,7 @@ public static Arguments[] parametersForAnalyze() throws IOException
SonarLintEngineTestConfiguration.builder()
.description("sonarlint-example-with-global-variables.js with rule javascript:S3827 to check 3 issues from global variable declaring without extra properties \"sonar.javascript.globals\"")
.requirePlugin("javascript")
.enabledLanguages(Language.JS)
.requireNodeJS()
.excludeRules("javascript:S3504")
.includeRules("javascript:S3827")
Expand Down Expand Up @@ -130,8 +133,9 @@ public static Arguments[] parametersForAnalyze() throws IOException
SonarLintEngineTestConfiguration.builder()
.description("sonarlint-example-with-global-variables.js with rule javascript:S3827 to check one issue from global variable declaring in extra properties \"sonar.javascript.globals\" for \"globalVariables,api\"")
.requirePlugin("javascript")
.enabledLanguages(Language.JS)
.requireNodeJS()
.excludeRules("javascript:S3504")
.excludeRules("javascript:S3504")
.includeRules("javascript:S3827")
.addClientInputFile(new File("./src/test/resources/sonarlint-example-with-global-variables.js"))
.addExtraProperty("sonar.javascript.globals", "globalVariables,api", SonarLintEngine.GLOBAL_SETTINGS_PROJECT)
Expand All @@ -155,6 +159,7 @@ public static Arguments[] parametersForAnalyze() throws IOException
.description("sonarlint-example-with-global-variables.js with rule javascript:S3827 to check two issues from global variable declaring with extra properties \"sonar.javascript.globals\" for \"AapiI\"")
.requirePlugin("javascript")
.requireNodeJS()
.enabledLanguages(Language.JS)
.excludeRules("javascript:S3504")
.includeRules("javascript:S3827")
.addClientInputFile(new File("./src/test/resources/sonarlint-example-with-global-variables.js"))
Expand Down Expand Up @@ -189,6 +194,7 @@ public static Arguments[] parametersForAnalyze() throws IOException
SonarLintEngineTestConfiguration.builder()
.description("sonarlint-example.css with rule css:S1116 to check one issue on line \"4\"")
.requirePlugin("javascript")
.enabledLanguages(Language.JS)
.requireNodeJS()
.includeRules("css:S1116")
.addClientInputFile(new File("./src/test/resources/sonarlint-example.css"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue;
import org.sonarsource.sonarlint.core.commons.IssueSeverity;
import org.sonarsource.sonarlint.core.commons.Language;
import org.sonarsource.sonarlint.core.commons.RuleType;

/**
Expand All @@ -57,6 +58,7 @@ public static Arguments[] parametersForAnalyze() throws IOException
SonarLintEngineTestConfiguration.builder()
.description("sonarlin-example.php with rule php:S101 but without php:S1105 to check php plugin")
.requirePlugin("php")
.enabledLanguages(Language.PHP)
.excludeRules("php:S1105", "php:S1808", "php:S1779")
.includeRules("php:S101")
.addClientInputFile(new File("./src/test/resources/sonarlint-example.php"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.mockito.Mockito;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.ProjectManagerImplementation;
import org.sonarsource.sonarlint.core.commons.Language;

/**
*
Expand Down Expand Up @@ -66,7 +67,7 @@ public void getRuleParameter() throws MalformedURLException, BackingStoreExcepti
String parameterName = "max";
String parameterValueOnProjectScope = "5";
String parameterValueOnGlobalScope = "4";
SonarLintEngineImpl sonarLintEngine = new SonarLintEngineImpl();
SonarLintEngineImpl sonarLintEngine = new SonarLintEngineImpl(new Language[] {Language.JAVA});
sonarLintEngine.waitingInitialization();
sonarLintEngine.getPreferences(SonarLintEngine.GLOBAL_SETTINGS_PROJECT).removeNode();
sonarLintEngine.setRuleParameter(ruleKey, parameterName, parameterValueOnProjectScope, mockedProjectWithProjectScope);
Expand Down Expand Up @@ -132,7 +133,7 @@ public void getMergedExtraProperties(
Map<String, String> exptectedProperties
) {
// Given
SonarLintEngineImpl sonarLintEngine = new SonarLintEngineImpl();
SonarLintEngineImpl sonarLintEngine = new SonarLintEngineImpl(new Language[] {});
sonarLintEngine.waitingInitialization();
sonarLintEngine.setExtraProperties(actualGlobalProperties, SonarLintEngine.GLOBAL_SETTINGS_PROJECT);
sonarLintEngine.setExtraProperties(actualProjectProperties, project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private SonarLintEngineImplTestUtils() {

public static void analyzeTesting(SonarLintEngineTestConfiguration testConfiguration, List<Issue> expectedIssue) throws BackingStoreException, IOException
{
SonarLintEngineImpl sonarLintEngine = new SonarLintEngineImpl();
SonarLintEngineImpl sonarLintEngine = new SonarLintEngineImpl(testConfiguration.getEnabledLanguages());
sonarLintEngine.waitingInitialization();
if (testConfiguration.isRequireNodeJS()) {
SonarLintTestUtils.installNodeJS();
Expand Down
Loading

0 comments on commit 106bb3a

Please sign in to comment.