Skip to content

Commit 7cfa463

Browse files
Merge branch 'master' into renovate/major-jetty-monorepo
2 parents 62bbc54 + c230575 commit 7cfa463

File tree

19 files changed

+195
-180
lines changed

19 files changed

+195
-180
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
default_install_hook_types: [pre-push]
2+
repos:
3+
- repo: https://github.com/SonarSource/sonar-secrets-pre-commit
4+
rev: v2.30.0.8284
5+
hooks:
6+
- id: sonar-secrets
7+
stages: [pre-push]

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ In our CI/CD pipeline, we use the following:
6868
- SONARCLOUD_PROJECT_TOKEN=[user-token]
6969

7070
These can be set either on the operating system or your preferred IDE test run configuration.
71+
72+
## `sonar-secrets-pre-commit` hook
73+
74+
If you are Sonar internal use `sonar-secrets-pre-commit` to prevent pushing secrets to the repository. The hook is configured in [.pre-commit-hooks.yaml](.pre-commit-hooks.yaml). Follow the instructions in the [`sonar-secrets-pre-commit` README](https://github.com/SonarSource/sonar-secrets-pre-commit?tab=readme-ov-file) to activate it.
75+
Despite its name, the secrets check is configured as a pre-push hook.

Tests/SonarScanner.MSBuild.PreProcessor.Test/AnalysisConfigProcessing/AnalysisConfigGeneratorTests.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,24 @@ public void GenerateFile_WritesSonarQubeVersion()
314314
}
315315

