Skip to content

TUnit Support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ indent_style = space
indent_size = 2
indent_style = space

[*.cs]
# Require "this." keyword qualification in code
dotnet_style_qualification_for_field = true:suggestion
dotnet_style_qualification_for_property = true:suggestion
dotnet_style_qualification_for_method = true:suggestion
dotnet_style_qualification_for_event = true:suggestion
[*]

# Microsoft .NET properties
csharp_new_line_before_members_in_object_initializers = false
csharp_preferred_modifier_order = public, private, protected, internal, file, static, new, abstract, virtual, sealed, readonly, override, extern, unsafe, volatile, async, required:suggestion

# ReSharper properties
resharper_blank_lines_around_auto_property = 1
resharper_blank_lines_around_field = 1
resharper_blank_lines_around_property = 1
resharper_braces_for_ifelse = required
12 changes: 6 additions & 6 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@
"allOf": [
{
"properties": {
"CI": {
"_ci": {
"type": "boolean",
"description": "Forces the continuous integration build flag"
},
"Configuration": {
"_configuration": {
"type": "string",
"description": "Configuration to build - Default is 'Debug' (local) or 'Release' (server)",
"enum": [
Expand All @@ -118,6 +118,10 @@
"Verify"
]
},
"_solution": {
"type": "string",
"description": "Path to a solution file that is automatically loaded. Default is AutoFixture.TUnit.sln"
},
"github-token": {
"type": "string",
"description": "GitHub auth token",
Expand All @@ -127,10 +131,6 @@
"type": "string",
"description": "NuGet API Key (secret)",
"default": "Secrets must be entered via 'nuke :secrets [profile]'"
},
"Solution": {
"type": "string",
"description": "Path to a solution file that is automatically loaded"
}
}
},
Expand Down
8 changes: 8 additions & 0 deletions AutoFixture.TUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
LICENCE.txt = LICENCE.txt
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestTypeFoundation", "tests\TestTypeFoundation\TestTypeFoundation.csproj", "{5F812210-5B61-4CBF-A033-4C33067A5DE9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +43,12 @@ Global
{4056BD12-5FA8-4D2E-274A-7E85E858A5FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4056BD12-5FA8-4D2E-274A-7E85E858A5FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4056BD12-5FA8-4D2E-274A-7E85E858A5FC}.Verify|Any CPU.ActiveCfg = Verify|Any CPU
{5F812210-5B61-4CBF-A033-4C33067A5DE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F812210-5B61-4CBF-A033-4C33067A5DE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F812210-5B61-4CBF-A033-4C33067A5DE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F812210-5B61-4CBF-A033-4C33067A5DE9}.Release|Any CPU.Build.0 = Release|Any CPU
{5F812210-5B61-4CBF-A033-4C33067A5DE9}.Verify|Any CPU.ActiveCfg = Verify|Any CPU
{5F812210-5B61-4CBF-A033-4C33067A5DE9}.Verify|Any CPU.Build.0 = Verify|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 2 additions & 0 deletions AutoFixture.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dotnet_diagnostic.CA5394.severity = none # CA5394: Do not use insecure randomnes

## StyleCop Analyzers ##

dotnet_diagnostic.SA1101.severity = none # SA1101: Prefix local calls with this.
dotnet_diagnostic.SA1116.severity = none # SA1116: The parameters should begin on the line after the declaration, whenever the parameter span across multiple line
dotnet_diagnostic.SA1117.severity = none # SA1117: The parameters should all be placed on the same line or each parameter should be placed on its own line.
dotnet_diagnostic.SA1118.severity = none # SA1118: The parameter spans multiple lines
Expand All @@ -35,6 +36,7 @@ dotnet_diagnostic.SA1202.severity = none # SA1202: 'public' members should come
dotnet_diagnostic.SA1203.severity = none # SA1203: Constant fields should appear before non-constant fields
dotnet_diagnostic.SA1204.severity = none # SA1204: Static members should appear before non-static members
dotnet_diagnostic.SA1214.severity = none # SA1214: Readonly fields should appear before non-readonly fields
dotnet_diagnostic.SA1309.severity = none # SA1309: Field names should not begin with an underscore
dotnet_diagnostic.SA1413.severity = none # SA1413: Use trailing comma in multi-line initializers
dotnet_diagnostic.SA1501.severity = none # SA1501: Statement should not be on a single line
dotnet_diagnostic.SA1503.severity = none # SA1503: Braces should not be omitted
Expand Down
84 changes: 42 additions & 42 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,52 @@
"continuous",
GitHubActionsImage.WindowsLatest,
AutoGenerate = false,
OnPullRequestBranches = new[] { MasterBranch, ReleaseBranch },
OnPullRequestBranches = [MasterBranch, ReleaseBranch],
PublishArtifacts = false,
InvokedTargets = new[] { nameof(Verify), nameof(Cover), nameof(Pack) },
InvokedTargets = [nameof(Verify), nameof(Cover), nameof(Pack)],
EnableGitHubToken = true)]
[GitHubActions(
"release",
GitHubActionsImage.WindowsLatest,
AutoGenerate = false,
OnPushTags = new[] { "v*" },
OnPushTags = ["v*"],
PublishArtifacts = true,
InvokedTargets = new[] { nameof(Verify), nameof(Cover), nameof(Publish) },
InvokedTargets = [nameof(Verify), nameof(Cover), nameof(Publish)],
EnableGitHubToken = true,
ImportSecrets = new[] { Secrets.NuGetApiKey })]
partial class Build : NukeBuild
ImportSecrets = [Secrets.NuGetApiKey])]
class Build : NukeBuild
{
public static int Main() => Execute<Build>(x => x.Compile);

const string MasterBranch = "master";
const string ReleaseBranch = "release/*";

[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
readonly Configuration _configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;

[Solution] readonly Solution Solution;
[GitRepository] readonly GitRepository GitRepository;
[GitVersion] readonly GitVersion GitVersion;
[CI] readonly GitHubActions GitHubActions;
[Solution("AutoFixture.TUnit.sln")] readonly Solution _solution;
[GitRepository] readonly GitRepository _gitRepository;
[GitVersion] readonly GitVersion _gitVersion;
[CI] readonly GitHubActions _gitHubActions;

[Parameter("GitHub auth token", Name = "github-token"), Secret] readonly string GitHubToken;
[Parameter("Forces the continuous integration build flag")] readonly bool CI;
[Parameter("GitHub auth token", Name = "github-token"), Secret] readonly string _gitHubToken;
[Parameter("Forces the continuous integration build flag")] readonly bool _ci;

[Secret] [Parameter("NuGet API Key (secret)", Name = Secrets.NuGetApiKey)] readonly string NuGetApiKey;
readonly string NuGetSource = "https://api.nuget.org/v3/index.json";
[Secret][Parameter("NuGet API Key (secret)", Name = Secrets.NuGetApiKey)] readonly string _nuGetApiKey;
readonly string _nuGetSource = "https://api.nuget.org/v3/index.json";

IEnumerable<Project> Excluded => new[]
{
Solution.GetProject("_build"),
Solution.GetProject("TestTypeFoundation")
};
IEnumerable<Project> Excluded =>
[
_solution.GetProject("_build"),
_solution.GetProject("TestTypeFoundation")
];

IEnumerable<Project> TestProjects => Solution.GetAllProjects("*Tests");
IEnumerable<Project> Libraries => Solution.Projects.Except(TestProjects).Except(Excluded);
IEnumerable<Project> TestProjects => _solution.GetAllProjects("*Tests");
IEnumerable<Project> Libraries => _solution.Projects.Except(TestProjects).Except(Excluded);
IEnumerable<Project> CSharpLibraries => Libraries.Where(x => x.Is(ProjectType.CSharpProject));
IEnumerable<AbsolutePath> Packages => PackagesDirectory.GlobFiles("*.nupkg");

bool IsContinuousIntegration => IsServerBuild || CI;
bool IsContinuousIntegration => IsServerBuild || _ci;

AbsolutePath SourceDirectory => RootDirectory / "src";
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
Expand All @@ -87,7 +87,7 @@ partial class Build : NukeBuild
Target Restore => _ => _
.Executes(() =>
{
DotNetRestore(s => s.SetProjectFile(Solution));
DotNetRestore(s => s.SetProjectFile(_solution));
});

Target Verify => _ => _
Expand All @@ -96,7 +96,7 @@ partial class Build : NukeBuild
.Executes(() =>
{
DotNetBuild(s => s
.SetProjectFile(Solution)
.SetProjectFile(_solution)
.SetConfiguration(Configuration.Verify)
.SetNoRestore(FinishedTargets.Contains(Restore))
.SetContinuousIntegrationBuild(IsContinuousIntegration));
Expand All @@ -107,14 +107,14 @@ partial class Build : NukeBuild
.Executes(() =>
{
DotNetBuild(s => s
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.SetProjectFile(_solution)
.SetConfiguration(_configuration)
.SetDeterministic(IsContinuousIntegration)
.SetContinuousIntegrationBuild(IsContinuousIntegration)
.SetVersion(GitVersion.NuGetVersionV2)
.SetAssemblyVersion(GitVersion.AssemblySemVer)
.SetFileVersion(GitVersion.AssemblySemFileVer)
.SetInformationalVersion(GitVersion.InformationalVersion)
.SetVersion(_gitVersion.NuGetVersionV2)
.SetAssemblyVersion(_gitVersion.AssemblySemVer)
.SetFileVersion(_gitVersion.AssemblySemFileVer)
.SetInformationalVersion(_gitVersion.InformationalVersion)
.SetNoRestore(FinishedTargets.Contains(Restore)));
});

Expand All @@ -124,8 +124,8 @@ partial class Build : NukeBuild
.Executes(() =>
{
DotNetTest(s => s
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.SetProjectFile(_solution)
.SetConfiguration(_configuration)
.SetResultsDirectory(TestResultsDirectory)
.SetNoBuild(FinishedTargets.Contains(Compile))
.When(_ => InvokedTargets.Contains(Cover), _ => _
Expand All @@ -147,7 +147,7 @@ partial class Build : NukeBuild
.Executes(() =>
{
ReportGenerator(_ => _
.SetFramework("net5.0")
.SetFramework("net8.0")
.SetAssemblyFilters("-TestTypeFoundation*")
.SetReports(TestResultsDirectory / "**" / "coverage.cobertura.xml")
.SetTargetDirectory(ReportsDirectory)
Expand All @@ -166,17 +166,17 @@ partial class Build : NukeBuild
.Executes(() =>
{
DotNetPack(s => s
.SetConfiguration(Configuration)
.SetConfiguration(_configuration)
.SetNoBuild(FinishedTargets.Contains(Compile))
.SetOutputDirectory(PackagesDirectory)
.SetSymbolPackageFormat(DotNetSymbolPackageFormat.snupkg)
.EnableIncludeSymbols()
.SetDeterministic(IsContinuousIntegration)
.SetContinuousIntegrationBuild(IsContinuousIntegration)
.SetVersion(GitVersion.NuGetVersionV2)
.SetAssemblyVersion(GitVersion.AssemblySemVer)
.SetFileVersion(GitVersion.AssemblySemFileVer)
.SetInformationalVersion(GitVersion.InformationalVersion)
.SetVersion(_gitVersion.NuGetVersionV2)
.SetAssemblyVersion(_gitVersion.AssemblySemVer)
.SetFileVersion(_gitVersion.AssemblySemFileVer)
.SetInformationalVersion(_gitVersion.InformationalVersion)
.CombineWith(CSharpLibraries, (s, p) => s.SetProject(p)));
});

Expand All @@ -188,10 +188,10 @@ partial class Build : NukeBuild
DotNetNuGetPush(s => s
.EnableSkipDuplicate()
.When(
_ => GitHubActions.IsOnSemVerTag(),
_ => _gitHubActions.IsOnSemVerTag(),
v => v
.SetApiKey(NuGetApiKey)
.SetSource(NuGetSource))
.SetApiKey(_nuGetApiKey)
.SetSource(_nuGetSource))
.CombineWith(Packages, (_, p) => _.SetTargetPath(p)));
});

Expand Down
17 changes: 8 additions & 9 deletions build/GitHubActionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Text.RegularExpressions;

namespace Nuke.Common.CI.GitHubActions
namespace Nuke.Common.CI.GitHubActions;

public static class GitHubActionsExtensions
{
public static class GitHubActionsExtensions
{
private static readonly Regex SemVerRef = new(@"^refs\/tags\/v(?<version>\d+\.\d+\.\d+)", RegexOptions.Compiled);
private static readonly Regex SemVerRef = new(@"^refs\/tags\/v(?<version>\d+\.\d+\.\d+)", RegexOptions.Compiled);

public static bool IsOnSemVerTag(this GitHubActions source)
{
return !string.IsNullOrWhiteSpace(source?.Ref)
&& SemVerRef.IsMatch(source.Ref);
}
public static bool IsOnSemVerTag(this GitHubActions source)
{
return !string.IsNullOrWhiteSpace(source?.Ref)
&& SemVerRef.IsMatch(source.Ref);
}
}
52 changes: 52 additions & 0 deletions src/AutoFixture.TUnit/ArgumentsAutoDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Diagnostics.CodeAnalysis;
using AutoFixture.TUnit.Internal;

namespace AutoFixture.TUnit;

/// <summary>
/// Provides a data source for a data theory, with the data coming from inline
/// values combined with auto-generated data specimens generated by AutoFixture.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes",
Justification = "This attribute is the root of a potential attribute hierarchy.")]
public class ArgumentsAutoDataAttribute : AutoFixtureDataSourceAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentsAutoDataAttribute" /> class.
/// </summary>
/// <param name="values">The data values to pass to the theory.</param>
public ArgumentsAutoDataAttribute(params object?[] values)
: this(() => new Fixture(), values)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ArgumentsAutoDataAttribute" /> class.
/// </summary>
/// <param name="fixtureFactory">The fixture factory.</param>
/// <param name="values">The data values to pass to the theory.</param>
/// <exception cref="ArgumentNullException"></exception>
protected ArgumentsAutoDataAttribute(Func<IFixture> fixtureFactory, params object?[]? values)
{
FixtureFactory = fixtureFactory ?? throw new ArgumentNullException(nameof(fixtureFactory));
Values = values ?? [null];
}

/// <summary>
/// Gets the fixture factory.
/// </summary>
public Func<IFixture> FixtureFactory { get; }

/// <summary>
/// Gets the data values to pass to the theory.
/// </summary>
public object?[] Values { get; }

/// <inheritdoc />
public override IEnumerable<object?[]> GetData(DataGeneratorMetadata dataGeneratorMetadata)
{
return new AutoDataSource(FixtureFactory, new InlineDataSource(Values))
.GenerateDataSources(dataGeneratorMetadata)
.Select(x => x());
}
}
51 changes: 51 additions & 0 deletions src/AutoFixture.TUnit/AutoDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Diagnostics.CodeAnalysis;
using AutoFixture.TUnit.Internal;

namespace AutoFixture.TUnit;

/// <summary>
/// Provides auto-generated data specimens generated by AutoFixture as an extension to
/// xUnit.net's Theory attribute.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes",
Justification = "This attribute is the root of a potential attribute hierarchy.")]
public class AutoDataAttribute : AutoFixtureDataSourceAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="AutoDataAttribute" /> class.
/// </summary>
/// <remarks>
/// <para>
/// This constructor overload initializes the <see cref="Fixture" /> to an instance of
/// <see cref="Fixture" />.
/// </para>
/// </remarks>
public AutoDataAttribute()
: this(() => new Fixture())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AutoDataAttribute" /> class
/// with the supplied <paramref name="fixtureFactory" />. Fixture will be created
/// on demand using the provided factory.
/// </summary>
/// <param name="fixtureFactory">The fixture factory used to construct the fixture.</param>
protected AutoDataAttribute(Func<IFixture> fixtureFactory)
{
FixtureFactory = fixtureFactory ?? throw new ArgumentNullException(nameof(fixtureFactory));
}

/// <summary>
/// Gets the fixture factory.
/// </summary>
public Func<IFixture> FixtureFactory { get; }

/// <inheritdoc />
public override IEnumerable<object?[]> GetData(DataGeneratorMetadata dataGeneratorMetadata)
{
var source = new AutoDataSource(FixtureFactory);

return source.GenerateDataSources(dataGeneratorMetadata).Select(x => x());
}
}
Loading