diff --git a/clio.tests/Command/ManageWindowsFeaturesCommandTestFixture.cs b/clio.tests/Command/ManageWindowsFeaturesCommandTestFixture.cs index 54f0bf36..886d2754 100644 --- a/clio.tests/Command/ManageWindowsFeaturesCommandTestFixture.cs +++ b/clio.tests/Command/ManageWindowsFeaturesCommandTestFixture.cs @@ -1,7 +1,11 @@ -using Autofac; +using System; +using System.Collections.Generic; +using Autofac; using Clio.Command; +using Clio.Common; using FluentAssertions; using NSubstitute; +using NSubstitute.Core; using NUnit.Framework; namespace Clio.Tests.Command; @@ -47,4 +51,73 @@ public void InstallComponent_Calls_WindowsFeatureManager(string actionName){ } -} \ No newline at end of file + [Test, Category("Unit")] + public void GetMissedComponents_ShouldInstallMissedComponents() { + // Arrange + var existingComponents = new List { + new WindowsFeature { Name = "Feature3", Installed = false }, + new WindowsFeature { Name = "Feature4", Installed = false } + }; + + IWorkingDirectoriesProvider wp = Substitute.For(); + IWindowsFeatureProvider windowsFeatureProvider = Substitute.For(); + windowsFeatureProvider.GetWindowsFeatures().Returns(existingComponents); + var windowsFeatureManager = new WindowsFeatureManager(wp, new ConsoleProgressbar(), windowsFeatureProvider) { + RequirmentNETFrameworkFeatures = ["Feature1", "Feature2"], + }; + + // Act + var missingComponents = windowsFeatureManager.GetMissedComponents(); + + // Assert + missingComponents.Should().HaveCount(2); + } + + [Test, Category("Unit")] + public void GetMissedComponents_CorrectWorking_IfAllFeatureExisting() { + // Arrange + var existingComponents = new List { + new WindowsFeature { Name = "Feature1", Installed = true }, + new WindowsFeature { Name = "Feature2", Installed = true } + }; + + IWorkingDirectoriesProvider wp = Substitute.For(); + IWindowsFeatureProvider windowsFeatureProvider = Substitute.For(); + windowsFeatureProvider.GetWindowsFeatures().Returns(existingComponents); + windowsFeatureProvider.GetActiveWindowsFeatures().Returns(["Feature1", "Feature2"]); + var windowsFeatureManager = new WindowsFeatureManager(wp, new ConsoleProgressbar(), windowsFeatureProvider) { + RequirmentNETFrameworkFeatures = ["Feature1", "Feature2"], + }; + + // Act + var missingComponents = windowsFeatureManager.GetMissedComponents(); + + // Assert + missingComponents.Should().HaveCount(0); + } + + [Test, Category("Unit")] + public void InstallMissingFeatures_NotThrow_IfAllFeatureExisting() { + // Arrange + var existingComponents = new List { + new WindowsFeature { Name = "Feature1", Installed = true }, + new WindowsFeature { Name = "Feature2", Installed = true } + }; + + IWorkingDirectoriesProvider wp = Substitute.For(); + IWindowsFeatureProvider windowsFeatureProvider = Substitute.For(); + windowsFeatureProvider.GetWindowsFeatures().Returns(existingComponents); + windowsFeatureProvider.GetActiveWindowsFeatures().Returns(["Feature1", "Feature2"]); + var windowsFeatureManager = new WindowsFeatureManager(wp, new ConsoleProgressbar(), windowsFeatureProvider) { + RequirmentNETFrameworkFeatures = ["Feature1", "Feature2"], + }; + + // Act + Action act = () => windowsFeatureManager.InstallMissingFeatures(); + // Assert + act.Should().NotThrow(); + + } + +} + diff --git a/clio/Command/WindowsFeatureManager.cs b/clio/Command/WindowsFeatureManager.cs index dd5745e2..8d9e3992 100644 --- a/clio/Command/WindowsFeatureManager.cs +++ b/clio/Command/WindowsFeatureManager.cs @@ -29,11 +29,13 @@ public class WindowsFeatureManager : IWindowsFeatureManager { public WindowsFeatureManager(IWorkingDirectoriesProvider workingDirectoriesProvider, - ConsoleProgressbar consoleProgressBar) { + ConsoleProgressbar consoleProgressBar, IWindowsFeatureProvider windowsFeatureProvider) { _workingDirectoriesProvider = workingDirectoriesProvider; _consoleProgressBar = consoleProgressBar; + _windowsFeatureProvider = windowsFeatureProvider; } + private string RequirmentNETFrameworkFeaturesFilePaths { get { return Path.Join(_workingDirectoriesProvider.TemplateDirectory, "windows_features", "RequirmentNetFramework.txt"); @@ -43,16 +45,30 @@ private string RequirmentNETFrameworkFeaturesFilePaths { private string GetInactiveFeaturesCode(string featureName) { - var windowsFeatures = GetWindowsFeatures(); + var windowsFeatures = _windowsFeatureProvider.GetWindowsFeatures(); var feature = windowsFeatures.FirstOrDefault(i => i.Name.ToLower() == featureName.ToLower() || i.Caption.ToLower() == featureName.ToLower()); return feature.Name; } - private List RequirmentNETFrameworkFeatures { - get { return File.ReadAllLines(RequirmentNETFrameworkFeaturesFilePaths).ToList(); } + private IEnumerable _requirmentNETFrameworkFeatures; + + public IEnumerable RequirmentNETFrameworkFeatures + { + get + { + if (_requirmentNETFrameworkFeatures == null) { + _requirmentNETFrameworkFeatures = File.ReadAllLines(RequirmentNETFrameworkFeaturesFilePaths); + } + return _requirmentNETFrameworkFeatures; + } + set + { + _requirmentNETFrameworkFeatures = value; + } } + public List GetMissedComponents() { var missedComponents = new List(); foreach (var item in RequirmentNETFrameworkFeatures) { @@ -87,9 +103,9 @@ public void UninstallFeature(string featureName) { public void InstallMissingFeatures(){ List missedComponents = GetMissedComponents(); - int maxLengthComponentName = GetActionMaxLength(missedComponents.Select(s => s.Name)); - _consoleProgressBar.MaxActionNameLength = maxLengthComponentName; if (missedComponents.Count > 0) { + int maxLengthComponentName = GetActionMaxLength(missedComponents.Select(s => s.Name)); + _consoleProgressBar.MaxActionNameLength = maxLengthComponentName; Console.WriteLine($"Found {missedComponents.Count} missed components"); foreach (WindowsFeature item in missedComponents) { InstallFeature(item.Name); @@ -136,41 +152,17 @@ private void SetFeatureState(string featureName, bool state) { } } - private List _windowsActiveFeatures; - private List windowsActiveFeatures { + private IEnumerable _windowsActiveFeatures; + private IEnumerable windowsActiveFeatures { get { if (_windowsActiveFeatures == null) { - var features = new List(); - try { - ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OptionalFeature WHERE InstallState = 1"); - ManagementObjectCollection featureCollection = searcher.Get(); - foreach (ManagementObject featureObject in featureCollection) { - string featureName = featureObject["Name"].ToString(); - features.Add(featureName); - string featureCaption = featureObject["Caption"].ToString(); - features.Add(featureCaption); - } - } catch (Exception) { - } - _windowsActiveFeatures = features; + _windowsActiveFeatures = _windowsFeatureProvider.GetActiveWindowsFeatures(); } return _windowsActiveFeatures; } } - private List GetWindowsFeatures() { - var features = new List(); - ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OptionalFeature"); - ManagementObjectCollection featureCollection = searcher.Get(); - foreach (ManagementObject featureObject in featureCollection) { - features.Add(new WindowsFeature() { - Name = featureObject["Name"].ToString(), - Caption = featureObject["Caption"].ToString() - }); - } - return features; - } - IWorkingDirectoriesProvider _workingDirectoriesProvider; ConsoleProgressbar _consoleProgressBar; + private IWindowsFeatureProvider _windowsFeatureProvider; } \ No newline at end of file