Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #43 from StrangeRanger/dev
Browse files Browse the repository at this point in the history
Improved tests and coverage, and some refactoring
  • Loading branch information
StrangeRanger authored Jan 17, 2024
2 parents e26d117 + 52dac5c commit 83ad1fb
Show file tree
Hide file tree
Showing 26 changed files with 714 additions and 244 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build-test-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ jobs:

- name: Install .NET Core
uses: actions/setup-dotnet@v3
with:
with:
dotnet-version: '8.0.x'

- name: Install Active Directory Module
run: |
Install-WindowsFeature RSAT-AD-PowerShell
Import-Module ActiveDirectory
- name: Dotnet Restore & Build
run: dotnet build $env:Project_File -c Debug -f net6.0-windows -r win-${{ matrix.targetplatform }}

Expand Down
6 changes: 6 additions & 0 deletions FAFB-PowerShell-Tool.Tests/CustomQueriesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FAFB_PowerShell_Tool.Tests;

public class CustomQueriesTest
{

}
41 changes: 22 additions & 19 deletions FAFB-PowerShell-Tool.Tests/FAFB-PowerShell-Tool.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<RootNamespace>FAFB_PowerShell_Tool.Tests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<RootNamespace>FAFB_PowerShell_Tool.Tests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FAFB-PowerShell-Tool\FAFB-PowerShell-Tool.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FAFB-PowerShell-Tool\FAFB-PowerShell-Tool.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion FAFB-PowerShell-Tool.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using Xunit;
73 changes: 28 additions & 45 deletions FAFB-PowerShell-Tool.Tests/GuiCommandTest.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,49 @@
using FAFB_PowerShell_Tool.PowerShell;
using FAFB_PowerShell_Tool.PowerShell.Commands;

namespace FAFB_PowerShell_Tool.Tests;

[TestClass]
public class GuiCommandTest
{
[TestMethod]
[Fact]
public void CommandNameIsCorrect()
{
InternalCommand internalCommand = new("Get-ADUser");
Assert.AreEqual("Get-ADUser", internalCommand.CommandName);
GuiCommand command = new("Get-ADUser");
Assert.Equal("Get-ADUser", command.CommandName);
}

[TestMethod]
public void CommandNameThrowsArgumentExceptionWhenNull()
{
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(null!));
}

[TestMethod]
public void CommandNameThrowsArgumentExceptionWhenWhitespace()

[Fact]
public void CommandStringGetIsCorrectWhenParametersAreSet()
{
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(""));
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(" "));
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(string.Empty));
GuiCommand command = new("Get-ADUser", new[] {"-Identity", "Test"});
Assert.Equal("Get-ADUser -Identity Test", command.CommandString);
Assert.Equal("-Identity", command.Parameters![0]);
Assert.Equal("Test", command.Parameters![1]);
}

[TestMethod]
[Fact]
public void PossibleParametersThrowsInvalidOperationExceptionWhenEmpty()
{
GuiCommand guiCommand = new("Get-ADUser");
Assert.ThrowsException<InvalidOperationException>(() => guiCommand.PossibleParameters);
}

[TestMethod]
public void PossibleParametersReturnsCorrectly()
{
GuiCommand guiCommand = new("Get-ADUser");
guiCommand.LoadCommandParametersAsync().Wait();
Assert.IsTrue(guiCommand.PossibleParameters.Contains("-Identity"));
Assert.IsTrue(guiCommand.PossibleParameters.Count > 0);
GuiCommand command = new("Get-ADUser");
Assert.Throws<InvalidOperationException>(() => command.PossibleParameters);
}

[TestMethod]
public void PossibleParametersReturnsCorrectlyWhenCalledTwice()
[Fact]
public async Task PossibleParametersReturnsCorrectly()
{
GuiCommand guiCommand = new("Get-ADUser");
guiCommand.LoadCommandParametersAsync().Wait();
guiCommand.LoadCommandParametersAsync().Wait();
Assert.IsTrue(guiCommand.PossibleParameters.Contains("-Identity"));
Assert.IsTrue(guiCommand.PossibleParameters.Count > 0);
Assert.AreEqual(guiCommand.PossibleParameters.Count, guiCommand.PossibleParameters.Distinct().Count());
GuiCommand command = new("Get-ADUser");
await command.LoadCommandParametersAsync();
Assert.Contains("-Identity", command.PossibleParameters);
Assert.True(command.PossibleParameters.Count > 0);
}

