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

Commit 2ccc51a

Browse files
authored
Merge pull request #544 from github/fixes/543-test-temp-files
Removed TempFileBaseClass.
2 parents 777bd2a + 36fc1a5 commit 2ccc51a

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

src/UnitTests/GitHub.App/Models/RepositoryModelTests.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,37 @@ public void DifferentContentEqualsFalse(long id1, string name1, string url1, lon
5454
}
5555

5656
[Collection("PackageServiceProvider global data tests")]
57-
public class PathConstructorTests : TempFileBaseClass
57+
public class PathConstructorTests : TestBaseClass
5858
{
5959
[Fact]
6060
public void NoRemoteUrl()
6161
{
62-
var provider = Substitutes.ServiceProvider;
63-
var gitservice = provider.GetGitService();
64-
var repo = Substitute.For<IRepository>();
65-
var path = Directory.CreateSubdirectory("repo-name");
66-
gitservice.GetUri(path.FullName).Returns((UriString)null);
67-
var model = new SimpleRepositoryModel(path.FullName);
68-
Assert.Equal("repo-name", model.Name);
62+
using (var temp = new TempDirectory())
63+
{
64+
var provider = Substitutes.ServiceProvider;
65+
var gitservice = provider.GetGitService();
66+
var repo = Substitute.For<IRepository>();
67+
var path = temp.Directory.CreateSubdirectory("repo-name");
68+
gitservice.GetUri(path.FullName).Returns((UriString)null);
69+
var model = new SimpleRepositoryModel(path.FullName);
70+
Assert.Equal("repo-name", model.Name);
71+
}
6972
}
7073

7174
[Fact]
7275
public void WithRemoteUrl()
7376
{
74-
var provider = Substitutes.ServiceProvider;
75-
var gitservice = provider.GetGitService();
76-
var repo = Substitute.For<IRepository>();
77-
var path = Directory.CreateSubdirectory("repo-name");
78-
gitservice.GetUri(path.FullName).Returns(new UriString("https://github.com/user/repo-name"));
79-
var model = new SimpleRepositoryModel(path.FullName);
80-
Assert.Equal("repo-name", model.Name);
81-
Assert.Equal("user", model.Owner);
77+
using (var temp = new TempDirectory())
78+
{
79+
var provider = Substitutes.ServiceProvider;
80+
var gitservice = provider.GetGitService();
81+
var repo = Substitute.For<IRepository>();
82+
var path = temp.Directory.CreateSubdirectory("repo-name");
83+
gitservice.GetUri(path.FullName).Returns(new UriString("https://github.com/user/repo-name"));
84+
var model = new SimpleRepositoryModel(path.FullName);
85+
Assert.Equal("repo-name", model.Name);
86+
Assert.Equal("user", model.Owner);
87+
}
8288
}
8389
}
8490

src/UnitTests/GitHub.Exports/SimpleRepositoryModelTests.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66
using UnitTests;
77
using Xunit;
88
using GitHub.Primitives;
9+
using Xunit.Abstractions;
910

1011
[Collection("PackageServiceProvider global data tests")]
11-
public class SimpleRepositoryModelTests : TempFileBaseClass
12+
public class SimpleRepositoryModelTests : TestBaseClass
1213
{
14+
ITestOutputHelper output;
15+
16+
public SimpleRepositoryModelTests(ITestOutputHelper output)
17+
{
18+
this.output = output;
19+
}
20+
1321
static void SetupRepository(string sha)
1422
{
1523
var provider = Substitutes.ServiceProvider;
@@ -48,17 +56,20 @@ static void SetupRepository(string sha)
4856
[InlineData(21, false, "[email protected]/foo/bar", "123123", @"src\dir\ThisIsFile1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/ThisIsFile1.cs")]
4957
public void GenerateUrl(int testid, bool createRootedPath, string baseUrl, string sha, string path, int startLine, int endLine, string expected)
5058
{
51-
SetupRepository(sha);
59+
using (var temp = new TempDirectory())
60+
{
61+
SetupRepository(sha);
5262

53-
var basePath = Directory.CreateSubdirectory("generate-url-test1-" + testid);
54-
if (createRootedPath && path != null)
55-
path = System.IO.Path.Combine(basePath.FullName, path);
56-
ISimpleRepositoryModel model = null;
57-
if (!String.IsNullOrEmpty(baseUrl))
58-
model = new SimpleRepositoryModel("bar", new UriString(baseUrl), basePath.FullName);
59-
else
60-
model = new SimpleRepositoryModel(basePath.FullName);
61-
var result = model.GenerateUrl(path, startLine, endLine);
62-
Assert.Equal(expected, result?.ToString());
63+
var basePath = temp.Directory.CreateSubdirectory("generate-url-test1-" + testid);
64+
if (createRootedPath && path != null)
65+
path = System.IO.Path.Combine(basePath.FullName, path);
66+
ISimpleRepositoryModel model = null;
67+
if (!String.IsNullOrEmpty(baseUrl))
68+
model = new SimpleRepositoryModel("bar", new UriString(baseUrl), basePath.FullName);
69+
else
70+
model = new SimpleRepositoryModel(basePath.FullName);
71+
var result = model.GenerateUrl(path, startLine, endLine);
72+
Assert.Equal(expected, result?.ToString());
73+
}
6374
}
6475
}

src/UnitTests/Helpers/TestBaseClass.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Octokit;
44
using System;
55
using System.IO;
6+
using Xunit.Abstractions;
67

78
/// <summary>
89
/// This base class will get its methods called by the most-derived
@@ -62,25 +63,23 @@ protected static PullRequest CreatePullRequest(User user, int id, ItemState stat
6263
commentCount, reviewCommentCount, 0, 0, 0, 0,
6364
null, false);
6465
}
65-
}
66-
67-
public class TempFileBaseClass : TestBaseClass
68-
{
69-
public DirectoryInfo Directory { get; set; }
7066

71-
public override void OnEntry()
67+
protected class TempDirectory : IDisposable
7268
{
73-
var f = Path.GetTempFileName();
74-
var name = Path.GetFileNameWithoutExtension(f);
75-
File.Delete(f);
76-
Directory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), name));
77-
Directory.Create();
78-
base.OnEntry();
79-
}
69+
public TempDirectory()
70+
{
71+
var f = Path.GetTempFileName();
72+
var name = Path.GetFileNameWithoutExtension(f);
73+
File.Delete(f);
74+
Directory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), name));
75+
Directory.Create();
76+
}
8077

81-
public override void OnExit()
82-
{
83-
Directory.Delete(true);
84-
base.OnExit();
78+
public DirectoryInfo Directory { get; }
79+
80+
public void Dispose()
81+
{
82+
Directory.Delete(true);
83+
}
8584
}
86-
}
85+
}

0 commit comments

Comments
 (0)