Skip to content

Commit 6b4be76

Browse files
committed
refactor
1 parent 4729fda commit 6b4be76

5 files changed

Lines changed: 139 additions & 83 deletions

File tree

GitContentSearch.Tests/GitHelperTests.cs

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,12 @@ public class GitHelperTests
1212
public void GetGitCommits_ShouldReturnEmptyArray_OnGitFailure()
1313
{
1414
// Arrange
15-
var processMock = new Mock<IProcessWrapper>();
16-
var process = new Mock<Process>();
17-
processMock.Setup(p => p.Start(It.IsAny<ProcessStartInfo>())).Returns(process.Object);
15+
var processWrapperMock = new Mock<IProcessWrapper>();
1816

19-
var outputStream = new MemoryStream();
20-
var writer = new StreamWriter(outputStream);
21-
writer.Write(string.Empty);
22-
writer.Flush();
23-
outputStream.Position = 0;
17+
var processResult = new ProcessResult(string.Empty, "Error occurred", 1);
18+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>())).Returns(processResult);
2419

25-
process.Setup(p => p.StandardOutput).Returns(new StreamReader(outputStream));
26-
process.Setup(p => p.StandardError).Returns(new StreamReader(new MemoryStream()));
27-
process.Setup(p => p.ExitCode).Returns(1); // Simulate a failure
28-
29-
var gitHelper = new GitHelper(processMock.Object);
20+
var gitHelper = new GitHelper(processWrapperMock.Object);
3021

3122
// Act
3223
var result = gitHelper.GetGitCommits("invalidCommit", "anotherInvalidCommit");
@@ -40,24 +31,87 @@ public void GetGitCommits_ShouldReturnEmptyArray_OnGitFailure()
4031
public void GetCommitTime_ShouldThrowException_OnProcessFailure()
4132
{
4233
// Arrange
43-
var processMock = new Mock<IProcessWrapper>();
44-
var process = new Mock<Process>();
45-
processMock.Setup(p => p.Start(It.IsAny<ProcessStartInfo>())).Returns(process.Object);
34+
var processWrapperMock = new Mock<IProcessWrapper>();
35+
36+
var processResult = new ProcessResult(string.Empty, "fatal: bad object invalidCommit", 1);
37+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>())).Returns(processResult);
38+
39+
var gitHelper = new GitHelper(processWrapperMock.Object);
40+
41+
// Act & Assert
42+
var exception = Assert.Throws<Exception>(() => gitHelper.GetCommitTime("invalidCommit"));
43+
Assert.Equal("Error getting commit time: fatal: bad object invalidCommit", exception.Message);
44+
}
45+
46+
[Fact]
47+
public void GetCommitTime_ShouldReturnCorrectTime_OnSuccess()
48+
{
49+
// Arrange
50+
var processWrapperMock = new Mock<IProcessWrapper>();
4651

47-
var outputStream = new MemoryStream();
48-
var writer = new StreamWriter(outputStream);
49-
writer.Write(string.Empty);
50-
writer.Flush();
51-
outputStream.Position = 0;
52+
var processResult = new ProcessResult("2023-08-21 12:34:56 +0000", string.Empty, 0);
53+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>())).Returns(processResult);
5254

53-
process.Setup(p => p.StandardOutput).Returns(new StreamReader(outputStream));
54-
process.Setup(p => p.StandardError).Returns(new StreamReader(new MemoryStream()));
55-
process.Setup(p => p.ExitCode).Returns(1); // Simulate a failure
55+
var gitHelper = new GitHelper(processWrapperMock.Object);
56+
57+
// Act
58+
var result = gitHelper.GetCommitTime("validCommitHash");
59+
60+
// Assert
61+
Assert.Equal("2023-08-21 12:34:56 +0000", result);
62+
}
63+
64+
[Fact]
65+
public void RunGitShow_ShouldCreateOutputFile_OnSuccess()
66+
{
67+
// Arrange
68+
var processWrapperMock = new Mock<IProcessWrapper>();
5669

57-
var gitHelper = new GitHelper(processMock.Object);
70+
var fileContent = "Sample file content from git show";
71+
var processResult = new ProcessResult(fileContent, string.Empty, 0);
72+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>())).Returns(processResult);
73+
74+
var gitHelper = new GitHelper(processWrapperMock.Object);
75+
76+
var outputFilePath = "test_output.txt";
77+
78+
// Ensure the output file does not exist before the test
79+
if (File.Exists(outputFilePath))
80+
File.Delete(outputFilePath);
81+
82+
// Act
83+
gitHelper.RunGitShow("validCommitHash", "path/to/file.txt", outputFilePath);
84+
85+
// Assert
86+
Assert.True(File.Exists(outputFilePath));
87+
var writtenContent = File.ReadAllText(outputFilePath);
88+
Assert.Equal(fileContent, writtenContent);
89+
90+
// Clean up
91+
File.Delete(outputFilePath);
92+
}
93+
94+
[Fact]
95+
public void RunGitShow_ShouldThrowException_OnProcessFailure()
96+
{
97+
// Arrange
98+
var processWrapperMock = new Mock<IProcessWrapper>();
99+
100+
var processResult = new ProcessResult(string.Empty, "fatal: path 'file.txt' does not exist in 'invalidCommitHash'", 1);
101+
processWrapperMock.Setup(pw => pw.Start(It.IsAny<ProcessStartInfo>())).Returns(processResult);
102+
103+
var gitHelper = new GitHelper(processWrapperMock.Object);
104+
105+
var outputFilePath = "test_output.txt";
58106

59107
// Act & Assert
60-
Assert.Throws<Exception>(() => gitHelper.GetCommitTime("invalidCommit"));
108+
var exception = Assert.Throws<Exception>(() =>
109+
gitHelper.RunGitShow("invalidCommitHash", "file.txt", outputFilePath));
110+
111+
Assert.Equal("Error running git show: fatal: path 'file.txt' does not exist in 'invalidCommitHash'", exception.Message);
112+
113+
// Ensure the output file was not created
114+
Assert.False(File.Exists(outputFilePath));
61115
}
62116
}
63117
}

