Skip to content

Commit

Permalink
Fixing #32
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Feb 15, 2022
1 parent 31f2c8d commit 0a1a6bd
Show file tree
Hide file tree
Showing 27 changed files with 148 additions and 24 deletions.
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Update="xunit.assert" Version="2.4.1" IsImplicitlyDefined="true"/>
<PackageReference Update="xunit.abstractions" Version="2.0.3" IsImplicitlyDefined="true"/>
<PackageReference Update="xunit.extensibility.execution" Version="2.4.1" IsImplicitlyDefined="true"/>
<PackageReference Update="Meziantou.Xunit.ParallelTestFramework" Version="1.0.0" IsImplicitlyDefined="true"/>

<PackageReference Update="NUnit" Version="3.13.2" IsImplicitlyDefined="true"/>
<PackageReference Update="NUnit3TestAdapter" Version="4.0.0" IsImplicitlyDefined="true"/>
Expand Down
2 changes: 2 additions & 0 deletions EasyTestFile.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Int64 x:Key="/Default/Environment/UnitTesting/ParallelProcessesCount/@EntryValue">5</s:Int64>
<s:String x:Key="/Default/Environment/UnitTesting/XunitProvider/TestDiscoveryFromArtifactsMethod/@EntryValue">TestRunner</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Xunit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
4 changes: 2 additions & 2 deletions src/EasyTestFile.Xunit/EasyTestFile.Xunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="xunit.assert" />
<PackageReference Include="xunit.abstractions" />
<PackageReference Include="xunit.extensibility.execution" />
Expand All @@ -19,7 +20,6 @@
</ItemGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\PackageDescription.md" Pack="true" PackagePath="$(PackageReadmeFile)"
Visible="false" />
<None Include="$(MSBuildThisFileDirectory)\PackageDescription.md" Pack="true" PackagePath="$(PackageReadmeFile)" Visible="false" />
</ItemGroup>
</Project>
9 changes: 6 additions & 3 deletions src/EasyTestFile.Xunit/EasyTestFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static TestFile Load(
[CallerFilePath] string sourceFile = "",
[CallerMemberName] string method = "")
{
return TestFileFactory.Create(settings, sourceFile, method);
var callingAssembly = Assembly.GetCallingAssembly();
return TestFileFactory.Create(callingAssembly, settings, sourceFile, method);
}

/// <summary>
Expand All @@ -41,7 +42,8 @@ public static Task<string> LoadAsText(
[CallerFilePath] string sourceFile = "",
[CallerMemberName] string method = "")
{
TestFile testFile = TestFileFactory.Create(settings, sourceFile, method);
var callingAssembly = Assembly.GetCallingAssembly();
TestFile testFile = TestFileFactory.Create(callingAssembly, settings, sourceFile, method);
return testFile.AsText();
}

