This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from StrangeRanger/dev
Improved tests and coverage, and some refactoring
- Loading branch information
Showing
26 changed files
with
714 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
FAFB-PowerShell-Tool.Tests/FAFB-PowerShell-Tool.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
global using Xunit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
|
||
|
||
|
||
} |
Oops, something went wrong.