Skip to content

Commit 15fc5e9

Browse files
committed
fix earliest and latest check
1 parent 7686e9e commit 15fc5e9

4 files changed

Lines changed: 141 additions & 5 deletions

File tree

GitContentSearch.Tests/GitContentSearcherTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void SearchContent_ShouldLogError_WhenEarliestCommitIsMoreRecentThanLates
175175
using (var stringWriter = new StringWriter())
176176
{
177177
// Act
178-
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit3", "commit4", stringWriter);
178+
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit4", "commit3", stringWriter);
179179

180180
// Assert
181181
var logContent = stringWriter.ToString();

GitContentSearch.Tests/GitHelperTests.cs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,138 @@ public void GetCommitTime_ShouldReturnCorrectTime_OnSuccess()
6060
// Assert
6161
Assert.Equal("2023-08-21 12:34:56 +0000", result);
6262
}
63+
64+
[Fact]
65+
public void GetGitCommits_ShouldReturnCorrectRange_WhenBothCommitsArePresent()
66+
{
67+
// Arrange
68+
var processWrapperMock = new Mock<IProcessWrapper>();
69+
70+
var gitLogOutput = "commit5\ncommit4\ncommit3\ncommit2\ncommit1";
71+
var processResult = new ProcessResult(gitLogOutput, string.Empty, 0);
72+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>(), null)).Returns(processResult);
73+
74+
var gitHelper = new GitHelper(processWrapperMock.Object);
75+
76+
// Act
77+
var result = gitHelper.GetGitCommits("commit2", "commit4");
78+
79+
// Assert
80+
Assert.Equal(new[] { "commit4", "commit3", "commit2" }, result);
81+
}
82+
83+
[Fact]
84+
public void GetGitCommits_ShouldReturnFullRange_WhenEarliestAndLatestAreEmpty()
85+
{
86+
// Arrange
87+
var processWrapperMock = new Mock<IProcessWrapper>();
88+
89+
var gitLogOutput = "commit5\ncommit4\ncommit3\ncommit2\ncommit1";
90+
var processResult = new ProcessResult(gitLogOutput, string.Empty, 0);
91+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>(), null)).Returns(processResult);
92+
93+
var gitHelper = new GitHelper(processWrapperMock.Object);
94+
95+
// Act
96+
var result = gitHelper.GetGitCommits("", "");
97+
98+
// Assert
99+
Assert.Equal(new[] { "commit5", "commit4", "commit3", "commit2", "commit1" }, result);
100+
}
101+
102+
[Fact]
103+
public void GetGitCommits_ShouldReturnPartialRange_WhenOnlyEarliestCommitIsSpecified()
104+
{
105+
// Arrange
106+
var processWrapperMock = new Mock<IProcessWrapper>();
107+
108+
var gitLogOutput = "commit5\ncommit4\ncommit3\ncommit2\ncommit1";
109+
var processResult = new ProcessResult(gitLogOutput, string.Empty, 0);
110+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>(), null)).Returns(processResult);
111+
112+
var gitHelper = new GitHelper(processWrapperMock.Object);
113+
114+
// Act
115+
var result = gitHelper.GetGitCommits("commit3", "");
116+
117+
// Assert
118+
Assert.Equal(new[] { "commit5", "commit4", "commit3" }, result);
119+
}
120+
121+
[Fact]
122+
public void GetGitCommits_ShouldReturnPartialRange_WhenOnlyLatestCommitIsSpecified()
123+
{
124+
// Arrange
125+
var processWrapperMock = new Mock<IProcessWrapper>();
126+
127+
var gitLogOutput = "commit5\ncommit4\ncommit3\ncommit2\ncommit1";
128+
var processResult = new ProcessResult(gitLogOutput, string.Empty, 0);
129+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>(), null)).Returns(processResult);
130+
131+
var gitHelper = new GitHelper(processWrapperMock.Object);
132+
133+
// Act
134+
var result = gitHelper.GetGitCommits("", "commit3");
135+
136+
// Assert
137+
Assert.Equal(new[] { "commit3", "commit2", "commit1" }, result);
138+
}
139+
140+
[Fact]
141+
public void GetGitCommits_ShouldReturnEmptyArray_WhenCommitsAreInWrongOrder()
142+
{
143+
// Arrange
144+
var processWrapperMock = new Mock<IProcessWrapper>();
145+
146+
var gitLogOutput = "commit5\ncommit4\ncommit3\ncommit2\ncommit1";
147+
var processResult = new ProcessResult(gitLogOutput, string.Empty, 0);
148+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>(), null)).Returns(processResult);
149+
150+
var gitHelper = new GitHelper(processWrapperMock.Object);
151+
152+
// Act
153+
var result = gitHelper.GetGitCommits("commit4", "commit2");
154+
155+
// Assert
156+
Assert.Empty(result);
157+
}
158+
159+
[Fact]
160+
public void GetGitCommits_ShouldReturnEmptyArray_WhenEarliestCommitIsNotFound()
161+
{
162+
// Arrange
163+
var processWrapperMock = new Mock<IProcessWrapper>();
164+
165+
var gitLogOutput = "commit5\ncommit4\ncommit3\ncommit2\ncommit1";
166+
var processResult = new ProcessResult(gitLogOutput, string.Empty, 0);
167+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>(), null)).Returns(processResult);
168+
169+
var gitHelper = new GitHelper(processWrapperMock.Object);
170+
171+
// Act
172+
var result = gitHelper.GetGitCommits("nonexistentCommit", "commit2");
173+
174+
// Assert
175+
Assert.Empty(result);
176+
}
177+
178+
[Fact]
179+
public void GetGitCommits_ShouldReturnEmptyArray_WhenLatestCommitIsNotFound()
180+
{
181+
// Arrange
182+
var processWrapperMock = new Mock<IProcessWrapper>();
183+
184+
var gitLogOutput = "commit5\ncommit4\ncommit3\ncommit2\ncommit1";
185+
var processResult = new ProcessResult(gitLogOutput, string.Empty, 0);
186+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>(), null)).Returns(processResult);
187+
188+
var gitHelper = new GitHelper(processWrapperMock.Object);
189+
190+
// Act
191+
var result = gitHelper.GetGitCommits("commit4", "nonexistentCommit");
192+
193+
// Assert
194+
Assert.Empty(result);
195+
}
63196
}
64197
}

