Skip to content

Commit

Permalink
Add a test that FileViewer.Command.ResetLines doesn't revert file rename
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonatas committed Oct 13, 2022
1 parent 35e8b07 commit 023b2d8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
2 changes: 2 additions & 0 deletions GitUI/Editor/FileViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,8 @@ public bool ConfirmResetLines
public ToolStripButton IgnoreAllWhitespacesButton => _fileViewer.ignoreAllWhitespaces;
public ToolStripMenuItem IgnoreAllWhitespacesMenuItem => _fileViewer.ignoreAllWhitespaceChangesToolStripMenuItem;

internal CommandStatus ExecuteCommand(Command command) => _fileViewer.ExecuteCommand((int)command);

internal void IgnoreWhitespaceAtEolToolStripMenuItem_Click(object sender, EventArgs e) => _fileViewer.IgnoreWhitespaceAtEolToolStripMenuItem_Click(sender, e);
internal void IgnoreWhitespaceChangesToolStripMenuItemClick(object sender, EventArgs e) => _fileViewer.IgnoreWhitespaceChangesToolStripMenuItemClick(sender, e);
internal void IgnoreAllWhitespaceChangesToolStripMenuItem_Click(object sender, EventArgs e) => _fileViewer.IgnoreAllWhitespaceChangesToolStripMenuItem_Click(sender, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using GitCommands;
using GitUI;
using GitUI.CommandsDialogs;
using GitUI.Editor;
using GitUI.UserControls;
using ICSharpCode.TextEditor;
using NUnit.Framework;

Expand Down Expand Up @@ -244,7 +246,7 @@ public void Should_stage_only_filtered_on_StageAll()
testform.StageAllToolItem.PerformClick();
var fileNotMatchedByFilterIsStillUnstaged = testform.UnstagedList.AllItems.Where(i => i.Item.Name == "file2.txt").Any();
var fileNotMatchedByFilterIsStillUnstaged = testform.UnstagedList.AllItems.Any(i => i.Item.Name == "file2.txt");
Assert.AreEqual(2, testform.StagedList.AllItemsCount);
Assert.AreEqual(1, testform.UnstagedList.AllItemsCount);
Expand Down Expand Up @@ -296,15 +298,15 @@ public void Should_unstage_only_filtered_on_UnstageAll()
testform.UnstageAllToolItem.PerformClick();
var fileNotMatchedByFilterIsStillStaged = testform.StagedList.AllItems.Where(i => i.Item.Name == "file2.txt").Any();
var fileNotMatchedByFilterIsStillStaged = testform.StagedList.AllItems.Any(i => i.Item.Name == "file2.txt");
Assert.AreEqual(2, testform.UnstagedList.AllItemsCount);
Assert.AreEqual(1, testform.StagedList.AllItemsCount);
Assert.IsTrue(fileNotMatchedByFilterIsStillStaged);
});
}

[Test, TestCaseSource(typeof(CommitMessageTestData), "TestCases")]
[Test, TestCaseSource(typeof(CommitMessageTestData), nameof(CommitMessageTestData.TestCases))]
public void AddSelectionToCommitMessage_shall_be_ignored_unless_diff_is_focused(
string message,
int selectionStart,
Expand All @@ -317,7 +319,7 @@ public void Should_unstage_only_filtered_on_UnstageAll()
expectedResult: false, expectedMessage: message, expectedSelectionStart: selectionStart);
}

[Test, TestCaseSource(typeof(CommitMessageTestData), "TestCases")]
[Test, TestCaseSource(typeof(CommitMessageTestData), nameof(CommitMessageTestData.TestCases))]
public void AddSelectionToCommitMessage_shall_be_ignored_if_no_difftext_is_selected(
string message,
int selectionStart,
Expand All @@ -330,7 +332,7 @@ public void Should_unstage_only_filtered_on_UnstageAll()
expectedResult: false, expectedMessage: message, expectedSelectionStart: selectionStart);
}

[Test, TestCaseSource(typeof(CommitMessageTestData), "TestCases")]
[Test, TestCaseSource(typeof(CommitMessageTestData), nameof(CommitMessageTestData.TestCases))]
public void AddSelectionToCommitMessage_shall_modify_the_commit_message(
string message,
int selectionStart,
Expand All @@ -344,7 +346,7 @@ public void Should_unstage_only_filtered_on_UnstageAll()
}

[Test]
public void editFileToolStripMenuItem_Click_no_selection_should_not_throw()
public void EditFileToolStripMenuItem_Click_no_selection_should_not_throw()
{
RunFormTest(async form =>
{
Expand Down Expand Up @@ -473,6 +475,52 @@ public void SelectedDiff_remembers_geometry()
});
}

[Test]
public void ShouldNotUndoRenameFileWhenResettingStagedLines()
{
RunFormTest(async form =>
{
FormCommit.TestAccessor ta = form.GetTestAccessor();
FileViewer.TestAccessor selectedDiff = ta.SelectedDiff.GetTestAccessor();
FileViewerInternal? selectedDiffInternal = selectedDiff.FileViewerInternal;
// Commit a file, rename it and introduce a slight content change
string contents = "this\nhas\nmany\nlines\nthis\nhas\nmany\nlines\nthis\nhas\nmany\nlines?\n";
_referenceRepository.CreateCommit("commit", contents, "original.txt");
_referenceRepository.DeleteRepoFile("original.txt");
contents = contents.Replace("?", "!");
_referenceRepository.CreateRepoFile("original2.txt", contents);
await ta.RescanChangesAsync();
ta.UnstagedList.SelectedItems = ta.UnstagedList.AllItems;
ta.UnstagedList.Focus();
ta.ExecuteCommand(FormCommit.Command.StageSelectedFile);
ta.StagedList.SelectedGitItem = ta.StagedList.AllItems.Single(i => i.Item.Name.Contains("original2.txt")).Item;
selectedDiffInternal.Focus();
while (selectedDiffInternal.GetTestAccessor().TextEditor.Document.TextLength < 30)
{
await Task.Delay(1);
}
selectedDiffInternal.GetTestAccessor().TextEditor.ActiveTextAreaControl.SelectionManager.SetSelection(
new TextLocation(2, 11), new TextLocation(5, 12));
selectedDiff.ConfirmResetLines = false;
selectedDiff.ExecuteCommand(FileViewer.Command.ResetLines);
await ta.RescanChangesAsync();
FileStatusItem? stagedAndRenamed = ta.StagedList.AllItems.FirstOrDefault(i => i.Item.Name.Contains("original2.txt"));
stagedAndRenamed.Should().NotBeNull();
stagedAndRenamed!.Item.IsRenamed.Should().BeTrue();
});
}

private void RunGeometryMemoryTest(Func<FormCommit, Rectangle> boundsAccessor, Action<Rectangle, Rectangle> testDriver)
{
var bounds1 = Rectangle.Empty;
Expand Down

0 comments on commit 023b2d8

Please sign in to comment.