Skip to content

Commit

Permalink
Multi-Language Support: Rework WriteSharedFiles (#2098)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource authored Jul 23, 2024
1 parent 153d1f5 commit cbc607d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
72 changes: 67 additions & 5 deletions Tests/SonarScanner.MSBuild.Shim.Test/PropertiesWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void WriteGlobalSettings_VerboseIsSkipped()

sonar.modules=


""");
}

Expand All @@ -89,7 +89,7 @@ public void WriteGlobalSettings_HostUrlIsSetToSonarCloudIo_IfNotSet()

sonar.modules=


""");
}

Expand All @@ -103,7 +103,7 @@ public void WriteGlobalSettings_HostUrlIsPassedThroughIfSet()

sonar.modules=


""");
}

Expand All @@ -118,7 +118,7 @@ public void WriteGlobalSettings_HostUrlIsSetToSonarCloudUrlIfSet()

sonar.modules=


""".NormalizeLineEndings());
}

Expand All @@ -136,10 +136,72 @@ public void WriteGlobalSettings_HostUrlIsKeptIfHostUrlAndSonarcloudUrlAreSet()

sonar.modules=


""".NormalizeLineEndings());
}

[TestMethod]
public void WriteSharedProperties_EmptySources_EmptyTests()
{
var propertiesWriter = new PropertiesWriter(new(), new TestLogger());
propertiesWriter.WriteSharedFiles([], []);
propertiesWriter.Flush().Should().Be("""

sonar.modules=


""");
}

[TestMethod]
public void WriteSharedProperties_WithSources_EmptyTests()
{
var propertiesWriter = new PropertiesWriter(new(), new TestLogger());
propertiesWriter.WriteSharedFiles([new("C:/dev/main.hs"), new("C:/dev/lambdas.hs")], []);
propertiesWriter.Flush().Should().Be("""
sonar.sources=\
C:\\dev\\main.hs,\
C:\\dev\\lambdas.hs

sonar.modules=


""");
}

[TestMethod]
public void WriteSharedProperties_EmptySources_WithTests()
{
var propertiesWriter = new PropertiesWriter(new(), new TestLogger());
propertiesWriter.WriteSharedFiles([], [new("C:/dev/test.hs"), new("C:/dev/test2.hs")]);
propertiesWriter.Flush().Should().Be("""
sonar.tests=\
C:\\dev\\test.hs,\
C:\\dev\\test2.hs

sonar.modules=


""");
}

[TestMethod]
public void WriteSharedProperties_WithSources_WithTests()
{
var propertiesWriter = new PropertiesWriter(new(), new TestLogger());
propertiesWriter.WriteSharedFiles([new("C:/dev/main.hs")], [new("C:/dev/test.hs")]);
propertiesWriter.Flush().Should().Be("""
sonar.sources=\
C:\\dev\\main.hs
sonar.tests=\
C:\\dev\\test.hs

sonar.modules=


""");
}

[TestMethod]
public void WriteAnalyzerOutputPaths_ForUnexpectedLanguage_DoNotWritesOutPaths()
{
Expand Down
2 changes: 1 addition & 1 deletion src/SonarScanner.MSBuild.Shim/PropertiesFileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public bool TryWriteProperties(PropertiesWriter writer, out IEnumerable<ProjectD
}

writer.WriteSonarProjectInfo(projectBaseDir);
writer.WriteSharedFiles(rootModuleFiles);
writer.WriteSharedFiles(rootModuleFiles, []); // TODO: Populate the test files
validProjects.ForEach(writer.WriteSettingsForProject);
// Handle global settings
writer.WriteGlobalSettings(analysisProperties);
Expand Down
10 changes: 7 additions & 3 deletions src/SonarScanner.MSBuild.Shim/PropertiesWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,15 @@ public void WriteSonarProjectInfo(DirectoryInfo projectBaseDir)
AppendKeyValue(SonarProperties.PullRequestCacheBasePath, config.GetConfigValue(SonarProperties.PullRequestCacheBasePath, null));
}

public void WriteSharedFiles(IEnumerable<FileInfo> sharedFiles)
public void WriteSharedFiles(ICollection<FileInfo> sources, ICollection<FileInfo> tests)
{
if (sharedFiles.Any())
if (sources.Count > 0)
{
AppendKeyValue("sonar", "sources", sharedFiles);
AppendKeyValue("sonar", "sources", sources);
}
if (tests.Count > 0)
{
AppendKeyValue("sonar", "tests", tests);
}
sb.AppendLine();
}
Expand Down

0 comments on commit cbc607d

Please sign in to comment.