GitContentSearch/GitContentSearcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void SearchContent(string filePath, string searchString, string earliestC
3131
return;
3232
}
3333

34-
if (Array.IndexOf(commits, earliestCommit) < Array.IndexOf(commits, latestCommit))
34+
if (Array.IndexOf(commits, earliestCommit) > Array.IndexOf(commits, latestCommit))
3535
{
3636
logWriter.WriteLine("Error: The earliest commit is more recent than the latest commit.");
3737
return;

GitContentSearch/GitHelper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,32 @@ private string[] FilterCommitsByRange(string[] commits, string earliest, string
9595
int startIndex = 0;
9696
int endIndex = commits.Length - 1;
9797

98+
// Find the index of the latest commit (should be closer to start of the array)
9899
if (!string.IsNullOrEmpty(latest))
99100
{
100101
startIndex = Array.IndexOf(commits, latest);
101102
if (startIndex == -1)
102103
{
103104
Console.WriteLine($"Latest commit {latest} not found.");
104-
startIndex = 0;
105+
return new string[0];
105106
}
106107
}
107108

109+
// Find the index of the earliest commit (should be closer to the end of the array)
108110
if (!string.IsNullOrEmpty(earliest))
109111
{
110112
endIndex = Array.IndexOf(commits, earliest);
111113
if (endIndex == -1)
112114
{
113115
Console.WriteLine($"Earliest commit {earliest} not found.");
114-
endIndex = commits.Length - 1;
116+
return new string[0];
115117
}
116118
}
117119

120+
// If the latest commit appears after the earliest commit in the list, the range is invalid
118121
if (startIndex > endIndex)
119122
{
120-
Console.WriteLine("Invalid commit range specified.");
123+
Console.WriteLine("Invalid commit range specified: latest commit is earlier than the earliest commit.");
121124
return new string[0];
122125
}
123126

0 commit comments

Comments
 (0)