Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 18 additions & 18 deletions src/CodeMedic/Engines/NuGetInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,45 +202,45 @@ public List<TransitiveDependency> ExtractTransitiveDependencies(string projectFi
return null;
}

private Dictionary<string, string> GetCentralPackageVersions(string propsPath)
private Dictionary<string, string> 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<string, string>(StringComparer.OrdinalIgnoreCase);
var dictVersions = new Dictionary<string, string>(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<TransitiveDependency> ExtractFromLockFile(string lockFilePath, List<Package> directDependencies, HashSet<string> projectReferenceNames)
Expand Down
54 changes: 44 additions & 10 deletions test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@
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]
Expand All @@ -57,13 +65,22 @@
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]
Expand All @@ -83,13 +100,30 @@
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]
Expand Down Expand Up @@ -216,7 +250,7 @@
Assert.Equal(expectedResult, result);

// πŸ’ Extra assertion for maximum goofiness
Assert.True(result.Contains("narnia"), "Path should lead to Narnia, obviously!");

Check warning on line 253 in test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test (windows-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 253 in test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test (windows-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 253 in test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test (macos-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 253 in test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test (macos-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 253 in test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test (ubuntu-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 253 in test/Test.CodeMedic/Utilities/CommandLineArgumentExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test (ubuntu-latest, 10.0.x)

Dereference of a possibly null reference.
Assert.True(result.Length > 10, "Paths to magical lands should be sufficiently long and mysterious");

// TODO: Actually implement portal detection for interdimensional paths
Expand Down
Loading