Skip to content
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

Update TUnit #525

Merged
merged 21 commits into from
Jul 13, 2024
1 change: 1 addition & 0 deletions src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
19 changes: 15 additions & 4 deletions src/ModularPipelines.GitHub/GitHubRepositoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Text.RegularExpressions;
using Initialization.Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ModularPipelines.Enums;
using ModularPipelines.Git;
using ModularPipelines.Git.Options;
Expand All @@ -13,6 +15,7 @@ namespace ModularPipelines.GitHub;
internal record GitHubRepositoryInfo : IGitHubRepositoryInfo, IInitializer
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<GitHubRepositoryInfo> _logger;

public bool IsInitialized { get; private set; }

Expand All @@ -24,9 +27,10 @@ internal record GitHubRepositoryInfo : IGitHubRepositoryInfo, IInitializer

public string? RepositoryName { get; private set; }

public GitHubRepositoryInfo(IServiceProvider serviceProvider)
public GitHubRepositoryInfo(IServiceProvider serviceProvider, ILogger<GitHubRepositoryInfo> logger)
{
_serviceProvider = serviceProvider;
_logger = logger;
}

public async Task InitializeAsync()
Expand All @@ -38,27 +42,34 @@ public async Task InitializeAsync()

await using var scope = _serviceProvider.CreateAsyncScope();
var git = scope.ServiceProvider.GetRequiredService<IGit>();

var options = new GitRemoteOptions
{
Arguments = ["get-url", "origin"],
ThrowOnNonZeroExitCode = false,
CommandLogging = CommandLogging.None,
CommandLogging = scope.ServiceProvider
.GetRequiredService<IOptions<LoggerFilterOptions>>()
.Value
.MinLevel == LogLevel.Debug
? CommandLogging.Default
: CommandLogging.None,
};

var remote = await git.Commands.Remote(options);
var remoteUrl = remote.StandardOutput;

if (string.IsNullOrEmpty(remoteUrl))
{
_logger.LogWarning("Error when detecting GitHub git repository: {Error}", remote.StandardError);

// Will not initialize as git repo is not setup
return;
}

// Parse owner and repository name from the remote URL
var endpoint = "github";
var sshPattern = $@"git@{endpoint}\.com:(?<owner>.+)/(?<name>.+)\.git";
var httpsPattern = $@"https://{endpoint}\.com/(?<owner>.+)/(?<name>.+)(\.git)?";
var httpsPattern = $@"https://(.*@)?{endpoint}\.com/(?<owner>.+)/(?<name>.+)(\.git)?";

var match = Regex.Match(remoteUrl, sshPattern);
if (!match.Success)
Expand Down
5 changes: 5 additions & 0 deletions src/ModularPipelines/Engine/DependencyPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@

private void Print(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return;

Check warning on line 49 in src/ModularPipelines/Engine/DependencyPrinter.cs

View check run for this annotation

Codecov / codecov/patch

src/ModularPipelines/Engine/DependencyPrinter.cs#L49

Added line #L49 was not covered by tests
}

