From 023b2d8438aa0758af2018d0fc9e998418989510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donatas=20Ma=C4=8Di=C5=ABnas?= Date: Thu, 13 Oct 2022 22:25:10 +0300 Subject: [PATCH] Add a test that FileViewer.Command.ResetLines doesn't revert file rename --- GitUI/Editor/FileViewer.cs | 2 + .../CommandsDialogs/FormCommitTests.cs | 60 +++++++++++++++++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/GitUI/Editor/FileViewer.cs b/GitUI/Editor/FileViewer.cs index beab73c2cee..9a84f370764 100644 --- a/GitUI/Editor/FileViewer.cs +++ b/GitUI/Editor/FileViewer.cs @@ -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); diff --git a/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs b/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs index ef43c4fa78a..b18141c8da8 100644 --- a/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs +++ b/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs @@ -4,6 +4,8 @@ using GitCommands; using GitUI; using GitUI.CommandsDialogs; +using GitUI.Editor; +using GitUI.UserControls; using ICSharpCode.TextEditor; using NUnit.Framework; @@ -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); @@ -296,7 +298,7 @@ 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); @@ -304,7 +306,7 @@ public void Should_unstage_only_filtered_on_UnstageAll() }); } - [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, @@ -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, @@ -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, @@ -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 => { @@ -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 boundsAccessor, Action testDriver) { var bounds1 = Rectangle.Empty;