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

Commit d25f6a4

Browse files
authored
Merge pull request #1005 from github/jcansdale/dont-spam-log
Stop TryOpenRepository tests spamming the log
2 parents 03a88ae + fdc42cd commit d25f6a4

File tree

4 files changed

+24
-57
lines changed

4 files changed

+24
-57
lines changed

src/GitHub.Exports/Services/IVSServices.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ namespace GitHub.Services
66
public interface IVSServices
77
{
88
string VSVersion { get; }
9-
10-
void ActivityLogMessage(string message);
11-
void ActivityLogWarning(string message);
12-
void ActivityLogError(string message);
139
bool TryOpenRepository(string directory);
1410
}
1511
}

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,30 @@
1313
using Microsoft.VisualStudio.Shell.Interop;
1414
using Rothko;
1515
using Serilog;
16-
using DTE = EnvDTE.DTE;
16+
using EnvDTE;
1717

1818
namespace GitHub.Services
1919
{
2020
[Export(typeof(IVSServices))]
2121
[PartCreationPolicy(CreationPolicy.Shared)]
2222
public class VSServices : IVSServices
2323
{
24-
static readonly ILogger log = LogManager.ForContext<VSServices>();
24+
readonly ILogger log;
2525
readonly IGitHubServiceProvider serviceProvider;
2626

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

30-
3130
[ImportingConstructor]
32-
public VSServices(IGitHubServiceProvider serviceProvider)
31+
public VSServices(IGitHubServiceProvider serviceProvider) :
32+
this(serviceProvider, LogManager.ForContext<VSServices>())
33+
{
34+
}
35+
36+
public VSServices(IGitHubServiceProvider serviceProvider, ILogger log)
3337
{
3438
this.serviceProvider = serviceProvider;
39+
this.log = log;
3540
}
3641

3742
string vsVersion;
@@ -45,41 +50,6 @@ public string VSVersion
4550
}
4651
}
4752

48-
49-
public void ActivityLogMessage(string message)
50-
{
51-
var log = serviceProvider.GetActivityLog();
52-
if (log != null)
53-
{
54-
if (!ErrorHandler.Succeeded(log.LogEntry((UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION,
55-
Info.ApplicationInfo.ApplicationSafeName, message)))
56-
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Could not log message to activity log: {0}", message));
57-
}
58-
}
59-
60-
public void ActivityLogError(string message)
61-
{
62-
var log = serviceProvider.GetActivityLog();
63-
if (log != null)
64-
{
65-
66-
if (!ErrorHandler.Succeeded(log.LogEntry((UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR,
67-
Info.ApplicationInfo.ApplicationSafeName, message)))
68-
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Could not log error to activity log: {0}", message));
69-
}
70-
}
71-
72-
public void ActivityLogWarning(string message)
73-
{
74-
var log = serviceProvider.GetActivityLog();
75-
if (log != null)
76-
{
77-
if (!ErrorHandler.Succeeded(log.LogEntry((UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_WARNING,
78-
Info.ApplicationInfo.ApplicationSafeName, message)))
79-
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Could not log warning to activity log: {0}", message));
80-
}
81-
}
82-
8353
/// <summary>Open a repository in Team Explorer</summary>
8454
/// <remarks>
8555
/// There doesn't appear to be a command that directly opens a target repo.
@@ -105,7 +75,7 @@ public bool TryOpenRepository(string repoPath)
10575
}
10676

10777
var repoDir = os.Directory.GetDirectory(repoPath);
108-
if(!repoDir.Exists)
78+
if (!repoDir.Exists)
10979
{
11080
return false;
11181
}
@@ -172,7 +142,7 @@ string GetVSVersion()
172142
return setupInstance.GetInstallationName().TrimPrefix(InstallationNamePrefix);
173143
}
174144
}
175-
catch(Exception ex)
145+
catch (Exception ex)
176146
{
177147
log.Error(ex, "Error getting the Visual Studio version");
178148
}

test/UnitTests/GitHub.Exports/VSServicesTests.cs

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

1011
public class VSServicesTests
1112
{
@@ -27,24 +28,22 @@ 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("");
36+
var success = target.TryOpenRepository(repoDir);
3537

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

3942
[Test]
4043
public void RepoDirExistsFalse_ReturnFalse()
4144
{
4245
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);
46+
var target = CreateVSServices(repoDir, repoDirExists: false);
4847

4948
var success = target.TryOpenRepository(repoDir);
5049

@@ -85,22 +84,23 @@ public void SolutionCreate_DeleteVsSolutionSubdir()
8584
directoryInfo.Received().Delete(true);
8685
}
8786

88-
VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte = null)
87+
VSServices CreateVSServices(string repoDir, IOperatingSystem os = null, DTE dte = null, bool repoDirExists = true, ILogger log = null)
8988
{
9089
os = os ?? Substitute.For<IOperatingSystem>();
9190
dte = dte ?? Substitute.For<DTE>();
91+
log = log ?? Substitute.For<ILogger>();
9292

9393
if (repoDir != null)
9494
{
9595
var directoryInfo = Substitute.For<IDirectoryInfo>();
96-
directoryInfo.Exists.Returns(true);
96+
directoryInfo.Exists.Returns(repoDirExists);
9797
os.Directory.GetDirectory(repoDir).Returns(directoryInfo);
9898
}
9999

100100
var provider = Substitute.For<IGitHubServiceProvider>();
101101
provider.TryGetService<DTE>().Returns(dte);
102102
provider.TryGetService<IOperatingSystem>().Returns(os);
103-
return new VSServices(provider);
103+
return new VSServices(provider, log);
104104
}
105105
}
106106

test/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<Reference Include="PresentationFramework" />
174174
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
175175
<HintPath>..\..\packages\Serilog.2.5.0\lib\net46\Serilog.dll</HintPath>
176+
<Private>True</Private>
176177
</Reference>
177178
<Reference Include="rothko, Version=0.0.3.0, Culture=neutral, PublicKeyToken=9f664c41f503810a, processorArchitecture=MSIL">
178179
<HintPath>..\..\packages\Rothko.0.0.3-ghfvs\lib\net45\rothko.dll</HintPath>

0 commit comments

Comments
 (0)