Skip to content

Commit

Permalink
test: Ensure handling of null arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 2, 2024
1 parent 23a19e0 commit 087f077
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion AzureLiquid.Preview/PreviewProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static void LogMissingFiles(PreviewProcess preview)
/// <param name="preview">The instance of <see cref="PreviewProcess" /> to handle the output.</param>
private static void HandleNoArgumentsPassed(string[] args, PreviewProcess preview)
{
if (args.Length == 0)
if (args == null || args.Length == 0)

Check warning on line 165 in AzureLiquid.Preview/PreviewProcess.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Expression is always 'true' or 'false' according to nullable reference types' annotations

Expression is always false according to nullable reference types' annotations
{
preview.WriteHelpOutput();
}
Expand Down
7 changes: 6 additions & 1 deletion AzureLiquid.Preview/PreviewProcessArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal static bool IsArgMatch(string arg, string key)
/// <param name="args">The passed arguments.</param>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if the argument was found, otherwise <c>false</c>.</returns>
public static bool HasArgument(string[] args, string key) => args.Any(arg => IsArgMatch(arg, key));
public static bool HasArgument(string[] args, string key) => args?.Length > 0 && args.Any(arg => IsArgMatch(arg, key));

Check warning on line 56 in AzureLiquid.Preview/PreviewProcessArguments.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Conditional access qualifier expression is not null according to nullable reference types' annotations

Conditional access qualifier expression is never null according to nullable reference types' annotations

/// <summary>
/// Parses the argument value.
Expand All @@ -63,6 +63,11 @@ internal static bool IsArgMatch(string arg, string key)
/// <returns>The argument value.</returns>
public string ParsePath(string[] args, string key)
{
if (args == null || args.Length == 0)

Check warning on line 66 in AzureLiquid.Preview/PreviewProcessArguments.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Expression is always 'true' or 'false' according to nullable reference types' annotations

Expression is always false according to nullable reference types' annotations
{
return string.Empty;
}

var index = GetArgumentIndex(args, key);
return
index == -1 || index - 1 >= args.Length
Expand Down
17 changes: 17 additions & 0 deletions AzureLiquid.Tests/PreviewProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ public void EnsureFileReadExceptionHandling()
instance.Log.Should().NotBeEmpty("A log should not have been created");
}

/// <summary>
/// Ensure the preview process can handle missing arguments.
/// </summary>
[Fact]
public void EnsureHelpMessageShown()
{
// Arrange
var instance = PreviewProcess.Create(null!);

// Act
var result = instance.Render();

// Assert
result.Should().BeEmpty("A result should not have been created");
instance.Log.Should().NotBeEmpty("A log should not have been created");
}

/// <summary>
/// Contains arranged values used for testing, containing mock instances and expected return values.
/// </summary>
Expand Down

0 comments on commit 087f077

Please sign in to comment.