Skip to content

Commit 933ae30

Browse files
authored
Add internal, parameterless ctors for generated proxies (#359)
Mocking libraries, like Moq, are based on Castle.Core and rely on its functionality for generating dynamic proxies. This is only possible when there is an accessible parameterless constructor. This changes re-adds such constructors to several base types. They are marked internal and obsolete so that nobody accidentially uses them in "real" code. To allow Castle.Core to access them altough they are internal we expose our internals to the dynamic proxy assembly. Fixes #355
1 parent 70ea5e5 commit 933ae30

10 files changed

+111
-2
lines changed

System.IO.Abstractions.TestingHelpers.Tests/FileSystemTests.cs

+86-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#if NET40
1+

22
using NUnit.Framework;
33

44
namespace System.IO.Abstractions.TestingHelpers.Tests
55
{
66
[TestFixture]
77
public class FileSystemTests
88
{
9+
#if NET40
910
[Test]
1011
public void Is_Serializable()
1112
{
@@ -17,6 +18,89 @@ public void Is_Serializable()
1718

1819
Assert.That(memoryStream.Length > 0, "Length didn't increase after serialization task.");
1920
}
21+
#endif
22+
23+
#if NETCOREAPP2_0
24+
[Test]
25+
public void Mock_File_Succeeds()
26+
{
27+
var fileSystemMock = new Moq.Mock<IFileSystem>();
28+
29+
Assert.DoesNotThrow(() =>
30+
fileSystemMock.Setup(x => x.File.ToString()).Returns("")
31+
);
32+
}
33+
34+
[Test]
35+
public void Mock_Directory_Succeeds()
36+
{
37+
var fileSystemMock = new Moq.Mock<IFileSystem>();
38+
39+
Assert.DoesNotThrow(() =>
40+
fileSystemMock.Setup(x => x.Directory.ToString()).Returns("")
41+
);
42+
}
43+
44+
[Test]
45+
public void Mock_FileInfo_Succeeds()
46+
{
47+
var fileSystemMock = new Moq.Mock<IFileSystem>();
48+
49+
Assert.DoesNotThrow(() =>
50+
fileSystemMock.Setup(x => x.FileInfo.ToString()).Returns("")
51+
);
52+
}
53+
54+
[Test]
55+
public void Mock_FileStream_Succeeds()
56+
{
57+
var fileSystemMock = new Moq.Mock<IFileSystem>();
58+
59+
Assert.DoesNotThrow(() =>
60+
fileSystemMock.Setup(x => x.FileStream.ToString()).Returns("")
61+
);
62+
}
63+
64+
[Test]
65+
public void Mock_Path_Succeeds()
66+
{
67+
var fileSystemMock = new Moq.Mock<IFileSystem>();
68+
69+
Assert.DoesNotThrow(() =>
70+
fileSystemMock.Setup(x => x.Path.ToString()).Returns("")
71+
);
72+
}
73+
74+
[Test]
75+
public void Mock_DirectoryInfo_Succeeds()
76+
{
77+
var fileSystemMock = new Moq.Mock<IFileSystem>();
78+
79+
Assert.DoesNotThrow(() =>
80+
fileSystemMock.Setup(x => x.DirectoryInfo.ToString()).Returns("")
81+
);
82+
}
83+
84+
[Test]
85+
public void Mock_DriveInfo_Succeeds()
86+
{
87+
var fileSystemMock = new Moq.Mock<IFileSystem>();
88+
89+
Assert.DoesNotThrow(() =>
90+
fileSystemMock.Setup(x => x.DirectoryInfo.ToString()).Returns("")
91+
);
92+
}
93+
94+
[Test]
95+
public void Mock_FileSystemWatcher_Succeeds()
96+
{
97+
var fileSystemMock = new Moq.Mock<IFileSystem>();
98+
99+
Assert.DoesNotThrow(() =>
100+
fileSystemMock.Setup(x => x.FileSystemWatcher.ToString()).Returns("")
101+
);
102+
}
103+
#endif
104+
20105
}
21106
}
22-
#endif

System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
<ItemGroup Condition="'$(TargetFramework)' != 'net40'">
4242
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
43+
<PackageReference Include="Moq" Version="4.10.0" />
4344
</ItemGroup>
4445

4546
<ItemGroup>

System.IO.Abstractions/DirectoryBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ protected DirectoryBase(IFileSystem fileSystem)
1212
FileSystem = fileSystem;
1313
}
1414

