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

SCAN4NET-260 Add python support #2331

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public void AdditionalFiles_ExcludedFilesIgnored(string excluded)
[DataRow("sonar.html.file.suffixes")]
[DataRow("sonar.javascript.file.suffixes")]
[DataRow("sonar.typescript.file.suffixes")]
[DataRow("sonar.python.file.suffixes")]
public void AdditionalFiles_ExtensionsFound_SingleProperty(string propertyName)
{
wrapper
Expand All @@ -268,6 +269,7 @@ public void AdditionalFiles_ExtensionsFound_MultipleProperties()
{
"valid.cs.html",
"valid.sql",
"valid.py",
"invalid.js",
"invalid.html",
"invalid.vb.html"
Expand All @@ -283,12 +285,13 @@ public void AdditionalFiles_ExtensionsFound_MultipleProperties()
[
new("sonar.html.file.suffixes", ".cs.html"),
new("sonar.tsql.file.suffixes", ".sql"),
new("sonar.python.file.suffixes", ".py"),
]
};

var files = sut.AdditionalFiles(analysisConfig, ProjectBaseDir);

files.Sources.Select(x => x.Name).Should().BeEquivalentTo("valid.cs.html", "valid.sql");
files.Sources.Select(x => x.Name).Should().BeEquivalentTo("valid.cs.html", "valid.sql", "valid.py");
files.Tests.Should().BeEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1356,8 +1356,11 @@ public void GenerateFile_AdditionalFiles_EndToEnd()
TestUtils.CreateProjectWithFiles(TestContext, project2, root);
string[] rootSources =
[
TestUtils.CreateEmptyFile(rootProjects, "rootSource.py"),
TestUtils.CreateEmptyFile(rootProjects, "rootSource.spec.py"),
TestUtils.CreateEmptyFile(rootProjects, "rootSource.spec.sql"),
TestUtils.CreateEmptyFile(rootProjects, "rootSource.sql"),
TestUtils.CreateEmptyFile(rootProjects, "rootSource.test.py"),
TestUtils.CreateEmptyFile(rootProjects, "rootSource.test.sql"),
TestUtils.CreateEmptyFile(rootProjects, "rootSource.ts"),
TestUtils.CreateEmptyFile(rootProjects, "rootSource.tsx"),
Expand All @@ -1372,18 +1375,21 @@ public void GenerateFile_AdditionalFiles_EndToEnd()
string[] project1Sources =
[
TestUtils.CreateEmptyFile(Path.Combine(rootProjects, project1), "project1.sql"),
TestUtils.CreateEmptyFile(Path.Combine(rootProjects, project1), "project1.py"),
TestUtils.CreateEmptyFile(Path.Combine(rootProjects, project1), "project1.ts")
];
string[] project2Sources =
[
TestUtils.CreateEmptyFile(Path.Combine(rootProjects, project2), "project2.tsx"),
TestUtils.CreateEmptyFile(Path.Combine(rootProjects, project2), "project2.sql"),
TestUtils.CreateEmptyFile(Path.Combine(rootProjects, project2), "project2.py"),
];

AnalysisProperties serverProperties =
[
new("sonar.typescript.file.suffixes", ".ts,.tsx"),
new("sonar.tsql.file.suffixes", "sql"),
new("sonar.python.file.suffixes", "py"),
];
var config = CreateValidConfig(root, serverProperties);

Expand All @@ -1399,9 +1405,9 @@ public void GenerateFile_AdditionalFiles_EndToEnd()
// Multiline string literal doesn't work here because of environment-specific line ending.
var propertiesFile = File.ReadAllText(result.FullPropertiesFilePath);
propertiesFile.Should()
.Contain($"sonar.sources=\\{Environment.NewLine}{string.Join($",\\{Environment.NewLine}", rootSources.Select(x => x.Replace("\\", "\\\\")))}");
.Contain($@"sonar.sources=\{Environment.NewLine}{string.Join($@",\{Environment.NewLine}", rootSources.Select(x => x.Replace(@"\", @"\\")))}");
propertiesFile.Should()
.Contain($"sonar.tests=\\{Environment.NewLine}{string.Join($",\\{Environment.NewLine}", rootTests.Select(x => x.Replace("\\", "\\\\")))}");
.Contain($@"sonar.tests=\{Environment.NewLine}{string.Join($@",\{Environment.NewLine}", rootTests.Select(x => x.Replace(@"\", @"\\")))}");

void AssertExpectedPathsAddedToModuleFiles(string projectId, string[] expectedPaths) =>
expectedPaths.Should().BeSubsetOf(result.Projects.Single(x => x.Project.ProjectName == projectId).SonarQubeModuleFiles.Select(x => x.FullName));
Expand Down
2 changes: 2 additions & 0 deletions its/projects/MultiLanguageSupport/frontend/PageOne.Script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def divide(numerator, denominator):
return numerator / denominator # FIXME denominator value might be 0 python:S1134
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def divide(numerator, denominator):
return numerator / denominator # FIXME denominator value might be 0 python:S1134
2 changes: 2 additions & 0 deletions its/projects/MultiLanguageSupport/src/Outside.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def divide(numerator, denominator):
return numerator / denominator # FIXME denominator value might be 0 python:S1134
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ void checkMultiLanguageSupportWithSdkFormat() throws Exception {
TestUtils.dumpAllIssues(ORCHESTRATOR);

List<Issue> issues = TestUtils.allIssues(ORCHESTRATOR);
assertThat(issues).hasSize(10)
assertThat(issues).hasSize(13)
.extracting(Issue::getRule, Issue::getComponent)
.containsExactlyInAnyOrder(
// "src/MultiLanguageSupport" directory
Expand All @@ -1046,13 +1046,16 @@ void checkMultiLanguageSupportWithSdkFormat() throws Exception {
tuple("javascript:S1529", "MultiLanguageSupport:src/MultiLanguageSupport/JavaScript.js"),
tuple("plsql:S1134", "MultiLanguageSupport:src/MultiLanguageSupport/NotIncluded.sql"),
tuple("plsql:S1134", "MultiLanguageSupport:src/MultiLanguageSupport/plsql.sql"),
tuple("python:S1134", "MultiLanguageSupport:src/MultiLanguageSupport/python.py"),
// "src/" directory
tuple("plsql:S1134", "MultiLanguageSupport:src/Outside.sql"),
tuple("javascript:S1529", "MultiLanguageSupport:src/Outside.js"),
tuple("python:S1134", "MultiLanguageSupport:src/Outside.py"),
// "frontend/" directory
tuple("javascript:S1529", "MultiLanguageSupport:frontend/PageOne.js"),
tuple("typescript:S1128", "MultiLanguageSupport:frontend/PageTwo.tsx"),
tuple("plsql:S1134", "MultiLanguageSupport:frontend/PageOne.Query.sql"));
tuple("plsql:S1134", "MultiLanguageSupport:frontend/PageOne.Query.sql"),
tuple("python:S1134", "MultiLanguageSupport:frontend/PageOne.Script.py"));
}

@Test
Expand Down Expand Up @@ -1108,7 +1111,8 @@ void checkMultiLanguageSupportReact() throws Exception {
tuple("javascript:S3358", "MultiLanguageSupportReact:ClientApp/src/setupProxy.js"),
tuple("javascript:S1117", "MultiLanguageSupportReact:ClientApp/src/setupProxy.js"),
tuple("csharpsquid:S4487", "MultiLanguageSupportReact:Controllers/WeatherForecastController.cs"),
tuple("csharpsquid:S4487", "MultiLanguageSupportReact:Pages/Error.cshtml.cs"));
tuple("csharpsquid:S4487", "MultiLanguageSupportReact:Pages/Error.cshtml.cs"),
tuple("python:S5754", "MultiLanguageSupportReact:ClientApp/node_modules/flatted/python/flatted.py"));
// tuple("csharpsquid:S6966", "MultiLanguageSupportReact:Program.cs") // Only reported on some versions of SQ.
}

