From 101d0d36dd3fdb37403d6398d41726509e494753 Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Sun, 13 Oct 2024 22:45:33 +0200 Subject: [PATCH] Some cleanup and sonar fixes --- src/RepoM.Api/IO/IPathFinder.cs | 11 --- src/RepoM.App/NotifyIconResources.xaml | 5 +- src/RepoM.App/NotifyIconViewModel.cs | 8 +- .../Internal/AzureDevOpsPullRequestService.cs | 6 +- .../Internal/ExtractRepositoryFromHeidi.cs | 8 +- .../PInvoke/WindowFinder.cs | 91 ------------------- .../AzureDevOpsPackageTests.cs | 5 +- .../HeidiPackageTest.cs | 4 +- .../LuceneQueryParserPackageTest.cs | 3 +- .../PluginInformationTests.cs | 8 +- .../SonarCloudPackageTest.cs | 4 +- .../StatisticsPackageTest.cs | 4 +- 12 files changed, 33 insertions(+), 124 deletions(-) delete mode 100644 src/RepoM.Api/IO/IPathFinder.cs delete mode 100644 src/RepoM.Plugin.WindowsExplorerGitInfo/PInvoke/WindowFinder.cs diff --git a/src/RepoM.Api/IO/IPathFinder.cs b/src/RepoM.Api/IO/IPathFinder.cs deleted file mode 100644 index 6df0a3fc..00000000 --- a/src/RepoM.Api/IO/IPathFinder.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace RepoM.Api.IO; - -using System; - -// todo, check original code where this was used?! -public interface IPathFinder -{ - bool CanHandle(string processName); - - string FindPath(IntPtr windowHandle); -} \ No newline at end of file diff --git a/src/RepoM.App/NotifyIconResources.xaml b/src/RepoM.App/NotifyIconResources.xaml index 39e087e7..6e1b1e5a 100644 --- a/src/RepoM.App/NotifyIconResources.xaml +++ b/src/RepoM.App/NotifyIconResources.xaml @@ -10,7 +10,10 @@ reuse the same context menu (which is a resource) again (which will have its DataContext set to the old TaskbarIcon) --> - + + + + /// Shows a window, if none is already open. /// - public ICommand OpenCommand => + public static ICommand OpenCommand => new DelegateCommand { CanExecuteFunc = () => (Application.Current.MainWindow as MainWindow)?.IsShown == false, CommandAction = () => (Application.Current.MainWindow as MainWindow)?.ShowAndActivate(), }; - public ICommand StartWithWindows => + public static ICommand StartWithWindows => new DelegateCommand { CanExecuteFunc = () => !AutoStart.IsStartup(APP_NAME), CommandAction = () => AutoStart.SetStartup(APP_NAME, true), }; - public ICommand DoNotStartWithWindows => + public static ICommand DoNotStartWithWindows => new DelegateCommand { CanExecuteFunc = () => AutoStart.IsStartup(APP_NAME), @@ -40,7 +40,7 @@ public class NotifyIconViewModel /// /// Shuts down the application. /// - public ICommand ExitApplicationCommand => + public static ICommand ExitApplicationCommand => new DelegateCommand { CommandAction = () => Application.Current.Shutdown(), diff --git a/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevOpsPullRequestService.cs b/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevOpsPullRequestService.cs index a1cc2168..b078d72e 100644 --- a/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevOpsPullRequestService.cs +++ b/src/RepoM.Plugin.AzureDevOps/Internal/AzureDevOpsPullRequestService.cs @@ -44,8 +44,7 @@ public AzureDevOpsPullRequestService(IAzureDevopsConfiguration configuration, IL { if (string.IsNullOrEmpty(configuration.AzureDevOpsPersonalAccessToken)) { - //throw new ArgumentNullException(nameof(configuration.AzureDevOpsPersonalAccessToken)); - _logger.LogInformation("No Azure configuration present"); + _logger.LogInformation("No Azure configuration present. Module will not be enabled."); } else { @@ -56,7 +55,6 @@ public AzureDevOpsPullRequestService(IAzureDevopsConfiguration configuration, IL _httpClient.DefaultRequestHeaders.Accept.Add(new("application/json")); _httpClient.DefaultRequestHeaders.Authorization = new("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($":{_configuration.AzureDevOpsPersonalAccessToken}"))); } - } catch (Exception e) { @@ -124,7 +122,7 @@ public async Task CreatePullRequestWithAutoCompleteAsync(IRepository repository, if (!response.IsSuccessStatusCode) { - var content = await response.Content.ReadAsStringAsync(); + var content = await response.Content.ReadAsStringAsync(CancellationToken.None); _logger.LogError("PR creation went wrong with following response: {Message}", content); throw new HttpRequestException(content, null, response.StatusCode); } diff --git a/src/RepoM.Plugin.Heidi/Internal/ExtractRepositoryFromHeidi.cs b/src/RepoM.Plugin.Heidi/Internal/ExtractRepositoryFromHeidi.cs index fae447d0..9c7d1b25 100644 --- a/src/RepoM.Plugin.Heidi/Internal/ExtractRepositoryFromHeidi.cs +++ b/src/RepoM.Plugin.Heidi/Internal/ExtractRepositoryFromHeidi.cs @@ -77,7 +77,7 @@ public bool TryExtract(HeidiSingleDatabaseConfiguration config, [NotNullWhen(tru return true; } - private static ReadOnlySpan HandleHashTag(ReadOnlySpan comment, ICollection tags) + private static ReadOnlySpan HandleHashTag(ReadOnlySpan comment, List tags) { comment = comment["#".Length..]; @@ -91,7 +91,7 @@ private static ReadOnlySpan HandleHashTag(ReadOnlySpan comment, ICol return comment; } - private static ReadOnlySpan HandleHashTagName(ReadOnlySpan comment, ICollection names) + private static ReadOnlySpan HandleHashTagName(ReadOnlySpan comment, List names) { comment = comment[HASH_TAG_NAME.Length..]; @@ -124,7 +124,7 @@ private static ReadOnlySpan HandleHashTagName(ReadOnlySpan comment, return comment; } - private static ReadOnlySpan HandleHashTagOrder(ReadOnlySpan comment, ICollection orders) + private static ReadOnlySpan HandleHashTagOrder(ReadOnlySpan comment, List orders) { comment = comment[HASH_TAG_ORDER.Length..]; @@ -150,7 +150,7 @@ private static ReadOnlySpan HandleHashTagOrder(ReadOnlySpan comment, return comment; } - private static ReadOnlySpan HandleHashTagRepo(ReadOnlySpan comment, ICollection repos) + private static ReadOnlySpan HandleHashTagRepo(ReadOnlySpan comment, List repos) { comment = comment[HASH_TAG_REPO.Length..]; diff --git a/src/RepoM.Plugin.WindowsExplorerGitInfo/PInvoke/WindowFinder.cs b/src/RepoM.Plugin.WindowsExplorerGitInfo/PInvoke/WindowFinder.cs deleted file mode 100644 index 44c23f88..00000000 --- a/src/RepoM.Plugin.WindowsExplorerGitInfo/PInvoke/WindowFinder.cs +++ /dev/null @@ -1,91 +0,0 @@ -namespace RepoM.Plugin.WindowsExplorerGitInfo.PInvoke; - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using RepoM.Api.IO; - -[SuppressMessage("ReSharper", "InconsistentNaming")] -[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Reviewed")] -[SuppressMessage("ReSharper", "IdentifierTypo")] -internal class WindowFinder -{ - private readonly IEnumerable _pathFinders; - - public WindowFinder(IEnumerable pathFinders) - { - _pathFinders = pathFinders; - } - - [DllImport("user32.dll", SetLastError = true)] - static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); - - [DllImport("user32.dll")] - static extern IntPtr GetForegroundWindow(); - - [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)] - public static extern bool SetWindowText(IntPtr hWnd, string strNewWindowName); - - [DllImport("user32.dll", CharSet = CharSet.Auto)] - static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, [Out] StringBuilder lParam); - - private static IntPtr FindActiveWindow() - { - return GetForegroundWindow(); - } - - private const uint WM_GETTEXT = 0x000D; - private const uint WM_GETTEXTLENGTH = 0x000E; - - public static string GetWindowTextRaw(IntPtr hwnd) - { - // Allocate correct string length first - var length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null!); - var sb = new StringBuilder(length + 1); - SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb); - return sb.ToString(); - } - - public void SetW(IntPtr handle, string text) - { - var current = GetWindowTextRaw(handle); - - var at = current.IndexOf('@'); - if (at > -1) - { - current = current[..at]; - } - - if (!current.EndsWith(" ", StringComparison.OrdinalIgnoreCase)) - { - current += " "; - } - - SetWindowText(handle, current + "@" + text); - } - - public WindowPath? GetPathOfCurrentWindow() - { - IntPtr handle = FindActiveWindow(); - - _ = GetWindowThreadProcessId(handle, out var procId); - - var proc = Process.GetProcessById((int)procId); - var processName = proc?.ProcessName; - - if (string.IsNullOrEmpty(processName)) - { - return null; - } - - IPathFinder? finder = _pathFinders.FirstOrDefault(f => f.CanHandle(processName)); - - var path = finder?.FindPath(handle) ?? string.Empty; - - return new WindowPath(handle, path); - } -} \ No newline at end of file diff --git a/tests/RepoM.Plugin.AzureDevOps.Tests/AzureDevOpsPackageTests.cs b/tests/RepoM.Plugin.AzureDevOps.Tests/AzureDevOpsPackageTests.cs index df04eb24..6723d015 100644 --- a/tests/RepoM.Plugin.AzureDevOps.Tests/AzureDevOpsPackageTests.cs +++ b/tests/RepoM.Plugin.AzureDevOps.Tests/AzureDevOpsPackageTests.cs @@ -1,3 +1,5 @@ +using FluentAssertions; + namespace RepoM.Plugin.AzureDevOps.Tests; using System; @@ -76,9 +78,10 @@ public async Task RegisterServices_ShouldFail_WhenExternalDependenciesAreNotRegi // act await sut.RegisterServicesAsync(_container, _packageConfiguration); + Action act = () => _container.Verify(VerificationOption.VerifyAndDiagnose); // assert - Assert.Throws(() => _container.Verify(VerificationOption.VerifyAndDiagnose)); + act.Should().ThrowExactly(); } [Theory] diff --git a/tests/RepoM.Plugin.Heidi.Tests/HeidiPackageTest.cs b/tests/RepoM.Plugin.Heidi.Tests/HeidiPackageTest.cs index da59adba..4f228847 100644 --- a/tests/RepoM.Plugin.Heidi.Tests/HeidiPackageTest.cs +++ b/tests/RepoM.Plugin.Heidi.Tests/HeidiPackageTest.cs @@ -4,6 +4,7 @@ namespace RepoM.Plugin.Heidi.Tests; using System.IO.Abstractions; using System.Threading.Tasks; using FakeItEasy; +using FluentAssertions; using Microsoft.Extensions.Logging; using RepoM.Core.Plugin; using RepoM.Plugin.Heidi.PersistentConfiguration; @@ -93,9 +94,10 @@ public async Task RegisterServices_ShouldFail_WhenExternalDependenciesAreNotRegi // act await sut.RegisterServicesAsync(_container, _packageConfiguration); + Action act = () => _container.Verify(VerificationOption.VerifyAndDiagnose); // assert - Assert.Throws(() => _container.Verify(VerificationOption.VerifyAndDiagnose)); + act.Should().ThrowExactly(); ; } private static void RegisterExternals(Container container) diff --git a/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserPackageTest.cs b/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserPackageTest.cs index a26803c9..5a7a4cc1 100644 --- a/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserPackageTest.cs +++ b/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserPackageTest.cs @@ -57,9 +57,10 @@ public void RegisterServices_ShouldFail_WhenExternalDependenciesAreNotRegistered // act sut.RegisterServices(_container); + Action act = () => _container.Verify(VerificationOption.VerifyAndDiagnose); // assert - Assert.Throws(() => _container.Verify(VerificationOption.VerifyAndDiagnose)); + act.Should().ThrowExactly(); } private static void RegisterExternals(Container container) diff --git a/tests/RepoM.Plugin.Misc.Tests/PluginInformationTests.cs b/tests/RepoM.Plugin.Misc.Tests/PluginInformationTests.cs index eb498205..13232af8 100644 --- a/tests/RepoM.Plugin.Misc.Tests/PluginInformationTests.cs +++ b/tests/RepoM.Plugin.Misc.Tests/PluginInformationTests.cs @@ -21,8 +21,8 @@ public void PluginAssembly_ShouldContainPackageAttributeWithValidValues(Assembly PackageAttribute? result = assembly.GetCustomAttribute(); // assert - Assert.NotNull(result); - result.Name.Should().NotBeNullOrWhiteSpace(); + result.Should().NotBeNull(); + result!.Name.Should().NotBeNullOrWhiteSpace(); result.ToolTip.Should().NotBeNullOrWhiteSpace(); } @@ -53,7 +53,7 @@ public void AssemblyDescription_ShouldBeUnique() private static PackageAttribute GetFromAssembly(Assembly assembly) { PackageAttribute? result = assembly.GetCustomAttribute(); - Assert.NotNull(result); - return result; + result.Should().NotBeNull(); + return result!; } } \ No newline at end of file diff --git a/tests/RepoM.Plugin.SonarCloud.Tests/SonarCloudPackageTest.cs b/tests/RepoM.Plugin.SonarCloud.Tests/SonarCloudPackageTest.cs index 4b5a8f3e..b102ba01 100644 --- a/tests/RepoM.Plugin.SonarCloud.Tests/SonarCloudPackageTest.cs +++ b/tests/RepoM.Plugin.SonarCloud.Tests/SonarCloudPackageTest.cs @@ -9,6 +9,7 @@ namespace RepoM.Plugin.SonarCloud.Tests; using Microsoft.Extensions.Logging; using RepoM.Core.Plugin; using RepoM.Plugin.SonarCloud.PersistentConfiguration; +using FluentAssertions; public class SonarCloudPackageTest { @@ -76,9 +77,10 @@ public async Task RegisterServices_ShouldFail_WhenExternalDependenciesAreNotRegi // act await sut.RegisterServicesAsync(_container, _packageConfiguration); + Action act = () => _container.Verify(VerificationOption.VerifyAndDiagnose); // assert - Assert.Throws(() => _container.Verify(VerificationOption.VerifyAndDiagnose)); + act.Should().ThrowExactly(); } private void RegisterExternals(Container container) diff --git a/tests/RepoM.Plugin.Statistics.Tests/StatisticsPackageTest.cs b/tests/RepoM.Plugin.Statistics.Tests/StatisticsPackageTest.cs index bf3849f2..94ea609f 100644 --- a/tests/RepoM.Plugin.Statistics.Tests/StatisticsPackageTest.cs +++ b/tests/RepoM.Plugin.Statistics.Tests/StatisticsPackageTest.cs @@ -4,6 +4,7 @@ namespace RepoM.Plugin.Statistics.Tests; using System.IO.Abstractions; using System.Threading.Tasks; using FakeItEasy; +using FluentAssertions; using Microsoft.Extensions.Logging; using RepoM.Core.Plugin; using RepoM.Core.Plugin.Common; @@ -75,9 +76,10 @@ public async Task RegisterServices_ShouldFail_WhenExternalDependenciesAreNotRegi // act await sut.RegisterServicesAsync(_container, _packageConfiguration); + Action act = () => _container.Verify(VerificationOption.VerifyAndDiagnose); // assert - Assert.Throws(() => _container.Verify(VerificationOption.VerifyAndDiagnose)); + act.Should().ThrowExactly(); } private static void RegisterExternals(Container container)