[TestMethod]
public void TestParameters()
[Fact]
public async Task PossibleParametersReturnsCorrectlyWhenCalledTwice()
{
GuiCommand guiCommand = new("Get-ADUser")
{
Parameters = new[] {"-Identity", "FAFB-Admin"}
};
Assert.AreEqual(guiCommand.Parameters[0], "-Identity");
Assert.AreEqual(guiCommand.Parameters[1], "FAFB-Admin");
GuiCommand command = new("Get-ADUser");
await command.LoadCommandParametersAsync();
await command.LoadCommandParametersAsync();
Assert.Contains("-Identity", command.PossibleParameters);
Assert.True(command.PossibleParameters.Count > 0);
Assert.Equal(command.PossibleParameters.Count, command.PossibleParameters.Distinct().Count());
}
}
20 changes: 20 additions & 0 deletions FAFB-PowerShell-Tool.Tests/InternalCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FAFB_PowerShell_Tool.PowerShell.Commands;

namespace FAFB_PowerShell_Tool.Tests;

public class InternalCommandTest
{
[Fact]
public void CommandNameIsCorrect()
{
InternalCommand command = new("Get-ADUser");
Assert.Equal("Get-ADUser", command.CommandName);
}

[Fact]
public void CommandStringGetIsCorrectWhenParametersAreSet()
{
InternalCommand command = new("Get-ADUser", new[] {"-Identity", "Test"});
Assert.Equal("Get-ADUser -Identity Test", command.CommandString);
}
}
34 changes: 30 additions & 4 deletions FAFB-PowerShell-Tool.Tests/PowerShellExecutorTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
using FAFB_PowerShell_Tool.PowerShell;
using FAFB_PowerShell_Tool.PowerShell.Commands;
using ArgumentException = System.ArgumentException;

namespace FAFB_PowerShell_Tool.Tests;

[TestClass]
public class PowerShellExecutorTest
{
[TestMethod]
public void ExecuteGeneratesStdOut()
[Fact]
public void ExecuteCommandReturnsAreCorrect()
{

PowerShellExecutor powerShell = new();
ReturnValues values = powerShell.Execute(new InternalCommand("Get-Process"));
Assert.False(values.HadErrors);
Assert.NotEmpty(values.StdOut);
Assert.Empty(values.StdErr);
}

[Fact]
public void ExecuteBadCommandReturnsAreCorrect()
{
PowerShellExecutor powerShell = new();
ReturnValues values = powerShell.Execute(new InternalCommand("BadCommand"));
Assert.True(values.HadErrors);
Assert.Empty(values.StdOut);
Assert.NotEmpty(values.StdErr);
}

[Fact]
public void ExecuteBadInternalCommandThrowsInvalidOperationException()
{
PowerShellExecutor powerShell = new();
Assert.Throws<ArgumentException>(() => powerShell.Execute(new InternalCommand("")));
Assert.Throws<ArgumentException>(() => powerShell.Execute(new InternalCommand(" ")));
Assert.Throws<ArgumentException>(() => powerShell.Execute(new InternalCommand(null!)));
Assert.Throws<ArgumentException>(() => powerShell.Execute(new InternalCommand(string.Empty)));
}

}
36 changes: 36 additions & 0 deletions FAFB-PowerShell-Tool.Tests/SaveOptionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using FAFB_PowerShell_Tool.PowerShell.Commands;
using FAFB_PowerShell_Tool;
using FAFB_PowerShell_Tool.PowerShell;

namespace FAFB_PowerShell_Tool.Tests;

public class SaveOptionsTest
{
[Fact]
public void SaveToCSVAppendsParameter()
{
var saveOptions = new PSSaveOptions();
InternalCommand command = new("Get-ADUser", new[] { "-Identity", "Test" });

InternalCommand returnedCommand = saveOptions.OutputToCSV(command);


Assert.Equal("Get-ADUser -Identity Test | Export-CSV ..\\..\\..\\SavedOutput\\output.csv", returnedCommand.CommandString);
}
[Fact]
public void ExecuteOutputToCSV()
{
PSSaveOptions pssave = new PSSaveOptions();
PowerShellExecutor executor = new PowerShellExecutor();
InternalCommand test_command = new("get-process");
ReturnValues temprv = executor.Execute(pssave.OutputToCSV(test_command));

//Check if a csv was made
Assert.True(File.Exists("..\\..\\..\\SavedOutput\\output.csv"));
//File.Delete("..\\..\\..\\FAFB-PowerShell-Tool\\SavedOutput\\output.csv");
}




}
Loading

0 comments on commit 83ad1fb

Please sign in to comment.