316316
[TestMethod]
317-
[DataRow("java1.exe", "", "java1.exe")]
318-
[DataRow("java1.exe", " ", "java1.exe")]
319-
[DataRow("java1.exe", null, "java1.exe")]
320-
[DataRow("", "java2.exe", "java2.exe")]
321-
[DataRow(" ", "java2.exe", "java2.exe")]
322-
[DataRow(null, "java2.exe", "java2.exe")]
323-
[DataRow("java1.exe", "java2.exe", "java1.exe")]
317+
[DataRow("java.exe", "", "/Absolute/Path/SetByUser/java.exe")]
318+
[DataRow("java.exe", " ", "/Absolute/Path/SetByUser/java.exe")]
319+
[DataRow("java.exe", null, "/Absolute/Path/SetByUser/java.exe")]
320+
[DataRow("", "java.exe", "/Absolute/Path/Resolved/java.exe")]
321+
[DataRow(" ", "java.exe", "/Absolute/Path/Resolved/java.exe")]
322+
[DataRow(null, "java.exe", "/Absolute/Path/Resolved/java.exe")]
323+
[DataRow("java.exe", "resolvedJava.exe", "/Absolute/Path/SetByUser/java.exe")]
324324
public void GenerateFile_JavaExePath_Cases(string setByUser, string resolved, string expected)
325325
{
326326
var analysisDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
327327
var settings = BuildSettings.CreateSettingsForTesting(analysisDir);
328328
Directory.CreateDirectory(settings.SonarConfigDirectory);
329329
var commandLineArguments = new ListPropertiesProvider([new Property(SonarProperties.JavaExePath, setByUser)]);
330330
var runtime = new TestRuntime();
331+
runtime.Directory.GetFullPath(setByUser).Returns("/Absolute/Path/SetByUser/" + setByUser);
332+
runtime.Directory.GetFullPath(resolved).Returns("/Absolute/Path/Resolved/" + resolved);
333+
runtime.Directory.GetFullPath(null).Returns((string)null);
334+
331335
var args = CreateProcessedArgs(commandLineArguments, EmptyPropertyProvider.Instance, runtime);
332336

333337
var config = AnalysisConfigGenerator.GenerateFile(args, settings, [], EmptyProperties, [], "1.2.3.4", resolved, null, null, runtime);
@@ -336,18 +340,21 @@ public void GenerateFile_JavaExePath_Cases(string setByUser, string resolved, st
336340
}
337341

338342
[TestMethod]
339-
[DataRow("userEngine.jar", "", "userEngine.jar")]
340-
[DataRow("userEngine.jar", " ", "userEngine.jar")]
341-
[DataRow("userEngine.jar", null, "userEngine.jar")]
342-
[DataRow("", "resolvedEngine.jar", "resolvedEngine.jar")]
343-
[DataRow(" ", "resolvedEngine.jar", "resolvedEngine.jar")]
344-
[DataRow(null, "resolvedEngine.jar", "resolvedEngine.jar")]
345-
[DataRow("userEngine.jar", "resolvedEngine.jar", "userEngine.jar")]
343+
[DataRow("userEngine.jar", "", "/Absolute/Path/SetByUser/userEngine.jar")]
344+
[DataRow("userEngine.jar", " ", "/Absolute/Path/SetByUser/userEngine.jar")]
345+
[DataRow("userEngine.jar", null, "/Absolute/Path/SetByUser/userEngine.jar")]
346+
[DataRow("", "resolvedEngine.jar", "/Absolute/Path/Resolved/resolvedEngine.jar")]
347+
[DataRow(" ", "resolvedEngine.jar", "/Absolute/Path/Resolved/resolvedEngine.jar")]
348+
[DataRow(null, "resolvedEngine.jar", "/Absolute/Path/Resolved/resolvedEngine.jar")]
349+
[DataRow("userEngine.jar", "resolvedEngine.jar", "/Absolute/Path/SetByUser/userEngine.jar")]
346350
public void GenerateFile_ScannerEngine(string setByUser, string resolved, string expected)
347351
{
348352
var settings = BuildSettings.CreateSettingsForTesting(TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext));
349353
Directory.CreateDirectory(settings.SonarConfigDirectory);
350354
var runtime = new TestRuntime();
355+
runtime.Directory.GetFullPath(setByUser).Returns("/Absolute/Path/SetByUser/" + setByUser);
356+
runtime.Directory.GetFullPath(resolved).Returns("/Absolute/Path/Resolved/" + resolved);
357+
runtime.Directory.GetFullPath(null).Returns((string)null);
351358
var args = CreateProcessedArgs(new ListPropertiesProvider([new Property(SonarProperties.EngineJarPath, setByUser)]), EmptyPropertyProvider.Instance, runtime);
352359

353360
var config = AnalysisConfigGenerator.GenerateFile(args, settings, [], EmptyProperties, [], "1.2.3.4", null, resolved, null, runtime);
@@ -359,10 +366,11 @@ public void GenerateFile_ScannerEngine(string setByUser, string resolved, string
359366
public void GenerateFile_ScannerCliPath()
360367
{
361368
var settings = BuildSettings.CreateSettingsForTesting(TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext));
362-
363-
AnalysisConfigGenerator.GenerateFile(CreateProcessedArgs(), settings, [], EmptyProperties, [], "1.2.3.4", null, null, "sonar-scanner", new TestRuntime())
369+
var runtime = new TestRuntime();
370+
runtime.Directory.GetFullPath("sonar-scanner").Returns(Path.Combine(TestUtils.DriveRoot(), "sonar-scanner"));
371+
AnalysisConfigGenerator.GenerateFile(CreateProcessedArgs(), settings, [], EmptyProperties, [], "1.2.3.4", null, null, "sonar-scanner", runtime)
364372
.SonarScannerCliPath
365-
.Should().Be("sonar-scanner");
373+
.Should().Be(Path.Combine(TestUtils.DriveRoot(), "sonar-scanner"));
366374
}
367375

368376
[TestMethod]

Tests/SonarScanner.MSBuild.PreProcessor.Test/AnalysisConfigProcessing/Processors/TruststorePropertiesProcessorTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public void Update_DefaultPropertyValues()
165165
cmdLineArgs.AddProperty(SonarProperties.HostUrl, "https://localhost:9000");
166166
var fileWrapper = Substitute.For<IFileWrapper>();
167167
fileWrapper.Exists(defaultTruststorePath).Returns(true);
168-
var processor = CreateProcessor(CreateProcessedArgs(cmdLineArgs, fileWrapper));
168+
var runtime = new TestRuntime();
169+
var processor = CreateProcessor(CreateProcessedArgs(cmdLineArgs, fileWrapper), runtime);
169170
var config = new AnalysisConfig { LocalSettings = [new Property(SonarProperties.UserHome, sonarUserHome)] };
170171

171172
processor.Update(config);
@@ -174,6 +175,7 @@ public void Update_DefaultPropertyValues()
174175
config.ScannerOptsSettings.Should().ContainSingle()
175176
.Which.Should().Match<Property>(x => x.Id == "javax.net.ssl.trustStore" && x.Value == $"\"{defaultTruststorePath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)}\"");
176177
config.HasBeginStepCommandLineTruststorePassword.Should().BeFalse();
178+
runtime.Directory.Received(1).GetFullPath(defaultTruststorePath);
177179
}
178180

