diff --git a/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs b/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs index 17d91dcff76..58e797efbe6 100644 --- a/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs +++ b/IntegrationTests/UI.IntegrationTests/CommandsDialogs/FormCommitTests.cs @@ -201,23 +201,29 @@ public void PreserveCommitMessageOnReopenFromAmendCommit() [Test] public void SelectMessageFromHistory() { + const string lastCommitMessage = "last commit message"; + AppSettings.LastCommitMessage = lastCommitMessage; + RunFormTest(form => { var commitMessageToolStripMenuItem = form.GetTestAccessor().CommitMessageToolStripMenuItem; // Verify the message appears correctly commitMessageToolStripMenuItem.ShowDropDown(); - Assert.AreEqual("A commit message", commitMessageToolStripMenuItem.DropDownItems[0].Text); + commitMessageToolStripMenuItem.DropDownItems[0].Text.Should().Be(lastCommitMessage); // Verify the message is selected correctly commitMessageToolStripMenuItem.DropDownItems[0].PerformClick(); - Assert.AreEqual("A commit message", form.GetTestAccessor().Message.Text); + form.GetTestAccessor().Message.Text.Should().Be(lastCommitMessage); }); } [Test] public void Should_handle_well_commit_message_in_commit_message_menu() { + const string lastCommitMessage = "last commit message"; + AppSettings.LastCommitMessage = lastCommitMessage; + _referenceRepository.CreateCommit("Only first line\n\nof a multi-line commit message\nmust be displayed in the menu"); _referenceRepository.CreateCommit("Too long commit message that should be shorten because first line of a commit message is only 50 chars long"); RunFormTest(form => @@ -226,8 +232,9 @@ public void Should_handle_well_commit_message_in_commit_message_menu() // Verify the message appears correctly commitMessageToolStripMenuItem.ShowDropDown(); - Assert.AreEqual("Too long commit message that should be shorten because first line of ...", commitMessageToolStripMenuItem.DropDownItems[0].Text); - Assert.AreEqual("Only first line", commitMessageToolStripMenuItem.DropDownItems[1].Text); + commitMessageToolStripMenuItem.DropDownItems[0].Text.Should().Be(lastCommitMessage); + commitMessageToolStripMenuItem.DropDownItems[1].Text.Should().Be("Too long commit message that should be shorten because first line of ..."); + commitMessageToolStripMenuItem.DropDownItems[2].Text.Should().Be("Only first line"); }); } diff --git a/IntegrationTests/UI.IntegrationTests/UserControls/RevisionGrid/RevisionGridControlTests.cs b/IntegrationTests/UI.IntegrationTests/UserControls/RevisionGrid/RevisionGridControlTests.cs index 58a0b9f8d8f..aabf3c664f9 100644 --- a/IntegrationTests/UI.IntegrationTests/UserControls/RevisionGrid/RevisionGridControlTests.cs +++ b/IntegrationTests/UI.IntegrationTests/UserControls/RevisionGrid/RevisionGridControlTests.cs @@ -199,6 +199,10 @@ public void View_reflects_reset_branch_filter() [Test] public void ToggleBetweenArtificialAndHeadCommits_no_empty([Values(false, true)] bool showGitStatusForArtificialCommits) { + File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "new.txt"), "new"); + File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "stage.txt"), "staged"); + _referenceRepository.Module.StageFile("stage.txt"); + RunToggleBetweenArtificialAndHeadCommitsTest( showGitStatusForArtificialCommits, revisionGridControl => @@ -231,7 +235,8 @@ public void ToggleBetweenArtificialAndHeadCommits_no_empty([Values(false, true)] [Test] public void ToggleBetweenArtificialAndHeadCommits_no_workdir_change([Values(false, true)] bool showGitStatusForArtificialCommits) { - File.Delete(Path.Combine(_referenceRepository.Module.WorkingDir, "A.txt")); + File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "stage.txt"), "staged"); + _referenceRepository.Module.StageFile("stage.txt"); RunToggleBetweenArtificialAndHeadCommitsTest( showGitStatusForArtificialCommits, @@ -267,7 +272,6 @@ public void ToggleBetweenArtificialAndHeadCommits_no_workdir_change([Values(fals [Test] public void ToggleBetweenArtificialAndHeadCommits_no_index_change([Values(false, true)] bool showGitStatusForArtificialCommits) { - _referenceRepository.Module.Reset(ResetMode.Hard); File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "new.txt"), "new"); RunToggleBetweenArtificialAndHeadCommitsTest( @@ -303,8 +307,6 @@ public void ToggleBetweenArtificialAndHeadCommits_no_index_change([Values(false, [Test] public void ToggleBetweenArtificialAndHeadCommits_no_change([Values(false, true)] bool showGitStatusForArtificialCommits) { - _referenceRepository.Module.Reset(ResetMode.Hard); - RunToggleBetweenArtificialAndHeadCommitsTest( showGitStatusForArtificialCommits, revisionGridControl => diff --git a/UnitTests/CommonTestUtils/ReferenceRepository.cs b/UnitTests/CommonTestUtils/ReferenceRepository.cs index dab9752d2b9..bf339fc36c5 100644 --- a/UnitTests/CommonTestUtils/ReferenceRepository.cs +++ b/UnitTests/CommonTestUtils/ReferenceRepository.cs @@ -34,6 +34,7 @@ public static void ResetRepo(ref ReferenceRepository? refRepo) catch (LibGit2Sharp.LockedFileException) { // the index is locked; this might be due to a concurrent or crashed process + refRepo.Dispose(); refRepo = new ReferenceRepository(); Trace.WriteLine("Repo is locked, creating new"); } @@ -46,12 +47,19 @@ public static void ResetRepo(ref ReferenceRepository? refRepo) private const string _fileName = "A.txt"; + private void IndexAdd(Repository repository, string fileName) + { + repository.Index.Add(fileName); + repository.Index.Write(); + } + private string Commit(Repository repository, string commitMessage) { LibGit2Sharp.Signature author = new("GitUITests", "unittests@gitextensions.com", DateTimeOffset.Now); var committer = author; LibGit2Sharp.CommitOptions options = new() { PrettifyMessage = false }; var commit = repository.Commit(commitMessage, author, committer, options); + repository.Index.Write(); return commit.Id.Sha; } @@ -66,7 +74,7 @@ public string CreateCommit(string commitMessage, string content = null) { using LibGit2Sharp.Repository repository = new(_moduleTestHelper.Module.WorkingDir); _moduleTestHelper.CreateRepoFile(_fileName, content ?? commitMessage); - repository.Index.Add(_fileName); + IndexAdd(repository, _fileName); CommitHash = Commit(repository, commitMessage); Console.WriteLine($"Created commit: {CommitHash}, message: {commitMessage}"); @@ -77,11 +85,11 @@ public string CreateCommit(string commitMessage, string content1, string fileNam { using LibGit2Sharp.Repository repository = new(_moduleTestHelper.Module.WorkingDir); _moduleTestHelper.CreateRepoFile(fileName1, content1); - repository.Index.Add(fileName1); + IndexAdd(repository, fileName1); if (content2 != null && fileName2 != null) { _moduleTestHelper.CreateRepoFile(fileName2, content2); - repository.Index.Add(fileName2); + IndexAdd(repository, fileName2); } CommitHash = Commit(repository, commitMessage); @@ -95,7 +103,7 @@ public string CreateCommitRelative(string fileRelativePath, string fileName, str { using Repository repository = new(_moduleTestHelper.Module.WorkingDir); _moduleTestHelper.CreateRepoFile(fileRelativePath, fileName, content ?? commitMessage); - repository.Index.Add(Path.Combine(fileRelativePath, fileName)); + IndexAdd(repository, Path.Combine(fileRelativePath, fileName)); CommitHash = Commit(repository, commitMessage); Console.WriteLine($"Created commit: {CommitHash}, message: {commitMessage}"); @@ -177,7 +185,7 @@ public void Stash(string stashMessage, string content = null) { using LibGit2Sharp.Repository repository = new(_moduleTestHelper.Module.WorkingDir); _moduleTestHelper.CreateRepoFile(_fileName, content ?? stashMessage); - repository.Index.Add(_fileName); + IndexAdd(repository, _fileName); LibGit2Sharp.Signature author = new("GitUITests", "unittests@gitextensions.com", DateTimeOffset.Now); Stash stash = repository.Stashes.Add(author, stashMessage);