diff --git a/Fix.Tests/ConsoleFixers/GitSimilarCommandTests.cs b/Fix.Tests/ConsoleFixers/GitSimilarCommandTests.cs index 1019d90..164d018 100644 --- a/Fix.Tests/ConsoleFixers/GitSimilarCommandTests.cs +++ b/Fix.Tests/ConsoleFixers/GitSimilarCommandTests.cs @@ -1,4 +1,3 @@ -using Fix.ConsoleHelpers; using Fix.CommandFixers; using System; using Xunit; @@ -27,5 +26,27 @@ The most similar command is Assert.Equal(nameof(GitSimilar), fix.Author); Assert.Equal("git status", fix.FixedCommand); } + + [Fact] + public void Can_fix_git_status_from_windows_terminal() + { + var consoleBuffer = @"PS C:\dev\app> git pu +git: 'pu' is not a git command. See 'git --help'. + +The most similar command is + pull + push + p4 +PS C:\dev\app> fix +"; + var lines = consoleBuffer.Split(Environment.NewLine); + + var manager = SetupTestsHelper.CreateActionManager(); + var fix = manager.GetFix(lines); + + Assert.True(fix.IsFixed); + Assert.Equal(nameof(GitSimilar), fix.Author); + Assert.Equal("git pull", fix.FixedCommand); + } } } \ No newline at end of file diff --git a/Fix/CommandFixers/ActionManager.cs b/Fix/CommandFixers/ActionManager.cs index d2a4f1b..bf06738 100644 --- a/Fix/CommandFixers/ActionManager.cs +++ b/Fix/CommandFixers/ActionManager.cs @@ -1,11 +1,14 @@ using Fix.ConsoleHelpers; +using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; namespace Fix.CommandFixers { public class ActionManager { + private static readonly Regex _normalizerRegex = new("^(PS )(.*>)(\\s)?(.*)", RegexOptions.Compiled); private List _fixActions = new(); public string LastCommand { get; private set; } = ""; @@ -15,10 +18,11 @@ public void AddFix(ICommandFixer fixAction) _fixActions.Add(fixAction); } - private string GetLastCommand(string[] lines) + private string GetLastCommand(IEnumerable lines) { var currentPath = ConsoleHelper.GetCurrentPath(); - var lastCommandLine = lines.Where(x => x.StartsWith(currentPath + ConsoleHelper.CONSOLE_SEPARATOR)) + var commandPrefix = currentPath + ConsoleHelper.CONSOLE_SEPARATOR; + var lastCommandLine = lines.Where(x => x.StartsWith(commandPrefix)) .Reverse() .Skip(1) .FirstOrDefault(); @@ -27,23 +31,25 @@ private string GetLastCommand(string[] lines) return ""; } var index = lastCommandLine.IndexOf(ConsoleHelper.CONSOLE_SEPARATOR); - return lastCommandLine[(index + 1)..]; + return lastCommandLine[(index + 1)..].Trim(); } - private string[] FilterLastCommand(string lastCommand, string[] lines) + private string[] FilterLastCommand(string lastCommand, IEnumerable lines) { var currentPath = ConsoleHelper.GetCurrentPath(); + var fullLastCommand = $"{currentPath}{ConsoleHelper.CONSOLE_SEPARATOR}{lastCommand}"; return lines.Reverse() .Skip(1) - .TakeWhile(x => x != $"{currentPath}{ConsoleHelper.CONSOLE_SEPARATOR}{lastCommand}") + .TakeWhile(x => x != fullLastCommand) .Reverse() .ToArray(); } public CommandFix GetFix(string[] consoleBufferInLines) { - LastCommand = GetLastCommand(consoleBufferInLines); - var lastCommandResult = FilterLastCommand(LastCommand, consoleBufferInLines); + var normalizedBuffer = consoleBufferInLines.Select(s => _normalizerRegex.Replace(s, "$2$4")); + LastCommand = GetLastCommand(normalizedBuffer); + var lastCommandResult = FilterLastCommand(LastCommand, normalizedBuffer); LogBuffer(lastCommandResult); diff --git a/Fix/Program.cs b/Fix/Program.cs index 2086f27..7d1b37e 100644 --- a/Fix/Program.cs +++ b/Fix/Program.cs @@ -9,11 +9,12 @@ class Program { static void Main(string[] args) { - if(args.Any(x => x == "-debug")) + if(args.Contains("-debug")) { ConsoleHelper.Debug = true; } - if (args.Any(x => x == "-plan")) + + if (args.Contains("-plan")) { ConsoleHelper.Plan = true; } @@ -23,6 +24,7 @@ static void Main(string[] args) if (lines.Length < 3) { Console.WriteLine("Nothing to fix"); + return; } var actionManager = new ActionManagerFactory().Create();