Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Fix.Tests/ConsoleFixers/GitSimilarCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Fix.ConsoleHelpers;
using Fix.CommandFixers;
using System;
using Xunit;
Expand Down Expand Up @@ -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);
}
}
}
20 changes: 13 additions & 7 deletions Fix/CommandFixers/ActionManager.cs
Original file line number Diff line number Diff line change
@@ -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<ICommandFixer> _fixActions = new();

public string LastCommand { get; private set; } = "";
Expand All @@ -15,10 +18,11 @@ public void AddFix(ICommandFixer fixAction)
_fixActions.Add(fixAction);
}

private string GetLastCommand(string[] lines)
private string GetLastCommand(IEnumerable<string> 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();
Expand All @@ -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<string> 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);

Expand Down
6 changes: 4 additions & 2 deletions Fix/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -23,6 +24,7 @@ static void Main(string[] args)
if (lines.Length < 3)
{
Console.WriteLine("Nothing to fix");
return;
}

var actionManager = new ActionManagerFactory().Create();
Expand Down