Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit df7050a

Browse files
committed
Don't spam log during TryOpenRepository test
Fixed `RepoDirExistsFalse_ReturnFalse` test.
1 parent b4304d9 commit df7050a

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/GitHub.Exports/Services/IVSServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public interface IVSServices
1010
void ActivityLogMessage(string message);
1111
void ActivityLogWarning(string message);
1212
void ActivityLogError(string message);
13-
bool TryOpenRepository(string directory);
13+
bool TryOpenRepository(string directory, bool logErrors = true);
1414
}
1515
}

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,24 @@ public void ActivityLogWarning(string message)
8484
/// </remarks>
8585
/// <param name="repoPath">The path to the repository to open</param>
8686
/// <returns>True if a transient solution was successfully created in target directory (which should trigger opening of repository).</returns>
87-
public bool TryOpenRepository(string repoPath)
87+
public bool TryOpenRepository(string repoPath, bool logErrors = true)
8888
{
8989
var os = serviceProvider.TryGetService<IOperatingSystem>();
9090
if (os == null)
9191
{
92-
log.Error("TryOpenRepository couldn't find IOperatingSystem service");
92+
if (logErrors) log.Error("TryOpenRepository couldn't find IOperatingSystem service");
9393
return false;
9494
}
9595

9696
var dte = serviceProvider.TryGetService<DTE>();
9797
if (dte == null)
9898
{
99-
log.Error("TryOpenRepository couldn't find DTE service");
99+
if (logErrors) log.Error("TryOpenRepository couldn't find DTE service");
100100
return false;
101101
}
102102

103103
var repoDir = os.Directory.GetDirectory(repoPath);
104-
if(!repoDir.Exists)
104+
if (!repoDir.Exists)
105105
{
106106
return false;
107107
}
@@ -116,16 +116,16 @@ public bool TryOpenRepository(string repoPath)
116116
}
117117
catch (Exception e)
118118
{
119-
log.Error(e, "Error opening repository");
119+
if (logErrors) log.Error(e, "Error opening repository");
120120
}
121121
finally
122122
{
123-
TryCleanupSolutionUserFiles(os, repoPath, TempSolutionName);
123+
TryCleanupSolutionUserFiles(os, repoPath, TempSolutionName, logErrors);
124124
}
125125
return solutionCreated;
126126
}
127127

128-
void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string slnName)
128+
void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string slnName, bool logErrors)
129129
{
130130
var vsTempPath = Path.Combine(repoPath, ".vs", slnName);
131131
try
@@ -139,7 +139,7 @@ void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string sl
139139
}
140140
catch (Exception e)
141141
{
142-
log.Error(e, "Couldn't clean up {TempPath}", vsTempPath);
142+
if (logErrors) log.Error(e, "Couldn't clean up {TempPath}", vsTempPath);
143143
}
144144
}
145145

@@ -162,7 +162,7 @@ string GetVSVersion()
162162
if (asm != null)
163163
return asm.GetName().Version.ToString();
164164
}
165-
catch(Exception ex)
165+
catch (Exception ex)
166166
{
167167
log.Error(ex, "Error getting the Visual Studio version");
168168
}

test/UnitTests/GitHub.Exports/VSServicesTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void SolutionCreateThrows_ReturnsFalse()
3131
ci => { throw new COMException(); });
3232
var target = CreateVSServices(repoDir, dte: dte);
3333

34-
var success = target.TryOpenRepository("");
34+
var success = target.TryOpenRepository(repoDir, logErrors: false);
3535

3636
Assert.False(success);
3737
}
@@ -40,11 +40,7 @@ public void SolutionCreateThrows_ReturnsFalse()
4040
public void RepoDirExistsFalse_ReturnFalse()
4141
{
4242
var repoDir = @"x:\repo";
43-
var os = Substitute.For<IOperatingSystem>();
44-
//var directoryInfo = Substitute.For<IDirectoryInfo>();
45-
//directoryInfo.Exists.Returns(false);
46-
//os.Directory.GetDirectory(repoDir).Returns(directoryInfo);
47-
var target = CreateVSServices(null, os: os);
43+
var target = CreateVSServices(repoDir, repoDirExists: false);
4844

4945
var success = target.TryOpenRepository(repoDir);
5046

@@ -64,7 +60,7 @@ public void DeleteThrowsIOException_ReturnTrue()
6460
ci => { throw new IOException(); });
6561
var target = CreateVSServices(repoDir, os: os);
6662

67-
var success = target.TryOpenRepository(repoDir);
63+
var success = target.TryOpenRepository(repoDir, logErrors: false);
6864

6965
Assert.True(success);
7066
}
@@ -85,15 +81,15 @@ public void SolutionCreate_DeleteVsSolutionSubdir()
8581
directoryInfo.Received().Delete(true);
8682
}
8783

88-
VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte = null)
84+
VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte = null, bool repoDirExists = true)
8985
{
9086
os = os ?? Substitute.For<IOperatingSystem>();
9187
dte = dte ?? Substitute.For<DTE>();
9288

9389
if (repoDir != null)
9490
{
9591
var directoryInfo = Substitute.For<IDirectoryInfo>();
96-
directoryInfo.Exists.Returns(true);
92+
directoryInfo.Exists.Returns(repoDirExists);
9793
os.Directory.GetDirectory(repoDir).Returns(directoryInfo);
9894
}
9995

0 commit comments

Comments
 (0)