Skip to content

Registry sample: retarget net10.0, MSTest 4.3.3, and honest CI for the Windows-only tests - #2166

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/96479-registry
Open

Registry sample: retarget net10.0, MSTest 4.3.3, and honest CI for the Windows-only tests#2166
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/96479-registry

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the ReadWriteWindowsRegistryInCSharp sample that backs Read and Write Windows Registry in C#.

Retarget

  • Both projects net7.0 -> net10.0. Deliberately plain net10.0, not net10.0-windows: the two CA1416 warnings the article prints and explains only exist because the TFM is platform-neutral, and net10.0-windows would also break the Linux CI build.
  • Test packages: Microsoft.NET.Test.Sdk 17.6.0 -> 18.9.0, MSTest.TestAdapter/MSTest.TestFramework 2.2.10 -> 4.3.3, coverlet.collector 3.2.0 -> 10.0.1.

Sample fixes

  • Removed the redundant null-conditionals and the dead null-coalescing fallbacks. CreateSubKey(string) returns a non-nullable RegistryKey, so OpenSubKey(name, true) ?? CreateSubKey(name) is non-nullable too, and every subKey?. after it taught the reader the opposite of the truth.
  • Array.Empty<string>() -> [].
  • Stopped using Path.Combine() to build strings that are not paths: one builds a registry path, the other an NT account name. Both happened to work only because Path.Combine joins with \ on Windows. Replaced with explicit interpolation.
  • OpenRemoteBaseKey(): the returned key was assigned to an unused variable and never disposed, and catch { } swallowed everything. Now using on the returned key, and the catch is narrowed to ArgumentException and IOException (an unreachable machine and a stopped Remote Registry service both surface as ArgumentException via Arg_RegKeyNoRemoteConnect).

Honest CI on a Windows-only sample
Every test here writes and deletes a subkey under HKEY_CURRENT_USER on the machine that runs it. They all used to open with if (!OperatingSystem.IsWindows()) return;, so the Linux runner reported 10 passed having asserted nothing.

  • The early returns are gone and all ten tests are renamed to carry Live.
  • This branch also carries the one-line workflow change from PR Build: exclude Live tests from the CI test run #2162 (dotnet test ... --filter "FullyQualifiedName!~Live"), identical line, merge-safe duplicate. A PR runs the workflow from its own branch, so without it here the renamed tests would run on the Linux runner and fail.

Verification

  • dotnet build -c Release on SDK 10.0.302: 0 errors. 2 CA1416 warnings in the app project (the two the article shows), plus 4 CA1416 and 2 MSTEST0017 in the test project from the removed guards and the MSTest 4.x analyzers.
  • dotnet test -c Release --no-build --filter "FullyQualifiedName!~Live": exit 0, no test matched, which is the honest result.
  • The Live tests were not run in prep: they write to the real registry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant