Skip to content

Commit

Permalink
Add logs for overwritten/added env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-jabbour-sonarsource committed Dec 16, 2024
1 parent 63957d5 commit a860c13
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class VCXCompilationDatabaseProviderTests
private IVCXCompilationDatabaseStorage storage;
private IFileConfigProvider fileConfigProvider;
private IEnvironmentVariableProvider envVarProvider;
private ILogger logger;

[TestInitialize]
public void TestInitialize()
Expand All @@ -45,6 +46,7 @@ public void TestInitialize()
fileConfigProvider = Substitute.For<IFileConfigProvider>();
envVarProvider = Substitute.For<IEnvironmentVariableProvider>();
envVarProvider.GetAll().Returns([]);
logger = Substitute.For<ILogger>();
}

[TestMethod]
Expand All @@ -54,7 +56,8 @@ public void MefCtor_CheckIsExported()
MefTestHelpers.CheckTypeCanBeImported<VCXCompilationDatabaseProvider, IVCXCompilationDatabaseProvider>(
MefTestHelpers.CreateExport<IVCXCompilationDatabaseStorage>(),
MefTestHelpers.CreateExport<IEnvironmentVariableProvider>(envVarProvider),
MefTestHelpers.CreateExport<IFileConfigProvider>());
MefTestHelpers.CreateExport<IFileConfigProvider>(),
MefTestHelpers.CreateExport<ILogger>());
}

[TestMethod]
Expand All @@ -67,7 +70,8 @@ public void CreateOrNull_NoFileConfig_ReturnsNull()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

testSubject.CreateOrNull(SourceFilePath).Should().BeNull();

Expand All @@ -83,7 +87,8 @@ public void CreateOrNull_FileConfig_CantStore_ReturnsNull()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

testSubject.CreateOrNull(SourceFilePath).Should().BeNull();

Expand All @@ -100,7 +105,8 @@ public void CreateOrNull_FileConfig_StoresAndReturnsHandle()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

testSubject.CreateOrNull(SourceFilePath).Should().Be(compilationDatabaseHandle);
}
Expand All @@ -114,7 +120,8 @@ public void CreateOrNull_NoEnvIncludeInFileConfig_UsesStatic()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

testSubject.CreateOrNull(SourceFilePath);

Expand All @@ -130,11 +137,13 @@ public void CreateOrNull_FileConfigHasEnvInclude_UsesDynamic()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

testSubject.CreateOrNull(SourceFilePath);

storage.Received(1).CreateDatabase(CDFile, CDDirectory, CDCommand, Arg.Is<IEnumerable<string>>(x => x.SequenceEqual(new [] { "Var1=Value1", "Var2=Value2", $"INCLUDE={EnvInclude}"})));
logger.Received(1).LogVerbose($"[VCXCompilationDatabaseProvider] Overwriting the value of environment variable \"INCLUDE\". Old value: \"static\", new value: \"{EnvInclude}\"");
}

[TestMethod]
Expand All @@ -146,11 +155,13 @@ public void CreateOrNull_NoStaticInclude_UsesDynamic()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

testSubject.CreateOrNull(SourceFilePath);

storage.Received(1).CreateDatabase(CDFile, CDDirectory, CDCommand, Arg.Is<IEnumerable<string>>(x => x.SequenceEqual(new [] { "Var1=Value1", "Var2=Value2", $"INCLUDE={EnvInclude}"})));
logger.Received(1).LogVerbose($"[VCXCompilationDatabaseProvider] Setting environment variable \"INCLUDE\". Value: \"{EnvInclude}\"");
}

[TestMethod]
Expand All @@ -162,7 +173,8 @@ public void CreateOrNull_StaticEnvVarsAreCached()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

testSubject.CreateOrNull(SourceFilePath);
testSubject.CreateOrNull(SourceFilePath);
Expand All @@ -180,12 +192,15 @@ public void CreateOrNull_EnvVarsContainHeaderPropertyForHeaderFiles()
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
fileConfigProvider);
fileConfigProvider,
logger);

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)));
logger.Received(1).LogVerbose($"[VCXCompilationDatabaseProvider] Setting environment variable \"INCLUDE\". Value: \"{EnvInclude}\"");
logger.Received(1).LogVerbose($"[VCXCompilationDatabaseProvider] Setting environment variable \"SONAR_CFAMILY_CAPTURE_PROPERTY_isHeaderFile\". Value: \"true\"");
}

private IFileConfig GetFileConfig(string envInclude = EnvInclude, bool isHeader = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ internal class VCXCompilationDatabaseProvider : IVCXCompilationDatabaseProvider
private readonly ImmutableList<EnvironmentEntry> staticEnvironmentVariableEntries;
private readonly IVCXCompilationDatabaseStorage storage;
private readonly IFileConfigProvider fileConfigProvider;
private readonly ILogger logger;

[method: ImportingConstructor]
public VCXCompilationDatabaseProvider(
IVCXCompilationDatabaseStorage storage,
IEnvironmentVariableProvider environmentVariableProvider,
IFileConfigProvider fileConfigProvider)
IFileConfigProvider fileConfigProvider,
ILogger logger)
{
this.storage = storage;
this.fileConfigProvider = fileConfigProvider;
this.logger = logger;
staticEnvironmentVariableEntries = ImmutableList.CreateRange(environmentVariableProvider.GetAll().Select(x => new EnvironmentEntry(x.name, x.value)));
}

Expand All @@ -66,14 +69,25 @@ private ImmutableList<EnvironmentEntry> GetEnvironmentEntries(IFileConfig fileCo
return environmentEntries;
}

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

private ImmutableList<EnvironmentEntry> UpdateEnvironmentWithEntry(ImmutableList<EnvironmentEntry> environmentEntries, EnvironmentEntry newEntry)
{
EnvironmentEntry oldEntry = environmentEntries.FirstOrDefault(x => x.Name == newEntry.Name);

if (oldEntry.Name != null)
{
logger.LogVerbose($"[VCXCompilationDatabaseProvider] Overwriting the value of environment variable \"{newEntry.Name}\". Old value: \"{oldEntry.Value}\", new value: \"{newEntry.Value}\"");
}
else
{
logger.LogVerbose($"[VCXCompilationDatabaseProvider] Setting environment variable \"{newEntry.Name}\". Value: \"{newEntry.Value}\"");
}
return environmentEntries.RemoveAll(x => x.Name == newEntry.Name).Add(newEntry);
}

private readonly struct EnvironmentEntry(string name, string value)
{
public string Name { get; } = name;
public string Value { get; } = value;
public string FormattedEntry { get; } = $"{name}={value}";
}
}

0 comments on commit a860c13

Please sign in to comment.