Skip to content

Commit 7686e9e

Browse files
committed
add earliest and latest check
1 parent c73e9f1 commit 7686e9e

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

GitContentSearch.Tests/GitContentSearcherTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,81 @@ public void SearchContent_ShouldLogCorrectly_WhenStringAppearsInSingleCommit()
108108
Assert.Contains("Search string \"search string\" last appears in commit commit3.", logContent);
109109
}
110110
}
111+
112+
[Fact]
113+
public void SearchContent_ShouldRestrictSearchToSpecifiedCommitRange()
114+
{
115+
// Arrange
116+
var gitHelperMock = new Mock<IGitHelper>();
117+
var fileSearcherMock = new Mock<IFileSearcher>();
118+
119+
// Simulate a broader range of commits
120+
var allCommits = new[] { "commit5", "commit4", "commit3", "commit2", "commit1" };
121+
var restrictedCommits = new[] { "commit3", "commit2" };
122+
123+
// Mock the GetGitCommits method to return only the restricted range when specified
124+
gitHelperMock.Setup(g => g.GetGitCommits("commit2", "commit3")).Returns(restrictedCommits);
125+
gitHelperMock.Setup(g => g.GetGitCommits(It.Is<string>(s => s != "commit2"), It.Is<string>(s => s != "commit3"))).Returns(allCommits);
126+
127+
gitHelperMock.Setup(g => g.GetCommitTime(It.IsAny<string>())).Returns("2023-08-21 12:00:00");
128+
129+
// Simulate string appearances based on the specific commit
130+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit1")), It.IsAny<string>())).Returns(false);
131+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit2")), It.IsAny<string>())).Returns(true);
132+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit3")), It.IsAny<string>())).Returns(false);
133+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit4")), It.IsAny<string>())).Returns(false);
134+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit5")), It.IsAny<string>())).Returns(false);
135+
136+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager());
137+
138+
using (var stringWriter = new StringWriter())
139+
{
140+
// Act
141+
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit2", "commit3", stringWriter);
142+
143+
// Assert
144+
var logContent = stringWriter.ToString();
145+
146+
// Ensure only the specified commit range was searched
147+
Assert.Contains("Checked commit: commit3", logContent);
148+
Assert.Contains("Checked commit: commit2", logContent);
149+
Assert.DoesNotContain("Checked commit: commit1", logContent);
150+
Assert.DoesNotContain("Checked commit: commit4", logContent);
151+
Assert.DoesNotContain("Checked commit: commit5", logContent);
152+
153+
// Check the log for the correct first and last appearance commits within the range
154+
Assert.Contains("Search string \"search string\" first appears in commit commit2.", logContent);
155+
Assert.Contains("Search string \"search string\" last appears in commit commit2.", logContent);
156+
}
157+
}
158+
159+
[Fact]
160+
public void SearchContent_ShouldLogError_WhenEarliestCommitIsMoreRecentThanLatestCommit()
161+
{
162+
// Arrange
163+
var gitHelperMock = new Mock<IGitHelper>();
164+
var fileSearcherMock = new Mock<IFileSearcher>();
165+
166+
// Simulate commits in descending order as returned by Git
167+
var allCommits = new[] { "commit5", "commit4", "commit3", "commit2", "commit1" };
168+
169+
// Mock the GetGitCommits method
170+
gitHelperMock.Setup(g => g.GetGitCommits(It.IsAny<string>(), It.IsAny<string>())).Returns(allCommits);
171+
gitHelperMock.Setup(g => g.GetCommitTime(It.IsAny<string>())).Returns("2023-08-21 12:00:00");
172+
173+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager());
174+
175+
using (var stringWriter = new StringWriter())
176+
{
177+
// Act
178+
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit3", "commit4", stringWriter);
179+
180+
// Assert
181+
var logContent = stringWriter.ToString();
182+
183+
// Ensure that an error message is logged
184+
Assert.Contains("Error: The earliest commit is more recent than the latest commit.", logContent);
185+
}
186+
}
111187
}
112188
}

GitContentSearch/GitContentSearcher.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public void SearchContent(string filePath, string searchString, string earliestC
3131
return;
3232
}
3333

34+
if (Array.IndexOf(commits, earliestCommit) < Array.IndexOf(commits, latestCommit))
35+
{
36+
logWriter.WriteLine("Error: The earliest commit is more recent than the latest commit.");
37+
return;
38+
}
39+
3440
int firstMatchIndex = FindFirstMatchIndex(commits, filePath, searchString, logWriter);
3541
int lastMatchIndex = FindLastMatchIndex(commits, filePath, searchString, logWriter, firstMatchIndex);
3642

0 commit comments

Comments
 (0)