Skip to content

Commit

Permalink
Update everything to .NET 9, apply new analysis suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
aannenko committed Nov 15, 2024
1 parent 0d315a9 commit c38f9e1
Show file tree
Hide file tree
Showing 71 changed files with 204 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Set up .NET SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.x'
dotnet-version: '9.x'

- name: Checkout repository
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'
dotnet-version: '9.x'

- name: Restore dependencies
run: dotnet restore src/TransmissionManager.sln
Expand Down
5 changes: 3 additions & 2 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_style_expression_bodied_local_functions = when_on_single_line:silent
csharp_style_expression_bodied_methods = when_on_single_line:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
Expand Down Expand Up @@ -129,7 +129,7 @@ csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
csharp_style_unused_value_expression_statement_preference = discard_variable:none

# 'using' directive preferences
csharp_using_directive_placement = outside_namespace:silent
Expand Down Expand Up @@ -243,6 +243,7 @@ dotnet_naming_style._fieldname.required_suffix =
dotnet_naming_style._fieldname.word_separator =
dotnet_naming_style._fieldname.capitalization = camel_case
csharp_prefer_static_anonymous_function = true:suggestion
csharp_prefer_system_threading_lock = true:suggestion

[*.{cs,vb}]
dotnet_style_operator_placement_when_wrapping = beginning_of_line
Expand Down
10 changes: 5 additions & 5 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="Coravel" Version="6.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="MinimalApis.Extensions" Version="0.11.0" />
<PackageVersion Include="NUnit" Version="4.2.2" />
<PackageVersion Include="NUnit.Analyzers" Version="4.3.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.4.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace TransmissionManager.Api.IntegrationTests;

