ユニットテストプロジェクトを追加
このコミットが含まれているのは:
11ファイル変更
+900
検索条件に一致するファイルはありません
@@ -0,0 +1,36 @@
|
||||
using GitHubUpdateNotifier;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
internal sealed class FakeReleaseProvider : IReleaseProvider
|
||||
{
|
||||
private readonly Func<CancellationToken, Task<IReadOnlyList<ReleaseData>>> _behavior;
|
||||
|
||||
public UpdateSource Source { get; }
|
||||
public int CallCount { get; private set; }
|
||||
public int LastRetryCount { get; private set; } = -1;
|
||||
public RepositoryReference? LastRepository { get; private set; }
|
||||
|
||||
private FakeReleaseProvider(UpdateSource source, Func<CancellationToken, Task<IReadOnlyList<ReleaseData>>> behavior)
|
||||
{
|
||||
Source = source;
|
||||
_behavior = behavior;
|
||||
}
|
||||
|
||||
public static FakeReleaseProvider Returning(UpdateSource source, params ReleaseData[] releases)
|
||||
=> new(source, _ => Task.FromResult<IReadOnlyList<ReleaseData>>(releases));
|
||||
|
||||
public static FakeReleaseProvider Throwing(UpdateSource source, Exception error)
|
||||
=> new(source, _ => Task.FromException<IReadOnlyList<ReleaseData>>(error));
|
||||
|
||||
public static FakeReleaseProvider Cancelling(UpdateSource source)
|
||||
=> new(source, ct => Task.FromException<IReadOnlyList<ReleaseData>>(new OperationCanceledException(ct)));
|
||||
|
||||
public Task<IReadOnlyList<ReleaseData>> GetReleasesAsync(RepositoryReference repository, int retryCount, CancellationToken cancellationToken)
|
||||
{
|
||||
CallCount++;
|
||||
LastRetryCount = retryCount;
|
||||
LastRepository = repository;
|
||||
return _behavior(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Exe</OutputType>
|
||||
<IsPackable>false</IsPackable>
|
||||
<AssemblyName>GitHubUpdateNotifier.Tests</AssemblyName>
|
||||
<RootNamespace>NotifierTests</RootNamespace>
|
||||
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.7.0" />
|
||||
<PackageReference Include="xunit.v3" Version="3.2.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GitHubUpdateNotifier\GitHubUpdateNotifier.csproj" />
|
||||
<Reference Include="YukkuriMovieMaker.Plugin">
|
||||
<HintPath>$(YMM4DirPath)YukkuriMovieMaker.Plugin.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,71 @@
|
||||
using GitHubUpdateNotifier;
|
||||
using Xunit;
|
||||
using static NotifierTests.TestSupport;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
public class ModelTests
|
||||
{
|
||||
[Fact]
|
||||
public void UpdateCheckResult_UpdateAvailableStatusWithoutUpdate_HasUpdateFalse()
|
||||
{
|
||||
var result = new UpdateCheckResult { Status = UpdateStatus.UpdateAvailable };
|
||||
Assert.False(result.HasUpdate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateInfo_FullName_CombinesOwnerAndName()
|
||||
=> Assert.Equal("acme/widget", SampleInfo().FullName);
|
||||
|
||||
[Fact]
|
||||
public void UpdateNotification_CarriesUpdateAndMessage()
|
||||
{
|
||||
var info = SampleInfo();
|
||||
var notification = new UpdateNotification(info, "hello");
|
||||
Assert.Same(info, notification.Update);
|
||||
Assert.Equal("hello", notification.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateCheckException_NoFailures_HasNoInner()
|
||||
{
|
||||
var exception = new UpdateCheckException(Repo("a", "b"), []);
|
||||
Assert.Null(exception.InnerException);
|
||||
Assert.Equal("a", exception.Owner);
|
||||
Assert.Equal("b", exception.Repository);
|
||||
Assert.Empty(exception.Failures);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateCheckException_SingleFailure_InnerIsThatException()
|
||||
{
|
||||
var inner = new InvalidOperationException("x");
|
||||
var exception = new UpdateCheckException(Repo(), [inner]);
|
||||
Assert.Same(inner, exception.InnerException);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateCheckException_MultipleFailures_InnerIsAggregate()
|
||||
{
|
||||
var first = new InvalidOperationException("1");
|
||||
var second = new InvalidOperationException("2");
|
||||
var exception = new UpdateCheckException(Repo(), [first, second]);
|
||||
var aggregate = Assert.IsType<AggregateException>(exception.InnerException);
|
||||
Assert.Equal(2, aggregate.InnerExceptions.Count);
|
||||
}
|
||||
|
||||
private static UpdateInfo SampleInfo()
|
||||
=> new()
|
||||
{
|
||||
Owner = "acme",
|
||||
Name = "widget",
|
||||
Version = Ver("1.0.0"),
|
||||
TagName = "v1.0.0",
|
||||
ReleaseName = "r",
|
||||
Body = "",
|
||||
IsPrerelease = false,
|
||||
PublishedAt = default,
|
||||
ReleaseUrl = new Uri("https://example.com/"),
|
||||
Source = UpdateSource.GitHub,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
using System.Net.Http;
|
||||
using GitHubUpdateNotifier;
|
||||
using Xunit;
|
||||
using static NotifierTests.TestSupport;
|
||||
using Notifier = GitHubUpdateNotifier.GitHubUpdateNotifier;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
public class NotifierFlowTests
|
||||
{
|
||||
private static Notifier Create(UpdateNotifierOptions options, params IReleaseProvider[] providers)
|
||||
=> new("owner", "repo", options, providers);
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Constructor_InvalidOwner_Throws(string? owner)
|
||||
=> Assert.ThrowsAny<ArgumentException>(() => new Notifier(owner!, "repo", Options("1.0.0")));
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Constructor_InvalidRepository_Throws(string? repository)
|
||||
=> Assert.ThrowsAny<ArgumentException>(() => new Notifier("owner", repository!, Options("1.0.0")));
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullOptions_Throws()
|
||||
=> Assert.Throws<ArgumentNullException>(() => new Notifier("owner", "repo", null!));
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullCurrentVersion_Throws()
|
||||
{
|
||||
var options = new UpdateNotifierOptions
|
||||
{
|
||||
CurrentVersion = null!,
|
||||
MessageFormatter = _ => "",
|
||||
NotificationHandler = (_, _) => Task.CompletedTask,
|
||||
};
|
||||
Assert.Throws<ArgumentNullException>(() => new Notifier("owner", "repo", options));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullMessageFormatter_Throws()
|
||||
{
|
||||
var options = new UpdateNotifierOptions
|
||||
{
|
||||
CurrentVersion = Ver("1.0.0"),
|
||||
MessageFormatter = null!,
|
||||
NotificationHandler = (_, _) => Task.CompletedTask,
|
||||
};
|
||||
Assert.Throws<ArgumentNullException>(() => new Notifier("owner", "repo", options));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullNotificationHandler_Throws()
|
||||
{
|
||||
var options = new UpdateNotifierOptions
|
||||
{
|
||||
CurrentVersion = Ver("1.0.0"),
|
||||
MessageFormatter = _ => "",
|
||||
NotificationHandler = null!,
|
||||
};
|
||||
Assert.Throws<ArgumentNullException>(() => new Notifier("owner", "repo", options));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PublicConstructor_BuildsWithDefaultProviders()
|
||||
=> Assert.NotNull(new Notifier("owner", "repo", Options("1.0.0")));
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_UpdateAvailable()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v2.0.0"));
|
||||
var result = await Create(Options("1.0.0"), provider).CheckAsync();
|
||||
Assert.Equal(UpdateStatus.UpdateAvailable, result.Status);
|
||||
Assert.True(result.HasUpdate);
|
||||
Assert.NotNull(result.Update);
|
||||
Assert.Equal(new Version(2, 0, 0), result.LatestVersion!.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_UpToDate_WhenEqual()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
var result = await Create(Options("1.0.0"), provider).CheckAsync();
|
||||
Assert.Equal(UpdateStatus.UpToDate, result.Status);
|
||||
Assert.False(result.HasUpdate);
|
||||
Assert.Null(result.Update);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_UpToDate_WhenCurrentNewer()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
var result = await Create(Options("2.0.0"), provider).CheckAsync();
|
||||
Assert.Equal(UpdateStatus.UpToDate, result.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_NoReleases_UpToDateWithCurrent()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox);
|
||||
var result = await Create(Options("1.5.0"), provider).CheckAsync();
|
||||
Assert.Equal(UpdateStatus.UpToDate, result.Status);
|
||||
Assert.Equal(new Version(1, 5, 0), result.LatestVersion!.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_AllProvidersFail_ReturnsFailed()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Throwing(UpdateSource.Manjubox, new HttpRequestException("boom"));
|
||||
var result = await Create(Options("1.0.0"), provider).CheckAsync();
|
||||
Assert.Equal(UpdateStatus.Failed, result.Status);
|
||||
Assert.IsType<UpdateCheckException>(result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_DoesNotInvokeHandler()
|
||||
{
|
||||
var handlerCalls = 0;
|
||||
var options = Options("1.0.0", handler: (_, _) => { handlerCalls++; return Task.CompletedTask; });
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v2.0.0"));
|
||||
await Create(options, provider).CheckAsync();
|
||||
Assert.Equal(0, handlerCalls);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_Cancellation_Throws()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Cancelling(UpdateSource.Manjubox);
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => Create(Options("1.0.0"), provider).CheckAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_PrereleaseExcludedByDefault()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox,
|
||||
Release("v1.0.0"), Release("v2.0.0", prerelease: true));
|
||||
var result = await Create(Options("1.0.0", includePrerelease: false), provider).CheckAsync();
|
||||
Assert.Equal(UpdateStatus.UpToDate, result.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckAsync_PrereleaseIncludedWhenRequested()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox,
|
||||
Release("v1.0.0"), Release("v2.0.0", prerelease: true));
|
||||
var result = await Create(Options("1.0.0", includePrerelease: true), provider).CheckAsync();
|
||||
Assert.Equal(UpdateStatus.UpdateAvailable, result.Status);
|
||||
Assert.True(result.Update!.IsPrerelease);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NotifyAsync_InvokesHandlerWithFormattedMessage()
|
||||
{
|
||||
UpdateNotification? captured = null;
|
||||
var options = Options("1.0.0",
|
||||
formatter: update => $"new {update.TagName}",
|
||||
handler: (notification, _) => { captured = notification; return Task.CompletedTask; });
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v2.0.0"));
|
||||
var result = await Create(options, provider).NotifyAsync();
|
||||
Assert.True(result.HasUpdate);
|
||||
Assert.NotNull(captured);
|
||||
Assert.Equal("new v2.0.0", captured!.Message);
|
||||
Assert.Same(result.Update, captured.Update);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NotifyAsync_NoUpdate_DoesNotInvokeHandler()
|
||||
{
|
||||
var calls = 0;
|
||||
var options = Options("2.0.0", handler: (_, _) => { calls++; return Task.CompletedTask; });
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
var result = await Create(options, provider).NotifyAsync();
|
||||
Assert.Equal(UpdateStatus.UpToDate, result.Status);
|
||||
Assert.Equal(0, calls);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NotifyAsync_Failed_DoesNotInvokeHandler()
|
||||
{
|
||||
var calls = 0;
|
||||
var options = Options("1.0.0", handler: (_, _) => { calls++; return Task.CompletedTask; });
|
||||
var provider = FakeReleaseProvider.Throwing(UpdateSource.Manjubox, new HttpRequestException("x"));
|
||||
var result = await Create(options, provider).NotifyAsync();
|
||||
Assert.Equal(UpdateStatus.Failed, result.Status);
|
||||
Assert.Equal(0, calls);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NotifyAsync_FormatterInvokedExactlyOnce()
|
||||
{
|
||||
var formatCalls = 0;
|
||||
var options = Options("1.0.0", formatter: update => { formatCalls++; return update.TagName; });
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v2.0.0"));
|
||||
await Create(options, provider).NotifyAsync();
|
||||
Assert.Equal(1, formatCalls);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NotifyAsync_PassesCancellationTokenToHandler()
|
||||
{
|
||||
CancellationToken received = default;
|
||||
using var cts = new CancellationTokenSource();
|
||||
var options = Options("1.0.0", handler: (_, ct) => { received = ct; return Task.CompletedTask; });
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v2.0.0"));
|
||||
await Create(options, provider).NotifyAsync(cts.Token);
|
||||
Assert.Equal(cts.Token, received);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Constructor_NegativeRetryCount_ClampedToZero()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
await Create(Options("1.0.0", retryCount: -5), provider).CheckAsync();
|
||||
Assert.Equal(0, provider.LastRetryCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Constructor_RetryCountForwarded()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
await Create(Options("1.0.0", retryCount: 7), provider).CheckAsync();
|
||||
Assert.Equal(7, provider.LastRetryCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using GitHubUpdateNotifier;
|
||||
using Xunit;
|
||||
using static NotifierTests.TestSupport;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
public class ReleaseProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public void Manjubox_Source_IsManjubox()
|
||||
=> Assert.Equal(UpdateSource.Manjubox, new ManjuboxReleaseProvider().Source);
|
||||
|
||||
[Fact]
|
||||
public void GitHub_Source_IsGitHub()
|
||||
=> Assert.Equal(UpdateSource.GitHub, new GitHubReleaseProvider().Source);
|
||||
|
||||
[Fact]
|
||||
public void Manjubox_BuildsDetailEndpoint()
|
||||
{
|
||||
var uri = InvokeBuildRequestUri(new ManjuboxReleaseProvider(), Repo("acme", "widget"));
|
||||
Assert.Equal("https://manjubox.net/api/ymm4plugins/github/detail/acme/widget", uri);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GitHub_BuildsReleasesEndpoint()
|
||||
{
|
||||
var uri = InvokeBuildRequestUri(new GitHubReleaseProvider(), Repo("acme", "widget"));
|
||||
Assert.Equal("https://api.github.com/repos/acme/widget/releases", uri);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Manjubox_EscapesSegments()
|
||||
{
|
||||
var uri = InvokeBuildRequestUri(new ManjuboxReleaseProvider(), Repo("a b", "c/d"));
|
||||
Assert.Equal("https://manjubox.net/api/ymm4plugins/github/detail/a%20b/c%2Fd", uri);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GitHub_EscapesSegments()
|
||||
{
|
||||
var uri = InvokeBuildRequestUri(new GitHubReleaseProvider(), Repo("a b", "c/d"));
|
||||
Assert.Equal("https://api.github.com/repos/a%20b/c%2Fd/releases", uri);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GitHub_ConfigureRequest_SetsAcceptAndApiVersionHeaders()
|
||||
{
|
||||
var request = InvokeConfigureRequest(new GitHubReleaseProvider());
|
||||
Assert.Contains(request.Headers.Accept, header => header.MediaType == "application/vnd.github+json");
|
||||
Assert.True(request.Headers.Contains("X-GitHub-Api-Version"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Manjubox_ConfigureRequest_LeavesRequestUnchanged()
|
||||
{
|
||||
var request = InvokeConfigureRequest(new ManjuboxReleaseProvider());
|
||||
Assert.Empty(request.Headers.Accept);
|
||||
Assert.False(request.Headers.Contains("X-GitHub-Api-Version"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System.Net.Http;
|
||||
using GitHubUpdateNotifier;
|
||||
using Xunit;
|
||||
using static NotifierTests.TestSupport;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
public class ReleaseResolverTests
|
||||
{
|
||||
private static ReleaseResolver Resolver(int retryCount, params IReleaseProvider[] providers)
|
||||
=> new(providers, retryCount);
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_FirstProviderSucceeds_ReturnsItsReleases()
|
||||
{
|
||||
var first = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
var second = FakeReleaseProvider.Returning(UpdateSource.GitHub, Release("v2.0.0"));
|
||||
var resolved = await Resolver(2, first, second).ResolveAsync(Repo(), default);
|
||||
Assert.Equal(UpdateSource.Manjubox, resolved.Source);
|
||||
Assert.Single(resolved.Releases);
|
||||
Assert.Equal(0, second.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_FirstFails_FallsBackToSecond()
|
||||
{
|
||||
var first = FakeReleaseProvider.Throwing(UpdateSource.Manjubox, new HttpRequestException("404"));
|
||||
var second = FakeReleaseProvider.Returning(UpdateSource.GitHub, Release("v2.0.0"));
|
||||
var resolved = await Resolver(2, first, second).ResolveAsync(Repo(), default);
|
||||
Assert.Equal(UpdateSource.GitHub, resolved.Source);
|
||||
Assert.Equal(1, first.CallCount);
|
||||
Assert.Equal(1, second.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_FirstReturnsEmpty_DoesNotFallBack()
|
||||
{
|
||||
var first = FakeReleaseProvider.Returning(UpdateSource.Manjubox);
|
||||
var second = FakeReleaseProvider.Returning(UpdateSource.GitHub, Release("v2.0.0"));
|
||||
var resolved = await Resolver(2, first, second).ResolveAsync(Repo(), default);
|
||||
Assert.Equal(UpdateSource.Manjubox, resolved.Source);
|
||||
Assert.Empty(resolved.Releases);
|
||||
Assert.Equal(0, second.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_AllFail_ThrowsUpdateCheckException()
|
||||
{
|
||||
var e1 = new HttpRequestException("a");
|
||||
var e2 = new InvalidOperationException("b");
|
||||
var first = FakeReleaseProvider.Throwing(UpdateSource.Manjubox, e1);
|
||||
var second = FakeReleaseProvider.Throwing(UpdateSource.GitHub, e2);
|
||||
var ex = await Assert.ThrowsAsync<UpdateCheckException>(
|
||||
() => Resolver(2, first, second).ResolveAsync(Repo("acme", "widget"), default));
|
||||
Assert.Equal("acme", ex.Owner);
|
||||
Assert.Equal("widget", ex.Repository);
|
||||
Assert.Equal(2, ex.Failures.Count);
|
||||
Assert.Same(e1, ex.Failures[0]);
|
||||
Assert.Same(e2, ex.Failures[1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_ProviderCancels_PropagatesWithoutFallback()
|
||||
{
|
||||
var first = FakeReleaseProvider.Cancelling(UpdateSource.Manjubox);
|
||||
var second = FakeReleaseProvider.Returning(UpdateSource.GitHub, Release("v2.0.0"));
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(
|
||||
() => Resolver(2, first, second).ResolveAsync(Repo(), default));
|
||||
Assert.Equal(0, second.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_PreCancelledToken_ThrowsBeforeAnyProvider()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
using var cts = new CancellationTokenSource();
|
||||
await cts.CancelAsync();
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(
|
||||
() => Resolver(2, provider).ResolveAsync(Repo(), cts.Token));
|
||||
Assert.Equal(0, provider.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResolveAsync_ForwardsRetryCountAndRepository()
|
||||
{
|
||||
var provider = FakeReleaseProvider.Returning(UpdateSource.Manjubox, Release("v1.0.0"));
|
||||
await Resolver(5, provider).ResolveAsync(Repo("acme", "widget"), default);
|
||||
Assert.Equal(5, provider.LastRetryCount);
|
||||
Assert.Equal(Repo("acme", "widget"), provider.LastRepository!.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
using GitHubUpdateNotifier;
|
||||
using Xunit;
|
||||
using static NotifierTests.TestSupport;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
public class ReleaseSelectorTests
|
||||
{
|
||||
[Fact]
|
||||
public void SelectLatest_EmptyList_ReturnsNull()
|
||||
=> Assert.Null(ReleaseSelector.SelectLatest(Repo(), Resolved(UpdateSource.GitHub), false));
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_PicksHighestVersion_NotListOrder()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.Manjubox,
|
||||
Release("v1.0.0"),
|
||||
Release("v2.5.0"),
|
||||
Release("v1.9.0"));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Equal(new Version(2, 5, 0), info!.Version.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_ExcludesDraft()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub,
|
||||
Release("v1.0.0"),
|
||||
Release("v3.0.0", draft: true));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Equal(new Version(1, 0, 0), info!.Version.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_HighestIsDraft_PicksNextNonDraft()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub,
|
||||
Release("v9.0.0", draft: true),
|
||||
Release("v2.0.0"),
|
||||
Release("v1.0.0"));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Equal(new Version(2, 0, 0), info!.Version.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_ExcludesPrerelease_WhenNotIncluded()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub,
|
||||
Release("v1.0.0"),
|
||||
Release("v2.0.0", prerelease: true));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, includePrerelease: false);
|
||||
Assert.Equal(new Version(1, 0, 0), info!.Version.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_IncludesPrerelease_WhenRequested()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub,
|
||||
Release("v1.0.0"),
|
||||
Release("v2.0.0", prerelease: true));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, includePrerelease: true);
|
||||
Assert.Equal(new Version(2, 0, 0), info!.Version.Version);
|
||||
Assert.True(info.IsPrerelease);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_SkipsUnparsableTags()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub,
|
||||
Release("not-a-version"),
|
||||
Release("v1.4.0"),
|
||||
Release(null));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Equal(new Version(1, 4, 0), info!.Version.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_AllUnparsable_ReturnsNull()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub, Release("x"), Release(null), Release(""));
|
||||
Assert.Null(ReleaseSelector.SelectLatest(Repo(), resolved, false));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_UsesHtmlUrl_WhenPresent()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub, Release("v1.0.0", htmlUrl: "https://example.com/r"));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Equal(new Uri("https://example.com/r"), info!.ReleaseUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_BuildsFallbackReleaseUrl_WhenHtmlUrlMissing()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub, Release("v1.0.0"));
|
||||
var info = ReleaseSelector.SelectLatest(Repo("acme", "widget"), resolved, false);
|
||||
Assert.Equal(new Uri("https://github.com/acme/widget/releases/tag/v1.0.0"), info!.ReleaseUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_BuildsFallbackReleaseUrl_WhenHtmlUrlNotAbsolute()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub, Release("v1.0.0", htmlUrl: "not a url"));
|
||||
var info = ReleaseSelector.SelectLatest(Repo("acme", "widget"), resolved, false);
|
||||
Assert.Equal(new Uri("https://github.com/acme/widget/releases/tag/v1.0.0"), info!.ReleaseUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_DownloadUrl_IsFirstValidAsset()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub, Release("v1.0.0", assets:
|
||||
[
|
||||
Asset("bad", null),
|
||||
Asset("good", "https://example.com/a.ymme"),
|
||||
Asset("later", "https://example.com/b.ymme"),
|
||||
]));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Equal(new Uri("https://example.com/a.ymme"), info!.DownloadUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_DownloadUrl_NullWhenNoAssets()
|
||||
{
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), Resolved(UpdateSource.GitHub, Release("v1.0.0")), false);
|
||||
Assert.Null(info!.DownloadUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_DownloadUrl_NullWhenAllAssetsInvalid()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub, Release("v1.0.0", assets: [Asset("x", null), Asset("y", "bad url")]));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Null(info!.DownloadUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_ReleaseName_FallsBackToTag()
|
||||
{
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), Resolved(UpdateSource.GitHub, Release("v1.0.0", name: null)), false);
|
||||
Assert.Equal("v1.0.0", info!.ReleaseName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_ReleaseName_UsesNameWhenPresent()
|
||||
{
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), Resolved(UpdateSource.GitHub, Release("v1.0.0", name: "Shiny")), false);
|
||||
Assert.Equal("Shiny", info!.ReleaseName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_Body_DefaultsToEmpty()
|
||||
{
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), Resolved(UpdateSource.GitHub, Release("v1.0.0", body: null)), false);
|
||||
Assert.Equal(string.Empty, info!.Body);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_PropagatesSourceAndMetadata()
|
||||
{
|
||||
var published = new DateTimeOffset(2026, 7, 14, 3, 27, 46, TimeSpan.Zero);
|
||||
var resolved = Resolved(UpdateSource.Manjubox, Release("v1.2.0", name: "n", body: "b", publishedAt: published));
|
||||
var info = ReleaseSelector.SelectLatest(Repo("acme", "widget"), resolved, false);
|
||||
Assert.Equal(UpdateSource.Manjubox, info!.Source);
|
||||
Assert.Equal("acme", info.Owner);
|
||||
Assert.Equal("widget", info.Name);
|
||||
Assert.Equal("acme/widget", info.FullName);
|
||||
Assert.Equal("v1.2.0", info.TagName);
|
||||
Assert.Equal(published, info.PublishedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectLatest_EqualVersions_KeepsFirstSeen()
|
||||
{
|
||||
var resolved = Resolved(UpdateSource.GitHub,
|
||||
Release("v1.0.0", name: "first"),
|
||||
Release("1.0.0", name: "second"));
|
||||
var info = ReleaseSelector.SelectLatest(Repo(), resolved, false);
|
||||
Assert.Equal("first", info!.ReleaseName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using System.Text.Json;
|
||||
using GitHubUpdateNotifier;
|
||||
using Xunit;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
public class ReleaseSerializationTests
|
||||
{
|
||||
private static IReadOnlyList<ReleaseData> Deserialize(string json)
|
||||
=> JsonSerializer.Deserialize<IReadOnlyList<ReleaseData>>(json, ReleaseSerialization.Options)!;
|
||||
|
||||
[Fact]
|
||||
public void Deserialize_MapsSnakeCaseFields()
|
||||
{
|
||||
const string json = """
|
||||
[
|
||||
{
|
||||
"tag_name": "v1.2.0",
|
||||
"name": "Release 1.2.0",
|
||||
"draft": false,
|
||||
"prerelease": true,
|
||||
"published_at": "2026-07-14T03:27:46Z",
|
||||
"html_url": "https://github.com/o/r/releases/tag/v1.2.0",
|
||||
"body": "changelog",
|
||||
"assets": [
|
||||
{
|
||||
"name": "a.ymme",
|
||||
"browser_download_url": "https://example.com/a.ymme",
|
||||
"content_type": "application/octet-stream",
|
||||
"size": 12345
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
""";
|
||||
var release = Assert.Single(Deserialize(json));
|
||||
Assert.Equal("v1.2.0", release.TagName);
|
||||
Assert.Equal("Release 1.2.0", release.Name);
|
||||
Assert.False(release.Draft);
|
||||
Assert.True(release.Prerelease);
|
||||
Assert.Equal(new DateTimeOffset(2026, 7, 14, 3, 27, 46, TimeSpan.Zero), release.PublishedAt);
|
||||
Assert.Equal("https://github.com/o/r/releases/tag/v1.2.0", release.HtmlUrl);
|
||||
Assert.Equal("changelog", release.Body);
|
||||
var asset = Assert.Single(release.Assets!);
|
||||
Assert.Equal("a.ymme", asset.Name);
|
||||
Assert.Equal("https://example.com/a.ymme", asset.BrowserDownloadUrl);
|
||||
Assert.Equal("application/octet-stream", asset.ContentType);
|
||||
Assert.Equal(12345, asset.Size);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deserialize_MissingFields_UseDefaults()
|
||||
{
|
||||
var release = Assert.Single(Deserialize("""[ { "tag_name": "v1.0.0" } ]"""));
|
||||
Assert.Equal("v1.0.0", release.TagName);
|
||||
Assert.Null(release.Name);
|
||||
Assert.Null(release.Body);
|
||||
Assert.Null(release.HtmlUrl);
|
||||
Assert.False(release.Draft);
|
||||
Assert.False(release.Prerelease);
|
||||
Assert.Null(release.Assets);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deserialize_IgnoresUnknownFields()
|
||||
{
|
||||
var release = Assert.Single(Deserialize("""[ { "tag_name": "v1.0.0", "unknown_field": 42, "author": { "login": "x" } } ]"""));
|
||||
Assert.Equal("v1.0.0", release.TagName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deserialize_EmptyArray_ReturnsEmpty()
|
||||
=> Assert.Empty(Deserialize("[]"));
|
||||
|
||||
[Fact]
|
||||
public void Deserialize_MatchesFieldNamesCaseInsensitively()
|
||||
{
|
||||
var release = Assert.Single(Deserialize("""[ { "Tag_Name": "v1.0.0" } ]"""));
|
||||
Assert.Equal("v1.0.0", release.TagName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using GitHubUpdateNotifier;
|
||||
using YukkuriMovieMaker.Plugin.Update;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
internal static class TestSupport
|
||||
{
|
||||
public static PluginVersion Ver(string value)
|
||||
{
|
||||
PluginVersion.TryParse(value, out var version);
|
||||
return version!;
|
||||
}
|
||||
|
||||
public static RepositoryReference Repo(string owner = "owner", string name = "repo")
|
||||
=> new(owner, name);
|
||||
|
||||
public static ReleaseData Release(
|
||||
string? tag,
|
||||
string? name = null,
|
||||
string? body = null,
|
||||
string? htmlUrl = null,
|
||||
bool draft = false,
|
||||
bool prerelease = false,
|
||||
DateTimeOffset publishedAt = default,
|
||||
IReadOnlyList<AssetData>? assets = null)
|
||||
=> new()
|
||||
{
|
||||
TagName = tag,
|
||||
Name = name,
|
||||
Body = body,
|
||||
HtmlUrl = htmlUrl,
|
||||
Draft = draft,
|
||||
Prerelease = prerelease,
|
||||
PublishedAt = publishedAt,
|
||||
Assets = assets,
|
||||
};
|
||||
|
||||
public static AssetData Asset(string? name, string? url, long size = 0, string? contentType = null)
|
||||
=> new()
|
||||
{
|
||||
Name = name,
|
||||
BrowserDownloadUrl = url,
|
||||
Size = size,
|
||||
ContentType = contentType,
|
||||
};
|
||||
|
||||
public static ResolvedReleases Resolved(UpdateSource source, params ReleaseData[] releases)
|
||||
=> new(source, releases);
|
||||
|
||||
public static UpdateNotifierOptions Options(
|
||||
string currentVersion,
|
||||
bool includePrerelease = false,
|
||||
int retryCount = 2,
|
||||
Func<UpdateInfo, string>? formatter = null,
|
||||
Func<UpdateNotification, CancellationToken, Task>? handler = null)
|
||||
=> new()
|
||||
{
|
||||
CurrentVersion = Ver(currentVersion),
|
||||
IncludePrerelease = includePrerelease,
|
||||
RetryCount = retryCount,
|
||||
MessageFormatter = formatter ?? (update => update.TagName),
|
||||
NotificationHandler = handler ?? ((_, _) => Task.CompletedTask),
|
||||
};
|
||||
|
||||
public static string InvokeBuildRequestUri(ReleaseProviderBase provider, RepositoryReference repository)
|
||||
{
|
||||
var method = typeof(ReleaseProviderBase).GetMethod("BuildRequestUri", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
return (string)method.Invoke(provider, [repository])!;
|
||||
}
|
||||
|
||||
public static HttpRequestMessage InvokeConfigureRequest(ReleaseProviderBase provider)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "https://example.invalid/");
|
||||
var method = typeof(ReleaseProviderBase).GetMethod("ConfigureRequest", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
method.Invoke(provider, [request]);
|
||||
return request;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using GitHubUpdateNotifier;
|
||||
using Xunit;
|
||||
|
||||
namespace NotifierTests;
|
||||
|
||||
public class VersionTagTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("1.2.3", 1, 2, 3)]
|
||||
[InlineData("v1.2.3", 1, 2, 3)]
|
||||
[InlineData("V1.2.3", 1, 2, 3)]
|
||||
[InlineData(" v1.0.0 ", 1, 0, 0)]
|
||||
[InlineData("1.2.3.4", 1, 2, 3)]
|
||||
public void TryParse_ParsesValidTags(string tag, int major, int minor, int build)
|
||||
{
|
||||
Assert.True(VersionTag.TryParse(tag, out var version));
|
||||
Assert.Equal(major, version!.Version.Major);
|
||||
Assert.Equal(minor, version.Version.Minor);
|
||||
Assert.Equal(build, version.Version.Build);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParse_KeepsPrereleaseSuffix()
|
||||
{
|
||||
Assert.True(VersionTag.TryParse("v1.2.3-beta", out var version));
|
||||
Assert.Equal(new Version(1, 2, 3), version!.Version);
|
||||
Assert.Equal("beta", version.Suffix);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("abc")]
|
||||
[InlineData("version")]
|
||||
[InlineData("v")]
|
||||
[InlineData("vabc")]
|
||||
[InlineData("1")]
|
||||
public void TryParse_RejectsInvalidTags(string? tag)
|
||||
{
|
||||
Assert.False(VersionTag.TryParse(tag, out var version));
|
||||
Assert.Null(version);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
<Solution>
|
||||
<Project Path="GitHubUpdateNotifier/GitHubUpdateNotifier.csproj" />
|
||||
<Project Path="GitHubUpdateNotifier.Tests/GitHubUpdateNotifier.Tests.csproj" />
|
||||
</Solution>
|
||||
新しいイシューから参照
ユーザーをブロックする