179181
[TestMethod]
@@ -216,6 +218,33 @@ public void Update_MapsTruststorePathToScannerOpts_Windows(string input, string
216218
Property.TryGetProperty(SonarProperties.TruststorePassword, config.LocalSettings, out _).Should().BeFalse();
217219
}
218220

221+
[TestMethod]
222+
[DataRow("../relative/path/to/truststore.pfx")]
223+
[DataRow("../path/to/My trustore.pfx")]
224+
[DataRow("\"../path/to/My trustore.pfx\"")]
225+
[DataRow("'../path/to/My trustore.pfx'")]
226+
public void Update_MapsTruststorePathToScannerOpts_RelativePathSavedAsAbsolute(string input)
227+
{
228+
var cmdLineArgs = new ListPropertiesProvider();
229+
var trimmedInput = input.Trim('\"', '\'');
230+
var absolutePath = Path.Combine(TestUtils.DriveRoot(), trimmedInput);
231+
cmdLineArgs.AddProperty(SonarProperties.HostUrl, "https://localhost:9000");
232+
cmdLineArgs.AddProperty(SonarProperties.TruststorePath, input);
233+
var runtime = new TestRuntime();
234+
runtime.Directory.GetFullPath(trimmedInput).Returns(absolutePath);
235+
var processor = CreateProcessor(CreateProcessedArgs(cmdLineArgs), runtime);
236+
var config = new AnalysisConfig { LocalSettings = [new Property(SonarProperties.TruststorePath, input)] };
237+
238+
processor.Update(config);
239+
240+
config.LocalSettings.Should().BeEmpty();
241+
config.ScannerOptsSettings.Should().ContainSingle();
242+
config.HasBeginStepCommandLineTruststorePassword.Should().BeFalse();
243+
AssertExpectedScannerOptsSettings("javax.net.ssl.trustStore", "\"" + absolutePath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + "\"", config);
244+
Property.TryGetProperty(SonarProperties.TruststorePath, config.LocalSettings, out _).Should().BeFalse();
245+
Property.TryGetProperty(SonarProperties.TruststorePassword, config.LocalSettings, out _).Should().BeFalse();
246+
}
247+
219248
[TestCategory(TestCategories.NoLinux)]
220249
[TestCategory(TestCategories.NoMacOS)]
221250
[TestMethod]

