Skip to content

Commit

Permalink
Add header file capture property to environment
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-jabbour-sonarsource committed Dec 14, 2024
1 parent b6e22d3 commit 87828dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,31 @@ public void CreateOrNull_StaticEnvVarsAreCached()
envVarProvider.Received(1).GetAll();
}

private IFileConfig GetFileConfig(string envInclude = EnvInclude)
[TestMethod]
public void CreateOrNull_EnvVarsContainHeaderPropertyForHeaderFiles()
{
var fileConfig = GetFileConfig(EnvInclude, true);
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
envVarProvider.GetAll().Returns([("Var1", "Value1"), ("Var2", "Value2")]);
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);

testSubject.CreateOrNull(SourceFilePath);

var expectedEnv = new[] { "Var1=Value1", "Var2=Value2", $"INCLUDE={EnvInclude}", "SONAR_CFAMILY_CAPTURE_PROPERTY_isHeaderFile=true" };
storage.Received(1).CreateDatabase(CDFile, CDDirectory, CDCommand, Arg.Is<IEnumerable<string>>(x => x.SequenceEqual(expectedEnv)));
}

private IFileConfig GetFileConfig(string envInclude = EnvInclude, bool isHeader = false)
{
var fileConfig = Substitute.For<IFileConfig>();
fileConfig.CDFile.Returns(CDFile);
fileConfig.CDDirectory.Returns(CDDirectory);
fileConfig.CDCommand.Returns(CDCommand);
fileConfig.EnvInclude.Returns(envInclude);
fileConfig.IsHeaderFile.Returns(isHeader);
return fileConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject;
internal class VCXCompilationDatabaseProvider : IVCXCompilationDatabaseProvider
{
private const string IncludeEntryName = "INCLUDE";
private const string IsHeaderEntryName = "SONAR_CFAMILY_CAPTURE_PROPERTY_isHeaderFile";
private readonly ImmutableList<EnvironmentEntry> staticEnvironmentVariableEntries;
private readonly IVCXCompilationDatabaseStorage storage;
private readonly IFileConfigProvider fileConfigProvider;
Expand All @@ -48,15 +49,27 @@ public VCXCompilationDatabaseProvider(

public ICompilationDatabaseHandle CreateOrNull(string filePath) =>
fileConfigProvider.Get(filePath, null) is { } fileConfig
? storage.CreateDatabase(fileConfig.CDFile, fileConfig.CDDirectory, fileConfig.CDCommand, GetEnvironmentEntries(fileConfig.EnvInclude).Select(x => x.FormattedEntry))
? storage.CreateDatabase(fileConfig.CDFile, fileConfig.CDDirectory, fileConfig.CDCommand, GetEnvironmentEntries(fileConfig).Select(x => x.FormattedEntry))
: null;

private ImmutableList<EnvironmentEntry> GetEnvironmentEntries(string fileConfigEnvInclude) =>
string.IsNullOrEmpty(fileConfigEnvInclude)
? staticEnvironmentVariableEntries
: staticEnvironmentVariableEntries
.RemoveAll(x => x.Name == IncludeEntryName)
.Add(new EnvironmentEntry(IncludeEntryName, fileConfigEnvInclude));
private ImmutableList<EnvironmentEntry> GetEnvironmentEntries(IFileConfig fileConfig)
{
ImmutableList<EnvironmentEntry> environmentEntries = staticEnvironmentVariableEntries;
if (!string.IsNullOrEmpty(fileConfig.EnvInclude))
{
environmentEntries = UpdateEnvironmentWithEntry(environmentEntries, new EnvironmentEntry(IncludeEntryName, fileConfig.EnvInclude));
}
if (fileConfig.IsHeaderFile)
{
environmentEntries = UpdateEnvironmentWithEntry(environmentEntries, new EnvironmentEntry(IsHeaderEntryName, "true"));
}
return environmentEntries;
}

private static ImmutableList<EnvironmentEntry> UpdateEnvironmentWithEntry(ImmutableList<EnvironmentEntry> environmentEntries, EnvironmentEntry newEntry) =>
environmentEntries.RemoveAll(x => x.Name == newEntry.Name).Add(newEntry);



private readonly struct EnvironmentEntry(string name, string value)
{
Expand Down

0 comments on commit 87828dc

Please sign in to comment.