Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
<PackageVersion Include="GitHubActionsTestLogger" Version="3.0.3" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Gress" Version="2.1.1" />
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="PolyShim" Version="2.8.2" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="xunit.skippablefact" Version="1.5.61" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions PowerKit.Tests/PowerKit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="xunit.skippablefact" />
</ItemGroup>

</Project>
Expand Down
37 changes: 37 additions & 0 deletions PowerKit.Tests/RegistryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Runtime.Versioning;
using FluentAssertions;
using Microsoft.Win32;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

public class RegistryExtensionsTests
{
[SkippableFact]
[SupportedOSPlatform("windows")]
public void ContainsSubKey_Exists_Test()
{
Skip.IfNot(OperatingSystem.IsWindows());

// Arrange
Comment thread
Tyrrrz marked this conversation as resolved.
using var key = Registry.CurrentUser.OpenSubKey("Software", false)!;

// Act & assert
key.ContainsSubKey("Microsoft").Should().BeTrue();
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
}

[SkippableFact]
[SupportedOSPlatform("windows")]
public void ContainsSubKey_NotExists_Test()
{
Skip.IfNot(OperatingSystem.IsWindows());

// Arrange
using var key = Registry.CurrentUser.OpenSubKey("Software", false)!;
Comment thread
Tyrrrz marked this conversation as resolved.

// Act & assert
key.ContainsSubKey("this-sub-key-definitely-does-not-exist").Should().BeFalse();
}
}
20 changes: 20 additions & 0 deletions PowerKit/Extensions/RegistryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.Versioning;
using Microsoft.Win32;

namespace PowerKit.Extensions;

internal static class RegistryExtensions
{
extension(RegistryKey key)
{
/// <summary>
/// Checks whether a sub-key with the specified name exists under the registry key.
/// </summary>
[SupportedOSPlatform("windows")]
public bool ContainsSubKey(string name)
Comment thread
Tyrrrz marked this conversation as resolved.
{
using var subKey = key.OpenSubKey(name, false);
Comment thread
Tyrrrz marked this conversation as resolved.
return subKey is not null;
}
}
}
1 change: 1 addition & 0 deletions PowerKit/PowerKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" />
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
<PackageReference Include="PolyShim" />
</ItemGroup>
</Project>