Console.WriteLine();
_collapsableLogging.StartConsoleLogGroupDirectToConsole("Dependency Chains");
_logger.LogInformation("The following dependency chains have been detected:\r\n{Chain}", value);
Expand Down
17 changes: 15 additions & 2 deletions src/ModularPipelines/Engine/OptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ public OptionsProvider(IPipelineServiceContainerWrapper pipelineServiceContainer
var types = _pipelineServiceContainerWrapper.ServiceCollection
.Select(sd => sd.ServiceType)
.Where(t => t.IsGenericType)
.Where(t => t.GetGenericTypeDefinition().IsAssignableTo(typeof(IConfigureOptions<>)) || t.GetGenericTypeDefinition().IsAssignableTo(typeof(IPostConfigureOptions<>)))
.Where(x => x.IsConstructedGenericType)
.Where(t =>
{
var genericTypeDefinition = t.GetGenericTypeDefinition();

return genericTypeDefinition.IsAssignableTo(typeof(IConfigureOptions<>))
|| genericTypeDefinition.IsAssignableTo(typeof(IPostConfigureOptions<>))
|| genericTypeDefinition.IsAssignableTo(typeof(IOptions<>))
|| genericTypeDefinition.IsAssignableTo(typeof(IOptionsMonitor<>))
|| genericTypeDefinition.IsAssignableTo(typeof(IOptionsSnapshot<>))
|| genericTypeDefinition.IsAssignableTo(typeof(IValidateOptions<>))
|| genericTypeDefinition.IsAssignableTo(typeof(IConfigureNamedOptions<>));
})
.Select(s => s.GetGenericArguments()[0])
.Distinct()
.ToList();

foreach (var option in types.Select(t => _serviceProvider.GetService(typeof(IOptions<>).MakeGenericType([t]))))
foreach (var option in types.Select(t => _serviceProvider.GetService(typeof(IOptions<>).MakeGenericType(t))))
{
yield return option!.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance)!.GetValue(option);
}
Expand Down
4 changes: 2 additions & 2 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
</PropertyGroup>

<ItemGroup Condition="'$(MSBuildProjectName)' != 'ModularPipelines.TestsForTests'">
<PackageReference Include="TUnit" Version="0.1.401-alpha01" />
<PackageReference Include="TUnit" Version="0.1.442" />
</ItemGroup>
</Project>
</Project>
6 changes: 4 additions & 2 deletions test/ModularPipelines.TestHelpers/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ private class DummyModule : Module
}
}

public async Task<T> RunModule<T>()
public Task<T> RunModule<T>() where T : ModuleBase => RunModule<T>(new TestHostSettings());

