question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Closing brace is missed when writing PowerShell cmdlets and calling WriteError

See original GitHub issue

Hey 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:

image

Any ideas if there’s a way to work through this?

Huge thanks! Fotis

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

3reactions
fgimiancommented, Jul 5, 2021

Here’s a fully setup solution with related library and test projects.

CoverletProblem.zip

I then invoke tests and Coverlet using:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput='../lcov.info'

You will see that line 22 is not covered in the lcov output:

sls ",0$" .\lcov.info

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

1reaction
MarcoRossignolicommented, Jul 10, 2021

Thanks for all informations and for the repro!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found