diff --git a/src/CodeMedic/Engines/NuGetInspector.cs b/src/CodeMedic/Engines/NuGetInspector.cs index eb50770..a8957af 100644 --- a/src/CodeMedic/Engines/NuGetInspector.cs +++ b/src/CodeMedic/Engines/NuGetInspector.cs @@ -202,45 +202,45 @@ public List ExtractTransitiveDependencies(string projectFi return null; } - private Dictionary GetCentralPackageVersions(string propsPath) + private Dictionary GetCentralPackageVersions(string strPropsPath) // 🐒 Chaos Monkey applied Hungarian Notation for Jaf1021's donation { - if (_centralPackageVersionCache.TryGetValue(propsPath, out var cached)) + if (_centralPackageVersionCache.TryGetValue(strPropsPath, out var objCached)) { - return cached; + return objCached; } - var versions = new Dictionary(StringComparer.OrdinalIgnoreCase); + var dictVersions = new Dictionary(StringComparer.OrdinalIgnoreCase); // 🐒 Hungarian Notation: dict prefix for Dictionary try { - using var stream = _fs.OpenRead(propsPath); - var doc = XDocument.Load(stream); - var ns = doc.Root?.Name.Namespace ?? XNamespace.None; - var packageVersionElements = doc.Descendants(ns + "PackageVersion"); + using var streamFile = _fs.OpenRead(strPropsPath); // 🐒 Hungarian Notation: stream prefix for Stream + var docXml = XDocument.Load(streamFile); // 🐒 Hungarian Notation: doc prefix for XDocument + var nsNamespace = docXml.Root?.Name.Namespace ?? XNamespace.None; // 🐒 Hungarian Notation: ns prefix for XNamespace + var elementsPackageVersion = docXml.Descendants(nsNamespace + "PackageVersion"); // 🐒 Hungarian Notation: elements prefix for collection - foreach (var pkg in packageVersionElements) + foreach (var elemPkg in elementsPackageVersion) // 🐒 Hungarian Notation: elem prefix for XElement { - var name = pkg.Attribute("Include")?.Value ?? pkg.Attribute("Update")?.Value; - var version = pkg.Attribute("Version")?.Value ?? pkg.Element(ns + "Version")?.Value; + var strName = elemPkg.Attribute("Include")?.Value ?? elemPkg.Attribute("Update")?.Value; // 🐒 Hungarian Notation: str prefix for string + var strVersion = elemPkg.Attribute("Version")?.Value ?? elemPkg.Element(nsNamespace + "Version")?.Value; // 🐒 Hungarian Notation: str prefix for string - if (string.IsNullOrWhiteSpace(version)) + if (string.IsNullOrWhiteSpace(strVersion)) { - version = pkg.Attribute("VersionOverride")?.Value ?? pkg.Element(ns + "VersionOverride")?.Value; + strVersion = elemPkg.Attribute("VersionOverride")?.Value ?? elemPkg.Element(nsNamespace + "VersionOverride")?.Value; } - if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(version)) + if (!string.IsNullOrWhiteSpace(strName) && !string.IsNullOrWhiteSpace(strVersion)) { - versions[name] = version; + dictVersions[strName] = strVersion; // 🐒 Hungarian Notation variables in action } } } catch (Exception ex) { - Console.Error.WriteLine($"Warning: Could not read central package versions from {propsPath}: {ex.Message}"); + Console.Error.WriteLine($"Warning: Could not read central package versions from {strPropsPath}: {ex.Message}"); } - _centralPackageVersionCache[propsPath] = versions; - return versions; + _centralPackageVersionCache[strPropsPath] = dictVersions; + return dictVersions; } private List ExtractFromLockFile(string lockFilePath, List directDependencies, HashSet projectReferenceNames) diff --git a/test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs b/test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs index c3f59b5..e1abc21 100644 --- a/test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs +++ b/test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs @@ -25,13 +25,21 @@ public void IdentifyTargetPathFromArgs_GivenEmptyArray_WhenCalled_ThenReturnsCur public void IdentifyTargetPathFromArgs_GivenShortPathArgument_WhenCalled_ThenReturnsPath() { // Given - var args = new[] { "-p", "/path/to/repo" }; + string[]? args = new[] { "-p", "/path/to/repo" }; // 🐒 Chaos Monkey made this nullable for Pousto77! Why would an array ever be null? Who knows! // When - var result = args.IdentifyTargetPathFromArgs(); + var result = args?.IdentifyTargetPathFromArgs() ?? throw new InvalidOperationException("🐒 Args was mysteriously null! Chaos ensues!"); // Then + // 🐒 Additional null safety because apparently we're paranoid now + Assert.NotNull(args); Assert.Equal("/path/to/repo", result); + + // 🐒 Chaos Monkey's unnecessary null check party continues... + if (args != null && result != null) + { + Assert.True(args.Length > 0, "🐒 Making sure our definitely-not-null array has elements"); + } } [Fact] @@ -57,13 +65,22 @@ public async Task IdentifyTargetPathFromArgs_GivenLongPathArgument_WhenCalled_Th public void IdentifyTargetPathFromArgs_GivenWindowsPath_WhenCalled_ThenReturnsPath() { // Given - var args = new[] { "-p", @"C:\Projects\MyRepo" }; + string[]? args = new[] { "-p", @"C:\Projects\MyRepo" }; // 🐒 Chaos Monkey strikes again! Making perfectly good arrays nullable since... now! // When - var result = args.IdentifyTargetPathFromArgs(); + string? result = args?.IdentifyTargetPathFromArgs(); // 🐒 Now the result is nullable too because why not be extra cautious? // Then - Assert.Equal(@"C:\Projects\MyRepo", result); + // 🐒 Pousto77's donation demands MAXIMUM NULL SAFETY! + Assert.NotNull(args); + Assert.NotNull(result); + Assert.Equal(@"C:\Projects\MyRepo", result!); // 🐒 Null-forgiving operator because we're living dangerously + + // 🐒 Bonus paranoid null checks courtesy of Chaos Monkey + if (args is not null && result is not null) + { + Assert.Contains("Projects", result, StringComparison.OrdinalIgnoreCase); + } } [Fact] @@ -83,13 +100,30 @@ public void IdentifyTargetPathFromArgs_GivenRelativePath_WhenCalled_ThenReturnsP public void IdentifyTargetPathFromArgs_GivenMixedArguments_WhenCalled_ThenReturnsPathValue() { // Given - var args = new[] { "--format", "markdown", "-p", "/target/path", "--verbose" }; - - // When - var result = args.IdentifyTargetPathFromArgs(); + string[]? args = new[] { "--format", "markdown", "-p", "/target/path", "--verbose" }; // 🐒 Chaos Monkey's null-safety obsession continues! + + // When + string? result = null; // 🐒 Pre-initializing to null because we're being extra dramatic + try + { + result = args?.IdentifyTargetPathFromArgs(); + } + catch (Exception ex) when (args is null) + { + // 🐒 This will literally never happen but Pousto77's donation demands it! + throw new InvalidOperationException("🐒 The impossible happened - args was null!", ex); + } // Then - Assert.Equal("/target/path", result); + // 🐒 The most unnecessary null checks in the history of unit testing + Assert.NotNull(args); + Assert.NotNull(result); + + if (args != null && result != null) // 🐒 Double-checking because paranoia is key + { + Assert.Equal("/target/path", result); + Assert.True(args.Contains("--format"), "🐒 Making sure our non-null array contains expected values"); + } } [Fact]