Skip to content

Commit

Permalink
added tests for mwf
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Shvedun committed Aug 22, 2024
1 parent 5455b2b commit f2ede6e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 36 deletions.
77 changes: 75 additions & 2 deletions clio.tests/Command/ManageWindowsFeaturesCommandTestFixture.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -47,4 +51,73 @@ public void InstallComponent_Calls_WindowsFeatureManager(string actionName){

}

}
[Test, Category("Unit")]
public void GetMissedComponents_ShouldInstallMissedComponents() {
// Arrange
var existingComponents = new List<WindowsFeature> {
new WindowsFeature { Name = "Feature3", Installed = false },
new WindowsFeature { Name = "Feature4", Installed = false }
};

IWorkingDirectoriesProvider wp = Substitute.For<IWorkingDirectoriesProvider>();
IWindowsFeatureProvider windowsFeatureProvider = Substitute.For<IWindowsFeatureProvider>();
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<WindowsFeature> {
new WindowsFeature { Name = "Feature1", Installed = true },
new WindowsFeature { Name = "Feature2", Installed = true }
};

IWorkingDirectoriesProvider wp = Substitute.For<IWorkingDirectoriesProvider>();
IWindowsFeatureProvider windowsFeatureProvider = Substitute.For<IWindowsFeatureProvider>();
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<WindowsFeature> {
new WindowsFeature { Name = "Feature1", Installed = true },
new WindowsFeature { Name = "Feature2", Installed = true }
};

IWorkingDirectoriesProvider wp = Substitute.For<IWorkingDirectoriesProvider>();
IWindowsFeatureProvider windowsFeatureProvider = Substitute.For<IWindowsFeatureProvider>();
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();

}

}

60 changes: 26 additions & 34 deletions clio/Command/WindowsFeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<string> RequirmentNETFrameworkFeatures {
get { return File.ReadAllLines(RequirmentNETFrameworkFeaturesFilePaths).ToList(); }
private IEnumerable<string> _requirmentNETFrameworkFeatures;

public IEnumerable<string> RequirmentNETFrameworkFeatures
{
get
{
if (_requirmentNETFrameworkFeatures == null) {
_requirmentNETFrameworkFeatures = File.ReadAllLines(RequirmentNETFrameworkFeaturesFilePaths);
}
return _requirmentNETFrameworkFeatures;
}
set
{
_requirmentNETFrameworkFeatures = value;
}
}


public List<WindowsFeature> GetMissedComponents() {
var missedComponents = new List<WindowsFeature>();
foreach (var item in RequirmentNETFrameworkFeatures) {
Expand Down Expand Up @@ -87,9 +103,9 @@ public void UninstallFeature(string featureName) {

public void InstallMissingFeatures(){
List<WindowsFeature> 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);
Expand Down Expand Up @@ -136,41 +152,17 @@ private void SetFeatureState(string featureName, bool state) {
}
}

private List<string> _windowsActiveFeatures;
private List<string> windowsActiveFeatures {
private IEnumerable<string> _windowsActiveFeatures;
private IEnumerable<string> windowsActiveFeatures {
get {
if (_windowsActiveFeatures == null) {
var features = new List<string>();
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<WindowsFeature> GetWindowsFeatures() {
var features = new List<WindowsFeature>();
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;
}

0 comments on commit f2ede6e

Please sign in to comment.