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

Commit

Permalink
Browse files Browse the repository at this point in the history
…l-Tool into dev
  • Loading branch information
J0seph0 committed Mar 9, 2024
2 parents 840e195 + 568a6cc commit f7f19d7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 32 deletions.
68 changes: 52 additions & 16 deletions ActiveDirectoryQuerier.Tests/AppConsoleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,12 +52,32 @@ public void Append_AppendsStringToConsole_SuccessfullyAppended()
{
// Arrange
AppConsole appConsole = new();
string output = "Output";
const string output = "Output";

// Act
appConsole.Append(output);

// 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);
}
}
13 changes: 11 additions & 2 deletions ActiveDirectoryQuerier/AppConsole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.IO;

namespace ActiveDirectoryQuerier;

Expand All @@ -14,8 +15,7 @@ public sealed class AppConsole : INotifyPropertyChanged
/// Gets or sets the console output.
/// </summary>
/// <important>
/// Do not set this property directly. Use the <see cref="Append"/> 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.
/// </important>
public string ConsoleOutput
{
Expand Down Expand Up @@ -56,6 +56,15 @@ public void Append(string outputText)
ConsoleOutput += outputText;
}

/// <summary>
/// Exports the console output to a text file.
/// </summary>
/// <param name="filePath">The file path to export the console output to.</param>
public void ExportToText(string filePath = "output.txt")
{
File.WriteAllText(filePath, ConsoleOutput);
}

/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions ActiveDirectoryQuerier/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="Add New Command Slot"
Command="{Binding AddCommandComboBoxRelay}"
Expand All @@ -160,6 +161,9 @@
<Button Content="Clear Console Output"
Command="{Binding ClearConsoleOutputRelay}"
Grid.Column="6" />
<Button Content="Clear Query"
Command="{Binding ClearQueryBuilderRelay}"
Grid.Column="7" />
</Grid>
<!-- GridSplitter for resizing console area in the second row -->
<GridSplitter Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Center"
Expand Down
33 changes: 19 additions & 14 deletions ActiveDirectoryQuerier/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using ActiveDirectoryQuerier.PowerShell;
using Microsoft.Win32;
Expand Down Expand Up @@ -208,12 +207,12 @@ public Command? SelectedComboBoxCommand
/// Command to output to a csv when executing
/// </summary>
public ICommand OutputToCsvFileRelay { get; }

/// <summary>
/// Command to export the console output to a text file.
/// </summary>
public ICommand ExportConsoleOutputRelay { get; }

/// <summary>
/// Command to clear the console output.
/// </summary>
Expand Down Expand Up @@ -278,7 +277,7 @@ public MainWindowViewModel()
Collection<PSObject> ReturnValue results = powerShell.ExecuteCommand(command);
}*/

private void ClearConsoleOutput(object _)
{
if (PowerShellOutput.ConsoleOutput.Length == 0)
Expand All @@ -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)
{
Expand Down Expand Up @@ -603,15 +602,22 @@ private async void OutputToCsvFileAsync(object _)
await File.WriteAllTextAsync(filePath, csv.ToString());
}
}

/// <summary>
/// Adds a new command selection ComboBox to the UI.
/// This method is for getting the currently selected command at anytime.
/// </summary>
/// <param name="_">This is the object that the command is bound to.</param>
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);
}
}

/// <summary>
Expand Down Expand Up @@ -715,7 +721,6 @@ private void RemoveParameterComboBox(object _)
/// <param name="commandParameter">This is not used but necessary for the relay command</param>
private void SaveCustomQueries(object commandParameter)
{

if (SelectedComboBoxCommand is null)
{
Trace.WriteLine("No command selected.");
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f7f19d7

Please sign in to comment.