Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Dec 13, 2024
1 parent f0fbbee commit dcbc939
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
20 changes: 20 additions & 0 deletions src/Core.UnitTests/EnvironmentVariableProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ namespace SonarLint.VisualStudio.Core.UnitTests
[TestClass]
public class EnvironmentVariableProviderTests
{
[TestMethod]
public void MefCtor_CheckIsExported() => MefTestHelpers.CheckTypeCanBeImported<EnvironmentVariableProvider, IEnvironmentVariableProvider>();

[TestMethod]
public void MefCtor_CheckIsSingleton() => MefTestHelpers.CheckIsSingletonMefComponent<EnvironmentVariableProvider>();

[TestMethod]
public void GetAll_ReturnsExpectedValues()
{
var testSubject = EnvironmentVariableProvider.Instance;
using var environmentVariableScope = new EnvironmentVariableScope();
environmentVariableScope.SetVariable("VAR1", "VAL1");
environmentVariableScope.SetVariable("VAR2", "VAL2");

var variables = testSubject.GetAll();

variables.Should().HaveCountGreaterThan(2);
variables.Should().Contain([("VAR1", "VAL1"), ("VAR2", "VAL2")]);
}

[TestMethod]
[DataRow(null)]
[DataRow("")]
Expand Down
9 changes: 6 additions & 3 deletions src/Core/EnvironmentVariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ public interface IEnvironmentVariableProvider
/// </summary>
string GetFolderPath(Environment.SpecialFolder folder);

List<(string name, string value)> GetEnvironmentVariables();
/// <summary>
/// Gets the current process environment variables
/// </summary>
List<(string name, string value)> GetAll();
}

[Export(typeof(IEnvironmentVariableProvider))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class EnvironmentVariableProvider : IEnvironmentVariableProvider
{
public static EnvironmentVariableProvider Instance { get; } = new EnvironmentVariableProvider();
public static EnvironmentVariableProvider Instance { get; } = new();

[ImportingConstructor]
private EnvironmentVariableProvider()
Expand All @@ -69,7 +72,7 @@ public string TryGet(string variableName)

public string GetFolderPath(Environment.SpecialFolder folder) => Environment.GetFolderPath(folder);

public List<(string name, string value)> GetEnvironmentVariables()
public List<(string name, string value)> GetAll()
{
var variables = new List<(string name, string value)>();
foreach (DictionaryEntry environmentVariable in Environment.GetEnvironmentVariables())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,34 @@ public class VCXCompilationDatabaseStorageTests
private IThreadHandling threadHandling;
private IVCXCompilationDatabaseStorage testSubject;
private TestLogger testLogger;
private EnvironmentVariableScope environmentVariableScope;

[TestMethod]
public void MefCtor_CheckIsExported() =>
MefTestHelpers.CheckTypeCanBeImported<VCXCompilationDatabaseStorage, IVCXCompilationDatabaseStorage>(
MefTestHelpers.CreateExport<IFileSystemService>(),
MefTestHelpers.CreateExport<IThreadHandling>(),
MefTestHelpers.CreateExport<ILogger>());

[TestMethod]
public void MefCtor_CheckIsSingleton() => MefTestHelpers.CheckIsSingletonMefComponent<VCXCompilationDatabaseStorage>();
private IEnvironmentVariableProvider environmentVariableProvider;

[TestInitialize]
public void TestInitialize()
{
fileSystemService = Substitute.For<IFileSystemService>();
threadHandling = Substitute.For<IThreadHandling>();
testLogger = new TestLogger();
environmentVariableScope = new EnvironmentVariableScope();
environmentVariableScope.SetVariable(CustomVariableName, CustomVariableValue);
testSubject = new VCXCompilationDatabaseStorage(fileSystemService, Substitute.For<IEnvironmentVariableProvider>(), threadHandling, testLogger);
environmentVariableProvider = Substitute.For<IEnvironmentVariableProvider>();
environmentVariableProvider.GetAll().Returns([(CustomVariableName, CustomVariableValue)]);
testSubject = new VCXCompilationDatabaseStorage(fileSystemService, environmentVariableProvider, threadHandling, testLogger);
}

[TestCleanup]
public void TestCleanup()
[TestMethod]
public void MefCtor_CheckIsExported()
{
environmentVariableScope.Dispose();
var variableProvider = Substitute.For<IEnvironmentVariableProvider>();
variableProvider.GetAll().Returns([]);
MefTestHelpers.CheckTypeCanBeImported<VCXCompilationDatabaseStorage, IVCXCompilationDatabaseStorage>(
MefTestHelpers.CreateExport<IFileSystemService>(),
MefTestHelpers.CreateExport<IEnvironmentVariableProvider>(variableProvider),
MefTestHelpers.CreateExport<IThreadHandling>(),
MefTestHelpers.CreateExport<ILogger>());
}

[TestMethod]
public void MefCtor_CheckIsSingleton() => MefTestHelpers.CheckIsSingletonMefComponent<VCXCompilationDatabaseStorage>();

[TestMethod]
public void CreateDatabase_NonCriticalException_ReturnsNull()
{
Expand Down Expand Up @@ -122,6 +121,7 @@ public void CreateDatabase_CreatesDifferentHandlesForSameFile()
Directory.GetParent(databaseHandle1.FilePath).FullName.Should().BeEquivalentTo(expectedDirectory);
Directory.GetParent(databaseHandle2.FilePath).FullName.Should().BeEquivalentTo(expectedDirectory);
Path.GetFileNameWithoutExtension(databaseHandle1.FilePath).Should().NotBe(Path.GetFileNameWithoutExtension(databaseHandle2.FilePath));
environmentVariableProvider.Received(1).GetAll();
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,24 @@ internal interface IVCXCompilationDatabaseStorage : IDisposable

[Export(typeof(IVCXCompilationDatabaseStorage))]
[PartCreationPolicy(CreationPolicy.Shared)]
[method: ImportingConstructor]
internal sealed class VCXCompilationDatabaseStorage(IFileSystemService fileSystemService, IEnvironmentVariableProvider environmentVariableProvider, IThreadHandling threadHandling, ILogger logger)
: IVCXCompilationDatabaseStorage
internal sealed class VCXCompilationDatabaseStorage : IVCXCompilationDatabaseStorage
{
private bool disposed;
private readonly string compilationDatabaseDirectoryPath = PathHelper.GetTempDirForTask(true, "VCXCD");

private readonly ImmutableList<string> environmentVariablePairs = ImmutableList.CreateRange(environmentVariableProvider.GetEnvironmentVariables().Select(x => GetEnvVarPair(x.name, x.value)));
private readonly ImmutableList<string> environmentVariablePairs;
private readonly IFileSystemService fileSystemService;
private readonly IThreadHandling threadHandling;
private readonly ILogger logger;

[ImportingConstructor]
public VCXCompilationDatabaseStorage(IFileSystemService fileSystemService, IEnvironmentVariableProvider environmentVariableProvider, IThreadHandling threadHandling, ILogger logger)
{
this.fileSystemService = fileSystemService;
this.threadHandling = threadHandling;
this.logger = logger;
environmentVariablePairs = ImmutableList.CreateRange(environmentVariableProvider.GetAll().Select(x => GetEnvVarPair(x.name, x.value)));
}

private static string GetEnvVarPair(string name, string value) => $"{name}={value}";

Expand Down

0 comments on commit dcbc939

Please sign in to comment.