[Parallelizable(ParallelScope.Self)]
public sealed class AddTorrentTests
internal sealed class AddTorrentTests
{
private static readonly Torrent[] _initialTorrents = [TestData.Database.CreateInitialTorrents()[0]];

Expand Down Expand Up @@ -98,8 +98,8 @@ public sealed class AddTorrentTests

#endregion

private TestWebAppliationFactory<Program> _factory;
private HttpClient _client;
private TestWebAppliationFactory<Program> _factory = default!;
private HttpClient _client = default!;

[OneTimeSetUp]
public void Setup()
Expand Down Expand Up @@ -161,7 +161,7 @@ public async Task AddTorrentAsync_WhenGivenExistingTorrentUri_ReturnsConflictRes
var error =
$"Addition of a torrent from the web page '{dto.WebPageUri}' has failed: 'Torrent already exists.'.";

Assert.That(problemDetails.Detail, Is.EqualTo(error));
Assert.That(problemDetails!.Detail, Is.EqualTo(error));
Assert.That(problemDetails.Extensions.TryGetValue("transmissionResult", out var transmissionResult));
Assert.That(transmissionResult?.ToString(), Is.EqualTo("Duplicate"));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace TransmissionManager.Api.IntegrationTests;

[Parallelizable(ParallelScope.Self)]
public sealed class DeleteTorrentByIdTests
internal sealed class DeleteTorrentByIdTests
{
private static readonly Torrent[] _torrents = [TestData.Database.CreateInitialTorrents()[0]];

private TestWebAppliationFactory<Program> _factory;
private HttpClient _client;
private TestWebAppliationFactory<Program> _factory = default!;
private HttpClient _client = default!;

[OneTimeSetUp]
public void Setup()
Expand Down Expand Up @@ -44,7 +44,7 @@ public async Task DeleteTorrentByIdAsync_WhenGivenExistingId_DeletesTorrent()
var problemDetails = await response.Content.ReadFromJsonAsync<ProblemDetails>().ConfigureAwait(false);

Assert.That(problemDetails, Is.Not.Null);
Assert.That(problemDetails.Detail, Is.EqualTo("Torrent with id 1 was not found."));
Assert.That(problemDetails!.Detail, Is.EqualTo("Torrent with id 1 was not found."));
}

[Test]
Expand All @@ -57,6 +57,6 @@ public async Task UpdateTorrentByIdAsync_WhenGivenNonExistingId_ReturnsNotFound(
var problemDetails = await response.Content.ReadFromJsonAsync<ProblemDetails>().ConfigureAwait(false);

Assert.That(problemDetails, Is.Not.Null);
Assert.That(problemDetails.Detail, Is.EqualTo("Torrent with id -1 was not found."));
Assert.That(problemDetails!.Detail, Is.EqualTo("Torrent with id -1 was not found."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace TransmissionManager.Api.IntegrationTests;

[Parallelizable(ParallelScope.Self)]
public sealed class FindTorrentByIdTests
internal sealed class FindTorrentByIdTests
{
private static readonly Torrent[] _torrents = [TestData.Database.CreateInitialTorrents()[0]];

private TestWebAppliationFactory<Program> _factory;
private HttpClient _client;
private TestWebAppliationFactory<Program> _factory = default!;
private HttpClient _client = default!;

[OneTimeSetUp]
public void Setup()
Expand Down Expand Up @@ -52,6 +52,6 @@ public async Task FindTorrentByIdAsync_WhenGivenNonExistingTorrentId_ReturnsNotF
var problem = await response.Content.ReadFromJsonAsync<ProblemDetails>().ConfigureAwait(false);

Assert.That(problem, Is.Not.Null);
Assert.That(problem.Detail, Is.EqualTo("Torrent with id 999 was not found."));
Assert.That(problem!.Detail, Is.EqualTo("Torrent with id 999 was not found."));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System.Net.Http.Json;
using Microsoft.AspNetCore.Http;
using System.Net;
using System.Net.Http.Json;
using TransmissionManager.Api.Actions.FindTorrentPage;
using TransmissionManager.Api.IntegrationTests.Helpers;
using TransmissionManager.Database.Models;

namespace TransmissionManager.Api.IntegrationTests;

[Parallelizable(ParallelScope.Self)]
public sealed class FindTorrentPageTests
internal sealed class FindTorrentPageTests
{
private static readonly Torrent[] _torrents = TestData.Database.CreateInitialTorrents();

private TestWebAppliationFactory<Program> _factory;
private HttpClient _client;
private TestWebAppliationFactory<Program> _factory = default!;
private HttpClient _client = default!;

[OneTimeSetUp]
public void Setup()
Expand Down Expand Up @@ -64,6 +66,7 @@ public async Task FindTorrentPageAsync_WhenGivenCorrectUriAndHashStringFilters_R
.ConfigureAwait(false);

AssertTorrentPage(1, page, parameters);
TorrentAssertions.AssertEqual(page.Torrents[0], 2, _torrents[1]);
}

[Test]
Expand All @@ -83,6 +86,54 @@ public async Task FindTorrentPageAsync_WhenGivenNonExistingFilterValues_ReturnsE
AssertTorrentPage(0, page, parameters);
}

[Test]
public async Task FindTorrentPageAsync_WhenGivenInvalidTakeParameter_ReturnsValidationProblem()
{
var parameters = new FindTorrentPageParameters(Take: 0);

var response = await _client.GetAsync(parameters.ToPathAndQueryString()).ConfigureAwait(false);

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));

var problem = await response.Content.ReadFromJsonAsync<HttpValidationProblemDetails>().ConfigureAwait(false);

Assert.That(problem, Is.Not.Null);
Assert.That(problem!.Errors, Has.Count.EqualTo(1));
Assert.That(problem!.Errors, Contains.Key(nameof(FindTorrentPageParameters.Take)));
}

[Test]
public async Task FindTorrentPageAsync_WhenGivenTooLargeValueOfTakeParameter_ReturnsValidationProblem()
{
var parameters = new FindTorrentPageParameters(Take: 1001);

var response = await _client.GetAsync(parameters.ToPathAndQueryString()).ConfigureAwait(false);

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));

var problem = await response.Content.ReadFromJsonAsync<HttpValidationProblemDetails>().ConfigureAwait(false);

Assert.That(problem, Is.Not.Null);
Assert.That(problem!.Errors, Has.Count.EqualTo(1));
Assert.That(problem!.Errors, Contains.Key(nameof(FindTorrentPageParameters.Take)));
}

[Test]
public async Task FindTorrentPageAsync_WhenGivenInvalidHashStringParameter_ReturnsValidationProblem()
{
var parameters = new FindTorrentPageParameters(HashString: "invalid");

var response = await _client.GetAsync(parameters.ToPathAndQueryString()).ConfigureAwait(false);

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));

var problem = await response.Content.ReadFromJsonAsync<HttpValidationProblemDetails>().ConfigureAwait(false);

Assert.That(problem, Is.Not.Null);
Assert.That(problem!.Errors, Has.Count.EqualTo(1));
Assert.That(problem!.Errors, Contains.Key(nameof(FindTorrentPageParameters.HashString)));
}

private static void AssertTorrentPage(
int expectedCount,
FindTorrentPageResponse page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TransmissionManager.Api.IntegrationTests.Helpers;

internal static class TestData
{
public static class Database
internal static class Database
{
public const string FirstTorrentHashString = "0bda511316a069e86dd8ee8a3610475d2013a7fa";
public const string FirstTorrentName = "TV Show 1";
Expand Down Expand Up @@ -113,7 +113,7 @@ internal static class WebPages
};
}

public static class Transmission
internal static class Transmission
{
public const string SessionHeaderName = "X-Transmission-Session-Id";
public const string SessionHeaderValue = "FctoNpkk6eYSSgmBV0B2DXI4SsLLSYc0lA5MdYkLpc9fDA59";
Expand Down Expand Up @@ -184,7 +184,7 @@ public static class Transmission
};
}

