diff --git a/src/GitVersion.Core.Tests/Helpers/TestConsole.cs b/src/GitVersion.Core.Tests/Helpers/TestConsole.cs index 6484730db2..8a73e0e7db 100644 --- a/src/GitVersion.Core.Tests/Helpers/TestConsole.cs +++ b/src/GitVersion.Core.Tests/Helpers/TestConsole.cs @@ -1,3 +1,4 @@ +using GitVersion.Extensions; using GitVersion.Helpers; using GitVersion.Logging; @@ -12,7 +13,7 @@ public class TestConsole(params string[] responses) : IConsole public void WriteLine() => this.log.Info(PathHelper.NewLine); - public void Write(string? msg) => this.log.Info(msg ?? throw new ArgumentNullException(nameof(msg))); + public void Write(string? msg) => this.log.Info(msg.NotNull()); public string ReadLine() => this.responses.Dequeue(); diff --git a/src/GitVersion.Core/Core/FileSystem.cs b/src/GitVersion.Core/Core/FileSystem.cs index d3c41b59ea..50bd61f3a2 100644 --- a/src/GitVersion.Core/Core/FileSystem.cs +++ b/src/GitVersion.Core/Core/FileSystem.cs @@ -24,16 +24,14 @@ public void WriteAllText(string? file, string fileContents) public void WriteAllText(string? file, string fileContents, Encoding encoding) { - if (string.IsNullOrEmpty(file)) - throw new ArgumentNullException(nameof(file)); + ArgumentException.ThrowIfNullOrWhiteSpace(file); File.WriteAllText(file, fileContents, encoding); } public IEnumerable DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption) { - if (string.IsNullOrEmpty(directory)) - throw new ArgumentNullException(nameof(directory)); + ArgumentException.ThrowIfNullOrWhiteSpace(directory); return Directory.EnumerateFiles(directory, searchPattern, searchOption); } diff --git a/src/GitVersion.Core/Extensions/EnumerableExtensions.cs b/src/GitVersion.Core/Extensions/EnumerableExtensions.cs index 085de0f618..764fb89008 100644 --- a/src/GitVersion.Core/Extensions/EnumerableExtensions.cs +++ b/src/GitVersion.Core/Extensions/EnumerableExtensions.cs @@ -4,12 +4,11 @@ public static class EnumerableExtensions { public static T? OnlyOrDefault(this IEnumerable source) { - switch (source) + ArgumentNullException.ThrowIfNull(source); + + if (source is IList { Count: 1 } list) { - case null: - throw new ArgumentNullException(nameof(source)); - case IList { Count: 1 } list: - return list[0]; + return list[0]; } using var e = source.GetEnumerator(); @@ -21,10 +20,7 @@ public static class EnumerableExtensions public static T SingleOfType(this IEnumerable source) { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.OfType().Single(); } diff --git a/src/GitVersion.Core/Helpers/PathHelper.cs b/src/GitVersion.Core/Helpers/PathHelper.cs index 761aa307b9..3c9d853a1d 100644 --- a/src/GitVersion.Core/Helpers/PathHelper.cs +++ b/src/GitVersion.Core/Helpers/PathHelper.cs @@ -42,8 +42,8 @@ public static string GetFullPath(string? path) public static string Combine(string? path1, string? path2) { - if (path1 == null || path2 == null) - throw new ArgumentNullException((path1 == null) ? nameof(path1) : nameof(path2)); + ArgumentException.ThrowIfNullOrWhiteSpace(path1); + ArgumentException.ThrowIfNullOrWhiteSpace(path2); return Path.Combine(path1, path2); } @@ -57,16 +57,19 @@ public static string Combine(string? path1) public static string Combine(string? path1, string? path2, string? path3) { - if (path1 == null || path2 == null || path3 == null) - throw new ArgumentNullException((path1 == null) ? nameof(path1) : (path2 == null) ? nameof(path2) : nameof(path3)); + ArgumentException.ThrowIfNullOrWhiteSpace(path1); + ArgumentException.ThrowIfNullOrWhiteSpace(path2); + ArgumentException.ThrowIfNullOrWhiteSpace(path3); return Path.Combine(path1, path2, path3); } public static string Combine(string? path1, string? path2, string? path3, string? path4) { - if (path1 == null || path2 == null || path3 == null || path4 == null) - throw new ArgumentNullException((path1 == null) ? nameof(path1) : (path2 == null) ? nameof(path2) : (path3 == null) ? nameof(path3) : nameof(path4)); + ArgumentException.ThrowIfNullOrWhiteSpace(path1); + ArgumentException.ThrowIfNullOrWhiteSpace(path2); + ArgumentException.ThrowIfNullOrWhiteSpace(path3); + ArgumentException.ThrowIfNullOrWhiteSpace(path4); return Path.Combine(path1, path2, path3, path4); } diff --git a/src/GitVersion.Core/Helpers/StringFormatWith.cs b/src/GitVersion.Core/Helpers/StringFormatWith.cs index c6732c24c5..5aa51b5ae0 100644 --- a/src/GitVersion.Core/Helpers/StringFormatWith.cs +++ b/src/GitVersion.Core/Helpers/StringFormatWith.cs @@ -30,15 +30,8 @@ internal static class StringFormatWithExtension /// public static string FormatWith(this string template, T? source, IEnvironment environment) { - if (template is null) - { - throw new ArgumentNullException(nameof(template)); - } - - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(template); + ArgumentNullException.ThrowIfNull(source); foreach (Match match in RegexPatterns.Common.ExpandTokensRegex.Matches(template).Cast()) { diff --git a/src/GitVersion.Core/Logging/LogExtensions.cs b/src/GitVersion.Core/Logging/LogExtensions.cs index 77dff958a6..56d83b3321 100644 --- a/src/GitVersion.Core/Logging/LogExtensions.cs +++ b/src/GitVersion.Core/Logging/LogExtensions.cs @@ -96,10 +96,7 @@ private static void Write(this ILog log, LogLevel level, LogAction? logAction) private static IDisposable WithVerbosity(this ILog log, Verbosity verbosity) { - if (log == null) - { - throw new ArgumentNullException(nameof(log)); - } + ArgumentNullException.ThrowIfNull(log); var lastVerbosity = log.Verbosity; log.Verbosity = verbosity; return Disposable.Create(() => log.Verbosity = lastVerbosity); diff --git a/src/GitVersion.Output/TemplateManager.cs b/src/GitVersion.Output/TemplateManager.cs index 8246369fc2..7eb60d3d54 100644 --- a/src/GitVersion.Output/TemplateManager.cs +++ b/src/GitVersion.Output/TemplateManager.cs @@ -15,10 +15,7 @@ internal class TemplateManager(TemplateType templateType) public string? GetTemplateFor(string fileExtension) { - if (fileExtension == null) - { - throw new ArgumentNullException(nameof(fileExtension)); - } + ArgumentNullException.ThrowIfNull(fileExtension); string? result = null; @@ -32,10 +29,7 @@ internal class TemplateManager(TemplateType templateType) public string? GetAddFormatFor(string fileExtension) { - if (fileExtension == null) - { - throw new ArgumentNullException(nameof(fileExtension)); - } + ArgumentNullException.ThrowIfNull(fileExtension); string? result = null; @@ -49,10 +43,7 @@ internal class TemplateManager(TemplateType templateType) public bool IsSupported(string fileExtension) { - if (fileExtension == null) - { - throw new ArgumentNullException(nameof(fileExtension)); - } + ArgumentNullException.ThrowIfNull(fileExtension); return this.templates.ContainsKey(fileExtension); } diff --git a/src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs b/src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs index 2da6a5069f..1ac92f2086 100644 --- a/src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs +++ b/src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs @@ -1,6 +1,7 @@ using GitVersion.Helpers; using GitVersion.Testing.Internal; using LibGit2Sharp; +using Shouldly; namespace GitVersion.Testing; @@ -17,7 +18,7 @@ protected RepositoryFixtureBase(Func repositoryBuilder) protected RepositoryFixtureBase(Repository repository) { SequenceDiagram = new(); - Repository = repository ?? throw new ArgumentNullException(nameof(repository)); + Repository = repository.ShouldNotBeNull(); Repository.Config.Set("user.name", "Test"); Repository.Config.Set("user.email", "test@email.com"); } diff --git a/src/GitVersion.Testing/Helpers/ProcessHelper.cs b/src/GitVersion.Testing/Helpers/ProcessHelper.cs index b5f269ce95..466472bd1e 100644 --- a/src/GitVersion.Testing/Helpers/ProcessHelper.cs +++ b/src/GitVersion.Testing/Helpers/ProcessHelper.cs @@ -76,10 +76,8 @@ public static class ProcessHelper // http://csharptest.net/532/using-processstart-to-capture-console-output/ public static int Run(Action output, Action errorOutput, TextReader? input, string exe, string args, string workingDirectory, params KeyValuePair[] environmentalVariables) { - if (string.IsNullOrEmpty(exe)) - throw new ArgumentNullException(nameof(exe)); - if (output == null) - throw new ArgumentNullException(nameof(output)); + ArgumentException.ThrowIfNullOrWhiteSpace(exe); + ArgumentNullException.ThrowIfNull(output); var psi = new ProcessStartInfo {