public async Task<T> RunModule<T>(TestHostSettings testHostSettings)
where T : ModuleBase
{
var host = await TestPipelineHostBuilder.Create()
var host = await TestPipelineHostBuilder.Create(testHostSettings)
.AddModule<T>()
.BuildHostAsync();

Expand Down
11 changes: 11 additions & 0 deletions test/ModularPipelines.TestHelpers/TestHostSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Extensions.Logging;
using ModularPipelines.Enums;

namespace ModularPipelines.TestHelpers;

public record TestHostSettings

Check warning on line 6 in test/ModularPipelines.TestHelpers/TestHostSettings.cs

View check run for this annotation

Codecov / codecov/patch

test/ModularPipelines.TestHelpers/TestHostSettings.cs#L6

Added line #L6 was not covered by tests
{
public CommandLogging CommandLogging { get; init; } = CommandLogging.Input | CommandLogging.Error;
public LogLevel LogLevel { get; init; } = LogLevel.Warning;
public bool ClearLogProviders { get; init; } = true;
}
14 changes: 10 additions & 4 deletions test/ModularPipelines.TestHelpers/TestPipelineHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ namespace ModularPipelines.TestHelpers;

public static class TestPipelineHostBuilder
{
public static PipelineHostBuilder Create()
public static PipelineHostBuilder Create() => Create(new TestHostSettings());

public static PipelineHostBuilder Create(TestHostSettings testHostSettings)
{
return new PipelineHostBuilder()
.SetLogLevel(LogLevel.Warning)
.SetLogLevel(testHostSettings.LogLevel)
.ConfigureServices((_, collection) =>
{
collection.AddSingleton(new ArmClient(new DefaultAzureCredential()));
collection.Configure<PipelineOptions>(opt =>
{
opt.DefaultCommandLogging = CommandLogging.Input | CommandLogging.Error;
opt.DefaultCommandLogging = testHostSettings.CommandLogging;
opt.ShowProgressInConsole = false;
opt.PrintResults = false;
});
collection.AddLogging(builder => builder.ClearProviders());

if(testHostSettings.ClearLogProviders)
{
collection.AddLogging(builder => builder.ClearProviders());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using ModularPipelines.Attributes;
using ModularPipelines.Attributes;
using ModularPipelines.Helpers;
using TUnit.Assertions.Extensions;

namespace ModularPipelines.UnitTests.Attributes;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
using TUnit.Core.Exceptions;
using TUnit.Core.Interfaces;

namespace ModularPipelines.UnitTests.Attributes;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class LinuxOnlyTestAttribute : Attribute, IApplicableTestAttribute
public class LinuxOnlyTestAttribute() : SkipAttribute("Linux only test")
{
/// <inheritdoc/>
public Task Apply(TestContext testContext)
public override Task<bool> ShouldSkip(TestContext testContext)
{
if (!OperatingSystem.IsLinux())
{
throw new SkipTestException("Linux only test");
}

return Task.CompletedTask;
return Task.FromResult(!OperatingSystem.IsLinux());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
using TUnit.Core.Exceptions;
using TUnit.Core.Interfaces;

namespace ModularPipelines.UnitTests.Attributes;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class WindowsOnlyTestAttribute : Attribute, IApplicableTestAttribute
public class WindowsOnlyTestAttribute() : SkipAttribute("Windows only test")
{
/// <inheritdoc/>
public Task Apply(TestContext testContext)
public override Task<bool> ShouldSkip(TestContext testContext)
{
if (!OperatingSystem.IsWindows())
{
throw new SkipTestException("Windows only test");
}

return Task.CompletedTask;
return Task.FromResult(!OperatingSystem.IsWindows());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ModularPipelines.Context;
using Microsoft.Extensions.Logging;
using ModularPipelines.Context;
using ModularPipelines.Enums;
using ModularPipelines.GitHub;
using ModularPipelines.GitHub.Extensions;
using ModularPipelines.Modules;
Expand All @@ -21,19 +23,30 @@ public class GitRepoModule : Module<IGitHubRepositoryInfo>
[Test]
public async Task GitHub_Repository_Information_Is_Populated()
{
var gitRepoModule = await RunModule<GitRepoModule>();
var gitRepoModule = await RunModule<GitRepoModule>(new TestHostSettings
{
CommandLogging = CommandLogging.Default,
LogLevel = LogLevel.Debug,
ClearLogProviders = false
});

var gitHubRepositoryInfo = gitRepoModule.Result.Value!;

await Assert.That(gitHubRepositoryInfo.IsGitHub).Is.True();
await Assert.That(gitHubRepositoryInfo.IsInitialized).Is.True();
await Assert.That(gitHubRepositoryInfo.RepositoryName).Is.Not.Null()
.And.Is.Not.Empty();
await Assert.That(gitHubRepositoryInfo.Owner).Is.Not.Null()
.And.Is.Not.Empty();
await Assert.That(gitHubRepositoryInfo.Endpoint).Is.Not.Null()
.And.Is.Not.Empty();
await Assert.That(gitHubRepositoryInfo.Identifier).Is.Not.Null()
.And.Is.Not.Empty();
Console.WriteLine($"GitHub Repository Info is: {gitHubRepositoryInfo}");

await using (Assert.Multiple())
{
await Assert.That(gitHubRepositoryInfo).Is.Not.Null();
await Assert.That(gitHubRepositoryInfo.IsInitialized).Is.True();
await Assert.That(gitHubRepositoryInfo.IsGitHub).Is.True();
await Assert.That(gitHubRepositoryInfo.RepositoryName).Is.Not.Null()
.And.Is.Not.Empty();
await Assert.That(gitHubRepositoryInfo.Owner).Is.Not.Null()
.And.Is.Not.Empty();
await Assert.That(gitHubRepositoryInfo.Endpoint).Is.Not.Null()
.And.Is.Not.Empty();
await Assert.That(gitHubRepositoryInfo.Identifier).Is.Not.Null()
.And.Is.Not.Empty();
}
}
}
Loading