diff --git a/ActiveDirectoryQuerier.Tests/AppConsoleTests.cs b/ActiveDirectoryQuerier.Tests/AppConsoleTests.cs index e95da7f..c934566 100644 --- a/ActiveDirectoryQuerier.Tests/AppConsoleTests.cs +++ b/ActiveDirectoryQuerier.Tests/AppConsoleTests.cs @@ -3,28 +3,44 @@ namespace ActiveDirectoryQuerier.Tests; -public class AppConsoleTests +public class AppConsoleTests : IDisposable { - [Fact] - public void ClearConsole_ClearsConsole_SuccessfullyCleared() + // TODO: Make sure this is how I should perform cleanups... + public void Dispose() + { + if (File.Exists("output.txt")) + { + File.Delete("output.txt"); + } + + if (File.Exists("output.csv")) + { + File.Delete("output.csv"); + } + + GC.SuppressFinalize(this); + } + + private static async Task<(AppConsole, ReturnValues)> ExecuteCommandAsync(Command command) { - // Arrange PowerShellExecutor powerShellExecutor = new(); - Command command = new("Get-Process"); AppConsole appConsole = new(); + ReturnValues result = await powerShellExecutor.ExecuteAsync(command); - // Act - command.Parameters.Add("Name", "explorer"); - // TODO: Maybe implement an interface of PowerShellExecutor and use a mock object to test to prevent execution - // errors. - var result = powerShellExecutor.Execute(command); + appConsole.Append(result.HadErrors ? result.StdErr : result.StdOut); - if (result.HadErrors) - { - throw new Exception("Error occurred while executing command"); - } + return (appConsole, result); + } + + [Fact] + public async Task ClearConsole_ClearsConsole_SuccessfullyCleared() + { + // Arrange + Command command = new("Get-Process"); + command.Parameters.Add("Name", "explorer"); + var (appConsole, _) = await ExecuteCommandAsync(command); - appConsole.Append(result.StdOut); + // Act appConsole.ClearConsole(); // Assert @@ -36,7 +52,7 @@ public void Append_AppendsStringToConsole_SuccessfullyAppended() { // Arrange AppConsole appConsole = new(); - string output = "Output"; + const string output = "Output"; // Act appConsole.Append(output); @@ -44,4 +60,24 @@ public void Append_AppendsStringToConsole_SuccessfullyAppended() // Assert Assert.Equal(output, appConsole.ConsoleOutput); } + + [Fact] + public async Task ExportToText_ExportToText_SuccessfullyExported() + { + // Arrange + Command command = new("Get-Process"); + command.Parameters.Add("Name", "explorer"); + var (appConsole, returnValues) = await ExecuteCommandAsync(command); + + // Act + appConsole.ExportToText(); + string fileContents = await File.ReadAllTextAsync("output.txt"); + + // Assert + Assert.True(File.Exists("output.txt")); + + Assert.Equal(returnValues.HadErrors ? string.Join(Environment.NewLine, returnValues.StdErr) + : string.Join(Environment.NewLine, returnValues.StdOut), + fileContents); + } } diff --git a/ActiveDirectoryQuerier/AppConsole.cs b/ActiveDirectoryQuerier/AppConsole.cs index f702eb2..efb44bf 100644 --- a/ActiveDirectoryQuerier/AppConsole.cs +++ b/ActiveDirectoryQuerier/AppConsole.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.IO; namespace ActiveDirectoryQuerier; @@ -14,8 +15,7 @@ public sealed class AppConsole : INotifyPropertyChanged /// Gets or sets the console output. /// /// - /// Do not set this property directly. Use the method instead. It's public for data binding - /// purposes. + /// Do not set this property directly. Use the Append method instead. It's public for data binding purposes. /// public string ConsoleOutput { @@ -56,6 +56,15 @@ public void Append(string outputText) ConsoleOutput += outputText; } + /// + /// Exports the console output to a text file. + /// + /// The file path to export the console output to. + public void ExportToText(string filePath = "output.txt") + { + File.WriteAllText(filePath, ConsoleOutput); + } + /// /// Raises the PropertyChanged event. /// diff --git a/ActiveDirectoryQuerier/MainWindow.xaml b/ActiveDirectoryQuerier/MainWindow.xaml index 7fda637..30bfaf1 100644 --- a/ActiveDirectoryQuerier/MainWindow.xaml +++ b/ActiveDirectoryQuerier/MainWindow.xaml @@ -138,6 +138,7 @@ +