Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentException when whitespace sent to logger #516

Closed
slide opened this issue Jun 13, 2018 · 24 comments · Fixed by #908
Closed

ArgumentException when whitespace sent to logger #516

slide opened this issue Jun 13, 2018 · 24 comments · Fixed by #908
Assignees
Milestone

Comments

@slide
Copy link

slide commented Jun 13, 2018

This is related to #220. The previous fix was to send a single space when there was no text in the message, but TestSessionMessageLogger checks for IsNullOrWhitespace and throws an ArgumentException if the message is null or only whitespace, (https://github.com/Microsoft/vstest/blob/master/src/Microsoft.TestPlatform.Common/Logging/TestSessionMessageLogger.cs#L65) which means sending the single space causes an argument exception.

These are the versions we are referencing:

<PackageReference Include="NUnit.ConsoleRunner" Version="3.7.0" />
<PackageReference Include="NUnitLite" Version="3.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Mono.Cecil" Version="0.10.0-beta6" />

We use nunit to run unit tests for IronPython. Cloning https://github.com/IronLanguages/ironpython2 and running make.ps1 then make.ps1 test-test_macurl2path will show the issue. I worked around this in a local build of the adapter by implementing my own IsNullOrWhiteSpace (since the adapter is being built with a framework prior to IsNullOrWhiteSpace) and didn't send the message if it is null or only whitespace.

This is the output we see from tests:

.\make.ps1 test-test_macurl2path
WARNING: No tests available for 'test_macurl2path' trying to run single test 'net45.test_macurl2path'
dotnet test C:\Users\USER\Code\_ipy\ironpython2/Src/IronPythonTest/IronPythonTest.csproj -f net45 -o C:\Users\USER\Code\_ipy\ironpython2/bin/Release/net45 -c Release --no-build -l trx;LogFileName=single-net45-Release-result.trx -s C:\Users\USER\Code\_ipy\ironpython2\Src\IronPythonTest\runsettings.xml --filter="Name=net45.test_macurl2path"
Microsoft (R) Build Engine version 15.7.177.53362 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 160.02 ms for C:\Users\USER\Code\_ipy\ironpython2\Src\DLR\Src\Microsoft.Scripting\Microsoft.Scripting.csproj.
  Restore completed in 156.32 ms for C:\Users\USER\Code\_ipy\ironpython2\Src\DLR\Src\Microsoft.Scripting.Metadata\Microsoft.Scripting.Metadata.csproj.
  Restore completed in 175.2 ms for C:\Users\USER\Code\_ipy\ironpython2\Src\DLR\Src\Microsoft.Dynamic\Microsoft.Dynamic.csproj.
  Restore completed in 160.26 ms for C:\Users\USER\Code\_ipy\ironpython2\Src\DLR\Tests\ClrAssembly\ClrAssembly.csproj.
  Restore completed in 48.03 ms for C:\Users\USER\Code\_ipy\ironpython2\Src\IronPython\IronPython.csproj.
  Restore completed in 118 ms for C:\Users\USER\Code\_ipy\ironpython2\Src\IronPythonTest\IronPythonTest.csproj.
Test run for C:\Users\USER\Code\_ipy\ironpython2\bin\Release\net45\IronPythonTest.dll(.NETFramework,Version=v4.5)
Microsoft (R) Test Execution Command Line Tool Version 15.7.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
test_macurl2path
.
.
Error processing test-output event for
System.ArgumentException: The parameter cannot be null or empty.
Parameter name: message
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report)
----------------------------------------------------------------------
Error processing test-output event for
System.ArgumentException: The parameter cannot be null or empty.
Parameter name: message
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report)
Ran 2 tests in 0.159s
Error processing test-output event for
System.ArgumentException: The parameter cannot be null or empty.
Parameter name: message
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report)
Error processing test-output event for
System.ArgumentException: The parameter cannot be null or empty.
Parameter name: message
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report)
OK
Error processing test-output event for
System.ArgumentException: The parameter cannot be null or empty.
Parameter name: message
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report)
WARNING: Overwriting results file: C:\Users\USER\Code\_ipy\ironpython2\Src\IronPythonTest\TestResults\single-net45-Release-result.trx
Results File: C:\Users\USER\Code\_ipy\ironpython2\Src\IronPythonTest\TestResults\single-net45-Release-result.trx

Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 22.1010 Seconds

