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 @@
+
+
public ICommand OutputToCsvFileRelay { get; }
-
+
///
/// Command to export the console output to a text file.
///
public ICommand ExportConsoleOutputRelay { get; }
-
+
///
/// Command to clear the console output.
///
@@ -278,7 +277,7 @@ public MainWindowViewModel()
Collection ReturnValue results = powerShell.ExecuteCommand(command);
}*/
-
+
private void ClearConsoleOutput(object _)
{
if (PowerShellOutput.ConsoleOutput.Length == 0)
@@ -289,14 +288,14 @@ private void ClearConsoleOutput(object _)
MessageBoxImage.Information);
return;
}
-
+
// Display a gui box confirming if the user wants to confirm the clear
MessageBoxResult result = MessageBox.Show("Are you sure you want to clear the console output?",
"Warning",
MessageBoxButton.YesNo,
MessageBoxImage.Warning,
MessageBoxResult.No);
-
+
// If the user selects yes, clear the console
if (result == MessageBoxResult.Yes)
{
@@ -603,15 +602,22 @@ private async void OutputToCsvFileAsync(object _)
await File.WriteAllTextAsync(filePath, csv.ToString());
}
}
-
+
///
- /// Adds a new command selection ComboBox to the UI.
+ /// This method is for getting the currently selected command at anytime.
///
/// This is the object that the command is bound to.
private void ExportConsoleOutput(object _)
{
- Trace.WriteLine("Not implemented yet.");
- MessageBox.Show("Not implemented yet.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+ SaveFileDialog saveFileDialog = new() { DefaultExt = ".txt", Filter = "Text documents (.txt)|*.txt" };
+
+ bool? result = saveFileDialog.ShowDialog();
+
+ if (result == true)
+ {
+ string filename = saveFileDialog.FileName;
+ PowerShellOutput.ExportToText(filename);
+ }
}
///
@@ -715,7 +721,6 @@ private void RemoveParameterComboBox(object _)
/// This is not used but necessary for the relay command
private void SaveCustomQueries(object commandParameter)
{
-
if (SelectedComboBoxCommand is null)
{
Trace.WriteLine("No command selected.");
@@ -730,7 +735,7 @@ private void SaveCustomQueries(object commandParameter)
{
// CustomQueries.query editingQuery = _customQuery.Queries.Find(item => item == isEditing);
GetCurrentQuery();
- Trace.WriteLine(_customQuery.Queries.IndexOf(_isEditing));
+ Trace.WriteLine(_customQuery.Queries.IndexOf(_isEditing));
_customQuery.Queries[_customQuery.Queries.IndexOf(_isEditing)] = _currentQuery;
_customQuery.SerializeMethod();
_isEditing = null;
@@ -809,9 +814,9 @@ private Button CreateCustomButton(Query? query = null)
MenuItem menuItem1 =
new() { Header = "Execute", Command = ExecuteCommandButtonRelay, CommandParameter = newButton };
- MenuItem outputToCSV = new() { Header = "Output to CSV", Command = OutputToCsvFileRelay };
+ MenuItem outputToCSV = new() { Header = "Output to CSV", Command = OutputToCsvFileRelay };
MenuItem outputToText = new() { Header = "Output to Text", Command = OutputToTextFileRelay };
- MenuItem exportToConsole = new() { Header = "Execute to Console", Command = ExecuteCommandRelay};
+ MenuItem exportToConsole = new() { Header = "Execute to Console", Command = ExecuteCommandRelay };
menuItem1.Items.Add(outputToCSV);
menuItem1.Items.Add(outputToText);