From f96579ce4bcbf221e1178322940230bd6a9b319f Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 9 Jan 2023 15:38:12 +0100 Subject: [PATCH] #3334 - fix nullable warnings --- .../ConfigNextVersionBaseVersionStrategyTests.cs | 2 ++ .../MergeMessageBaseVersionStrategyTests.cs | 4 ++++ .../VersionInBranchNameBaseVersionStrategyTests.cs | 12 +++++++++++- .../Extensions/ServiceCollectionExtensions.cs | 2 +- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/ConfigNextVersionBaseVersionStrategyTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/ConfigNextVersionBaseVersionStrategyTests.cs index 1f48aa2e24..6db52ed9f4 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/ConfigNextVersionBaseVersionStrategyTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/ConfigNextVersionBaseVersionStrategyTests.cs @@ -49,6 +49,8 @@ public void ConfigNextVersionTest(string nextVersion, string expectedVersion) var branchMock = GitToolsTestingExtensions.CreateMockBranch("main", GitToolsTestingExtensions.CreateMockCommit()); var branchConfiguration = context.Configuration.GetBranchConfiguration(branchMock); var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration); + + strategy.ShouldNotBeNull(); return strategy.GetBaseVersions(new(branchMock, effectiveConfiguration)).SingleOrDefault(); } } diff --git a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs index 396755a2eb..c3673bf7a4 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs @@ -40,6 +40,8 @@ public void ShouldNotAllowIncrementOfVersion() var context = contextBuilder.ServicesProvider.GetRequiredService>().Value; var branchConfiguration = context.Configuration.GetBranchConfiguration(mockBranch); var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration); + + strategy.ShouldNotBeNull(); var baseVersion = strategy.GetBaseVersions(new(mockBranch, effectiveConfiguration)).Single(); baseVersion.ShouldIncrement.ShouldBe(false); @@ -173,6 +175,8 @@ private static void AssertMergeMessage(string message, string? expectedVersion, var context = contextBuilder.ServicesProvider.GetRequiredService>().Value; var branchConfiguration = context.Configuration.GetBranchConfiguration(mockBranch); var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration); + + strategy.ShouldNotBeNull(); var baseVersion = strategy.GetBaseVersions(new(mockBranch, effectiveConfiguration)).SingleOrDefault(); if (expectedVersion == null) diff --git a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs index da49ec0d21..2ffd0b1cc3 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs @@ -25,6 +25,8 @@ public void CanTakeVersionFromNameOfReleaseBranch(string branchName, string expe var configuration = GitFlowConfigurationBuilder.New.Build(); var branchConfiguration = configuration.GetBranchConfiguration(branchName); var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration); + + strategy.ShouldNotBeNull(); var baseVersion = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration)).Single(); baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion); @@ -45,6 +47,8 @@ public void ShouldNotTakeVersionFromNameOfNonReleaseBranch(string branchName) var configuration = GitFlowConfigurationBuilder.New.Build(); var branchConfiguration = configuration.GetBranchConfiguration(branchName); var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration); + + strategy.ShouldNotBeNull(); var baseVersions = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration)); baseVersions.ShouldBeEmpty(); @@ -67,6 +71,9 @@ public void CanTakeVersionFromNameOfConfiguredReleaseBranch(string branchName, s var configuration = GitFlowConfigurationBuilder.New.Build(); var branchConfiguration = configuration.GetBranchConfiguration(branchName); var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration); + + strategy.ShouldNotBeNull(); + var baseVersion = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration)).Single(); baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion); @@ -90,12 +97,15 @@ public void CanTakeVersionFromNameOfRemoteReleaseBranch(string branchName, strin var configuration = GitFlowConfigurationBuilder.New.Build(); var branchConfiguration = configuration.GetBranchConfiguration(branchName); var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration); + + strategy.ShouldNotBeNull(); + var baseVersion = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration)).Single(); baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion); } - private static IVersionStrategy GetVersionStrategy(string workingDirectory, IGitRepository repository, string branch, GitVersionConfiguration? configuration = null) + private static IVersionStrategy? GetVersionStrategy(string workingDirectory, IGitRepository repository, string branch, GitVersionConfiguration? configuration = null) { var sp = BuildServiceProvider(workingDirectory, repository, branch, configuration); return sp.GetServiceForType(); diff --git a/src/GitVersion.Core/Extensions/ServiceCollectionExtensions.cs b/src/GitVersion.Core/Extensions/ServiceCollectionExtensions.cs index d4d6467f3d..3ab937a31f 100644 --- a/src/GitVersion.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/GitVersion.Core/Extensions/ServiceCollectionExtensions.cs @@ -10,6 +10,6 @@ public static IServiceCollection AddModule(this IServiceCollection serviceCollec return serviceCollection; } - public static TService GetServiceForType(this IServiceProvider serviceProvider) => + public static TService? GetServiceForType(this IServiceProvider serviceProvider) => serviceProvider.GetServices().SingleOrDefault(t => t?.GetType() == typeof(TType)); }