Expand All @@ -57,7 +59,8 @@ public static Task<Stream> LoadAsStream(
[CallerFilePath] string sourceFile = "",
[CallerMemberName] string method = "")
{
TestFile testFile = TestFileFactory.Create(settings, sourceFile, method);
var callingAssembly = Assembly.GetCallingAssembly();
TestFile testFile = TestFileFactory.Create(callingAssembly, settings, sourceFile, method);
return testFile.AsStream();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/EasyTestFile.Xunit/Internal/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace EasyTestFileXunit.Internal
{
using System;
using System.Reflection;
using System.Threading;

internal static class AssemblyResolver
{
Expand Down
13 changes: 0 additions & 13 deletions src/EasyTestFile.Xunit/Internal/MethodInfoResolver.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
namespace EasyTestFileXunit.Internal
{
using System;
using System.Reflection;
using EasyTestFileXunit;

internal static class MethodInfoResolver
{
// //var type = info.ReflectedType!;

public static bool TryGet(out MethodInfo? value)
{
return UsesEasyTestFileAttribute.TryGet(out value);
}

public static MethodInfo Get()
{
if (!TryGet(out MethodInfo? info))
{
throw new Exception("Expected Test.TypeInfo and Test.Method to not be null. Raise a Pull Request with a test that replicates this problem.");
}

return info!;
}
}
}
12 changes: 9 additions & 3 deletions src/EasyTestFile.Xunit/Internal/TestFileFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class TestFileFactory
public static TestFile Create(
EasyTestFileSettings? settings,
Assembly testAssembly,
MethodInfo methodInfo,
MethodInfo? methodInfo,
[CallerFilePath] string sourceFile = "",
[CallerMemberName] string method = "")
{
Expand All @@ -32,12 +32,18 @@ public static TestFile Create(
}

public static TestFile Create(
Assembly callingAssembly,
EasyTestFileSettings? settings = null,
[CallerFilePath] string sourceFile = "",
[CallerMemberName] string method = "")
{
MethodInfo methodInfo = MethodInfoResolver.Get();
Assembly assembly = AssemblyResolver.Get(methodInfo);
Assembly assembly = callingAssembly;

if (MethodInfoResolver.TryGet(out MethodInfo? methodInfo))
{
assembly = AssemblyResolver.Get(methodInfo!);
}

return Create(settings, assembly, methodInfo, sourceFile, method);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/EasyTestFile/Internals/FileNameResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ internal static string GetFileNamePrefix(EasyTestFileSettings settings, TestAsse

internal static (string Relative, string Absolute) GetDirectories(EasyTestFileSettings settings, TestAssemblyInfo testAssemblyInfo, TestMethodInfo testMethodInfo)
{
_ = testAssemblyInfo;
var absoluteDir = testMethodInfo.SanitizedDirectory;
if (settings.Directory is not null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/EasyTestFile/Internals/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal readonly struct TestMethodInfo
/// <exception cref="ArgumentNullException">Thrown when a parameter is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when a parameter contains an invalid value..</exception>
/// <exception cref="Exception">Thrown when something else goes wrong.</exception>
internal TestMethodInfo(MethodInfo info, string sourceFile, string method) :
internal TestMethodInfo(MethodInfo? info, string sourceFile, string method) :
this(sourceFile, method)
{
_ = info;
Expand Down Expand Up @@ -38,7 +38,7 @@ private TestMethodInfo(string sourceFile, string method)

if (string.IsNullOrEmpty(dirName))
{
throw new Exception("Could not determine directory.");
throw new InternalErrorRaisePullRequestException($"Could not determine directory name of the sourcefile '{sourceFile}'.");
}

dirName = dirName.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ProjectReference Include="..\..\src\EasyTestFile.Json\EasyTestFile.Json.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
From textfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
From textfile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
From textfile3
47 changes: 47 additions & 0 deletions tests/EasyTestFile.Nunit.Tests/Property/PropertyLoading.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace EasyTestFileNunit.Tests.Property;

using System;
using System.Threading.Tasks;
using FluentAssertions;
using global::EasyTestFile;
using global::EasyTestFile.Json;
using NUnit.Framework;

public class PropertyLoading
{
// EasyTestFile loads the data before the test started.
private string _text1;
private string _text2;

private static TestFile MyData => EasyTestFileNunit.EasyTestFile.Load();

private static TestFile MyData2 { get; } = EasyTestFileNunit.EasyTestFile.Load();

private static string MyData3 => EasyTestFileNunit.EasyTestFile.LoadAsText().GetAwaiter().GetResult();

[SetUp]
public async Task InitializeAsync()
{
// this is executed before the test
_text1 = await MyData.AsText();
_text2 = await MyData2.AsText();
}

[Test]
public void Test1()
{
_text1.Should().Be("From textfile");
}

[Test]
public void Test2()
{
_text2.Should().Be("From textfile2");
}

[Test]
public void Test3()
{
MyData3.Should().Be("From textfile3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
{EasyTestFile}/Folder1/Partial/MyPartialTest.Test2.testfile.txt,
{EasyTestFile}/Folder1/Partial/Sub/MyPartialTest.Test2.testfile.txt,
{EasyTestFile}/Folder1/Partial/Sub/MyPartialTest.Test4.testfile.txt,
{EasyTestFile}/Property/PropertyLoading.MyData.testfile.txt,
{EasyTestFile}/Property/PropertyLoading.MyData2.testfile.txt,
{EasyTestFile}/Property/PropertyLoading.MyData3.testfile.txt,
{EasyTestFile}/Samples/UnitTestClass.JsonTestFile.testfile.json,
{EasyTestFile}/Samples/UnitTestClass.LoadAsStream.testfile.txt,
{EasyTestFile}/Samples/UnitTestClass.LoadAsTestFile.testfile.txt,
Expand Down
1 change: 1 addition & 0 deletions tests/EasyTestFile.Tests/EasyTestFile.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ProjectReference Include="..\..\src\EasyTestFile.Json\EasyTestFile.Json.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ProjectReference Include="..\..\src\EasyTestFile.Json\EasyTestFile.Json.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ProjectReference Include="..\..\src\EasyTestFile.Json\EasyTestFile.Json.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ProjectReference Include="..\..\src\EasyTestFile.Json\EasyTestFile.Json.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5;net6</TargetFrameworks>
<RootNamespace>EasyTestFileXunit.Resources</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ProjectReference Include="..\..\src\EasyTestFile.Xunit\EasyTestFile.Xunit.csproj" />
<ProjectReference Include="..\..\src\EasyTestFile\EasyTestFile.csproj" />
<ProjectReference Include="..\EasyTestFile.Xunit.Resources\EasyTestFile.Xunit.Resources.csproj" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
From textfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
From textfile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
From textfile3
56 changes: 56 additions & 0 deletions tests/EasyTestFile.Xunit.Tests/Property/PropertyLoading.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace EasyTestFileXunit.Tests.Property;

using System;
using System.Threading.Tasks;
using FluentAssertions;
using global::EasyTestFile;
using global::EasyTestFile.Json;
using Xunit;

[UsesEasyTestFile]
public class PropertyLoading : IAsyncLifetime
{
// EasyTestFile loads the data before the test (fact, theory) started.
// This uses a different approach to detect the current assembly.
// Detection when the test is started is done using the UseEasyTestFile attribute,
// detection before the test is started is done using Assembly.GetCallingAssembly().

private string _text1;
private string _text2;

private static TestFile MyData => EasyTestFileXunit.EasyTestFile.Load();

private static TestFile MyData2 { get; } = EasyTestFileXunit.EasyTestFile.Load();

private static string MyData3 => EasyTestFileXunit.EasyTestFile.LoadAsText().GetAwaiter().GetResult();

public async Task InitializeAsync()
{
// this is executed before the test
_text1 = await MyData.AsText();
_text2 = await MyData2.AsText();
}

public Task DisposeAsync()
{
return Task.CompletedTask;
}

[Fact]
public void Test1()
{
_text1.Should().Be("From textfile");
}

[Fact]
public void Test2()
{
_text2.Should().Be("From textfile2");
}

[Fact]
public void Test3()
{
MyData3.Should().Be("From textfile3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
{EasyTestFile}/Folder1/Partial/MyPartialTest.Test2.testfile.txt,
{EasyTestFile}/Folder1/Partial/Sub/MyPartialTest.Test2.testfile.txt,
{EasyTestFile}/Folder1/Partial/Sub/MyPartialTest.Test4.testfile.txt,
{EasyTestFile}/Property/PropertyLoading.MyData.testfile.txt,
{EasyTestFile}/Property/PropertyLoading.MyData2.testfile.txt,
{EasyTestFile}/Property/PropertyLoading.MyData3.testfile.txt,
{EasyTestFile}/Samples/UnitTestClass.JsonTestFile.testfile.json,
{EasyTestFile}/Samples/UnitTestClass.LoadAsStream.testfile.txt,
{EasyTestFile}/Samples/UnitTestClass.LoadAsTestFile.testfile.txt,
Expand Down

0 comments on commit 0a1a6bd

Please sign in to comment.