mirror of
https://github.com/actions/runner.git
synced 2026-01-24 05:21:25 +08:00
simplify
This commit is contained in:
@@ -1138,8 +1138,8 @@ namespace GitHub.Runner.Worker.Dap
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
// Return appropriate response format based on input type
|
||||
if (command.WasJsonInput)
|
||||
// Return appropriate response format based on output type
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return CreateSuccessResponse(new EvaluateResponseBody
|
||||
{
|
||||
|
||||
@@ -80,45 +80,78 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
catch (StepCommandException ex)
|
||||
{
|
||||
return ex.ToResult();
|
||||
return FormatErrorResult(ex.ErrorCode, ex.Message, command.Output);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Trace.Error($"Step command failed: {ex}");
|
||||
return StepCommandResult.Fail(StepCommandErrors.InvalidCommand, ex.Message);
|
||||
return FormatErrorResult(StepCommandErrors.InvalidCommand, ex.Message, command.Output);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats an error result based on the requested output format.
|
||||
/// </summary>
|
||||
private StepCommandResult FormatErrorResult(string errorCode, string message, OutputFormat outputFormat)
|
||||
{
|
||||
if (outputFormat == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = false,
|
||||
Error = errorCode,
|
||||
Message = JsonConvert.SerializeObject(new { Success = false, Error = errorCode, Message = message })
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return StepCommandResult.Fail(errorCode, message);
|
||||
}
|
||||
}
|
||||
|
||||
#region Command Handlers
|
||||
|
||||
/// <summary>
|
||||
/// Handles the !step list command.
|
||||
/// Handles the steps list command.
|
||||
/// </summary>
|
||||
private StepCommandResult HandleList(ListCommand command)
|
||||
{
|
||||
var steps = _manipulator.GetAllSteps();
|
||||
var output = FormatStepList(steps, command.Verbose);
|
||||
|
||||
return new StepCommandResult
|
||||
var resultData = new
|
||||
{
|
||||
Success = true,
|
||||
Message = output,
|
||||
Result = new
|
||||
steps = steps.Select(s => new
|
||||
{
|
||||
steps = steps.Select(s => new
|
||||
{
|
||||
index = s.Index,
|
||||
name = s.Name,
|
||||
type = s.Type,
|
||||
typeDetail = s.TypeDetail,
|
||||
status = s.Status.ToString().ToLower(),
|
||||
change = s.Change?.ToString().ToUpper()
|
||||
}).ToList(),
|
||||
totalCount = steps.Count,
|
||||
completedCount = steps.Count(s => s.Status == StepStatus.Completed),
|
||||
pendingCount = steps.Count(s => s.Status == StepStatus.Pending)
|
||||
}
|
||||
index = s.Index,
|
||||
name = s.Name,
|
||||
type = s.Type,
|
||||
typeDetail = s.TypeDetail,
|
||||
status = s.Status.ToString().ToLower(),
|
||||
change = s.Change?.ToString().ToUpper()
|
||||
}).ToList(),
|
||||
totalCount = steps.Count,
|
||||
completedCount = steps.Count(s => s.Status == StepStatus.Completed),
|
||||
pendingCount = steps.Count(s => s.Status == StepStatus.Pending)
|
||||
};
|
||||
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = JsonConvert.SerializeObject(new { Success = true, Result = resultData }),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = FormatStepList(steps, command.Verbose),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -190,7 +223,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the !step add run command.
|
||||
/// Handles the steps add run command.
|
||||
/// </summary>
|
||||
private StepCommandResult HandleAddRun(AddRunCommand command, IExecutionContext jobContext)
|
||||
{
|
||||
@@ -216,26 +249,43 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
Trace.Info($"Added run step '{actionStep.DisplayName}' at position {index}");
|
||||
|
||||
return new StepCommandResult
|
||||
var resultData = new
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Step added at position {index}: {actionStep.DisplayName}",
|
||||
Result = new
|
||||
index,
|
||||
step = new
|
||||
{
|
||||
index,
|
||||
step = new
|
||||
{
|
||||
name = stepInfo.Name,
|
||||
type = stepInfo.Type,
|
||||
typeDetail = stepInfo.TypeDetail,
|
||||
status = stepInfo.Status.ToString().ToLower()
|
||||
}
|
||||
name = stepInfo.Name,
|
||||
type = stepInfo.Type,
|
||||
typeDetail = stepInfo.TypeDetail,
|
||||
status = stepInfo.Status.ToString().ToLower(),
|
||||
change = stepInfo.Change?.ToString().ToUpper()
|
||||
}
|
||||
};
|
||||
|
||||
var textMessage = $"Step added at position {index}: {actionStep.DisplayName}";
|
||||
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = JsonConvert.SerializeObject(new { Success = true, Message = textMessage, Result = resultData }),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = textMessage,
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the !step add uses command with action download integration.
|
||||
/// Handles the steps add uses command with action download integration.
|
||||
/// Downloads the action via IActionManager and handles pre/post steps.
|
||||
/// </summary>
|
||||
private async Task<StepCommandResult> HandleAddUsesAsync(AddUsesCommand command, IExecutionContext jobContext)
|
||||
@@ -380,29 +430,46 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
// - Enforce enterprise policies
|
||||
// For now, allow all actions in prototype
|
||||
|
||||
return new StepCommandResult
|
||||
var resultData = new
|
||||
{
|
||||
Success = true,
|
||||
Message = messageBuilder.ToString(),
|
||||
Result = new
|
||||
index = mainStepIndex,
|
||||
preStepIndex,
|
||||
hasPreStep = preStepIndex.HasValue,
|
||||
step = new
|
||||
{
|
||||
index = mainStepIndex,
|
||||
preStepIndex,
|
||||
hasPreStep = preStepIndex.HasValue,
|
||||
step = new
|
||||
{
|
||||
name = stepInfo.Name,
|
||||
type = stepInfo.Type,
|
||||
typeDetail = stepInfo.TypeDetail,
|
||||
status = stepInfo.Status.ToString().ToLower()
|
||||
},
|
||||
actionDownloaded = !isLocalOrDocker
|
||||
}
|
||||
name = stepInfo.Name,
|
||||
type = stepInfo.Type,
|
||||
typeDetail = stepInfo.TypeDetail,
|
||||
status = stepInfo.Status.ToString().ToLower(),
|
||||
change = stepInfo.Change?.ToString().ToUpper()
|
||||
},
|
||||
actionDownloaded = !isLocalOrDocker
|
||||
};
|
||||
|
||||
var textMessage = messageBuilder.ToString();
|
||||
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = JsonConvert.SerializeObject(new { Success = true, Message = textMessage, Result = resultData }),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = textMessage,
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the !step edit command.
|
||||
/// Handles the steps edit command.
|
||||
/// </summary>
|
||||
private StepCommandResult HandleEdit(EditCommand command)
|
||||
{
|
||||
@@ -529,21 +596,36 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
});
|
||||
|
||||
var changesStr = changes.Count > 0 ? string.Join(", ", changes) : "no changes";
|
||||
|
||||
return new StepCommandResult
|
||||
var resultData = new
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Step {command.Index} updated ({changesStr})",
|
||||
Result = new
|
||||
{
|
||||
index = command.Index,
|
||||
changes
|
||||
}
|
||||
index = command.Index,
|
||||
changes
|
||||
};
|
||||
|
||||
var textMessage = $"Step {command.Index} updated ({changesStr})";
|
||||
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = JsonConvert.SerializeObject(new { Success = true, Message = textMessage, Result = resultData }),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = textMessage,
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the !step remove command.
|
||||
/// Handles the steps remove command.
|
||||
/// </summary>
|
||||
private StepCommandResult HandleRemove(RemoveCommand command)
|
||||
{
|
||||
@@ -556,20 +638,36 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
Trace.Info($"Removed step '{stepName}' from position {command.Index}");
|
||||
|
||||
return new StepCommandResult
|
||||
var resultData = new
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Step {command.Index} removed: {stepName}",
|
||||
Result = new
|
||||
{
|
||||
index = command.Index,
|
||||
removedStep = stepName
|
||||
}
|
||||
index = command.Index,
|
||||
removedStep = stepName
|
||||
};
|
||||
|
||||
var textMessage = $"Step {command.Index} removed: {stepName}";
|
||||
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = JsonConvert.SerializeObject(new { Success = true, Message = textMessage, Result = resultData }),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = textMessage,
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the !step move command.
|
||||
/// Handles the steps move command.
|
||||
/// </summary>
|
||||
private StepCommandResult HandleMove(MoveCommand command)
|
||||
{
|
||||
@@ -582,21 +680,37 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
Trace.Info($"Moved step '{stepName}' from position {command.FromIndex} to {newIndex}");
|
||||
|
||||
return new StepCommandResult
|
||||
var resultData = new
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Step moved from position {command.FromIndex} to {newIndex}: {stepName}",
|
||||
Result = new
|
||||
{
|
||||
fromIndex = command.FromIndex,
|
||||
toIndex = newIndex,
|
||||
stepName
|
||||
}
|
||||
fromIndex = command.FromIndex,
|
||||
toIndex = newIndex,
|
||||
stepName
|
||||
};
|
||||
|
||||
var textMessage = $"Step moved from position {command.FromIndex} to {newIndex}: {stepName}";
|
||||
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = JsonConvert.SerializeObject(new { Success = true, Message = textMessage, Result = resultData }),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = textMessage,
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the !step export command.
|
||||
/// Handles the steps export command.
|
||||
/// Generates YAML output for modified steps with optional change comments.
|
||||
/// </summary>
|
||||
private StepCommandResult HandleExport(ExportCommand command)
|
||||
@@ -616,21 +730,35 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
var yaml = _serializer.ToYaml(toExport, command.WithComments);
|
||||
|
||||
return new StepCommandResult
|
||||
var resultData = new
|
||||
{
|
||||
Success = true,
|
||||
Message = yaml,
|
||||
Result = new
|
||||
{
|
||||
yaml,
|
||||
totalSteps = steps.Count,
|
||||
exportedSteps = toExport.Count(),
|
||||
addedCount = changes.Count(c => c.Type == ChangeType.Added),
|
||||
modifiedCount = changes.Count(c => c.Type == ChangeType.Modified),
|
||||
movedCount = changes.Count(c => c.Type == ChangeType.Moved),
|
||||
removedCount = changes.Count(c => c.Type == ChangeType.Removed)
|
||||
}
|
||||
yaml,
|
||||
totalSteps = steps.Count,
|
||||
exportedSteps = toExport.Count(),
|
||||
addedCount = changes.Count(c => c.Type == ChangeType.Added),
|
||||
modifiedCount = changes.Count(c => c.Type == ChangeType.Modified),
|
||||
movedCount = changes.Count(c => c.Type == ChangeType.Moved),
|
||||
removedCount = changes.Count(c => c.Type == ChangeType.Removed)
|
||||
};
|
||||
|
||||
if (command.Output == OutputFormat.Json)
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = JsonConvert.SerializeObject(new { Success = true, Result = resultData }),
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StepCommandResult
|
||||
{
|
||||
Success = true,
|
||||
Message = yaml,
|
||||
Result = resultData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -2,20 +2,30 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using GitHub.Runner.Common;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for parsing step commands from REPL strings or JSON.
|
||||
/// Output format for step command responses.
|
||||
/// </summary>
|
||||
public enum OutputFormat
|
||||
{
|
||||
/// <summary>Human-readable text output (default)</summary>
|
||||
Text,
|
||||
/// <summary>JSON output for programmatic use</summary>
|
||||
Json
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for parsing step commands from REPL strings.
|
||||
/// </summary>
|
||||
[ServiceLocator(Default = typeof(StepCommandParser))]
|
||||
public interface IStepCommandParser : IRunnerService
|
||||
{
|
||||
/// <summary>
|
||||
/// Parses a command string (REPL or JSON) into a structured StepCommand.
|
||||
/// Parses a command string into a structured StepCommand.
|
||||
/// </summary>
|
||||
/// <param name="input">The input string (e.g., "!step list --verbose" or JSON)</param>
|
||||
/// <param name="input">The input string (e.g., "steps list --verbose")</param>
|
||||
/// <returns>Parsed StepCommand</returns>
|
||||
/// <exception cref="StepCommandException">If parsing fails</exception>
|
||||
StepCommand Parse(string input);
|
||||
@@ -24,7 +34,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
/// Checks if the input is a step command.
|
||||
/// </summary>
|
||||
/// <param name="input">The input string to check</param>
|
||||
/// <returns>True if this is a step command (REPL or JSON format)</returns>
|
||||
/// <returns>True if this is a step command (starts with "steps")</returns>
|
||||
bool IsStepCommand(string input);
|
||||
}
|
||||
|
||||
@@ -36,13 +46,13 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
public abstract class StepCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether the original input was JSON (affects response format).
|
||||
/// Output format for the command response.
|
||||
/// </summary>
|
||||
public bool WasJsonInput { get; set; }
|
||||
public OutputFormat Output { get; set; } = OutputFormat.Text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// !step list [--verbose]
|
||||
/// steps list [--verbose]
|
||||
/// </summary>
|
||||
public class ListCommand : StepCommand
|
||||
{
|
||||
@@ -50,7 +60,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// !step add run "script" [options]
|
||||
/// steps add run "script" [options]
|
||||
/// </summary>
|
||||
public class AddRunCommand : StepCommand
|
||||
{
|
||||
@@ -66,7 +76,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// !step add uses "action@ref" [options]
|
||||
/// steps add uses "action@ref" [options]
|
||||
/// </summary>
|
||||
public class AddUsesCommand : StepCommand
|
||||
{
|
||||
@@ -81,7 +91,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// !step edit <index> [modifications]
|
||||
/// steps edit <index> [modifications]
|
||||
/// </summary>
|
||||
public class EditCommand : StepCommand
|
||||
{
|
||||
@@ -101,7 +111,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// !step remove <index>
|
||||
/// steps remove <index>
|
||||
/// </summary>
|
||||
public class RemoveCommand : StepCommand
|
||||
{
|
||||
@@ -109,7 +119,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// !step move <from> [position options]
|
||||
/// steps move <from> [position options]
|
||||
/// </summary>
|
||||
public class MoveCommand : StepCommand
|
||||
{
|
||||
@@ -118,7 +128,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// !step export [--changes-only] [--with-comments]
|
||||
/// steps export [--changes-only] [--with-comments]
|
||||
/// </summary>
|
||||
public class ExportCommand : StepCommand
|
||||
{
|
||||
@@ -178,7 +188,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Parser implementation for step commands (REPL and JSON formats).
|
||||
/// Parser implementation for step commands.
|
||||
/// </summary>
|
||||
public sealed class StepCommandParser : RunnerService, IStepCommandParser
|
||||
{
|
||||
@@ -194,12 +204,9 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
var trimmed = input.Trim();
|
||||
|
||||
// REPL command format: !step ...
|
||||
if (trimmed.StartsWith("!step", StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
|
||||
// JSON format: {"cmd": "step.*", ...}
|
||||
if (trimmed.StartsWith("{") && trimmed.Contains("\"cmd\"") && trimmed.Contains("\"step."))
|
||||
// Command format: steps ...
|
||||
if (trimmed.StartsWith("steps ", StringComparison.OrdinalIgnoreCase) ||
|
||||
trimmed.Equals("steps", StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
@@ -208,239 +215,9 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
public StepCommand Parse(string input)
|
||||
{
|
||||
var trimmed = input?.Trim() ?? "";
|
||||
|
||||
if (trimmed.StartsWith("{"))
|
||||
{
|
||||
return ParseJsonCommand(trimmed);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ParseReplCommand(trimmed);
|
||||
}
|
||||
return ParseReplCommand(trimmed);
|
||||
}
|
||||
|
||||
#region JSON Parsing
|
||||
|
||||
private StepCommand ParseJsonCommand(string json)
|
||||
{
|
||||
JObject obj;
|
||||
try
|
||||
{
|
||||
obj = JObject.Parse(json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, $"Invalid JSON: {ex.Message}");
|
||||
}
|
||||
|
||||
var cmd = obj["cmd"]?.ToString();
|
||||
if (string.IsNullOrEmpty(cmd))
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, "Missing 'cmd' field in JSON");
|
||||
}
|
||||
|
||||
StepCommand result = cmd switch
|
||||
{
|
||||
"step.list" => ParseJsonListCommand(obj),
|
||||
"step.add" => ParseJsonAddCommand(obj),
|
||||
"step.edit" => ParseJsonEditCommand(obj),
|
||||
"step.remove" => ParseJsonRemoveCommand(obj),
|
||||
"step.move" => ParseJsonMoveCommand(obj),
|
||||
"step.export" => ParseJsonExportCommand(obj),
|
||||
_ => throw new StepCommandException(StepCommandErrors.InvalidCommand, $"Unknown command: {cmd}")
|
||||
};
|
||||
|
||||
result.WasJsonInput = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
private ListCommand ParseJsonListCommand(JObject obj)
|
||||
{
|
||||
return new ListCommand
|
||||
{
|
||||
Verbose = obj["verbose"]?.Value<bool>() ?? false
|
||||
};
|
||||
}
|
||||
|
||||
private StepCommand ParseJsonAddCommand(JObject obj)
|
||||
{
|
||||
var type = obj["type"]?.ToString()?.ToLower();
|
||||
|
||||
if (type == "run")
|
||||
{
|
||||
var script = obj["script"]?.ToString();
|
||||
if (string.IsNullOrEmpty(script))
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, "Missing 'script' field for run step");
|
||||
}
|
||||
|
||||
return new AddRunCommand
|
||||
{
|
||||
Script = script,
|
||||
Name = obj["name"]?.ToString(),
|
||||
Shell = obj["shell"]?.ToString(),
|
||||
WorkingDirectory = obj["workingDirectory"]?.ToString(),
|
||||
Env = ParseJsonDictionary(obj["env"]),
|
||||
Condition = obj["if"]?.ToString(),
|
||||
ContinueOnError = obj["continueOnError"]?.Value<bool>() ?? false,
|
||||
Timeout = obj["timeout"]?.Value<int>(),
|
||||
Position = ParseJsonPosition(obj["position"])
|
||||
};
|
||||
}
|
||||
else if (type == "uses")
|
||||
{
|
||||
var action = obj["action"]?.ToString();
|
||||
if (string.IsNullOrEmpty(action))
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, "Missing 'action' field for uses step");
|
||||
}
|
||||
|
||||
return new AddUsesCommand
|
||||
{
|
||||
Action = action,
|
||||
Name = obj["name"]?.ToString(),
|
||||
With = ParseJsonDictionary(obj["with"]),
|
||||
Env = ParseJsonDictionary(obj["env"]),
|
||||
Condition = obj["if"]?.ToString(),
|
||||
ContinueOnError = obj["continueOnError"]?.Value<bool>() ?? false,
|
||||
Timeout = obj["timeout"]?.Value<int>(),
|
||||
Position = ParseJsonPosition(obj["position"])
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.InvalidType,
|
||||
$"Invalid step type: '{type}'. Must be 'run' or 'uses'.");
|
||||
}
|
||||
}
|
||||
|
||||
private EditCommand ParseJsonEditCommand(JObject obj)
|
||||
{
|
||||
var index = obj["index"]?.Value<int>();
|
||||
if (!index.HasValue)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, "Missing 'index' field for edit command");
|
||||
}
|
||||
|
||||
return new EditCommand
|
||||
{
|
||||
Index = index.Value,
|
||||
Name = obj["name"]?.ToString(),
|
||||
Script = obj["script"]?.ToString(),
|
||||
Action = obj["action"]?.ToString(),
|
||||
Shell = obj["shell"]?.ToString(),
|
||||
WorkingDirectory = obj["workingDirectory"]?.ToString(),
|
||||
Condition = obj["if"]?.ToString(),
|
||||
With = ParseJsonDictionary(obj["with"]),
|
||||
Env = ParseJsonDictionary(obj["env"]),
|
||||
RemoveWith = ParseJsonStringList(obj["removeWith"]),
|
||||
RemoveEnv = ParseJsonStringList(obj["removeEnv"]),
|
||||
ContinueOnError = obj["continueOnError"]?.Value<bool>(),
|
||||
Timeout = obj["timeout"]?.Value<int>()
|
||||
};
|
||||
}
|
||||
|
||||
private RemoveCommand ParseJsonRemoveCommand(JObject obj)
|
||||
{
|
||||
var index = obj["index"]?.Value<int>();
|
||||
if (!index.HasValue)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, "Missing 'index' field for remove command");
|
||||
}
|
||||
|
||||
return new RemoveCommand { Index = index.Value };
|
||||
}
|
||||
|
||||
private MoveCommand ParseJsonMoveCommand(JObject obj)
|
||||
{
|
||||
var from = obj["from"]?.Value<int>();
|
||||
if (!from.HasValue)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, "Missing 'from' field for move command");
|
||||
}
|
||||
|
||||
var position = ParseJsonPosition(obj["position"]);
|
||||
if (position.Type == PositionType.Last)
|
||||
{
|
||||
// Default 'last' is fine for add, but move needs explicit position
|
||||
// unless explicitly set
|
||||
var posObj = obj["position"];
|
||||
if (posObj == null)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError, "Missing 'position' field for move command");
|
||||
}
|
||||
}
|
||||
|
||||
return new MoveCommand
|
||||
{
|
||||
FromIndex = from.Value,
|
||||
Position = position
|
||||
};
|
||||
}
|
||||
|
||||
private ExportCommand ParseJsonExportCommand(JObject obj)
|
||||
{
|
||||
return new ExportCommand
|
||||
{
|
||||
ChangesOnly = obj["changesOnly"]?.Value<bool>() ?? false,
|
||||
WithComments = obj["withComments"]?.Value<bool>() ?? false
|
||||
};
|
||||
}
|
||||
|
||||
private StepPosition ParseJsonPosition(JToken token)
|
||||
{
|
||||
if (token == null || token.Type == JTokenType.Null)
|
||||
return StepPosition.Last();
|
||||
|
||||
if (token.Type == JTokenType.Object)
|
||||
{
|
||||
var obj = (JObject)token;
|
||||
|
||||
if (obj["at"] != null)
|
||||
return StepPosition.At(obj["at"].Value<int>());
|
||||
if (obj["after"] != null)
|
||||
return StepPosition.After(obj["after"].Value<int>());
|
||||
if (obj["before"] != null)
|
||||
return StepPosition.Before(obj["before"].Value<int>());
|
||||
if (obj["first"]?.Value<bool>() == true)
|
||||
return StepPosition.First();
|
||||
if (obj["last"]?.Value<bool>() == true)
|
||||
return StepPosition.Last();
|
||||
}
|
||||
|
||||
return StepPosition.Last();
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ParseJsonDictionary(JToken token)
|
||||
{
|
||||
if (token == null || token.Type != JTokenType.Object)
|
||||
return null;
|
||||
|
||||
var result = new Dictionary<string, string>();
|
||||
foreach (var prop in ((JObject)token).Properties())
|
||||
{
|
||||
result[prop.Name] = prop.Value?.ToString() ?? "";
|
||||
}
|
||||
return result.Count > 0 ? result : null;
|
||||
}
|
||||
|
||||
private List<string> ParseJsonStringList(JToken token)
|
||||
{
|
||||
if (token == null || token.Type != JTokenType.Array)
|
||||
return null;
|
||||
|
||||
var result = new List<string>();
|
||||
foreach (var item in (JArray)token)
|
||||
{
|
||||
var str = item?.ToString();
|
||||
if (!string.IsNullOrEmpty(str))
|
||||
result.Add(str);
|
||||
}
|
||||
return result.Count > 0 ? result : null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region REPL Parsing
|
||||
|
||||
private StepCommand ParseReplCommand(string input)
|
||||
@@ -448,10 +225,10 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
// Tokenize the input, respecting quoted strings
|
||||
var tokens = Tokenize(input);
|
||||
|
||||
if (tokens.Count < 2 || !tokens[0].Equals("!step", StringComparison.OrdinalIgnoreCase))
|
||||
if (tokens.Count < 2 || !tokens[0].Equals("steps", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError,
|
||||
"Invalid command format. Expected: !step <command> [args...]");
|
||||
"Invalid command format. Expected: steps <command> [args...]");
|
||||
}
|
||||
|
||||
var subCommand = tokens[1].ToLower();
|
||||
@@ -510,7 +287,10 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
private ListCommand ParseReplListCommand(List<string> tokens)
|
||||
{
|
||||
var cmd = new ListCommand();
|
||||
// Extract --output flag before processing other options
|
||||
var outputFormat = ExtractOutputFlag(tokens);
|
||||
|
||||
var cmd = new ListCommand { Output = outputFormat };
|
||||
|
||||
for (int i = 2; i < tokens.Count; i++)
|
||||
{
|
||||
@@ -531,36 +311,43 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
private StepCommand ParseReplAddCommand(List<string> tokens)
|
||||
{
|
||||
// Extract --output flag before processing other options
|
||||
var outputFormat = ExtractOutputFlag(tokens);
|
||||
|
||||
if (tokens.Count < 3)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError,
|
||||
"Usage: !step add <run|uses> <script|action> [options]");
|
||||
"Usage: steps add <run|uses> <script|action> [options]");
|
||||
}
|
||||
|
||||
var type = tokens[2].ToLower();
|
||||
|
||||
StepCommand cmd;
|
||||
if (type == "run")
|
||||
{
|
||||
return ParseReplAddRunCommand(tokens);
|
||||
cmd = ParseReplAddRunCommand(tokens);
|
||||
}
|
||||
else if (type == "uses")
|
||||
{
|
||||
return ParseReplAddUsesCommand(tokens);
|
||||
cmd = ParseReplAddUsesCommand(tokens);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.InvalidType,
|
||||
$"Invalid step type: '{type}'. Must be 'run' or 'uses'.");
|
||||
}
|
||||
|
||||
cmd.Output = outputFormat;
|
||||
return cmd;
|
||||
}
|
||||
|
||||
private AddRunCommand ParseReplAddRunCommand(List<string> tokens)
|
||||
{
|
||||
// !step add run "script" [options]
|
||||
// steps add run "script" [options]
|
||||
if (tokens.Count < 4)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError,
|
||||
"Usage: !step add run \"<script>\" [--name \"...\"] [--shell <shell>] [--at|--after|--before <n>]");
|
||||
"Usage: steps add run \"<script>\" [--name \"...\"] [--shell <shell>] [--at|--after|--before <n>]");
|
||||
}
|
||||
|
||||
var cmd = new AddRunCommand
|
||||
@@ -626,11 +413,11 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
private AddUsesCommand ParseReplAddUsesCommand(List<string> tokens)
|
||||
{
|
||||
// !step add uses "action@ref" [options]
|
||||
// steps add uses "action@ref" [options]
|
||||
if (tokens.Count < 4)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError,
|
||||
"Usage: !step add uses <action@ref> [--name \"...\"] [--with key=value] [--at|--after|--before <n>]");
|
||||
"Usage: steps add uses <action@ref> [--name \"...\"] [--with key=value] [--at|--after|--before <n>]");
|
||||
}
|
||||
|
||||
var cmd = new AddUsesCommand
|
||||
@@ -696,11 +483,14 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
private EditCommand ParseReplEditCommand(List<string> tokens)
|
||||
{
|
||||
// !step edit <index> [modifications]
|
||||
// Extract --output flag before processing other options
|
||||
var outputFormat = ExtractOutputFlag(tokens);
|
||||
|
||||
// steps edit <index> [modifications]
|
||||
if (tokens.Count < 3)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError,
|
||||
"Usage: !step edit <index> [--name \"...\"] [--script \"...\"] [--if \"...\"]");
|
||||
"Usage: steps edit <index> [--name \"...\"] [--script \"...\"] [--if \"...\"]");
|
||||
}
|
||||
|
||||
if (!int.TryParse(tokens[2], out var index))
|
||||
@@ -712,6 +502,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
var cmd = new EditCommand
|
||||
{
|
||||
Index = index,
|
||||
Output = outputFormat,
|
||||
With = new Dictionary<string, string>(),
|
||||
Env = new Dictionary<string, string>(),
|
||||
RemoveWith = new List<string>(),
|
||||
@@ -785,11 +576,14 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
private RemoveCommand ParseReplRemoveCommand(List<string> tokens)
|
||||
{
|
||||
// !step remove <index>
|
||||
// Extract --output flag before processing other options
|
||||
var outputFormat = ExtractOutputFlag(tokens);
|
||||
|
||||
// steps remove <index>
|
||||
if (tokens.Count < 3)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError,
|
||||
"Usage: !step remove <index>");
|
||||
"Usage: steps remove <index>");
|
||||
}
|
||||
|
||||
if (!int.TryParse(tokens[2], out var index))
|
||||
@@ -798,16 +592,19 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
$"Invalid index: {tokens[2]}. Must be a number.");
|
||||
}
|
||||
|
||||
return new RemoveCommand { Index = index };
|
||||
return new RemoveCommand { Index = index, Output = outputFormat };
|
||||
}
|
||||
|
||||
private MoveCommand ParseReplMoveCommand(List<string> tokens)
|
||||
{
|
||||
// !step move <from> --to|--after|--before <index>|--first|--last
|
||||
// Extract --output flag before processing other options
|
||||
var outputFormat = ExtractOutputFlag(tokens);
|
||||
|
||||
// steps move <from> --to|--after|--before <index>|--first|--last
|
||||
if (tokens.Count < 3)
|
||||
{
|
||||
throw new StepCommandException(StepCommandErrors.ParseError,
|
||||
"Usage: !step move <from> --to|--after|--before <index>|--first|--last");
|
||||
"Usage: steps move <from> --to|--after|--before <index>|--first|--last");
|
||||
}
|
||||
|
||||
if (!int.TryParse(tokens[2], out var fromIndex))
|
||||
@@ -816,7 +613,7 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
$"Invalid from index: {tokens[2]}. Must be a number.");
|
||||
}
|
||||
|
||||
var cmd = new MoveCommand { FromIndex = fromIndex };
|
||||
var cmd = new MoveCommand { FromIndex = fromIndex, Output = outputFormat };
|
||||
|
||||
// Parse position
|
||||
for (int i = 3; i < tokens.Count; i++)
|
||||
@@ -858,7 +655,10 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
private ExportCommand ParseReplExportCommand(List<string> tokens)
|
||||
{
|
||||
var cmd = new ExportCommand();
|
||||
// Extract --output flag before processing other options
|
||||
var outputFormat = ExtractOutputFlag(tokens);
|
||||
|
||||
var cmd = new ExportCommand { Output = outputFormat };
|
||||
|
||||
for (int i = 2; i < tokens.Count; i++)
|
||||
{
|
||||
@@ -883,6 +683,36 @@ namespace GitHub.Runner.Worker.Dap.StepCommands
|
||||
|
||||
#region Argument Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Extracts and removes the --output flag from tokens, returning the output format.
|
||||
/// Supports: --output json, --output text, -o json, -o text, --output=json, --output=text
|
||||
/// </summary>
|
||||
private OutputFormat ExtractOutputFlag(List<string> tokens)
|
||||
{
|
||||
for (int i = 0; i < tokens.Count; i++)
|
||||
{
|
||||
var token = tokens[i].ToLower();
|
||||
|
||||
if (token == "--output" || token == "-o")
|
||||
{
|
||||
if (i + 1 < tokens.Count)
|
||||
{
|
||||
var format = tokens[i + 1].ToLower();
|
||||
tokens.RemoveAt(i); // Remove flag
|
||||
tokens.RemoveAt(i); // Remove value (now at same index)
|
||||
return format == "json" ? OutputFormat.Json : OutputFormat.Text;
|
||||
}
|
||||
}
|
||||
else if (token.StartsWith("--output="))
|
||||
{
|
||||
var format = token.Substring("--output=".Length);
|
||||
tokens.RemoveAt(i);
|
||||
return format == "json" ? OutputFormat.Json : OutputFormat.Text;
|
||||
}
|
||||
}
|
||||
return OutputFormat.Text;
|
||||
}
|
||||
|
||||
private string GetNextArg(List<string> tokens, ref int index, string optName)
|
||||
{
|
||||
if (index + 1 >= tokens.Count)
|
||||
|
||||
@@ -1,687 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GitHub.Runner.Worker.Dap.StepCommands;
|
||||
using Xunit;
|
||||
|
||||
namespace GitHub.Runner.Common.Tests.Worker.Dap.StepCommands
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for StepCommandParser JSON API functionality.
|
||||
/// Tests the parsing of JSON commands for browser extension integration.
|
||||
/// </summary>
|
||||
public sealed class StepCommandParserJsonL0 : IDisposable
|
||||
{
|
||||
private TestHostContext _hc;
|
||||
private StepCommandParser _parser;
|
||||
|
||||
public StepCommandParserJsonL0()
|
||||
{
|
||||
_hc = new TestHostContext(this);
|
||||
_parser = new StepCommandParser();
|
||||
_parser.Initialize(_hc);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_hc?.Dispose();
|
||||
}
|
||||
|
||||
#region IsStepCommand Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void IsStepCommand_DetectsJsonFormat()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
Assert.True(_parser.IsStepCommand("{\"cmd\":\"step.list\"}"));
|
||||
Assert.True(_parser.IsStepCommand("{\"cmd\": \"step.add\", \"type\": \"run\"}"));
|
||||
Assert.True(_parser.IsStepCommand(" { \"cmd\" : \"step.export\" } "));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void IsStepCommand_RejectsInvalidJson()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
Assert.False(_parser.IsStepCommand("{\"cmd\":\"other.command\"}"));
|
||||
Assert.False(_parser.IsStepCommand("{\"action\":\"step.list\"}"));
|
||||
Assert.False(_parser.IsStepCommand("{\"type\":\"step\"}"));
|
||||
Assert.False(_parser.IsStepCommand("{}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void IsStepCommand_HandlesBothFormats()
|
||||
{
|
||||
// REPL format
|
||||
Assert.True(_parser.IsStepCommand("!step list"));
|
||||
Assert.True(_parser.IsStepCommand("!STEP ADD run \"test\""));
|
||||
|
||||
// JSON format
|
||||
Assert.True(_parser.IsStepCommand("{\"cmd\":\"step.list\"}"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON List Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_ListCommand_Basic()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.list\"}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ListCommand>(command);
|
||||
var listCmd = (ListCommand)command;
|
||||
Assert.False(listCmd.Verbose);
|
||||
Assert.True(listCmd.WasJsonInput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_ListCommand_WithVerbose()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.list\",\"verbose\":true}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var listCmd = Assert.IsType<ListCommand>(command);
|
||||
Assert.True(listCmd.Verbose);
|
||||
Assert.True(listCmd.WasJsonInput);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON Add Run Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_AddRunCommand_Basic()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"run\",\"script\":\"npm test\"}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(command);
|
||||
Assert.Equal("npm test", addCmd.Script);
|
||||
Assert.True(addCmd.WasJsonInput);
|
||||
Assert.Equal(PositionType.Last, addCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_AddRunCommand_AllOptions()
|
||||
{
|
||||
// Arrange
|
||||
var json = @"{
|
||||
""cmd"": ""step.add"",
|
||||
""type"": ""run"",
|
||||
""script"": ""npm run build"",
|
||||
""name"": ""Build App"",
|
||||
""shell"": ""bash"",
|
||||
""workingDirectory"": ""./src"",
|
||||
""if"": ""success()"",
|
||||
""env"": {""NODE_ENV"": ""production"", ""CI"": ""true""},
|
||||
""continueOnError"": true,
|
||||
""timeout"": 30,
|
||||
""position"": {""after"": 3}
|
||||
}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(command);
|
||||
Assert.Equal("npm run build", addCmd.Script);
|
||||
Assert.Equal("Build App", addCmd.Name);
|
||||
Assert.Equal("bash", addCmd.Shell);
|
||||
Assert.Equal("./src", addCmd.WorkingDirectory);
|
||||
Assert.Equal("success()", addCmd.Condition);
|
||||
Assert.NotNull(addCmd.Env);
|
||||
Assert.Equal("production", addCmd.Env["NODE_ENV"]);
|
||||
Assert.Equal("true", addCmd.Env["CI"]);
|
||||
Assert.True(addCmd.ContinueOnError);
|
||||
Assert.Equal(30, addCmd.Timeout);
|
||||
Assert.Equal(PositionType.After, addCmd.Position.Type);
|
||||
Assert.Equal(3, addCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_AddRunCommand_MissingScript_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"run\"}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("script", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON Add Uses Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_AddUsesCommand_Basic()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"uses\",\"action\":\"actions/checkout@v4\"}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddUsesCommand>(command);
|
||||
Assert.Equal("actions/checkout@v4", addCmd.Action);
|
||||
Assert.True(addCmd.WasJsonInput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_AddUsesCommand_AllOptions()
|
||||
{
|
||||
// Arrange
|
||||
var json = @"{
|
||||
""cmd"": ""step.add"",
|
||||
""type"": ""uses"",
|
||||
""action"": ""actions/setup-node@v4"",
|
||||
""name"": ""Setup Node.js"",
|
||||
""with"": {""node-version"": ""20"", ""cache"": ""npm""},
|
||||
""env"": {""NODE_OPTIONS"": ""--max-old-space-size=4096""},
|
||||
""if"": ""always()"",
|
||||
""continueOnError"": false,
|
||||
""timeout"": 10,
|
||||
""position"": {""at"": 2}
|
||||
}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddUsesCommand>(command);
|
||||
Assert.Equal("actions/setup-node@v4", addCmd.Action);
|
||||
Assert.Equal("Setup Node.js", addCmd.Name);
|
||||
Assert.NotNull(addCmd.With);
|
||||
Assert.Equal("20", addCmd.With["node-version"]);
|
||||
Assert.Equal("npm", addCmd.With["cache"]);
|
||||
Assert.NotNull(addCmd.Env);
|
||||
Assert.Equal("--max-old-space-size=4096", addCmd.Env["NODE_OPTIONS"]);
|
||||
Assert.Equal("always()", addCmd.Condition);
|
||||
Assert.False(addCmd.ContinueOnError);
|
||||
Assert.Equal(10, addCmd.Timeout);
|
||||
Assert.Equal(PositionType.At, addCmd.Position.Type);
|
||||
Assert.Equal(2, addCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_AddUsesCommand_MissingAction_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"uses\"}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("action", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_AddCommand_InvalidType_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"invalid\",\"script\":\"echo test\"}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.InvalidType, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON Edit Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_EditCommand_Basic()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.edit\",\"index\":3,\"name\":\"New Name\"}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var editCmd = Assert.IsType<EditCommand>(command);
|
||||
Assert.Equal(3, editCmd.Index);
|
||||
Assert.Equal("New Name", editCmd.Name);
|
||||
Assert.True(editCmd.WasJsonInput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_EditCommand_AllOptions()
|
||||
{
|
||||
// Arrange
|
||||
var json = @"{
|
||||
""cmd"": ""step.edit"",
|
||||
""index"": 4,
|
||||
""name"": ""Updated Step"",
|
||||
""script"": ""npm run test:ci"",
|
||||
""shell"": ""pwsh"",
|
||||
""workingDirectory"": ""./tests"",
|
||||
""if"": ""failure()"",
|
||||
""with"": {""key1"": ""value1""},
|
||||
""env"": {""DEBUG"": ""true""},
|
||||
""removeWith"": [""oldKey""],
|
||||
""removeEnv"": [""OBSOLETE""],
|
||||
""continueOnError"": true,
|
||||
""timeout"": 15
|
||||
}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var editCmd = Assert.IsType<EditCommand>(command);
|
||||
Assert.Equal(4, editCmd.Index);
|
||||
Assert.Equal("Updated Step", editCmd.Name);
|
||||
Assert.Equal("npm run test:ci", editCmd.Script);
|
||||
Assert.Equal("pwsh", editCmd.Shell);
|
||||
Assert.Equal("./tests", editCmd.WorkingDirectory);
|
||||
Assert.Equal("failure()", editCmd.Condition);
|
||||
Assert.NotNull(editCmd.With);
|
||||
Assert.Equal("value1", editCmd.With["key1"]);
|
||||
Assert.NotNull(editCmd.Env);
|
||||
Assert.Equal("true", editCmd.Env["DEBUG"]);
|
||||
Assert.NotNull(editCmd.RemoveWith);
|
||||
Assert.Contains("oldKey", editCmd.RemoveWith);
|
||||
Assert.NotNull(editCmd.RemoveEnv);
|
||||
Assert.Contains("OBSOLETE", editCmd.RemoveEnv);
|
||||
Assert.True(editCmd.ContinueOnError);
|
||||
Assert.Equal(15, editCmd.Timeout);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_EditCommand_MissingIndex_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.edit\",\"name\":\"New Name\"}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("index", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON Remove Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_RemoveCommand()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.remove\",\"index\":5}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var removeCmd = Assert.IsType<RemoveCommand>(command);
|
||||
Assert.Equal(5, removeCmd.Index);
|
||||
Assert.True(removeCmd.WasJsonInput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_RemoveCommand_MissingIndex_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.remove\"}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("index", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON Move Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MoveCommand_After()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.move\",\"from\":5,\"position\":{\"after\":2}}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(command);
|
||||
Assert.Equal(5, moveCmd.FromIndex);
|
||||
Assert.Equal(PositionType.After, moveCmd.Position.Type);
|
||||
Assert.Equal(2, moveCmd.Position.Index);
|
||||
Assert.True(moveCmd.WasJsonInput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MoveCommand_Before()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.move\",\"from\":3,\"position\":{\"before\":5}}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(command);
|
||||
Assert.Equal(3, moveCmd.FromIndex);
|
||||
Assert.Equal(PositionType.Before, moveCmd.Position.Type);
|
||||
Assert.Equal(5, moveCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MoveCommand_First()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.move\",\"from\":5,\"position\":{\"first\":true}}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(command);
|
||||
Assert.Equal(PositionType.First, moveCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MoveCommand_Last()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.move\",\"from\":2,\"position\":{\"last\":true}}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(command);
|
||||
Assert.Equal(PositionType.Last, moveCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MoveCommand_At()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.move\",\"from\":5,\"position\":{\"at\":3}}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(command);
|
||||
Assert.Equal(PositionType.At, moveCmd.Position.Type);
|
||||
Assert.Equal(3, moveCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MoveCommand_MissingFrom_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.move\",\"position\":{\"after\":2}}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("from", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MoveCommand_MissingPosition_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.move\",\"from\":5}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("position", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON Export Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_ExportCommand_Default()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.export\"}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var exportCmd = Assert.IsType<ExportCommand>(command);
|
||||
Assert.False(exportCmd.ChangesOnly);
|
||||
Assert.False(exportCmd.WithComments);
|
||||
Assert.True(exportCmd.WasJsonInput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_ExportCommand_WithOptions()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.export\",\"changesOnly\":true,\"withComments\":true}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var exportCmd = Assert.IsType<ExportCommand>(command);
|
||||
Assert.True(exportCmd.ChangesOnly);
|
||||
Assert.True(exportCmd.WithComments);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JSON Error Handling Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_InvalidJson_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{invalid json}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("Invalid JSON", ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_MissingCmd_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"action\":\"list\"}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("cmd", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_UnknownCommand_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.unknown\"}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.InvalidCommand, ex.ErrorCode);
|
||||
Assert.Contains("unknown", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_EmptyJson_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{}";
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(json));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Position Parsing Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_PositionDefaults_ToLast()
|
||||
{
|
||||
// Arrange - position is optional for add commands
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"run\",\"script\":\"test\"}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(command);
|
||||
Assert.Equal(PositionType.Last, addCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_NullPosition_DefaultsToLast()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"run\",\"script\":\"test\",\"position\":null}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(command);
|
||||
Assert.Equal(PositionType.Last, addCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void ParseJson_EmptyPosition_DefaultsToLast()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.add\",\"type\":\"run\",\"script\":\"test\",\"position\":{}}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(command);
|
||||
Assert.Equal(PositionType.Last, addCmd.Position.Type);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WasJsonInput Flag Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_JsonInput_SetsWasJsonInputTrue()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"cmd\":\"step.list\"}";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(json);
|
||||
|
||||
// Assert
|
||||
Assert.True(command.WasJsonInput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ReplInput_SetsWasJsonInputFalse()
|
||||
{
|
||||
// Arrange
|
||||
var repl = "!step list";
|
||||
|
||||
// Act
|
||||
var command = _parser.Parse(repl);
|
||||
|
||||
// Assert
|
||||
Assert.False(command.WasJsonInput);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
726
src/Test/L0/Worker/Dap/StepCommands/StepCommandParserL0.cs
Normal file
726
src/Test/L0/Worker/Dap/StepCommands/StepCommandParserL0.cs
Normal file
@@ -0,0 +1,726 @@
|
||||
using System;
|
||||
using GitHub.Runner.Worker.Dap.StepCommands;
|
||||
using Xunit;
|
||||
|
||||
namespace GitHub.Runner.Common.Tests.Worker.Dap.StepCommands
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for StepCommandParser.
|
||||
/// Tests parsing of "steps" commands with the --output flag.
|
||||
/// </summary>
|
||||
public sealed class StepCommandParserL0 : IDisposable
|
||||
{
|
||||
private TestHostContext _hc;
|
||||
private StepCommandParser _parser;
|
||||
|
||||
public StepCommandParserL0()
|
||||
{
|
||||
_hc = new TestHostContext(this);
|
||||
_parser = new StepCommandParser();
|
||||
_parser.Initialize(_hc);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_hc?.Dispose();
|
||||
}
|
||||
|
||||
#region IsStepCommand Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void IsStepCommand_DetectsStepsPrefix()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
Assert.True(_parser.IsStepCommand("steps list"));
|
||||
Assert.True(_parser.IsStepCommand("steps add run \"test\""));
|
||||
Assert.True(_parser.IsStepCommand("STEPS LIST")); // case insensitive
|
||||
Assert.True(_parser.IsStepCommand(" steps list ")); // whitespace
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void IsStepCommand_RejectsInvalid()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
Assert.False(_parser.IsStepCommand("step list")); // missing 's'
|
||||
Assert.False(_parser.IsStepCommand("!step list")); // old format
|
||||
Assert.False(_parser.IsStepCommand("stepslist")); // no space
|
||||
Assert.False(_parser.IsStepCommand(""));
|
||||
Assert.False(_parser.IsStepCommand(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void IsStepCommand_AllowsStepsAlone()
|
||||
{
|
||||
// "steps" alone should be detected (even if parsing will fail for lack of subcommand)
|
||||
Assert.True(_parser.IsStepCommand("steps"));
|
||||
Assert.True(_parser.IsStepCommand(" steps "));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Output Format Flag Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ListCommand_WithOutputJson()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list --output json") as ListCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ListCommand_WithOutputText()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list --output text") as ListCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Text, cmd.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ListCommand_DefaultOutputIsText()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list") as ListCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Text, cmd.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddCommand_WithOutputFlag()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"echo test\" --output json") as AddRunCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
Assert.Equal("echo test", cmd.Script);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_OutputFlag_ShortForm()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list -o json") as ListCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_OutputFlag_EqualsForm()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list --output=json") as ListCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_OutputFlag_TextEqualsForm()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list --output=text") as ListCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Text, cmd.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_EditCommand_WithOutputJson()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps edit 3 --name \"New Name\" --output json") as EditCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
Assert.Equal(3, cmd.Index);
|
||||
Assert.Equal("New Name", cmd.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_RemoveCommand_WithOutputJson()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps remove 5 --output json") as RemoveCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
Assert.Equal(5, cmd.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_WithOutputJson()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps move 3 --after 5 --output json") as MoveCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
Assert.Equal(3, cmd.FromIndex);
|
||||
Assert.Equal(PositionType.After, cmd.Position.Type);
|
||||
Assert.Equal(5, cmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ExportCommand_WithOutputJson()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps export --output json") as ExportCommand;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(OutputFormat.Json, cmd.Output);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region List Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ListCommand_Basic()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list");
|
||||
|
||||
// Assert
|
||||
var listCmd = Assert.IsType<ListCommand>(cmd);
|
||||
Assert.False(listCmd.Verbose);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ListCommand_WithVerbose()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list --verbose");
|
||||
|
||||
// Assert
|
||||
var listCmd = Assert.IsType<ListCommand>(cmd);
|
||||
Assert.True(listCmd.Verbose);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ListCommand_WithVerboseShort()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps list -v");
|
||||
|
||||
// Assert
|
||||
var listCmd = Assert.IsType<ListCommand>(cmd);
|
||||
Assert.True(listCmd.Verbose);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Add Run Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_Basic()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"npm test\"");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.Equal("npm test", addCmd.Script);
|
||||
Assert.Equal(PositionType.Last, addCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_AllOptions()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"npm run build\" --name \"Build App\" --shell bash --after 3");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.Equal("npm run build", addCmd.Script);
|
||||
Assert.Equal("Build App", addCmd.Name);
|
||||
Assert.Equal("bash", addCmd.Shell);
|
||||
Assert.Equal(PositionType.After, addCmd.Position.Type);
|
||||
Assert.Equal(3, addCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_WithEnv()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"npm test\" --env NODE_ENV=test --env CI=true");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.NotNull(addCmd.Env);
|
||||
Assert.Equal("test", addCmd.Env["NODE_ENV"]);
|
||||
Assert.Equal("true", addCmd.Env["CI"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_WithContinueOnError()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"npm test\" --continue-on-error");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.True(addCmd.ContinueOnError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_WithTimeout()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"npm test\" --timeout 30");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.Equal(30, addCmd.Timeout);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_PositionFirst()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"echo first\" --first");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.Equal(PositionType.First, addCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_PositionAt()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"echo at\" --at 5");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.Equal(PositionType.At, addCmd.Position.Type);
|
||||
Assert.Equal(5, addCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_PositionBefore()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add run \"echo before\" --before 3");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddRunCommand>(cmd);
|
||||
Assert.Equal(PositionType.Before, addCmd.Position.Type);
|
||||
Assert.Equal(3, addCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddRunCommand_MissingScript_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps add run"));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Add Uses Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddUsesCommand_Basic()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add uses actions/checkout@v4");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddUsesCommand>(cmd);
|
||||
Assert.Equal("actions/checkout@v4", addCmd.Action);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddUsesCommand_AllOptions()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps add uses actions/setup-node@v4 --name \"Setup Node\" --with node-version=20 --with cache=npm");
|
||||
|
||||
// Assert
|
||||
var addCmd = Assert.IsType<AddUsesCommand>(cmd);
|
||||
Assert.Equal("actions/setup-node@v4", addCmd.Action);
|
||||
Assert.Equal("Setup Node", addCmd.Name);
|
||||
Assert.NotNull(addCmd.With);
|
||||
Assert.Equal("20", addCmd.With["node-version"]);
|
||||
Assert.Equal("npm", addCmd.With["cache"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddUsesCommand_MissingAction_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps add uses"));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_AddCommand_InvalidType_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps add invalid \"test\""));
|
||||
Assert.Equal(StepCommandErrors.InvalidType, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Edit Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_EditCommand_Basic()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps edit 3 --name \"New Name\"");
|
||||
|
||||
// Assert
|
||||
var editCmd = Assert.IsType<EditCommand>(cmd);
|
||||
Assert.Equal(3, editCmd.Index);
|
||||
Assert.Equal("New Name", editCmd.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_EditCommand_AllOptions()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps edit 4 --name \"Updated\" --script \"npm test\" --shell pwsh --if \"failure()\"");
|
||||
|
||||
// Assert
|
||||
var editCmd = Assert.IsType<EditCommand>(cmd);
|
||||
Assert.Equal(4, editCmd.Index);
|
||||
Assert.Equal("Updated", editCmd.Name);
|
||||
Assert.Equal("npm test", editCmd.Script);
|
||||
Assert.Equal("pwsh", editCmd.Shell);
|
||||
Assert.Equal("failure()", editCmd.Condition);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_EditCommand_MissingIndex_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps edit --name \"Test\""));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_EditCommand_InvalidIndex_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps edit abc --name \"Test\""));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Remove Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_RemoveCommand()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps remove 5");
|
||||
|
||||
// Assert
|
||||
var removeCmd = Assert.IsType<RemoveCommand>(cmd);
|
||||
Assert.Equal(5, removeCmd.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_RemoveCommand_MissingIndex_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps remove"));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Move Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_After()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps move 5 --after 2");
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(cmd);
|
||||
Assert.Equal(5, moveCmd.FromIndex);
|
||||
Assert.Equal(PositionType.After, moveCmd.Position.Type);
|
||||
Assert.Equal(2, moveCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_Before()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps move 3 --before 5");
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(cmd);
|
||||
Assert.Equal(3, moveCmd.FromIndex);
|
||||
Assert.Equal(PositionType.Before, moveCmd.Position.Type);
|
||||
Assert.Equal(5, moveCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_First()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps move 5 --first");
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(cmd);
|
||||
Assert.Equal(PositionType.First, moveCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_Last()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps move 2 --last");
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(cmd);
|
||||
Assert.Equal(PositionType.Last, moveCmd.Position.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_To()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps move 5 --to 3");
|
||||
|
||||
// Assert
|
||||
var moveCmd = Assert.IsType<MoveCommand>(cmd);
|
||||
Assert.Equal(PositionType.At, moveCmd.Position.Type);
|
||||
Assert.Equal(3, moveCmd.Position.Index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_MissingFrom_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps move --after 2"));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_MoveCommand_MissingPosition_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps move 5"));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Export Command Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ExportCommand_Default()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps export");
|
||||
|
||||
// Assert
|
||||
var exportCmd = Assert.IsType<ExportCommand>(cmd);
|
||||
Assert.False(exportCmd.ChangesOnly);
|
||||
Assert.False(exportCmd.WithComments);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_ExportCommand_WithOptions()
|
||||
{
|
||||
// Arrange & Act
|
||||
var cmd = _parser.Parse("steps export --changes-only --with-comments");
|
||||
|
||||
// Assert
|
||||
var exportCmd = Assert.IsType<ExportCommand>(cmd);
|
||||
Assert.True(exportCmd.ChangesOnly);
|
||||
Assert.True(exportCmd.WithComments);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Error Handling Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_InvalidFormat_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("step list"));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
Assert.Contains("steps", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_UnknownCommand_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps unknown"));
|
||||
Assert.Equal(StepCommandErrors.InvalidCommand, ex.ErrorCode);
|
||||
Assert.Contains("unknown", ex.Message.ToLower());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_Empty_Throws()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse(""));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_StepsAlone_Throws()
|
||||
{
|
||||
// "steps" without a subcommand should throw
|
||||
var ex = Assert.Throws<StepCommandException>(() => _parser.Parse("steps"));
|
||||
Assert.Equal(StepCommandErrors.ParseError, ex.ErrorCode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Case Insensitivity Tests
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_CaseInsensitive_StepsKeyword()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
Assert.IsType<ListCommand>(_parser.Parse("STEPS list"));
|
||||
Assert.IsType<ListCommand>(_parser.Parse("Steps list"));
|
||||
Assert.IsType<ListCommand>(_parser.Parse("sTePs list"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void Parse_CaseInsensitive_Subcommand()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
Assert.IsType<ListCommand>(_parser.Parse("steps LIST"));
|
||||
Assert.IsType<ListCommand>(_parser.Parse("steps List"));
|
||||
Assert.IsType<AddRunCommand>(_parser.Parse("steps ADD run \"test\""));
|
||||
Assert.IsType<EditCommand>(_parser.Parse("steps EDIT 1"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user