229 行
8.8 KiB
C#
229 行
8.8 KiB
C#
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);
|
|
}
|
|
}
|