From a860c13f0754164819a701df77d8ebd546e144c3 Mon Sep 17 00:00:00 2001 From: Michael Jabbour Date: Mon, 16 Dec 2024 00:11:25 +0100 Subject: [PATCH] Add logs for overwritten/added env vars --- .../VCXCompilationDatabaseProviderTests.cs | 33 ++++++++++++++----- .../VCXCompilationDatabaseProvider.cs | 22 ++++++++++--- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs index f238cfd9f..025ddb6d1 100644 --- a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs +++ b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/VCXCompilationDatabaseProviderTests.cs @@ -37,6 +37,7 @@ public class VCXCompilationDatabaseProviderTests private IVCXCompilationDatabaseStorage storage; private IFileConfigProvider fileConfigProvider; private IEnvironmentVariableProvider envVarProvider; + private ILogger logger; [TestInitialize] public void TestInitialize() @@ -45,6 +46,7 @@ public void TestInitialize() fileConfigProvider = Substitute.For(); envVarProvider = Substitute.For(); envVarProvider.GetAll().Returns([]); + logger = Substitute.For(); } [TestMethod] @@ -54,7 +56,8 @@ public void MefCtor_CheckIsExported() MefTestHelpers.CheckTypeCanBeImported( MefTestHelpers.CreateExport(), MefTestHelpers.CreateExport(envVarProvider), - MefTestHelpers.CreateExport()); + MefTestHelpers.CreateExport(), + MefTestHelpers.CreateExport()); } [TestMethod] @@ -67,7 +70,8 @@ public void CreateOrNull_NoFileConfig_ReturnsNull() var testSubject = new VCXCompilationDatabaseProvider( storage, envVarProvider, - fileConfigProvider); + fileConfigProvider, + logger); testSubject.CreateOrNull(SourceFilePath).Should().BeNull(); @@ -83,7 +87,8 @@ public void CreateOrNull_FileConfig_CantStore_ReturnsNull() var testSubject = new VCXCompilationDatabaseProvider( storage, envVarProvider, - fileConfigProvider); + fileConfigProvider, + logger); testSubject.CreateOrNull(SourceFilePath).Should().BeNull(); @@ -100,7 +105,8 @@ public void CreateOrNull_FileConfig_StoresAndReturnsHandle() var testSubject = new VCXCompilationDatabaseProvider( storage, envVarProvider, - fileConfigProvider); + fileConfigProvider, + logger); testSubject.CreateOrNull(SourceFilePath).Should().Be(compilationDatabaseHandle); } @@ -114,7 +120,8 @@ public void CreateOrNull_NoEnvIncludeInFileConfig_UsesStatic() var testSubject = new VCXCompilationDatabaseProvider( storage, envVarProvider, - fileConfigProvider); + fileConfigProvider, + logger); testSubject.CreateOrNull(SourceFilePath); @@ -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>(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] @@ -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>(x => x.SequenceEqual(new [] { "Var1=Value1", "Var2=Value2", $"INCLUDE={EnvInclude}"}))); + logger.Received(1).LogVerbose($"[VCXCompilationDatabaseProvider] Setting environment variable \"INCLUDE\". Value: \"{EnvInclude}\""); } [TestMethod] @@ -162,7 +173,8 @@ public void CreateOrNull_StaticEnvVarsAreCached() var testSubject = new VCXCompilationDatabaseProvider( storage, envVarProvider, - fileConfigProvider); + fileConfigProvider, + logger); testSubject.CreateOrNull(SourceFilePath); testSubject.CreateOrNull(SourceFilePath); @@ -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>(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) diff --git a/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs b/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs index a925fd24b..b4ff05f13 100644 --- a/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs +++ b/src/Integration.Vsix/CFamily/VcxProject/VCXCompilationDatabaseProvider.cs @@ -35,15 +35,18 @@ internal class VCXCompilationDatabaseProvider : IVCXCompilationDatabaseProvider private readonly ImmutableList 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))); } @@ -66,14 +69,25 @@ private ImmutableList GetEnvironmentEntries(IFileConfig fileCo return environmentEntries; } - private static ImmutableList UpdateEnvironmentWithEntry(ImmutableList environmentEntries, EnvironmentEntry newEntry) => - environmentEntries.RemoveAll(x => x.Name == newEntry.Name).Add(newEntry); - + private ImmutableList UpdateEnvironmentWithEntry(ImmutableList 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}"; } }