public static class Endpoints
internal static class Endpoints
{
public const string Torrents = "/api/v1/torrents";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace TransmissionManager.Api.IntegrationTests.Helpers;

public sealed class TestWebAppliationFactory<TProgram>(
internal sealed class TestWebAppliationFactory<TProgram>(
Torrent[] initialTorrents,
IReadOnlyDictionary<TestRequest, TestResponse>? torrentPageRequestResponseMap,
IReadOnlyDictionary<TestRequest, TestResponse>? transmissionRequestResponseMap)
Expand All @@ -40,25 +40,25 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)

base.ConfigureWebHost(builder);

_ = builder.UseEnvironment("Testing");
builder.UseEnvironment("Testing");

DisposeOfDbConnection();
_connection = new SqliteConnection("Data Source=:memory:");
_connection.Open();

_ = builder.ConfigureServices(services =>
builder.ConfigureServices(services =>
{
_ = services
services
.RemoveAll<DbContextOptions<AppDbContext>>()
.AddDbContext<AppDbContext>(options => options.UseSqlite(_connection));

_ = services.PostConfigure(nameof(TorrentWebPageClient), (HttpClientFactoryOptions options) =>
services.PostConfigure(nameof(TorrentWebPageClient), (HttpClientFactoryOptions options) =>
{
options.HttpMessageHandlerBuilderActions.Add(builder =>
builder.PrimaryHandler = new FakeHttpMessageHandler(_torrentPageRequestResponseMap));
});

_ = services.PostConfigure(nameof(TransmissionClient), (HttpClientFactoryOptions options) =>
services.PostConfigure(nameof(TransmissionClient), (HttpClientFactoryOptions options) =>
{
options.HttpMessageHandlerBuilderActions.Add(builder =>
builder.PrimaryHandler = new FakeHttpMessageHandler(_transmissionRequestResponseMap));
Expand All @@ -73,7 +73,7 @@ protected override IHost CreateHost(IHostBuilder builder)
using var scope = host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
dbContext.Torrents.AddRange(initialTorrents);
_ = dbContext.SaveChanges();
dbContext.SaveChanges();

return host;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ namespace TransmissionManager.Api.IntegrationTests.Helpers;

internal static class TorrentAssertions
{
public static void AssertEqual(Torrent actual, long expectedId, Torrent expected)
public static void AssertEqual(Torrent? actual, long expectedId, Torrent expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expectedId));
Assert.That(actual!.Id, Is.EqualTo(expectedId));
Assert.That(actual.HashString, Is.EqualTo(expected.HashString));
Assert.That(actual.Name, Is.EqualTo(expected.Name));
Assert.That(actual.WebPageUri, Is.EqualTo(expected.WebPageUri));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace TransmissionManager.Api.IntegrationTests;

[Parallelizable(ParallelScope.Self)]
public sealed class RefreshTorrentByIdTests
internal sealed class RefreshTorrentByIdTests
{
private static readonly Torrent[] _initialTorrents = TestData.Database.CreateInitialTorrents();

Expand Down Expand Up @@ -175,8 +175,8 @@ public sealed class RefreshTorrentByIdTests

#endregion

private TestWebAppliationFactory<Program> _factory;
private HttpClient _client;
private TestWebAppliationFactory<Program> _factory = default!;
private HttpClient _client = default!;

[OneTimeSetUp]
public void Setup()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AnalysisLevel>latest-all</AnalysisLevel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace TransmissionManager.Api.IntegrationTests;

[Parallelizable(ParallelScope.Self)]
public sealed class UpdateTorrentByIdTests
internal sealed class UpdateTorrentByIdTests
{
private static readonly Torrent[] _torrents = TestData.Database.CreateInitialTorrents();

private TestWebAppliationFactory<Program> _factory;
private HttpClient _client;
private TestWebAppliationFactory<Program> _factory = default!;
private HttpClient _client = default!;

[OneTimeSetUp]
public void Setup()
Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task UpdateTorrentByIdAsync_WhenGivenExistingIdAndValidData_Updates
Assert.That(torrent, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(torrent.DownloadDir, Is.EqualTo(dto.DownloadDir));
Assert.That(torrent!.DownloadDir, Is.EqualTo(dto.DownloadDir));
Assert.That(torrent.MagnetRegexPattern, Is.EqualTo(dto.MagnetRegexPattern));
Assert.That(torrent.Cron, Is.EqualTo(dto.Cron));
});
Expand All @@ -72,6 +72,6 @@ public async Task UpdateTorrentByIdAsync_WhenGivenNonExistingId_ReturnsNotFound(
var problemDetails = await response.Content.ReadFromJsonAsync<ProblemDetails>().ConfigureAwait(false);

Assert.That(problemDetails, Is.Not.Null);
Assert.That(problemDetails.Detail, Is.EqualTo("Torrent with id -1 was not found."));
Assert.That(problemDetails!.Detail, Is.EqualTo("Torrent with id -1 was not found."));
}
}
Loading

0 comments on commit c38f9e1

Please sign in to comment.