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

Commit 50c18b0

Browse files
committed
Inject ILogger into VSServices when testing
1 parent 8aa9c2c commit 50c18b0

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-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, bool logErrors = true);
13+
bool TryOpenRepository(string directory);
1414
}
1515
}

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@ namespace GitHub.Services
1717
[PartCreationPolicy(CreationPolicy.Shared)]
1818
public class VSServices : IVSServices
1919
{
20-
static readonly ILogger log = LogManager.ForContext<VSServices>();
20+
readonly ILogger log;
2121
readonly IGitHubServiceProvider serviceProvider;
2222

2323
// Use a prefix (~$) that is defined in the default VS gitignore.
2424
public const string TempSolutionName = "~$GitHubVSTemp$~";
2525

26-
2726
[ImportingConstructor]
28-
public VSServices(IGitHubServiceProvider serviceProvider)
27+
public VSServices(IGitHubServiceProvider serviceProvider) :
28+
this(serviceProvider, LogManager.ForContext<VSServices>())
29+
{
30+
}
31+
32+
public VSServices(IGitHubServiceProvider serviceProvider, ILogger log)
2933
{
3034
this.serviceProvider = serviceProvider;
35+
this.log = log;
3136
}
3237

3338
string vsVersion;
@@ -41,7 +46,6 @@ public string VSVersion
4146
}
4247
}
4348

44-
4549
public void ActivityLogMessage(string message)
4650
{
4751
var log = serviceProvider.GetActivityLog();
@@ -84,19 +88,19 @@ public void ActivityLogWarning(string message)
8488
/// </remarks>
8589
/// <param name="repoPath">The path to the repository to open</param>
8690
/// <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, bool logErrors = true)
91+
public bool TryOpenRepository(string repoPath)
8892
{
8993
var os = serviceProvider.TryGetService<IOperatingSystem>();
9094
if (os == null)
9195
{
92-
if (logErrors) log.Error("TryOpenRepository couldn't find IOperatingSystem service");
96+
log.Error("TryOpenRepository couldn't find IOperatingSystem service");
9397
return false;
9498
}
9599

96100
var dte = serviceProvider.TryGetService<DTE>();
97101
if (dte == null)
98102
{
99-
if (logErrors) log.Error("TryOpenRepository couldn't find DTE service");
103+
log.Error("TryOpenRepository couldn't find DTE service");
100104
return false;
101105
}
102106

@@ -116,16 +120,16 @@ public bool TryOpenRepository(string repoPath, bool logErrors = true)
116120
}
117121
catch (Exception e)
118122
{
119-
if (logErrors) log.Error(e, "Error opening repository");
123+
log.Error(e, "Error opening repository");
120124
}
121125
finally
122126
{
123-
TryCleanupSolutionUserFiles(os, repoPath, TempSolutionName, logErrors);
127+
TryCleanupSolutionUserFiles(os, repoPath, TempSolutionName);
124128
}
125129
return solutionCreated;
126130
}
127131

128-
void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string slnName, bool logErrors)
132+
void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string slnName)
129133
{
130134
var vsTempPath = Path.Combine(repoPath, ".vs", slnName);
131135
try
@@ -139,7 +143,7 @@ void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string sl
139143
}
140144
catch (Exception e)
141145
{
142-
if (logErrors) log.Error(e, "Couldn't clean up {TempPath}", vsTempPath);
146+
log.Error(e, "Couldn't clean up {TempPath}", vsTempPath);
143147
}
144148
}
145149

test/UnitTests/GitHub.Exports/VSServicesTests.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Xunit;
77
using DTE = EnvDTE.DTE;
88
using Rothko;
9+
using Serilog;
910

1011
public class VSServicesTests
1112
{
@@ -27,13 +28,15 @@ public void SolutionCreateThrows_ReturnsFalse()
2728
{
2829
var repoDir = @"x:\repo";
2930
var dte = Substitute.For<DTE>();
30-
dte.Solution.When(s => s.Create(Arg.Any<string>(), Arg.Any<string>())).Do(
31-
ci => { throw new COMException(); });
32-
var target = CreateVSServices(repoDir, dte: dte);
31+
var log = Substitute.For<ILogger>();
32+
var ex = new COMException();
33+
dte.Solution.When(s => s.Create(Arg.Any<string>(), Arg.Any<string>())).Do(ci => { throw ex; });
34+
var target = CreateVSServices(repoDir, dte: dte, log: log);
3335

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

3638
Assert.False(success);
39+
log.Received(1).Error(ex, "Error opening repository");
3740
}
3841

3942
[Fact]
@@ -60,7 +63,7 @@ public void DeleteThrowsIOException_ReturnTrue()
6063
ci => { throw new IOException(); });
6164
var target = CreateVSServices(repoDir, os: os);
6265

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

6568
Assert.True(success);
6669
}
@@ -81,10 +84,11 @@ public void SolutionCreate_DeleteVsSolutionSubdir()
8184
directoryInfo.Received().Delete(true);
8285
}
8386

84-
VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte = null, bool repoDirExists = true)
87+
VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte = null, bool repoDirExists = true, ILogger log = null)
8588
{
8689
os = os ?? Substitute.For<IOperatingSystem>();
8790
dte = dte ?? Substitute.For<DTE>();
91+
log = log ?? Substitute.For<ILogger>();
8892

8993
if (repoDir != null)
9094
{
@@ -96,7 +100,7 @@ VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte
96100
var provider = Substitute.For<IGitHubServiceProvider>();
97101
provider.TryGetService<DTE>().Returns(dte);
98102
provider.TryGetService<IOperatingSystem>().Returns(os);
99-
return new VSServices(provider);
103+
return new VSServices(provider, log);
100104
}
101105
}
102106

test/UnitTests/UnitTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@
170170
</Reference>
171171
<Reference Include="PresentationCore" />
172172
<Reference Include="PresentationFramework" />
173+
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
174+
<HintPath>..\..\packages\Serilog.2.5.0\lib\net46\Serilog.dll</HintPath>
175+
<Private>True</Private>
176+
</Reference>
173177
<Reference Include="System" />
174178
<Reference Include="System.ComponentModel.Composition" />
175179
<Reference Include="System.Core" />

test/UnitTests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<package id="Rx-PlatformServices" version="2.2.5-custom" targetFramework="net45" />
3333
<package id="Rx-Testing" version="2.2.5-custom" targetFramework="net45" />
3434
<package id="Rx-XAML" version="2.2.5-custom" targetFramework="net45" />
35+
<package id="Serilog" version="2.5.0" targetFramework="net461" />
3536
<package id="xunit" version="2.3.1" targetFramework="net461" />
3637
<package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
3738
<package id="xunit.analyzers" version="0.7.0" targetFramework="net461" />

0 commit comments

Comments
 (0)