Tests/SonarScanner.MSBuild.PreProcessor.Test/packages.lock.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@
136136
},
137137
"Google.Protobuf": {
138138
"type": "Transitive",
139-
"resolved": "3.32.1",
140-
"contentHash": "HbFFRNU46e1E+qhGvwCDAO2EZ80WADqlkNqlOBYvGi7SNxwMPkTrReZgCg9/TrXwLO/n0za+AIiT2IplzqrjZw==",
139+
"resolved": "3.33.0",
140+
"contentHash": "+kIa03YipuiSDeRuZwcDcXS1xBQAFeGLIjuLbgJr2i+TlwBPYAqdnQZJ2SDVzIgDyy+q+n/400WyWyrJ5ZqCgQ==",
141141
"dependencies": {
142142
"System.Memory": "4.5.3"
143143
}
@@ -1752,7 +1752,7 @@
17521752
"sonarscanner.msbuild.preprocessor": {
17531753
"type": "Project",
17541754
"dependencies": {
1755-
"Google.Protobuf": "[3.32.1, )",
1755+
"Google.Protobuf": "[3.33.0, )",
17561756
"Newtonsoft.Json": "[13.0.4, )",
17571757
"SharpZipLib": "[1.4.2, )",
17581758
"SonarScanner.MSBuild.Common": "[1.0.0, )",
@@ -1927,8 +1927,8 @@
19271927
},
19281928
"Google.Protobuf": {
19291929
"type": "Transitive",
1930-
"resolved": "3.32.1",
1931-
"contentHash": "HbFFRNU46e1E+qhGvwCDAO2EZ80WADqlkNqlOBYvGi7SNxwMPkTrReZgCg9/TrXwLO/n0za+AIiT2IplzqrjZw=="
1930+
"resolved": "3.33.0",
1931+
"contentHash": "+kIa03YipuiSDeRuZwcDcXS1xBQAFeGLIjuLbgJr2i+TlwBPYAqdnQZJ2SDVzIgDyy+q+n/400WyWyrJ5ZqCgQ=="
19321932
},
19331933
"GraphQL": {
19341934
"type": "Transitive",
@@ -3942,7 +3942,7 @@
39423942
"sonarscanner.msbuild.preprocessor": {
39433943
"type": "Project",
39443944
"dependencies": {
3945-
"Google.Protobuf": "[3.32.1, )",
3945+
"Google.Protobuf": "[3.33.0, )",
39463946
"Newtonsoft.Json": "[13.0.4, )",
39473947
"SharpZipLib": "[1.4.2, )",
39483948
"SonarScanner.MSBuild.Common": "[1.0.0, )",

Tests/SonarScanner.MSBuild.Shim.Test/ScannerEngineInputGeneratorTest.GenerateResult.cs

Lines changed: 8 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ namespace SonarScanner.MSBuild.Shim.Test;
2222

2323
public partial class ScannerEngineInputGeneratorTest
2424
{
25-
private static string EscapedDirectorySeparator => Path.DirectorySeparatorChar == '\\' ? @"\\" : Path.DirectorySeparatorChar.ToString();
26-
2725
[TestMethod]
2826
public void GenerateResult_NoProjectInfoFiles()
2927
{
@@ -46,7 +44,6 @@ public void GenerateResult_ValidFiles()
4644
{
4745
// Only non-excluded projects with files to analyze should be marked as valid
4846
var testDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
49-
var testDirEscaped = testDir.Replace(@"\", @"\\");
5047
var withoutFilesDir = Path.Combine(testDir, "withoutFiles");
5148
var withoutFilesGuid = Guid.NewGuid();
5249
var withFiles1Guid = Guid.NewGuid();
@@ -66,121 +63,14 @@ public void GenerateResult_ValidFiles()
6663

6764
// One valid project info file -> file created
6865
AssertScannerInputCreated(result);
69-
result.ScannerEngineInput.ToString().Should().BeIgnoringLineEndings(
70-
$$"""
71-
{
72-
"scannerProperties": [
73-
{
74-
"key": "sonar.scanner.app",
75-
"value": "ScannerMSBuild"
76-
},
77-
{
78-
"key": "sonar.scanner.appVersion",
79-
"value": "{{Utilities.ScannerVersion}}"
80-
},
81-
{
82-
"key": "sonar.scanner.bootstrapStartTime",
83-
"value": "1735689600000"
84-
},
85-
{
86-
"key": "sonar.projectKey",
87-
"value": "{{config.SonarProjectKey}}"
88-
},
89-
{
90-
"key": "sonar.projectName",
91-
"value": "{{config.SonarProjectName}}"
92-
},
93-
{
94-
"key": "sonar.projectVersion",
95-
"value": "1.0"
96-
},
97-
{
98-
"key": "sonar.working.directory",
99-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}.sonar"
100-
},
101-
{
102-
"key": "sonar.projectBaseDir",
103-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}projects"
104-
},
105-
{
106-
"key": "sonar.sources",
107-
"value": ""
108-
},
109-
{
110-
"key": "sonar.tests",
111-
"value": ""
112-
},
113-
{
114-
"key": "sonar.modules",
115-
"value": "{{withFiles1Guid.ToString().ToUpper()}},{{withFiles2Guid.ToString().ToUpper()}}"
116-
},
117-
{
118-
"key": "{{withFiles1Guid.ToString().ToUpper()}}.sonar.projectKey",
119-
"value": "{{config.SonarProjectKey}}:{{withFiles1Guid.ToString().ToUpper()}}"
120-
},
121-
{
122-
"key": "{{withFiles1Guid.ToString().ToUpper()}}.sonar.projectName",
123-
"value": "withFiles1"
124-
},
125-
{
126-
"key": "{{withFiles1Guid.ToString().ToUpper()}}.sonar.projectBaseDir",
127-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}projects{{EscapedDirectorySeparator}}withFiles1"
128-
},
129-
{
130-
"key": "{{withFiles1Guid.ToString().ToUpper()}}.sonar.working.directory",
131-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}.sonar{{EscapedDirectorySeparator}}mod0"
132-
},
133-
{
134-
"key": "{{withFiles1Guid.ToString().ToUpper()}}.sonar.sourceEncoding",
135-
"value": "utf-8"
136-
},
137-
{
138-
"key": "{{withFiles1Guid.ToString().ToUpper()}}.sonar.tests",
139-
"value": ""
140-
},
141-
{
142-
"key": "{{withFiles1Guid.ToString().ToUpper()}}.sonar.sources",
143-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}projects{{EscapedDirectorySeparator}}withFiles1{{EscapedDirectorySeparator}}contentFile1.txt"
144-
},
145-
{
146-
"key": "{{withFiles2Guid.ToString().ToUpper()}}.sonar.projectKey",
147-
"value": "{{config.SonarProjectKey}}:{{withFiles2Guid.ToString().ToUpper()}}"
148-
},
149-
{
150-
"key": "{{withFiles2Guid.ToString().ToUpper()}}.sonar.projectName",
151-
"value": "withFiles2"
152-
},
153-
{
154-
"key": "{{withFiles2Guid.ToString().ToUpper()}}.sonar.projectBaseDir",
155-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}projects{{EscapedDirectorySeparator}}withFiles2"
156-
},
157-
{
158-
"key": "{{withFiles2Guid.ToString().ToUpper()}}.sonar.working.directory",
159-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}.sonar{{EscapedDirectorySeparator}}mod1"
160-
},
161-
{
162-
"key": "{{withFiles2Guid.ToString().ToUpper()}}.sonar.sourceEncoding",
163-
"value": "utf-8"
164-
},
165-
{
166-
"key": "{{withFiles2Guid.ToString().ToUpper()}}.sonar.tests",
167-
"value": ""
168-
},
169-
{
170-
"key": "{{withFiles2Guid.ToString().ToUpper()}}.sonar.sources",
171-
"value": "{{testDirEscaped}}{{EscapedDirectorySeparator}}projects{{EscapedDirectorySeparator}}withFiles2{{EscapedDirectorySeparator}}contentFile1.txt"
172-
},
173-
{
174-
"key": "sonar.visualstudio.enable",
175-
"value": "false"
176-
},
177-
{
178-
"key": "sonar.host.url",
179-
"value": "http://sonarqube.com"
180-
}
181-
]
182-
}
183-
""");
66+
67+
var reader = new ScannerEngineInputReader(result.ScannerEngineInput.ToString());
68+
reader.AssertProperty($"{withFiles1Guid.ToString().ToUpper()}.sonar.projectBaseDir", $"{testDir}{Path.DirectorySeparatorChar}projects{Path.DirectorySeparatorChar}withFiles1");
69+
reader.AssertProperty($"{withFiles1Guid.ToString().ToUpper()}.sonar.tests", string.Empty);
70+
reader.AssertProperty($"{withFiles1Guid.ToString().ToUpper()}.sonar.sources", $"{testDir}{Path.DirectorySeparatorChar}projects{Path.DirectorySeparatorChar}withFiles1{Path.DirectorySeparatorChar}contentFile1.txt");
71+
reader.AssertProperty($"{withFiles2Guid.ToString().ToUpper()}.sonar.projectBaseDir", $"{testDir}{Path.DirectorySeparatorChar}projects{Path.DirectorySeparatorChar}withFiles2");
72+
reader.AssertProperty($"{withFiles2Guid.ToString().ToUpper()}.sonar.tests", string.Empty);
73+
reader.AssertProperty($"{withFiles2Guid.ToString().ToUpper()}.sonar.sources", $"{testDir}{Path.DirectorySeparatorChar}projects{Path.DirectorySeparatorChar}withFiles2{Path.DirectorySeparatorChar}contentFile1.txt");
18474
}
18575

18676
[TestMethod]

Tests/SonarScanner.MSBuild.Tasks.IntegrationTest/SonarScanner.MSBuild.Tasks.IntegrationTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<PackageReference Include="Microsoft.Build.Framework" Version="17.11.4" />
77
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="3.14.2075" />
88
<PackageReference Include="MSBuild.StructuredLogger" Version="2.3.71" />
9-
<PackageReference Include="System.Text.Json" Version="9.0.9" />
9+
<PackageReference Include="System.Text.Json" Version="9.0.10" />
1010
</ItemGroup>
1111
<ItemGroup>
1212
<ProjectReference Include="..\..\src\SonarScanner.MSBuild.Shim\SonarScanner.MSBuild.Shim.csproj" />

0 commit comments

Comments
 (0)