GitContentSearch/GitHelper.cs

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,18 @@ public string GetCommitTime(string commitHash)
2323
CreateNoWindow = true
2424
};
2525

26-
using (var process = _processWrapper.Start(startInfo))
27-
{
28-
if (process == null)
29-
{
30-
throw new Exception("Failed to start git process.");
31-
}
32-
33-
string output = process.StandardOutput.ReadToEnd().Trim();
34-
string error = process.StandardError.ReadToEnd().Trim();
35-
process.WaitForExit();
26+
var result = _processWrapper.Start(startInfo);
3627

37-
if (process.ExitCode != 0)
38-
{
39-
throw new Exception($"Error getting commit time: {error}");
40-
}
41-
42-
return output;
28+
if (result.ExitCode != 0)
29+
{
30+
throw new Exception($"Error getting commit time: {result.StandardError}");
4331
}
32+
33+
return result.StandardOutput;
4434
}
4535

4636
public void RunGitShow(string commit, string filePath, string outputFile)
4737
{
48-
// Ensure the file path is properly formatted for Git
4938
if (filePath.StartsWith("/"))
5039
{
5140
filePath = filePath.Substring(1); // Remove the leading slash if it exists
@@ -63,26 +52,14 @@ public void RunGitShow(string commit, string filePath, string outputFile)
6352
CreateNoWindow = true
6453
};
6554

66-
using (var process = Process.Start(startInfo))
67-
{
68-
if (process == null)
69-
{
70-
throw new Exception("Failed to start git process.");
71-
}
72-
73-
using (var outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
74-
{
75-
process.StandardOutput.BaseStream.CopyTo(outputStream);
76-
}
55+
var result = _processWrapper.Start(startInfo);
7756

78-
string error = process.StandardError.ReadToEnd().Trim();
79-
process.WaitForExit();
80-
81-
if (process.ExitCode != 0)
82-
{
83-
throw new Exception($"Error running git show: {error}");
84-
}
57+
if (result.ExitCode != 0)
58+
{
59+
throw new Exception($"Error running git show: {result.StandardError}");
8560
}
61+
62+
File.WriteAllText(outputFile, result.StandardOutput);
8663
}
8764

8865
public string[] GetGitCommits(string earliest, string latest)
@@ -97,28 +74,17 @@ public string[] GetGitCommits(string earliest, string latest)
9774
CreateNoWindow = true
9875
};
9976

100-
using (var process = Process.Start(startInfo))
101-
{
102-
if (process == null)
103-
{
104-
Console.WriteLine("Failed to start git process.");
105-
return Array.Empty<string>();
106-
}
107-
108-
string output = process.StandardOutput.ReadToEnd().Trim();
109-
string error = process.StandardError.ReadToEnd().Trim();
110-
process.WaitForExit();
77+
var result = _processWrapper.Start(startInfo);
11178

112-
if (process.ExitCode != 0)
113-
{
114-
Console.WriteLine($"Error retrieving git commits: {error}");
115-
return Array.Empty<string>();
116-
}
79+
if (result.ExitCode != 0)
80+
{
81+
Console.WriteLine($"Error retrieving git commits: {result.StandardError}");
82+
return Array.Empty<string>();
83+
}
11784

118-
var commits = output.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
85+
var commits = result.StandardOutput.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
11986

120-
return FilterCommitsByRange(commits, earliest, latest);
121-
}
87+
return FilterCommitsByRange(commits, earliest, latest);
12288
}
12389

12490
private string[] FilterCommitsByRange(string[] commits, string earliest, string latest)

GitContentSearch/Interfaces/IProcessWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace GitContentSearch
44
{
55
public interface IProcessWrapper
66
{
7-
Process Start(ProcessStartInfo startInfo);
7+
ProcessResult Start(ProcessStartInfo startInfo);
88
}
99
}

GitContentSearch/ProcessResult.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace GitContentSearch
8+
{
9+
public class ProcessResult
10+
{
11+
public string StandardOutput { get; }
12+
public string StandardError { get; }
13+
public int ExitCode { get; }
14+
15+
public ProcessResult(string standardOutput, string standardError, int exitCode)
16+
{
17+
StandardOutput = standardOutput;
18+
StandardError = standardError;
19+
ExitCode = exitCode;
20+
}
21+
}
22+
}

GitContentSearch/ProcessWrapper.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,23 @@ namespace GitContentSearch
44
{
55
public class ProcessWrapper : IProcessWrapper
66
{
7-
public Process Start(ProcessStartInfo startInfo)
7+
public ProcessResult Start(ProcessStartInfo startInfo)
88
{
9-
return Process.Start(startInfo);
9+
using (var process = Process.Start(startInfo))
10+
{
11+
if (process == null)
12+
{
13+
throw new Exception("Failed to start process.");
14+
}
15+
16+
string standardOutput = process.StandardOutput.ReadToEnd().Trim();
17+
string standardError = process.StandardError.ReadToEnd().Trim();
18+
process.WaitForExit(); // Ensure the process has completed
19+
20+
int exitCode = process.ExitCode;
21+
22+
return new ProcessResult(standardOutput, standardError, exitCode);
23+
}
1024
}
1125
}
1226
}

0 commit comments

Comments
 (0)