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

Commit ccd3c49

Browse files
committed
Removed TempFileBaseClass.
Instead tests should construct a new instance of `TestBaseClass.TempDirectory` and use that. Fixes #543.
1 parent 6256763 commit ccd3c49

File tree

3 files changed

+67
-46
lines changed

3 files changed

+67
-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;
@@ -46,17 +54,20 @@ static void SetupRepository(string sha)
4654
[InlineData(19, false, "[email protected]/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
4755
public void GenerateUrl(int testid, bool createRootedPath, string baseUrl, string sha, string path, int startLine, int endLine, string expected)
4856
{
49-
SetupRepository(sha);
57+
using (var temp = new TempDirectory(output))
58+
{
59+
SetupRepository(sha);
5060

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

src/UnitTests/Helpers/TestBaseClass.cs

Lines changed: 22 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,28 @@ 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+
ITestOutputHelper output;
8070

81-
public override void OnExit()
82-
{
83-
Directory.Delete(true);
84-
base.OnExit();
71+
public TempDirectory(ITestOutputHelper output = null)
72+
{
73+
this.output = output;
74+
var f = Path.GetTempFileName();
75+
var name = Path.GetFileNameWithoutExtension(f);
76+
File.Delete(f);
77+
Directory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), name));
78+
Directory.Create();
79+
output?.WriteLine("Created " + Directory);
80+
}
81+
82+
public DirectoryInfo Directory { get; }
83+
84+
public void Dispose()
85+
{
86+
output?.WriteLine("Deleted " + Directory);
87+
Directory.Delete(true);
88+
}
8589
}
86-
}
90+
}

0 commit comments

Comments
 (0)