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

Commit 083533d

Browse files
committed
Fix GenerateUrl tests using dependency injection
Inject IGitService into LocalRepositoryModel when testing.
1 parent 96fab8d commit 083533d

File tree

3 files changed

+50
-51
lines changed

3 files changed

+50
-51
lines changed

src/GitHub.Exports/Models/LocalRepositoryModel.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class LocalRepositoryModel : RepositoryModel, ILocalRepositoryModel, IEqu
2424
/// <param name="name">The repository name.</param>
2525
/// <param name="cloneUrl">The repository's clone URL.</param>
2626
/// <param name="localPath">The repository's local path.</param>
27-
public LocalRepositoryModel(string name, UriString cloneUrl, string localPath)
28-
: base(name, cloneUrl)
27+
public LocalRepositoryModel(string name, UriString cloneUrl, string localPath, IGitService gitService = null)
28+
: base(name, cloneUrl, gitService)
2929
{
3030
Guard.ArgumentNotEmptyString(localPath, nameof(localPath));
3131

@@ -37,8 +37,8 @@ public LocalRepositoryModel(string name, UriString cloneUrl, string localPath)
3737
/// Initializes a new instance of the <see cref="LocalRepositoryModel"/> class.
3838
/// </summary>
3939
/// <param name="path">The repository's local path.</param>
40-
public LocalRepositoryModel(string path)
41-
: base(path)
40+
public LocalRepositoryModel(string path, IGitService gitService = null)
41+
: base(path, gitService)
4242
{
4343
LocalPath = path;
4444
Icon = Octicon.repo;
@@ -51,7 +51,7 @@ public void Refresh()
5151
{
5252
if (LocalPath == null)
5353
return;
54-
CloneUrl = GitService.GitServiceHelper.GetUri(LocalPath);
54+
CloneUrl = GitService.GetUri(LocalPath);
5555
}
5656

5757
/// <summary>
@@ -68,7 +68,7 @@ public async Task<UriString> GenerateUrl(LinkType linkType, string path = null,
6868
if (CloneUrl == null)
6969
return null;
7070

71-
var sha = await GitService.GitServiceHelper.GetLatestPushedSha(path ?? LocalPath);
71+
var sha = await GitService.GetLatestPushedSha(path ?? LocalPath);
7272
// this also incidentally checks whether the repo has a valid LocalPath
7373
if (String.IsNullOrEmpty(sha))
7474
return CloneUrl.ToRepositoryUrl().AbsoluteUri;
@@ -157,7 +157,7 @@ public string HeadSha
157157
{
158158
get
159159
{
160-
using (var repo = GitService.GitServiceHelper.GetRepository(LocalPath))
160+
using (var repo = GitService.GetRepository(LocalPath))
161161
{
162162
return repo?.Commits.FirstOrDefault()?.Sha ?? string.Empty;
163163
}
@@ -172,7 +172,7 @@ public IBranch CurrentBranch
172172
get
173173
{
174174
// BranchModel doesn't keep a reference to Repository
175-
using (var repo = GitService.GitServiceHelper.GetRepository(LocalPath))
175+
using (var repo = GitService.GetRepository(LocalPath))
176176
{
177177
return new BranchModel(repo?.Head, this);
178178
}

src/GitHub.Exports/Models/RepositoryModel.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class RepositoryModel : NotificationAwareObject, IRepositoryModel
2424
/// <param name="cloneUrl">The repository's clone URL.</param>
2525
public RepositoryModel(
2626
string name,
27-
UriString cloneUrl)
27+
UriString cloneUrl,
28+
IGitService gitService = null) : this(gitService)
2829
{
2930
Guard.ArgumentNotEmptyString(name, nameof(name));
3031
Guard.ArgumentNotNull(cloneUrl, nameof(cloneUrl));
@@ -40,16 +41,21 @@ public RepositoryModel(
4041
/// The path to the local repository from which repository name and clone URL will be
4142
/// extracted.
4243
/// </param>
43-
protected RepositoryModel(string path)
44+
protected RepositoryModel(string path, IGitService gitService = null) : this(gitService)
4445
{
4546
Guard.ArgumentNotNull(path, nameof(path));
4647

4748
var dir = new DirectoryInfo(path);
4849
if (!dir.Exists)
4950
throw new ArgumentException("Path does not exist", nameof(path));
50-
var uri = GitService.GitServiceHelper.GetUri(path);
51+
var uri = gitService.GetUri(path);
5152
Name = uri?.RepositoryName ?? dir.Name;
52-
CloneUrl = GitService.GitServiceHelper.GetUri(path);
53+
CloneUrl = gitService.GetUri(path);
54+
}
55+
56+
RepositoryModel(IGitService gitService = null)
57+
{
58+
GitService = gitService ?? Services.GitService.GitServiceHelper;
5359
}
5460

5561
/// <summary>
@@ -107,5 +113,7 @@ public void SetIcon(bool isPrivate, bool isFork)
107113
? Octicon.repo_forked
108114
: Octicon.repo;
109115
}
116+
117+
protected IGitService GitService { get; }
110118
}
111119
}
Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,15 @@
1-
using System;
1+
using System.Threading.Tasks;
2+
using System.Collections.Generic;
23
using GitHub.Models;
3-
using GitHub.VisualStudio;
4-
using LibGit2Sharp;
4+
using GitHub.Exports;
5+
using GitHub.Services;
6+
using GitHub.Primitives;
57
using NSubstitute;
6-
using UnitTests;
8+
using LibGit2Sharp;
79
using NUnit.Framework;
8-
using GitHub.Primitives;
9-
using System.Collections.Generic;
10-
using System.Threading.Tasks;
11-
using GitHub.Exports;
1210

13-
//[Collection("PackageServiceProvider global data tests")]
1411
public class LocalRepositoryModelTests : TestBaseClass
1512
{
16-
/**ITestOutputHelper output;
17-
18-
public LocalRepositoryModelTests(ITestOutputHelper output)
19-
{
20-
this.output = output;
21-
}**/
22-
23-
static void SetupRepository(string sha)
24-
{
25-
var provider = Substitutes.ServiceProvider;
26-
var gitservice = provider.GetGitService();
27-
var repo = Substitute.For<IRepository>();
28-
gitservice.GetRepository(Args.String).Returns(repo);
29-
gitservice.GetLatestPushedSha(Args.String).Returns(Task.FromResult(sha));
30-
if (!String.IsNullOrEmpty(sha))
31-
{
32-
var refs = Substitute.For<ReferenceCollection>();
33-
var refrence = Substitute.For<Reference>();
34-
refs.ReachableFrom(Arg.Any<IEnumerable<Reference>>(), Arg.Any<IEnumerable<Commit>>()).Returns(new Reference[] { refrence });
35-
repo.Refs.Returns(refs);
36-
var commit = Substitute.For<Commit>();
37-
commit.Sha.Returns(sha);
38-
repo.Commits.Returns(new FakeCommitLog { commit });
39-
}
40-
}
41-
4213
[TestCase(1, LinkType.Blob, false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
4314
[TestCase(2, LinkType.Blob, false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
4415
[TestCase(3, LinkType.Blob, false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
@@ -71,18 +42,38 @@ public async Task GenerateUrl(int testid, LinkType linkType, bool createRootedPa
7142
{
7243
using (var temp = new TempDirectory())
7344
{
74-
SetupRepository(sha);
45+
var gitService = CreateGitService(sha);
7546

7647
var basePath = temp.Directory.CreateSubdirectory("generate-url-test1-" + testid);
7748
if (createRootedPath && path != null)
7849
path = System.IO.Path.Combine(basePath.FullName, path);
7950
ILocalRepositoryModel model = null;
80-
if (!String.IsNullOrEmpty(baseUrl))
81-
model = new LocalRepositoryModel("bar", new UriString(baseUrl), basePath.FullName);
51+
if (!string.IsNullOrEmpty(baseUrl))
52+
model = new LocalRepositoryModel("bar", new UriString(baseUrl), basePath.FullName, gitService);
8253
else
83-
model = new LocalRepositoryModel(basePath.FullName);
54+
model = new LocalRepositoryModel(basePath.FullName, gitService);
8455
var result = await model.GenerateUrl(linkType, path, startLine, endLine);
8556
Assert.That(expected, Is.EqualTo(result?.ToString()));
8657
}
8758
}
59+
60+
static IGitService CreateGitService(string sha)
61+
{
62+
var gitservice = Substitute.For<IGitService>();
63+
var repo = Substitute.For<IRepository>();
64+
gitservice.GetRepository(Args.String).Returns(repo);
65+
gitservice.GetLatestPushedSha(Args.String).Returns(Task.FromResult(sha));
66+
if (!string.IsNullOrEmpty(sha))
67+
{
68+
var refs = Substitute.For<ReferenceCollection>();
69+
var refrence = Substitute.For<Reference>();
70+
refs.ReachableFrom(Arg.Any<IEnumerable<Reference>>(), Arg.Any<IEnumerable<Commit>>()).Returns(new Reference[] { refrence });
71+
repo.Refs.Returns(refs);
72+
var commit = Substitute.For<Commit>();
73+
commit.Sha.Returns(sha);
74+
repo.Commits.Returns(new FakeCommitLog { commit });
75+
}
76+
77+
return gitservice;
78+
}
8879
}

0 commit comments

Comments
 (0)