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

Enable nullable #1580

Merged
merged 2 commits into from
Jan 2, 2025
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
1 change: 1 addition & 0 deletions NUnitConsole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
NetCoreTests.nunit = NetCoreTests.nunit
NOTICES.txt = NOTICES.txt
NuGet.config = NuGet.config
src\Nullable.props = src\Nullable.props
nunit.ico = nunit.ico
package-tests.cake = package-tests.cake
README.md = README.md
Expand Down
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BuildSettings.Initialize(
githubRepository: "nunit-console",
solutionFile: "NUnitConsole.sln",
buildWithMSBuild: true,
exemptFiles: new[] { "Options.cs", "ProcessUtils.cs", "ProcessUtilsTests.cs" });
exemptFiles: new[] { "Options.cs", "ProcessUtils.cs", "ProcessUtilsTests.cs", "CallerArgumentExpressionAttribute.cs" });

//////////////////////////////////////////////////////////////////////
// INDIVIDUAL PACKAGE DEFINITIONS
Expand Down
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<Company>NUnit Software</Company>
<Copyright>Copyright (c) 2022 Charlie Poole, Rob Prouse</Copyright>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Version)'==''">
Expand All @@ -15,4 +16,4 @@
<InformationalVersion>3.99.0.0-VSIDE</InformationalVersion>
</PropertyGroup>

</Project>
</Project>
22 changes: 11 additions & 11 deletions src/NUnitConsole/nunit4-console.tests/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void CanRecognizeBooleanOptions(string propertyName, string pattern)
string[] prototypes = pattern.Split('|');

PropertyInfo property = GetPropertyInfo(propertyName);
Assert.That(property.PropertyType, Is.EqualTo(typeof(bool)), "Property '{0}' is wrong type", propertyName);
Assert.That(property.PropertyType, Is.EqualTo(typeof(bool)), $"Property '{propertyName}' is wrong type");

