Skip to content

Commit

Permalink
Merge pull request #249 from NoxOrg/feature/git-improve
Browse files Browse the repository at this point in the history
- added tests for git init
  • Loading branch information
jan-schutte committed Jun 11, 2024
2 parents acbe896 + e325f3b commit eb92593
Show file tree
Hide file tree
Showing 22 changed files with 279 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Nox.Cli.Plugin.Git.Abstractions;

public interface IGitClient
{
Task<GitResponse> Init(string branchName = "main");
Task<GitResponse> Add(string filePattern);
Task<GitResponse> Commit(string message);
Task<GitResponse> Init(string branchName = "main", bool suppressWarnings = false);
Task<GitResponse> Add(string filePattern, bool suppressWarnings = false);
Task<GitResponse> Commit(string message, bool suppressWarnings = false);
}
13 changes: 11 additions & 2 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitAdd_v1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},

Expand All @@ -43,11 +49,13 @@ public NoxActionMetaData Discover()

private string? _path;
private string? _filePattern;
private bool? _suppressWarnings;

public Task BeginAsync(IDictionary<string,object> inputs)
{
_path = inputs.Value<string>("path");
_filePattern = inputs.ValueOrDefault<string>("file-pattern", this);
_suppressWarnings = inputs.ValueOrDefault<bool>("suppress-warnings", this);
return Task.CompletedTask;
}

Expand All @@ -58,7 +66,8 @@ public async Task<IDictionary<string, object>> 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");
}
Expand All @@ -74,7 +83,7 @@ public async Task<IDictionary<string, object>> 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);
Expand Down
41 changes: 32 additions & 9 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand All @@ -26,12 +26,12 @@ public GitClient(string workingDirectory)

}

public async Task<GitResponse> Init(string branchName = "main")
public async Task<GitResponse> 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)
{
Expand All @@ -41,12 +41,12 @@ public async Task<GitResponse> Init(string branchName = "main")
return response;
}

public async Task<GitResponse> Add(string filePattern)
public async Task<GitResponse> 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)
{
Expand All @@ -56,12 +56,12 @@ public async Task<GitResponse> Add(string filePattern)
return response;
}

public async Task<GitResponse> Commit(string message)
public async Task<GitResponse> 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)
{
Expand All @@ -71,15 +71,38 @@ public async Task<GitResponse> Commit(string message)
return response;
}

private async Task<GitResponse> ExecuteAsync(string arguments)
private async Task<GitResponse> ExecuteAsync(string arguments, bool suppressWarnings)
{
var response = new GitResponse();
var processInfo = GetProcessStartInfo(arguments);
using var process = new Process();
process.StartInfo = processInfo;
process.Start();
var output = await process.StandardOutput.ReadToEndAsync();
var error = await process.StandardError.ReadToEndAsync();
var errors = new List<string>();
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))
{
Expand Down
14 changes: 11 additions & 3 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitCommit_v1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},

Expand All @@ -43,11 +48,13 @@ public NoxActionMetaData Discover()

private string? _path;
private string? _message;
private bool? _suppressWarnings;

public Task BeginAsync(IDictionary<string,object> inputs)
{
_path = inputs.Value<string>("path");
_message = inputs.Value<string>("commit-message");
_suppressWarnings = inputs.ValueOrDefault<bool>("suppress-warnings", this);
return Task.CompletedTask;
}

Expand All @@ -58,7 +65,8 @@ public async Task<IDictionary<string, object>> 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");
}
Expand All @@ -74,7 +82,7 @@ public async Task<IDictionary<string, object>> 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);
Expand Down
14 changes: 11 additions & 3 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInit_v1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},

Expand All @@ -43,11 +48,13 @@ public NoxActionMetaData Discover()

private string? _path;
private string? _branchName;
private bool? _suppressWarnings;

public Task BeginAsync(IDictionary<string,object> inputs)
{
_path = inputs.Value<string>("path");
_branchName = inputs.ValueOrDefault<string>("branch-name", this);
_suppressWarnings = inputs.ValueOrDefault<bool>("suppress-warnings", this);
return Task.CompletedTask;
}

Expand All @@ -58,7 +65,8 @@ public async Task<IDictionary<string, object>> 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");
}
Expand All @@ -74,7 +82,7 @@ public async Task<IDictionary<string, object>> 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);
Expand Down
17 changes: 13 additions & 4 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.Git/GitInitialize_v1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},

Expand All @@ -56,13 +62,15 @@ public NoxActionMetaData Discover()
private string? _branchName;
private string? _filePattern;
private string? _message;
private bool? _suppressWarnings;

public Task BeginAsync(IDictionary<string,object> inputs)
{
_path = inputs.Value<string>("path");
_branchName = inputs.ValueOrDefault<string>("branch-name", this);
_filePattern = inputs.ValueOrDefault<string>("file-pattern", this);
_message = inputs.ValueOrDefault<string>("commit-message", this);
_suppressWarnings = inputs.ValueOrDefault<bool>("suppress-warnings", this);
return Task.CompletedTask;
}

Expand All @@ -75,7 +83,8 @@ public async Task<IDictionary<string, object>> 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");
}
Expand All @@ -91,23 +100,23 @@ public async Task<IDictionary<string, object>> 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);
ctx.SetErrorMessage(response.Message);
}
else
{
response = await client.Add(_filePattern);
response = await client.Add(_filePattern, _suppressWarnings.Value);
if (response.Status == GitCommandStatus.Error)
{
ctx.SetState(ActionState.Error);
ctx.SetErrorMessage(response.Message);
}
else
{
response = await client.Commit(_message);
response = await client.Commit(_message, _suppressWarnings.Value);
if (response.Status == GitCommandStatus.Error)
{
ctx.SetState(ActionState.Error);
Expand Down
14 changes: 14 additions & 0 deletions src/Nox.Cli.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
5 changes: 5 additions & 0 deletions src/Nox.Cli/Actions/NoxWorkflowContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@ private string MaskSecretsInDisplayText(string input)
}
return result;
}

internal void SetCurrentActionForTests()
{
_currentAction = new NoxAction();
}
}


Expand Down
6 changes: 6 additions & 0 deletions src/Nox.Cli/Nox.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@
<ProjectReference Include="..\Nox.Cli.Shared.DTO\Nox.Cli.Shared.DTO.csproj" />
<ProjectReference Include="..\Nox.Cli.Variables\Nox.Cli.Variables.csproj" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Plugin.Git.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Nox.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Plugin.Powershell.Tests;
namespace TestHelpers;

public static class FileHelpers
{
Expand Down
Loading

0 comments on commit eb92593

Please sign in to comment.