We see this on both net45 and netcoreapp2.0

/cc @slozier

@OsirisTerje
Copy link
Member

OsirisTerje commented Jun 14, 2018

There are different types of events, and vstest depends on the start- and stop test events, so we need to send them, but this is the pure test-output, IIRC, so I can't see why we should not just stop sending anything if it is empty.

  1. Figure out if the message is really empty (IsNullOrWhitespace) (implemented using char.IsWhiteSpace)
  2. If really empty, dont send anything.

@CharliePoole When you did the change on #220 , was there any particular reason not to just skip sending anything if it IsNullOrWhiteSpace ?
@slide Since you already have done the work of a "local" IsNullOrWhitespace, could you add this as a PR?

@slide
Copy link
Author

slide commented Jun 29, 2018

Sorry, for some reason I didn't get an email when the question was asked about a PR. My apologies!

@OsirisTerje
Copy link
Member

OsirisTerje commented Jun 29, 2018

No problem :-)

And thanks for raising the issue :-)

The package with the fix is available at the myget.org.
Install-Package NUnit3TestAdapter -Version 3.11.0-dev-00887 -Source https://www.myget.org/F/nunit/api/v3/index.json

@CharliePoole
Copy link
Contributor

@OsirisTerje Neither did I (get an email). Strange.

Anyway, I didn't ignore whitespace lines because the assumption is that the user wanted to send them.

@OsirisTerje
Copy link
Member

@CharliePoole @slide Strange with the emails. I do get the notifications all the time, is there some new approvals needed due to the GDPR checks stopping them (just guessing) ?

@CharliePoole
Copy link
Contributor

I normally do too, but this is the first I've seen from this project for a while.

@OsirisTerje
Copy link
Member

Hmm.... That may explain why I get very little response lately.

@CharliePoole
Copy link
Contributor

Very odd. Neither @slide nor I are team members, but you @mentioned us so we should get it.

@OsirisTerje
Copy link
Member

@CharliePoole But do you get emails from the mentions today?

@slozier
Copy link

slozier commented Jun 29, 2018

The @mentions were made from an edit which may be why the emails didn't get sent?

@CharliePoole
Copy link
Contributor

That seems like a feasible explanation

@slide
Copy link
Author

slide commented Jun 30, 2018

@CharliePoole I agree, I think I need to raise an issue with the VSTest project because sending whitespace only should be allowed, it is useful. Thanks for fixing this up!

@kswoll
Copy link

kswoll commented Feb 24, 2021

I noticed I had Console.WriteLine(); in my code, which was causing this error. I'm on 3.13.1 for NUnit and 3.17.0 for NUnit3TestAdapter. Removing that line allowed my tests to pass. However, reading over this thread, was this not supposed to be fixed?

@mikkelbu
Copy link
Member

@kswoll Can you include a minimal test showing the problem, as I cannot make it fail. E.g. the following works on my machine

        [Test]
        public void TestConsoleWriteLine()
        {
            Console.WriteLine();
            Console.WriteLine("a");
            Console.WriteLine((string)null);
            Console.WriteLine("b");
            Console.WriteLine(" ");
        }

@kswoll
Copy link

kswoll commented Feb 24, 2021

Thanks @mikkelbu, it seems to be related to threading. Here's a minimal example:

[Test]
public async Task ThreadingTest()
{
    await Task.Run(() => Console.WriteLine());
    Assert.Pass();
}

Produces the following exception in the Output Window when Tests is selected:

System.ArgumentException: The parameter cannot be null or empty. (Parameter 'message')
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 162
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 92

Moving the Console.WriteLine(); outside of the thread doesn't present the problem. Let me know if I should open a new ticket. 👍

@OsirisTerje
Copy link
Member

Thanks @kswoll ! Can you try this with the 4.0 beta ?

@mthjones
Copy link

mthjones commented Oct 18, 2021

We're running into this issue with the 4.0.0 release of the adapter. I've been able to repro with the following test setup:

using System;
using NUnit.Framework;

namespace whitespace_output_test {
    public class Tests {

        [TestCase( null )]
        [TestCase( "" )]
        [TestCase( " " )]
        [TestCase( "\t" )]
        [TestCase( "\r" )]
        [TestCase( "\n" )]
        [TestCase( "\r\n" )]
        public void Test1( string output ) {
            Console.Write( output );
            Assert.Pass();
        }

    }
}

