From e325f3b615539cdd894cd953943dfc921b567e55 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Jun 2024 15:53:16 +0200 Subject: [PATCH] - added tests for git init - improve context testing --- .../Abstractions/IGitClient.cs | 6 +- .../Nox.Cli.Plugin.Git/GitAdd_v1.cs | 13 ++- .../Nox.Cli.Plugin.Git/GitClient.cs | 41 +++++-- .../Nox.Cli.Plugin.Git/GitCommit_v1.cs | 14 ++- .../Nox.Cli.Plugin.Git/GitInit_v1.cs | 14 ++- .../Nox.Cli.Plugin.Git/GitInitialize_v1.cs | 17 ++- src/Nox.Cli.sln | 14 +++ src/Nox.Cli/Actions/NoxWorkflowContext.cs | 5 + src/Nox.Cli/Nox.Cli.csproj | 6 + src/Nox.Cli/Properties/launchSettings.json | 2 +- .../TestHelpers}/FileHelpers.cs | 2 +- src/TestHelpers/TestHelpers.csproj | 9 ++ tests/Plugin.File.Tests/CopyFileTests.cs | 7 +- tests/Plugin.File.Tests/CopyFolderTests.cs | 3 +- tests/Plugin.File.Tests/FolderRenameTests.cs | 3 +- tests/Plugin.File.Tests/Helpers.cs | 37 ------- .../Plugin.File.Tests.csproj | 1 + tests/Plugin.Git.Tests/InitTests.cs | 103 ++++++++++++++++++ .../Plugin.Git.Tests/Plugin.Git.Tests.csproj | 43 ++++++++ tests/Plugin.Git.Tests/files/repo/sample.txt | 5 + .../Plugin.Powershell.Tests.csproj | 1 + tests/Plugin.Powershell.Tests/PwshTests.cs | 1 + 22 files changed, 279 insertions(+), 68 deletions(-) rename {tests/Plugin.Powershell.Tests => src/TestHelpers}/FileHelpers.cs (95%) create mode 100644 src/TestHelpers/TestHelpers.csproj delete mode 100644 tests/Plugin.File.Tests/Helpers.cs create mode 100644 tests/Plugin.Git.Tests/InitTests.cs create mode 100644 tests/Plugin.Git.Tests/Plugin.Git.Tests.csproj create mode 100644 tests/Plugin.Git.Tests/files/repo/sample.txt diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/Abstractions/IGitClient.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/Abstractions/IGitClient.cs index 61bc624..a4d524c 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/Abstractions/IGitClient.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/Abstractions/IGitClient.cs @@ -2,7 +2,7 @@ namespace Nox.Cli.Plugin.Git.Abstractions; public interface IGitClient { - Task Init(string branchName = "main"); - Task Add(string filePattern); - Task Commit(string message); + Task Init(string branchName = "main", bool suppressWarnings = false); + Task Add(string filePattern, bool suppressWarnings = false); + Task Commit(string message, bool suppressWarnings = false); } \ No newline at end of file diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitAdd_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitAdd_v1.cs index 7238e33..14f2470 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitAdd_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitAdd_v1.cs @@ -27,6 +27,12 @@ public NoxActionMetaData Discover() Description = "The name of the file to add, or a patten of files to add", Default = "*", IsRequired = false + }, + ["suppress-warnings"] = new NoxActionInput { + Id = "suppress-warnings", + Description = "Indicate whether the plugin should ignore warnings.", + Default = false, + IsRequired = false } }, @@ -43,11 +49,13 @@ public NoxActionMetaData Discover() private string? _path; private string? _filePattern; + private bool? _suppressWarnings; public Task BeginAsync(IDictionary inputs) { _path = inputs.Value("path"); _filePattern = inputs.ValueOrDefault("file-pattern", this); + _suppressWarnings = inputs.ValueOrDefault("suppress-warnings", this); return Task.CompletedTask; } @@ -58,7 +66,8 @@ public async Task> ProcessAsync(INoxWorkflowContext ctx.SetState(ActionState.Error); if (string.IsNullOrEmpty(_path) || - string.IsNullOrEmpty(_filePattern)) + string.IsNullOrEmpty(_filePattern) || + _suppressWarnings == null) { ctx.SetErrorMessage("The Git add action was not initialized"); } @@ -74,7 +83,7 @@ public async Task> ProcessAsync(INoxWorkflowContext else { var client = new GitClient(fullPath); - var response = await client.Add(_filePattern); + var response = await client.Add(_filePattern, _suppressWarnings.Value); if (response.Status == GitCommandStatus.Error) { ctx.SetState(ActionState.Error); diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitClient.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitClient.cs index f3cbf8c..79b418b 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitClient.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitClient.cs @@ -13,7 +13,7 @@ public GitClient(string workingDirectory) //Verify that git is installed try { - var response = ExecuteAsync("--version").Result; + var response = ExecuteAsync("--version", false).Result; if (response.Status == GitCommandStatus.Error) { throw new Exception("Git executable not found!"); @@ -26,12 +26,12 @@ public GitClient(string workingDirectory) } - public async Task Init(string branchName = "main") + public async Task Init(string branchName = "main", bool suppressWarnings = false) { var response = new GitResponse(); try { - return await ExecuteAsync($"init -b {branchName}"); + return await ExecuteAsync($"init -b {branchName}", suppressWarnings); } catch (Exception ex) { @@ -41,12 +41,12 @@ public async Task Init(string branchName = "main") return response; } - public async Task Add(string filePattern) + public async Task Add(string filePattern, bool suppressWarnings = false) { var response = new GitResponse(); try { - return await ExecuteAsync($"add --all {filePattern}"); + return await ExecuteAsync($"add --all {filePattern}", suppressWarnings); } catch (Exception ex) { @@ -56,12 +56,12 @@ public async Task Add(string filePattern) return response; } - public async Task Commit(string message) + public async Task Commit(string message, bool suppressWarnings = false) { var response = new GitResponse(); try { - return await ExecuteAsync($"commit -m \"{message}\""); + return await ExecuteAsync($"commit -m \"{message}\"", suppressWarnings); } catch (Exception ex) { @@ -71,7 +71,7 @@ public async Task Commit(string message) return response; } - private async Task ExecuteAsync(string arguments) + private async Task ExecuteAsync(string arguments, bool suppressWarnings) { var response = new GitResponse(); var processInfo = GetProcessStartInfo(arguments); @@ -79,7 +79,30 @@ private async Task ExecuteAsync(string arguments) process.StartInfo = processInfo; process.Start(); var output = await process.StandardOutput.ReadToEndAsync(); - var error = await process.StandardError.ReadToEndAsync(); + var errors = new List(); + var errorLine = await process.StandardError.ReadLineAsync(); + while (!string.IsNullOrWhiteSpace(errorLine)) + { + if (errorLine.StartsWith("warning", StringComparison.InvariantCultureIgnoreCase)) + { + if (!suppressWarnings) errors.Add(errorLine); + } + else + { + errors.Add(errorLine); + } + + errorLine = await process.StandardError.ReadLineAsync(); + } + + var error = ""; + if (errors.Count != 0) + { + foreach (var item in errors) + { + error += item + Environment.NewLine; + } + } await process.WaitForExitAsync(); if (!string.IsNullOrWhiteSpace(error)) { diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitCommit_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitCommit_v1.cs index ffcd446..563c9c8 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitCommit_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitCommit_v1.cs @@ -21,12 +21,17 @@ public NoxActionMetaData Discover() Default = string.Empty, IsRequired = true }, - ["commit-message"] = new NoxActionInput { Id = "commit-message", Description = "The message to associate with the commit.", Default = string.Empty, IsRequired = true + }, + ["suppress-warnings"] = new NoxActionInput { + Id = "suppress-warnings", + Description = "Indicate whether the plugin should ignore warnings.", + Default = false, + IsRequired = false } }, @@ -43,11 +48,13 @@ public NoxActionMetaData Discover() private string? _path; private string? _message; + private bool? _suppressWarnings; public Task BeginAsync(IDictionary inputs) { _path = inputs.Value("path"); _message = inputs.Value("commit-message"); + _suppressWarnings = inputs.ValueOrDefault("suppress-warnings", this); return Task.CompletedTask; } @@ -58,7 +65,8 @@ public async Task> ProcessAsync(INoxWorkflowContext ctx.SetState(ActionState.Error); if (string.IsNullOrEmpty(_path) || - string.IsNullOrEmpty(_message)) + string.IsNullOrEmpty(_message) || + _suppressWarnings == null) { ctx.SetErrorMessage("The Git commit action was not initialized"); } @@ -74,7 +82,7 @@ public async Task> ProcessAsync(INoxWorkflowContext else { var client = new GitClient(fullPath); - var response = await client.Commit(_message); + var response = await client.Commit(_message, _suppressWarnings.Value); if (response.Status == GitCommandStatus.Error) { ctx.SetState(ActionState.Error); diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInit_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInit_v1.cs index 7879fc3..487fd20 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInit_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInit_v1.cs @@ -21,12 +21,17 @@ public NoxActionMetaData Discover() Default = string.Empty, IsRequired = true }, - ["branch-name"] = new NoxActionInput { Id = "branch-name", Description = "The name of the default branch to create. Defaults to main", Default = "main", IsRequired = false + }, + ["suppress-warnings"] = new NoxActionInput { + Id = "suppress-warnings", + Description = "Indicate whether the plugin should ignore warnings.", + Default = false, + IsRequired = false } }, @@ -43,11 +48,13 @@ public NoxActionMetaData Discover() private string? _path; private string? _branchName; + private bool? _suppressWarnings; public Task BeginAsync(IDictionary inputs) { _path = inputs.Value("path"); _branchName = inputs.ValueOrDefault("branch-name", this); + _suppressWarnings = inputs.ValueOrDefault("suppress-warnings", this); return Task.CompletedTask; } @@ -58,7 +65,8 @@ public async Task> ProcessAsync(INoxWorkflowContext ctx.SetState(ActionState.Error); if (string.IsNullOrEmpty(_path) || - string.IsNullOrEmpty(_branchName)) + string.IsNullOrEmpty(_branchName) || + _suppressWarnings == null) { ctx.SetErrorMessage("The Git init action was not initialized"); } @@ -74,7 +82,7 @@ public async Task> ProcessAsync(INoxWorkflowContext else { var client = new GitClient(fullPath); - var response = await client.Init(_branchName); + var response = await client.Init(_branchName, _suppressWarnings.Value); if (response.Status == GitCommandStatus.Error) { ctx.SetState(ActionState.Error); diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInitialize_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInitialize_v1.cs index 2edfbaf..ce1400b 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInitialize_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInitialize_v1.cs @@ -38,6 +38,12 @@ public NoxActionMetaData Discover() Description = "The message to associate with the commit.", Default = "Initial Commit", IsRequired = false + }, + ["suppress-warnings"] = new NoxActionInput { + Id = "suppress-warnings", + Description = "Indicate whether the plugin should ignore warnings.", + Default = false, + IsRequired = false } }, @@ -56,6 +62,7 @@ public NoxActionMetaData Discover() private string? _branchName; private string? _filePattern; private string? _message; + private bool? _suppressWarnings; public Task BeginAsync(IDictionary inputs) { @@ -63,6 +70,7 @@ public Task BeginAsync(IDictionary inputs) _branchName = inputs.ValueOrDefault("branch-name", this); _filePattern = inputs.ValueOrDefault("file-pattern", this); _message = inputs.ValueOrDefault("commit-message", this); + _suppressWarnings = inputs.ValueOrDefault("suppress-warnings", this); return Task.CompletedTask; } @@ -75,7 +83,8 @@ public async Task> ProcessAsync(INoxWorkflowContext if (string.IsNullOrEmpty(_path) || string.IsNullOrEmpty(_branchName) || string.IsNullOrEmpty(_filePattern) || - string.IsNullOrEmpty(_message)) + string.IsNullOrEmpty(_message) || + _suppressWarnings == null) { ctx.SetErrorMessage("The Git initialize action was not initialized"); } @@ -91,7 +100,7 @@ public async Task> ProcessAsync(INoxWorkflowContext else { var client = new GitClient(fullPath); - var response = await client.Init(_branchName); + var response = await client.Init(_branchName, _suppressWarnings.Value); if (response.Status == GitCommandStatus.Error) { ctx.SetState(ActionState.Error); @@ -99,7 +108,7 @@ public async Task> ProcessAsync(INoxWorkflowContext } else { - response = await client.Add(_filePattern); + response = await client.Add(_filePattern, _suppressWarnings.Value); if (response.Status == GitCommandStatus.Error) { ctx.SetState(ActionState.Error); @@ -107,7 +116,7 @@ public async Task> ProcessAsync(INoxWorkflowContext } else { - response = await client.Commit(_message); + response = await client.Commit(_message, _suppressWarnings.Value); if (response.Status == GitCommandStatus.Error) { ctx.SetState(ActionState.Error); diff --git a/src/Nox.Cli.sln b/src/Nox.Cli.sln index 3f7ff0a..d5c0b82 100755 --- a/src/Nox.Cli.sln +++ b/src/Nox.Cli.sln @@ -79,6 +79,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugin.Powershell.Tests", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nox.Cli.Plugin.Git", "Nox.Cli.Plugins\Nox.Cli.Plugin.Git\Nox.Cli.Plugin.Git.csproj", "{45F0637F-945A-42BD-8A2D-5FCFFDA741C8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugin.Git.Tests", "..\tests\Plugin.Git.Tests\Plugin.Git.Tests.csproj", "{8309A464-7247-4939-AE15-C59B5EC3D516}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestHelpers", "TestHelpers\TestHelpers.csproj", "{413D3BDF-2572-4DD0-A2F7-5FBEFD9DAC19}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -209,6 +213,14 @@ Global {45F0637F-945A-42BD-8A2D-5FCFFDA741C8}.Debug|Any CPU.Build.0 = Debug|Any CPU {45F0637F-945A-42BD-8A2D-5FCFFDA741C8}.Release|Any CPU.ActiveCfg = Release|Any CPU {45F0637F-945A-42BD-8A2D-5FCFFDA741C8}.Release|Any CPU.Build.0 = Release|Any CPU + {8309A464-7247-4939-AE15-C59B5EC3D516}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8309A464-7247-4939-AE15-C59B5EC3D516}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8309A464-7247-4939-AE15-C59B5EC3D516}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8309A464-7247-4939-AE15-C59B5EC3D516}.Release|Any CPU.Build.0 = Release|Any CPU + {413D3BDF-2572-4DD0-A2F7-5FBEFD9DAC19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {413D3BDF-2572-4DD0-A2F7-5FBEFD9DAC19}.Debug|Any CPU.Build.0 = Debug|Any CPU + {413D3BDF-2572-4DD0-A2F7-5FBEFD9DAC19}.Release|Any CPU.ActiveCfg = Release|Any CPU + {413D3BDF-2572-4DD0-A2F7-5FBEFD9DAC19}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -246,6 +258,8 @@ Global {66B69D09-2AD6-4D55-9A71-6DA593BDF170} = {BDFC35C7-EFDC-4502-B69B-45ECB3C805CE} {5469BDB7-DBFC-4C9E-A8CF-36AA2D7D7FE5} = {BDFC35C7-EFDC-4502-B69B-45ECB3C805CE} {45F0637F-945A-42BD-8A2D-5FCFFDA741C8} = {E3FA4748-9B96-42E7-BDE8-1F08AA8AD4D9} + {8309A464-7247-4939-AE15-C59B5EC3D516} = {BDFC35C7-EFDC-4502-B69B-45ECB3C805CE} + {413D3BDF-2572-4DD0-A2F7-5FBEFD9DAC19} = {70B359E7-710C-4583-99A0-1F59333C3AD1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B440C24E-F11D-4F28-9EE5-F7AB214679E6} diff --git a/src/Nox.Cli/Actions/NoxWorkflowContext.cs b/src/Nox.Cli/Actions/NoxWorkflowContext.cs index d7cfde4..afbfac6 100755 --- a/src/Nox.Cli/Actions/NoxWorkflowContext.cs +++ b/src/Nox.Cli/Actions/NoxWorkflowContext.cs @@ -432,6 +432,11 @@ private string MaskSecretsInDisplayText(string input) } return result; } + + internal void SetCurrentActionForTests() + { + _currentAction = new NoxAction(); + } } diff --git a/src/Nox.Cli/Nox.Cli.csproj b/src/Nox.Cli/Nox.Cli.csproj index 6e32557..2f1022d 100644 --- a/src/Nox.Cli/Nox.Cli.csproj +++ b/src/Nox.Cli/Nox.Cli.csproj @@ -73,4 +73,10 @@ + + + + <_Parameter1>Plugin.Git.Tests + + \ No newline at end of file diff --git a/src/Nox.Cli/Properties/launchSettings.json b/src/Nox.Cli/Properties/launchSettings.json index e712be3..0782971 100755 --- a/src/Nox.Cli/Properties/launchSettings.json +++ b/src/Nox.Cli/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "Nox.Cli": { "commandName": "Project", - "commandLineArgs": "new solution --offline", + "commandLineArgs": "new solution", "workingDirectory": "/home/jan/demo/CliDemo", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/tests/Plugin.Powershell.Tests/FileHelpers.cs b/src/TestHelpers/FileHelpers.cs similarity index 95% rename from tests/Plugin.Powershell.Tests/FileHelpers.cs rename to src/TestHelpers/FileHelpers.cs index 2bbdcb0..2613e84 100644 --- a/tests/Plugin.Powershell.Tests/FileHelpers.cs +++ b/src/TestHelpers/FileHelpers.cs @@ -1,4 +1,4 @@ -namespace Plugin.Powershell.Tests; +namespace TestHelpers; public static class FileHelpers { diff --git a/src/TestHelpers/TestHelpers.csproj b/src/TestHelpers/TestHelpers.csproj new file mode 100644 index 0000000..3a63532 --- /dev/null +++ b/src/TestHelpers/TestHelpers.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/tests/Plugin.File.Tests/CopyFileTests.cs b/tests/Plugin.File.Tests/CopyFileTests.cs index d6e88c4..9312f1c 100644 --- a/tests/Plugin.File.Tests/CopyFileTests.cs +++ b/tests/Plugin.File.Tests/CopyFileTests.cs @@ -6,6 +6,7 @@ using Nox.Cli.Variables.Secrets; using Nox.Secrets.Abstractions; using Nox.Solution; +using TestHelpers; using Xunit; namespace Plugin.File.Tests; @@ -17,7 +18,7 @@ public async Task Can_copy_a_File_if_destination_folder_does_not_exist() { var path = "./files/copy-file"; //Ensure the target folder does not exist - Helpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); + FileHelpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); var plugin = new FileCopyFile_v1(); var inputs = new Dictionary @@ -42,7 +43,7 @@ public async Task Must_not_copy_if_destination_folder_exists_and_overwrite_not_s { var path = "./files/copy-file"; //Ensure the target folder exists - Helpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); + FileHelpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); Directory.CreateDirectory(Path.Combine(path, "new-target")); await System.IO.File.WriteAllTextAsync(Path.Combine(path, "new-target/Sample.txt"), "Hello World"); @@ -72,7 +73,7 @@ public async Task Can_copy_a_File_if_destination_folder_exists_and_overwrite_spe { var path = "./files/copy-file"; //Ensure the target folder exists - Helpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); + FileHelpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); Directory.CreateDirectory(Path.Combine(path, "new-target")); await System.IO.File.WriteAllTextAsync(Path.Combine(path, "new-target/Sample.txt"), "Hello World"); diff --git a/tests/Plugin.File.Tests/CopyFolderTests.cs b/tests/Plugin.File.Tests/CopyFolderTests.cs index 23820d0..7ab9990 100644 --- a/tests/Plugin.File.Tests/CopyFolderTests.cs +++ b/tests/Plugin.File.Tests/CopyFolderTests.cs @@ -6,6 +6,7 @@ using Nox.Cli.Variables.Secrets; using Nox.Secrets.Abstractions; using Nox.Solution; +using TestHelpers; using Xunit; namespace Plugin.File.Tests; @@ -17,7 +18,7 @@ public async Task Can_copy_a_File_if_destination_folder_does_not_exist() { var path = "./files/copy-folder"; //Ensure the target folder does not exist - Helpers.PurgeFolderRecursive(Path.Combine(path, "target"), true); + FileHelpers.PurgeFolderRecursive(Path.Combine(path, "target"), true); var plugin = new FileCopyFolder_v1(); var inputs = new Dictionary diff --git a/tests/Plugin.File.Tests/FolderRenameTests.cs b/tests/Plugin.File.Tests/FolderRenameTests.cs index 9d0d43a..82808e8 100644 --- a/tests/Plugin.File.Tests/FolderRenameTests.cs +++ b/tests/Plugin.File.Tests/FolderRenameTests.cs @@ -7,6 +7,7 @@ using Nox.Cli.Variables.Secrets; using Nox.Secrets.Abstractions; using Nox.Solution; +using TestHelpers; using Xunit; namespace Plugin.File.Tests; @@ -25,7 +26,7 @@ public async Task Can_Rename_a_Folder() if (Directory.Exists(Path.Combine(path, "Sample-After"))) { - Helpers.PurgeFolderRecursive(Path.Combine(path, "Sample-After"), true); + FileHelpers.PurgeFolderRecursive(Path.Combine(path, "Sample-After"), true); } CopyRecursively(Path.Combine(path, "Sample"), Path.Combine(path, "Sample-Before")); diff --git a/tests/Plugin.File.Tests/Helpers.cs b/tests/Plugin.File.Tests/Helpers.cs deleted file mode 100644 index 69e4018..0000000 --- a/tests/Plugin.File.Tests/Helpers.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Plugin.File.Tests; - -public static class Helpers -{ - public static void PurgeFolderRecursive(string path, bool includeRoot) - { - if (Directory.Exists(path)) - { - PurgeFolder(path); - var di = new DirectoryInfo(path); - - foreach (var dir in di.GetDirectories()) - { - PurgeFolder(dir.FullName); - dir.Delete(true); - } - - if (includeRoot) - { - Directory.Delete(path); - } - } - } - - public static void PurgeFolder(string path) - { - if (Directory.Exists(path)) - { - var di = new DirectoryInfo(path); - - foreach (var file in di.GetFiles()) - { - file.Delete(); - } - } - } -} \ No newline at end of file diff --git a/tests/Plugin.File.Tests/Plugin.File.Tests.csproj b/tests/Plugin.File.Tests/Plugin.File.Tests.csproj index 9eb3c0d..d59605d 100644 --- a/tests/Plugin.File.Tests/Plugin.File.Tests.csproj +++ b/tests/Plugin.File.Tests/Plugin.File.Tests.csproj @@ -45,6 +45,7 @@ + diff --git a/tests/Plugin.Git.Tests/InitTests.cs b/tests/Plugin.Git.Tests/InitTests.cs new file mode 100644 index 0000000..76b6097 --- /dev/null +++ b/tests/Plugin.Git.Tests/InitTests.cs @@ -0,0 +1,103 @@ +using Moq; +using Nox.Cli.Abstractions; +using Nox.Cli.Abstractions.Caching; +using Nox.Cli.Actions; +using Nox.Cli.Configuration; +using Nox.Cli.Plugin.Git; +using Nox.Cli.Variables.Secrets; +using Nox.Secrets.Abstractions; +using Nox.Solution; +using TestHelpers; + +namespace Plugin.Git.Tests; + +public class InitTests +{ + [Fact] + public async Task Can_initialize_a_local_git_repo() + { + var folder = "./files/repo"; + var gitFolder = Path.Combine(folder, ".git"); + FileHelpers.PurgeFolderRecursive(gitFolder, true); + var plugin = new GitInit_v1(); + var inputs = new Dictionary + { + {"path", folder}, + {"branch-name", "test"} + }; + await plugin.BeginAsync(inputs); + var wfConfig = new WorkflowConfiguration(); + var sln = Mock.Of(); + var orgResolver = Mock.Of(); + var cacheMan = Mock.Of(); + var lteConfig = Mock.Of(); + var secretsResolver = Mock.Of(); + var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver); + ctx.SetCurrentActionForTests(); + await plugin.ProcessAsync(ctx); + Assert.Equal(ActionState.Success, ctx.CurrentAction!.State); + Assert.True(Directory.Exists(gitFolder)); + FileHelpers.PurgeFolderRecursive(gitFolder, true); + } + + [Fact] + public async Task Must_get_warning_if_initializing_more_than_once() + { + var folder = "./files/repo"; + var gitFolder = Path.Combine(folder, ".git"); + FileHelpers.PurgeFolderRecursive(gitFolder, true); + var plugin = new GitInit_v1(); + var inputs = new Dictionary + { + {"path", folder}, + {"branch-name", "test"} + }; + await plugin.BeginAsync(inputs); + var wfConfig = new WorkflowConfiguration(); + var sln = Mock.Of(); + var orgResolver = Mock.Of(); + var cacheMan = Mock.Of(); + var lteConfig = Mock.Of(); + var secretsResolver = Mock.Of(); + var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver); + ctx.SetCurrentActionForTests(); + await plugin.ProcessAsync(ctx); + Assert.Equal(ActionState.Success, ctx.CurrentAction!.State); + Assert.True(Directory.Exists(gitFolder)); + await plugin.ProcessAsync(ctx); + Assert.Equal(ActionState.Error, ctx.CurrentAction.State); + Assert.StartsWith("warning: re-init: ignored", ctx.CurrentAction.ErrorMessage); + FileHelpers.PurgeFolderRecursive(gitFolder, true); + } + + [Fact] + public async Task Must_not_get_warning_if_initializing_more_than_once_and_suppress_warnings_on() + { + var folder = "./files/repo"; + var gitFolder = Path.Combine(folder, ".git"); + FileHelpers.PurgeFolderRecursive(gitFolder, true); + var plugin = new GitInit_v1(); + var inputs = new Dictionary + { + {"path", folder}, + {"branch-name", "test"}, + {"suppress-warnings", true} + }; + await plugin.BeginAsync(inputs); + var wfConfig = new WorkflowConfiguration(); + var sln = Mock.Of(); + var orgResolver = Mock.Of(); + var cacheMan = Mock.Of(); + var lteConfig = Mock.Of(); + var secretsResolver = Mock.Of(); + var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver); + ctx.SetCurrentActionForTests(); + await plugin.ProcessAsync(ctx); + Assert.Equal(ActionState.Success, ctx.CurrentAction!.State); + Assert.True(Directory.Exists(gitFolder)); + await plugin.ProcessAsync(ctx); + Assert.Equal(ActionState.Success, ctx.CurrentAction.State); + FileHelpers.PurgeFolderRecursive(gitFolder, true); + } + +} \ No newline at end of file diff --git a/tests/Plugin.Git.Tests/Plugin.Git.Tests.csproj b/tests/Plugin.Git.Tests/Plugin.Git.Tests.csproj new file mode 100644 index 0000000..647184b --- /dev/null +++ b/tests/Plugin.Git.Tests/Plugin.Git.Tests.csproj @@ -0,0 +1,43 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + Always + + + + + + diff --git a/tests/Plugin.Git.Tests/files/repo/sample.txt b/tests/Plugin.Git.Tests/files/repo/sample.txt new file mode 100644 index 0000000..6e93975 --- /dev/null +++ b/tests/Plugin.Git.Tests/files/repo/sample.txt @@ -0,0 +1,5 @@ +The quick brown fox jumps over the lazy dog. +The quick brown fox jumps over the lazy dog. +The quick brown fox jumps over the lazy dog. +The quick brown fox jumps over the lazy dog. +The quick brown fox jumps over the lazy dog. diff --git a/tests/Plugin.Powershell.Tests/Plugin.Powershell.Tests.csproj b/tests/Plugin.Powershell.Tests/Plugin.Powershell.Tests.csproj index 6814fdc..3052888 100644 --- a/tests/Plugin.Powershell.Tests/Plugin.Powershell.Tests.csproj +++ b/tests/Plugin.Powershell.Tests/Plugin.Powershell.Tests.csproj @@ -26,6 +26,7 @@ + diff --git a/tests/Plugin.Powershell.Tests/PwshTests.cs b/tests/Plugin.Powershell.Tests/PwshTests.cs index 9af12ad..bc858f4 100644 --- a/tests/Plugin.Powershell.Tests/PwshTests.cs +++ b/tests/Plugin.Powershell.Tests/PwshTests.cs @@ -6,6 +6,7 @@ using Nox.Cli.Variables.Secrets; using Nox.Secrets.Abstractions; using Nox.Solution; +using TestHelpers; namespace Plugin.Powershell.Tests;