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

Commit 3e63188

Browse files
committed
Change ctor to simplify code
1 parent 131e229 commit 3e63188

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

src/GitHub.Api/SimpleApiClient.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GitHub.Primitives;
66
using GitHub.Services;
77
using Octokit;
8+
using GitHub.Extensions;
89

910
namespace GitHub.Api
1011
{
@@ -23,10 +24,13 @@ public class SimpleApiClient : ISimpleApiClient
2324
bool? isEnterprise;
2425
bool? hasWiki;
2526

26-
public SimpleApiClient(HostAddress hostAddress, UriString repoUrl, IGitHubClient githubClient,
27+
public SimpleApiClient(UriString repoUrl, IGitHubClient githubClient,
2728
Lazy<IEnterpriseProbeTask> enterpriseProbe, Lazy<IWikiProbe> wikiProbe)
2829
{
29-
HostAddress = hostAddress;
30+
Guard.ArgumentNotNull(repoUrl, nameof(repoUrl));
31+
Guard.ArgumentNotNull(githubClient, nameof(githubClient));
32+
33+
HostAddress = HostAddress.Create(repoUrl);
3034
OriginalUrl = repoUrl;
3135
client = githubClient;
3236
this.enterpriseProbe = enterpriseProbe;
@@ -48,7 +52,7 @@ async Task<Repository> GetRepositoryInternal()
4852
await sem.WaitAsync();
4953
try
5054
{
51-
if (owner == null && OriginalUrl != null)
55+
if (owner == null)
5256
{
5357
var ownerLogin = OriginalUrl.Owner;
5458
var repositoryName = OriginalUrl.RepositoryName;

src/GitHub.Api/SimpleApiClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enter
3030
public ISimpleApiClient Create(UriString repositoryUrl)
3131
{
3232
var hostAddress = HostAddress.Create(repositoryUrl);
33-
return cache.GetOrAdd(repositoryUrl, new SimpleApiClient(hostAddress, repositoryUrl,
33+
return cache.GetOrAdd(repositoryUrl, new SimpleApiClient(repositoryUrl,
3434
new GitHubClient(productHeader, new SimpleCredentialStore(hostAddress), hostAddress.ApiUri),
3535
lazyEnterpriseProbe, lazyWikiProbe));
3636
}

src/GitHub.Extensions/Guard.cs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using NullGuard;
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Globalization;
@@ -8,6 +9,19 @@ namespace GitHub.Extensions
89
{
910
public static class Guard
1011
{
12+
public static void ArgumentNotNull([AllowNull]object value, string name)
13+
{
14+
if (value != null) return;
15+
string message = String.Format(CultureInfo.InvariantCulture, "Failed Null Check on '{0}'", name);
16+
#if DEBUG
17+
if (!InUnitTestRunner())
18+
{
19+
Debug.Fail(message);
20+
}
21+
#endif
22+
throw new ArgumentNullException(name, message);
23+
}
24+
1125
public static void ArgumentNonNegative(int value, string name)
1226
{
1327
if (value > -1) return;
@@ -78,29 +92,7 @@ public static void ArgumentInRange(int value, int minValue, int maxValue, string
7892
// Borrowed from Splat.
7993
static bool InUnitTestRunner()
8094
{
81-
var testAssemblies = new[] {
82-
"CSUNIT",
83-
"NUNIT",
84-
"XUNIT",
85-
"MBUNIT",
86-
"PEX.",
87-
"NBEHAVE",
88-
};
89-
90-
try
91-
{
92-
return SearchForAssembly(testAssemblies);
93-
}
94-
catch (Exception)
95-
{
96-
return false;
97-
}
98-
}
99-
100-
static bool SearchForAssembly(IEnumerable<string> assemblyList)
101-
{
102-
return AppDomain.CurrentDomain.GetAssemblies()
103-
.Any(x => assemblyList.Any(name => x.FullName.ToUpperInvariant().Contains(name)));
95+
return Splat.ModeDetector.InUnitTestRunner();
10496
}
10597
}
10698
}

src/UnitTests/GitHub.Api/SimpleApiClientTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public async Task RetrievesRepositoryFromWeb()
2121
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
2222
var wikiProbe = Substitute.For<IWikiProbe>();
2323
var client = new SimpleApiClient(
24-
gitHubHost,
2524
"https://github.com/github/visualstudio",
2625
gitHubClient,
2726
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
@@ -43,7 +42,6 @@ public async Task RetrievesCachedRepositoryForSubsequentCalls()
4342
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
4443
var wikiProbe = Substitute.For<IWikiProbe>();
4544
var client = new SimpleApiClient(
46-
gitHubHost,
4745
"https://github.com/github/visualstudio",
4846
gitHubClient,
4947
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
@@ -73,7 +71,6 @@ public async Task ReturnsTrueWhenWikiProbeReturnsOk(WikiProbeResult probeResult,
7371
wikiProbe.ProbeAsync(repository)
7472
.Returns(_ => Task.FromResult(probeResult), _ => { throw new Exception("Only call it once"); });
7573
var client = new SimpleApiClient(
76-
gitHubHost,
7774
"https://github.com/github/visualstudio",
7875
gitHubClient,
7976
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
@@ -94,7 +91,6 @@ public void ReturnsFalseWhenWeHaveNotRequestedRepository()
9491
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
9592
var wikiProbe = Substitute.For<IWikiProbe>();
9693
var client = new SimpleApiClient(
97-
gitHubHost,
9894
"https://github.com/github/visualstudio",
9995
gitHubClient,
10096
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
@@ -123,7 +119,6 @@ public async Task ReturnsTrueWhenEnterpriseProbeReturnsOk(EnterpriseProbeResult
123119
.Returns(_ => Task.FromResult(probeResult), _ => { throw new Exception("Only call it once"); });
124120
var wikiProbe = Substitute.For<IWikiProbe>();
125121
var client = new SimpleApiClient(
126-
gitHubHost,
127122
"https://github.com/github/visualstudio",
128123
gitHubClient,
129124
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
@@ -144,7 +139,6 @@ public void ReturnsFalseWhenWeHaveNotRequestedRepository()
144139
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
145140
var wikiProbe = Substitute.For<IWikiProbe>();
146141
var client = new SimpleApiClient(
147-
gitHubHost,
148142
"https://github.com/github/visualstudio",
149143
gitHubClient,
150144
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),

0 commit comments

Comments
 (0)