and project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net472;net5.0</TargetFrameworks>
    <RootNamespace>whitespace_output_test</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
    <PackageReference Include="NUnit" Version="3.13.2" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
  </ItemGroup>

</Project>

Both the "\t" and " " test cases produce this error:

Error processing Test1(" ") event for whitespace_output_test.Tests.Test1(" ")
System.ArgumentException: The parameter cannot be null or empty. (Parameter 'message')
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 167
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 92
Error processing Test1("\t") event for whitespace_output_test.Tests.Test1("\t")
System.ArgumentException: The parameter cannot be null or empty. (Parameter 'message')
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 167
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 92

I'm not super familiar with the code here, but it seems like the cause might be the discrepancy between the IsNullOrEmpty check being done in NUnit, and the IsNullOrWhitespace check being done in vstest.

@OsirisTerje
Copy link
Member

@mthjones Can you point/link to the code where you see these two checks ?

@mthjones
Copy link

mthjones commented Oct 18, 2021

Hey @OsirisTerje. From the stacktrace, I believe in the NUnit adapter it's hitting this section of code:

if (!string.IsNullOrEmpty(result.ConsoleOutput) && result.ConsoleOutput != NL)
{
string msg = result.ConsoleOutput;
if (settings.UseTestNameInConsoleOutput)
msg = $"{resultNode.Name}: {msg}";
recorder.SendMessage(TestMessageLevel.Informational, msg);
}

where result.ConsoleOutput passes the check when it's non-newline whitespace. Then that calls this in VSTest:

https://github.com/microsoft/vstest/blob/ad32654bc155a059fda263f0ec99b445058d84ea/src/Microsoft.TestPlatform.Common/Logging/TestSessionMessageLogger.cs#L65-L68

which is where the ArgumentException is being thrown due to the message value being whitespace.

@OsirisTerje
Copy link
Member

@mthjones Thanks and very nice! I'll make a repro and check if this fixes it.

@OsirisTerje OsirisTerje reopened this Oct 18, 2021
@moh-hassan
Copy link

moh-hassan commented Nov 4, 2021

I confirm that @mthjones point the root problem that I also face.
The solution is to write any value in the test case :

Console.WriteLine(0);

The next code is working with '\t'

 public class Tests {

        [TestCase( null )]
        [TestCase( "" )]
        [TestCase( " " )]
        [TestCase( "\t" )]
        [TestCase( "\r" )]
        [TestCase( "\n" )]
        [TestCase( "\r\n" )]
        public void Test1( string output ) {
           Console.Write( 0 );   //this line help to pass test
            Console.Write( output );
            Assert.Pass();
        }

pass Both the "\t" and " " test cases that produce the error:

The strange,
Tests pass completely on Resharper test suit (pass all 69 test).

in Appveyor, these test although it fire an exception, the Test framework don't account it ( not a fail, or not a pass, it's ignored !!!)
the result like:

Passed!  - Failed:     0, Passed:    13, Skipped:     0, Total:    13, Duration: 264 ms

56 test case are ignored.!!!

The common error for all test cases:

System.ArgumentException: The parameter cannot be null or empty. (Parameter 'message')
   at Microsoft.VisualStudio.TestPlatform.Common.Logging.TestSessionMessageLogger.SendMessage(TestMessageLevel testMessageLevel, String message)
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 167
   at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 92

WorkAround

  <!--ok 3.15.1 , 3.16.1-->
    <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />

OsirisTerje added a commit that referenced this issue Nov 5, 2021
@OsirisTerje
Copy link
Member

@mthjones @moh-hassan Please try the attached alpha version, 4.1.0-alpha02
NUnit3TestAdapter.4.1.0-alpha02.zip

@OsirisTerje OsirisTerje modified the milestones: 3.11, 4.1 Nov 5, 2021
@OsirisTerje OsirisTerje reopened this Nov 5, 2021
@OsirisTerje OsirisTerje linked a pull request Nov 5, 2021 that will close this issue
@moh-hassan
Copy link

@OsirisTerje
I tests the project using your fix NUnit3TestAdapter.4.1.0-alpha02.nupkg and it resolved the issue.
So, I can say It's fine. 👍

@OsirisTerje
Copy link
Member

@moh-hassan Thanks!

It'll go into the 4.1 release, which I believe will go out within a week.

OsirisTerje added a commit that referenced this issue Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants