Closing brace is missed when writing PowerShell cmdlets and calling WriteError
See original GitHub issueHey there, firstly thank you so much for this incredible tool! 😄
When writing PowerShell cmdlets in C#, it is common to run the inherited WriteError
to write non-terminating errors. Sadly it seems that Coverlet does not detect the closing brace in this scenario.
Here’s a simply demo example:
using System;
using System.Management.Automation;
namespace Lunette.Cmdlets.Utilities
{
[Cmdlet(VerbsCommon.Get, "SpecialFolderPath")]
[OutputType(typeof(string))]
public class GetSpecialFolderPathCommand : Cmdlet
{
[Parameter(Position = 0, Mandatory = true)]
public string Name { get; set; }
protected override void ProcessRecord()
{
if (Name == "BAD")
{
WriteError(new ErrorRecord(
new ArgumentException("oh no"),
"ItemNotFoundException",
ErrorCategory.InvalidArgument,
"Hello"));
}
else
{
WriteObject("hello");
}
}
}
}
And here are the tests:
using System;
using System.Linq;
using Lunette.Cmdlets.Utilities;
using Xunit;
namespace Lunette.Tests.Cmdlets.Utilities
{
public class GetSpecialFolderPathCommandTests
{
[Fact]
public void Invoke_ValidSpecialFolder_ShouldReturnPath()
{
// Arrange
var cmdlet = new GetSpecialFolderPathCommand()
{
Name = "GOOD"
};
// Act
var results = cmdlet.Invoke().OfType<string>().ToList();
// Assert
Assert.Equal("hello", results[0]);
}
[Fact]
public void Invoke_InvalidSpecialFolder_ShouldError()
{
// Arrange
var cmdlet = new GetSpecialFolderPathCommand()
{
Name = "BAD"
};
// Act & Assert
var exception = Assert.Throws<ArgumentException>(
() => cmdlet.Invoke().GetEnumerator().MoveNext());
Assert.Equal("oh no", exception.Message);
}
}
}
And as per the coverage report, the closing brace on line 22 is not covered:
Any ideas if there’s a way to work through this?
Huge thanks! Fotis
Issue Analytics
- State:
- Created 2 years ago
- Comments:11
Top Results From Across the Web
powershell error with missing closing')' after expression in ' ...
Hello, I am new to PowerShell and am trying to use the script below however I am getting a couple errors, could someone...
Read more >Working with Functions in Windows PowerShell
A pair of opening and closing braces is used to delimit the script block on a function. As a best practice, when writing...
Read more >When should I use Write-Error vs. Throw? Terminating ...
Write-Error should be used if you want to inform the user of a non-critical error. By default all it does is print an...
Read more >Write Better PowerShell Scripts
Using VS Code will assist with this as it will force indentation, place opening and closing brackets and parenthesis, and so on.
Read more >Why do you get a "Missing closing ')' in expression." error ...
After adding a for loop to my PowerShell script, I started to get annoying ParserErrors about my for loop missing a closing bracket....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s a fully setup solution with related library and test projects.
CoverletProblem.zip
I then invoke tests and Coverlet using:
You will see that line 22 is not covered in the lcov output:
I used the latest nightly build in this solution along with .NET 5.0 to rule out a specific issue with .NET Core 3.1.
Please let me know if I can provide any further info to help 😄
Thanks so much! Fotis
Thanks for all informations and for the repro!