15+
[Obsolete("This constructor only exists to support mocking libraries.", error: true)]
16+
internal DirectoryBase() { }
17+
1518
/// <summary>
1619
/// Exposes the underlying filesystem implementation. This is useful for implementing extension methods.
1720
/// </summary>

System.IO.Abstractions/DirectoryInfoBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ protected DirectoryInfoBase(IFileSystem fileSystem) : base(fileSystem)
1111
{
1212
}
1313

14+
[Obsolete("This constructor only exists to support mocking libraries.", error: true)]
15+
internal DirectoryInfoBase() { }
16+
1417
/// <inheritdoc cref="DirectoryInfo.Create()"/>
1518
public abstract void Create();
1619
#if NET40

System.IO.Abstractions/DriveInfoBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ protected DriveInfoBase(IFileSystem fileSystem)
88
FileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
99
}
1010

11+
[Obsolete("This constructor only exists to support mocking libraries.", error: true)]
12+
internal DriveInfoBase() { }
13+
1114
/// <summary>
1215
/// Exposes the underlying filesystem implementation. This is useful for implementing extension methods.
1316
/// </summary>

System.IO.Abstractions/FileBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ protected FileBase(IFileSystem fileSystem)
1313
FileSystem = fileSystem;
1414
}
1515

16+
[Obsolete("This constructor only exists to support mocking libraries.", error: true)]
17+
internal FileBase() { }
18+
1619
/// <summary>
1720
/// Exposes the underlying filesystem implementation. This is useful for implementing extension methods.
1821
/// </summary>

System.IO.Abstractions/FileInfoBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ protected FileInfoBase(IFileSystem fileSystem) : base(fileSystem)
1010
{
1111
}
1212

13+
[Obsolete("This constructor only exists to support mocking libraries.", error: true)]
14+
internal FileInfoBase() { }
15+
1316
/// <inheritdoc cref="FileInfo.AppendText"/>
1417
public abstract StreamWriter AppendText();
1518

System.IO.Abstractions/FileSystemInfoBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ protected FileSystemInfoBase(IFileSystem fileSystem)
99
FileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
1010
}
1111

12+
[Obsolete("This constructor only exists to support mocking libraries.", error: true)]
13+
internal FileSystemInfoBase() { }
14+
1215
/// <summary>
1316
/// Exposes the underlying filesystem implementation. This is useful for implementing extension methods.
1417
/// </summary>

System.IO.Abstractions/PathBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ protected PathBase(IFileSystem fileSystem)
99
this.FileSystem = fileSystem;
1010
}
1111

12+
[Obsolete("This constructor only exists to support mocking libraries.", error: true)]
13+
internal PathBase() { }
14+
1215
/// <summary>
1316
/// Exposes the underlying filesystem implementation. This is useful for implementing extension methods.
1417
/// </summary>

System.IO.Abstractions/Properties/AssemblyInfo.cs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#if DEBUG
77
[assembly: InternalsVisibleTo("System.IO.Abstractions.TestingHelpers.Tests")]
8+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
89
#else
910
[assembly: InternalsVisibleTo("System.IO.Abstractions.TestingHelpers.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001001160c7a0f907c400c5392975b66d2f3752fb82625d5674d386b83896d4d4ae8d0ef8319ef391fbb3466de0058ad2f361b8f5cb8a32ecb4e908bece5c519387552cedd2ca0250e36b59c6d6dc3dc260ca73a7e27c3add4ae22d5abaa562225d7ba34d427e8f3f52928a46a674deb0208eca7d379aa22712355b91a55a5ce521d2")]
11+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
1012
#endif
13+

0 commit comments

Comments
 (0)