Expand Down Expand Up @@ -1165,14 +1169,14 @@ void checkMultiLanguageSupportAngular() throws Exception {
tuple("csharpsquid:S4487", "MultiLanguageSupportAngular:Controllers/WeatherForecastController.cs"),
tuple("csharpsquid:S4487", "MultiLanguageSupportAngular:Pages/Error.cshtml.cs"),
// tuple("csharpsquid:S6966", "MultiLanguageSupportAngular:Program.cs"), // Only reported on some versions of SQ.
// Some css, less and scss files are analyzed in node_modules. This is because the IT
// Some css, less, scss and python files are analyzed in node_modules. This is because the IT
// are running without scm support. Normally these files are excluded by the scm ignore settings.
// js/ts files in node_modules are additionally excluded by sonar.javascript.exclusions or sonar.typescript.exclusions
// and are therefore not reported here.
tuple("css:S4649", "MultiLanguageSupportAngular:ClientApp/node_modules/serve-index/public/style.css"),
tuple("css:S4654", "MultiLanguageSupportAngular:ClientApp/node_modules/less/test/browser/less/urls.less"),
tuple("css:S4654", "MultiLanguageSupportAngular:ClientApp/node_modules/bootstrap/scss/forms/_form-check.scss"));

tuple("css:S4654", "MultiLanguageSupportAngular:ClientApp/node_modules/bootstrap/scss/forms/_form-check.scss"),
tuple("python:S5754", "MultiLanguageSupportAngular:ClientApp/node_modules/flatted/python/flatted.py"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private static Orchestrator createOrchestrator() {
.addPlugin(TestUtils.getMavenLocation("org.sonarsource.dotnet", "sonar-vbnet-plugin", System.getProperty("sonar.vbnetplugin.version", "DEV")))
.addPlugin(TestUtils.getMavenLocation("org.sonarsource.xml", "sonar-xml-plugin", System.getProperty("sonar.xmlplugin.version", "LATEST_RELEASE")))
// The following plugin versions are hardcoded because `DEV` is not compatible with SQ < 8.9, to be fixed with this issue: https://github.com/SonarSource/sonar-scanner-msbuild/issues/1486
.addPlugin(TestUtils.getMavenLocation("org.sonarsource.python", "sonar-python-plugin", System.getProperty("sonar.pythonplugin.version", "3.4.1.8066")))
.addPlugin(TestUtils.getMavenLocation("org.sonarsource.javascript", "sonar-javascript-plugin", System.getProperty("sonar.javascriptplugin.version", "7.4.4.15624")))
.addPlugin(TestUtils.getMavenLocation("com.sonarsource.plsql", "sonar-plsql-plugin", System.getProperty("sonar.plsqlplugin.version", "3.6.1.3873")))
.activateLicense();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,22 @@ public static List<Issue> issuesForComponent(Orchestrator orchestrator, String c
}

public static List<Issue> allIssues(Orchestrator orchestrator) {
return newWsClient(orchestrator)
.issues()
.search(new org.sonarqube.ws.client.issues.SearchRequest())
.getIssuesList();
List<Issue> results = new ArrayList<>();
var issues = newWsClient(orchestrator).issues();
int page = 1;
while(true)
{
var pageResult = issues.search(new org.sonarqube.ws.client.issues.SearchRequest().setP(String.valueOf(page)));
results.addAll(pageResult.getIssuesList());
if (pageResult.getPaging().getTotal() == results.size())
{
return results;
}
else
{
page++;
}
}
}

public static String getDefaultBranchName(Orchestrator orchestrator) {
Expand Down
3 changes: 2 additions & 1 deletion src/SonarScanner.MSBuild.Shim/AdditionalFilesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public class AdditionalFilesService(IDirectoryWrapper directoryWrapper, ILogger
"sonar.css.file.suffixes",
"sonar.html.file.suffixes",
"sonar.javascript.file.suffixes",
"sonar.typescript.file.suffixes"
"sonar.typescript.file.suffixes",
"sonar.python.file.suffixes",
];

private static readonly IReadOnlyList<string> SupportedTestLanguages =
Expand Down