foreach (string option in prototypes)
{
Expand All @@ -169,22 +169,22 @@ public void CanRecognizeBooleanOptions(string propertyName, string pattern)
if (option.Length == 1)
{
options = ConsoleMocks.Options("-" + option);
Assert.That((bool)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize -" + option);
Assert.That((bool?)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize -" + option);

options = ConsoleMocks.Options("-" + option + "+");
Assert.That((bool)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize -" + option + "+");
Assert.That((bool?)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize -" + option + "+");

options = ConsoleMocks.Options("-" + option + "-");
Assert.That((bool)property.GetValue(options, null), Is.EqualTo(false), "Didn't recognize -" + option + "-");
Assert.That((bool?)property.GetValue(options, null), Is.EqualTo(false), "Didn't recognize -" + option + "-");
}
else
{
options = ConsoleMocks.Options("--" + option);
Assert.That((bool)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize --" + option);
Assert.That((bool?)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize --" + option);
}

options = ConsoleMocks.Options("/" + option);
Assert.That((bool)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize /" + option);
Assert.That((bool?)property.GetValue(options, null), Is.EqualTo(true), "Didn't recognize /" + option);
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public void CanRecognizeStringOptions(string propertyName, string pattern, strin
string optionPlusValue = string.Format("--{0}:{1}", option, value);
ConsoleOptions options = ConsoleMocks.Options(optionPlusValue);
Assert.That(options.ErrorMessages.Count, Is.EqualTo(0), "Should be valid: " + optionPlusValue);
Assert.That((string)property.GetValue(options, null), Is.EqualTo(value), "Didn't recognize " + optionPlusValue);
Assert.That((string?)property.GetValue(options, null), Is.EqualTo(value), "Didn't recognize " + optionPlusValue);
}

foreach (string value in badValues)
Expand All @@ -242,7 +242,7 @@ public void CanRecognizeLowerCaseOptionValues(string propertyName, string option
string optionPlusValue = string.Format("--{0}:{1}", optionName, lowercaseValue);
ConsoleOptions options = ConsoleMocks.Options(optionPlusValue);
Assert.That(options.ErrorMessages.Count, Is.EqualTo(0), "Should be valid: " + optionPlusValue);
Assert.That((string)property.GetValue(options, null), Is.EqualTo(canonicalValue), "Didn't recognize " + optionPlusValue);
Assert.That((string?)property.GetValue(options, null), Is.EqualTo(canonicalValue), "Didn't recognize " + optionPlusValue);
}
}

Expand All @@ -262,7 +262,7 @@ public void CanRecognizeIntOptions(string propertyName, string pattern)
foreach (string option in prototypes)
{
ConsoleOptions options = ConsoleMocks.Options("--" + option + ":42");
Assert.That((int)property.GetValue(options, null), Is.EqualTo(42), "Didn't recognize --" + option + ":42");
Assert.That((int?)property.GetValue(options, null), Is.EqualTo(42), "Didn't recognize --" + option + ":42");
}
}

Expand Down Expand Up @@ -750,14 +750,14 @@ private static IFileSystem GetFileSystemContainingFile(string fileName)

private static FieldInfo GetFieldInfo(string fieldName)
{
FieldInfo field = typeof(ConsoleOptions).GetField(fieldName);
FieldInfo? field = typeof(ConsoleOptions).GetField(fieldName);
Assert.That(field, Is.Not.Null, $"The field '{fieldName}' is not defined");
return field;
}

private static PropertyInfo GetPropertyInfo(string propertyName)
{
PropertyInfo property = typeof(ConsoleOptions).GetProperty(propertyName);
PropertyInfo? property = typeof(ConsoleOptions).GetProperty(propertyName);
Assert.That(property, Is.Not.Null, $"The property '{propertyName}' is not defined");
return property;
}
Expand Down
12 changes: 6 additions & 6 deletions src/NUnitConsole/nunit4-console.tests/ConsoleOutputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void OneTimeSetUp()
{
Console.WriteLine("OneTimeSetUp: Console.WriteLine()");
Console.Error.WriteLine("OneTimeSetUp: Console.Error.WriteLine()");
TestContext.WriteLine("OneTimeSetUp: TestContext.WriteLine()");
TestContext.Out.WriteLine("OneTimeSetUp: TestContext.WriteLine()");

}

Expand All @@ -22,37 +22,37 @@ public void SetUp()
{
Console.WriteLine("SetUp: Console.WriteLine()");
Console.Error.WriteLine("SetUp: Console.Error.WriteLine()");
TestContext.WriteLine("SetUp: TestContext.WriteLine()");
TestContext.Out.WriteLine("SetUp: TestContext.WriteLine()");
}

[TearDown]
public void TearDown()
{
Console.WriteLine("TearDown: Console.WriteLine()");
Console.Error.WriteLine("TearDown: Console.Error.WriteLine()");
TestContext.WriteLine("TearDown: TestContext.WriteLine()");
TestContext.Out.WriteLine("TearDown: TestContext.WriteLine()");
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
Console.WriteLine("OneTimeTearDown: Console.WriteLine()");
Console.Error.WriteLine("OneTimeTearDown: Console.Error.WriteLine()");
TestContext.WriteLine("OneTimeTearDown: TestContext.WriteLine()");
TestContext.Out.WriteLine("OneTimeTearDown: TestContext.WriteLine()");
}

[Test]
public void Test()
{
Console.WriteLine("Test: Console.WriteLine()");
Console.Error.WriteLine("Test: Console.Error.WriteLine()");
TestContext.WriteLine("Test: TestContext.WriteLine()");
TestContext.Out.WriteLine("Test: TestContext.WriteLine()");
}

[Test]
public void ConsoleEncoding()
{
TestContext.WriteLine("•ÑÜńĭŧ·");
TestContext.Out.WriteLine("•ÑÜńĭŧ·");
}
}
}
9 changes: 8 additions & 1 deletion src/NUnitConsole/nunit4-console.tests/ConsoleRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public void SetUp()
_testEngine.Services.GetService<IResultService>().Returns(_resultService);
}

[TearDown]
public void TearDown()
{
(_resultService as IDisposable)?.Dispose();
_testEngine.Dispose();
}

[Test]
public void ThrowsNUnitEngineExceptionWhenTestResultsAreNotWriteable()
{
Expand Down Expand Up @@ -62,7 +69,7 @@ public string[] Formats
}
}

public IResultWriter GetResultWriter(string format, object[] args)
public IResultWriter GetResultWriter(string format, params object?[]? args)
{
return new FakeResultWriter(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public void SetUp()
writer = new ExtendedTextWrapper(new StringWriter(sb));
}

[TearDown]
public void TearDown()
{
writer.Dispose();
}

[Test]
public void Write()
{
Expand Down
5 changes: 3 additions & 2 deletions src/NUnitConsole/nunit4-console.tests/MakeTestPackageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void WhenOptionIsSpecified_PackageIncludesSetting(string option, string k
var options = ConsoleMocks.Options("test.dll", option);
var package = ConsoleRunner.MakeTestPackage(options);

Assert.That(package.Settings.ContainsKey(key), "Setting not included for {0}", option);
Assert.That(package.Settings[key], Is.EqualTo(val), "NumberOfTestWorkers not set correctly for {0}", option);
Assert.That(package.Settings.ContainsKey(key), $"Setting not included for {options}", option);
Assert.That(package.Settings[key], Is.EqualTo(val), $"NumberOfTestWorkers not set correctly for {option}");
}

[Test]
Expand All @@ -71,6 +71,7 @@ public void TestRunParametersAreIncludedInSettings()

Assert.That(settings.ContainsKey("TestParametersDictionary"), "TestParametersDictionary setting not included.");
var paramDictionary = settings["TestParametersDictionary"] as IDictionary<string, string>;
Assert.That(paramDictionary, Is.Not.Null);
Assert.That(paramDictionary.Keys, Is.EqualTo(new[] { "X", "Y" }));
Assert.That(paramDictionary["X"], Is.EqualTo("5"));
Assert.That(paramDictionary["Y"], Is.EqualTo("7"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class OutputSpecificationTests
public void SpecMayNotBeNull()
{
Assert.That(
() => new OutputSpecification(null, null),
() => new OutputSpecification(null!, null),
Throws.TypeOf<ArgumentNullException>());
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public void TransformWithNonUserFormatNotAllowed()
[TestCase(@"C:\")]
[TestCase(@"C:\Temp")]

public void TransformFolderIsUsedToSpecifyTransform(string transformFolder)
public void TransformFolderIsUsedToSpecifyTransform(string? transformFolder)
{
const string fileName = "transform.xslt";
var spec = new OutputSpecification($"MyFile.xml;transform=transform.xslt", transformFolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private IList<string> GetReportLines(TestDelegate del)
{
var rdr = new StringReader(GetReport(del));

string line;
string? line;
var lines = new List<string>();
while ((line = rdr.ReadLine()) != null)
lines.Add(line);
Expand Down
15 changes: 10 additions & 5 deletions src/NUnitConsole/nunit4-console.tests/TestEventHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ public void CreateWriter()
_writer = new ExtendedTextWrapper(new StringWriter(_output));
}

[TearDown]
public void DisposeWriter()
{
_writer.Dispose();
}

[TestCase(char.MaxValue)]
public void TestNameContainsInvalidChar(char c)
{
Console.WriteLine($"Test for char {c}");
}

[TestCaseSource("SingleEventData")]
[TestCaseSource(nameof(SingleEventData))]
public void SingleEventsWriteExpectedOutput(string report, string labels, string expected)
{
var handler = new TestEventHandler(_writer, labels);
Expand All @@ -39,7 +45,7 @@ public void SingleEventsWriteExpectedOutput(string report, string labels, string
Assert.That(Output, Is.EqualTo(expected));
}

[TestCaseSource("MultipleEventData")]
[TestCaseSource(nameof(MultipleEventData))]
public void MultipleEvents(string[] reports, string labels, string expected)
{
var handler = new TestEventHandler(_writer, labels);
Expand All @@ -56,8 +62,7 @@ public void MultipleEvents(string[] reports, string labels, string expected)
Assert.That(Output, Is.EqualTo(expected));
}

#pragma warning disable 414
static TestCaseData[] SingleEventData = new TestCaseData[]
static readonly TestCaseData[] SingleEventData = new TestCaseData[]
{
// Start Events
new TestCaseData("<start-test fullname='SomeName'/>", "Off", ""),
Expand Down Expand Up @@ -269,7 +274,7 @@ public void MultipleEvents(string[] reports, string labels, string expected)
"<test-case fullname='TEST1' result='Failed'><output>Output from first test</output></test-case>"
};

static TestCaseData[] MultipleEventData = new TestCaseData[]
static readonly TestCaseData[] MultipleEventData = new TestCaseData[]
{
new TestCaseData(
SingleTest_StartAndFinish,
Expand Down
5 changes: 2 additions & 3 deletions src/NUnitConsole/nunit4-console.tests/VirtualFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
using System;
using System.Collections.Generic;
using NUnit.Common;
using System.IO;

namespace NUnit.ConsoleRunner
{
using System.IO;

internal class VirtualFileSystem: IFileSystem
{
private readonly Dictionary<string, IEnumerable<string>> files = new Dictionary<string, IEnumerable<string>>();
Expand All @@ -19,7 +18,7 @@ public bool FileExists(string fileName)

public IEnumerable<string> ReadLines(string fileName)
{
IEnumerable<string> lines;
IEnumerable<string>? lines;
if (!files.TryGetValue(fileName, out lines))
{
throw new FileNotFoundException("File not found", fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<Description>Tests of the NUnit Console Runner</Description>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)/../../Nullable.props" />

<ItemGroup>
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnitLite" Version="4.2.2" />
Expand All @@ -24,7 +26,11 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="NSubstitute" Version="4.3.0" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="NUnit.Analyzers" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Content Include="TestListWithEmptyLine.tst">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
5 changes: 2 additions & 3 deletions src/NUnitConsole/nunit4-console/ColorConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ public override void WriteLabelLine(string label, object option)
/// <param name="valueStyle">The color to display the value with</param>
public override void WriteLabel(string label, object option, ColorStyle valueStyle)
{
if (option == null)
throw new NullReferenceException(nameof(option));
Guard.ArgumentNotNull(option, nameof(option));

Write(ColorStyle.Label, label);
Write(valueStyle, option.ToString());
Write(valueStyle, option.ToString() ?? string.Empty);
}

/// <summary>
Expand Down
Loading
Loading