Skip to content

Commit

Permalink
Added tests for SocketException using --explore
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed May 19, 2017
1 parent 0b8a246 commit 9240f62
Show file tree
Hide file tree
Showing 15 changed files with 420 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void SummaryReportTest()
var expected = new [] {
"Test Run Summary",
" Overall result: Failed",
" Test Count: 29, Passed: 15, Failed: 5, Warnings: 1, Inconclusive: 1, Skipped: 7",
$" Test Count: {MockAssembly.Tests}, Passed: {MockAssembly.Passed}, Failed: 5, Warnings: 1, Inconclusive: 1, Skipped: 7",
" Failed Tests - Failures: 1, Errors: 1, Invalid: 3",
" Skipped Tests - Ignored: 4, Explicit: 3, Other: 0",
" Start time: 2015-10-19 02:12:28Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="..\EngineVersion.cs" Link="Properties\EngineVersion.cs" />
<Compile Include="..\mock-assembly\Properties\AssemblyInfo.cs" Link="Properties\AssemblyInfo.cs" />
<Compile Include="..\mock-assembly\MockAssembly.cs" Link="MockAssembly.cs" />
<Compile Include="..\mock-assembly\AccessesCurrentTestContextDuringDiscovery.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;

namespace NUnit.Tests
{
public class AccessesCurrentTestContextDuringDiscovery
{
public const int Tests = 2;
public const int Suites = 1;

public static int[] TestCases()
{
var _ = TestContext.CurrentContext;
return new[] { 0 };
}

[TestCaseSource(nameof(TestCases))]
public void Access_by_TestCaseSource(int arg) { }

[Test]
public void Access_by_ValueSource([ValueSource(nameof(TestCases))] int arg) { }
}
}
41 changes: 22 additions & 19 deletions src/NUnitEngine/mock-assembly/MockAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand All @@ -33,7 +33,7 @@ namespace Assemblies
/// MockAssembly is intended for those few tests that can only
/// be made to work by loading an entire assembly. Please don't
/// add any other entries or use it for other purposes.
///
///
/// Most tests used as data for NUnit's own tests should be
/// in the testdata assembly.
/// </summary>
Expand All @@ -56,7 +56,8 @@ public class MockAssembly
+ BadFixture.Tests
+ FixtureWithTestCases.Tests
+ ParameterizedFixture.Tests
+ GenericFixtureConstants.Tests;
+ GenericFixtureConstants.Tests
+ AccessesCurrentTestContextDuringDiscovery.Tests;

public const int Suites = MockTestFixture.Suites
+ Singletons.OneTestCase.Suites
Expand All @@ -67,6 +68,7 @@ public class MockAssembly
+ FixtureWithTestCases.Suites
+ ParameterizedFixture.Suites
+ GenericFixtureConstants.Suites
+ AccessesCurrentTestContextDuringDiscovery.Suites
+ NamespaceSuites;

public const int TestStartedEvents = Tests - IgnoredFixture.Tests - BadFixture.Tests - ExplicitFixture.Tests;
Expand All @@ -75,7 +77,7 @@ public class MockAssembly
public const int TestOutputEvents = 1;

public const int Nodes = Tests + Suites;

public const int ExplicitFixtures = 1;
public const int SuitesRun = Suites - ExplicitFixtures;

Expand All @@ -84,7 +86,8 @@ public class MockAssembly
+ TestAssembly.MockTestFixture.Tests
+ FixtureWithTestCases.Tests
+ ParameterizedFixture.Tests
+ GenericFixtureConstants.Tests;
+ GenericFixtureConstants.Tests
+ AccessesCurrentTestContextDuringDiscovery.Tests;

public const int Skipped_Ignored = MockTestFixture.Skipped_Ignored + IgnoredFixture.Tests;
public const int Skipped_Explicit = MockTestFixture.Skipped_Explicit + ExplicitFixture.Tests;
Expand Down Expand Up @@ -181,10 +184,10 @@ namespace Singletons
public class OneTestCase
{
public const int Tests = 1;
public const int Suites = 1;
public const int Suites = 1;

[Test]
public virtual void TestCase()
public virtual void TestCase()
{}
}
}
Expand Down Expand Up @@ -215,7 +218,7 @@ public class IgnoredFixture

[Test]
public void Test2() { }

[Test]
public void Test3() { }
}
Expand Down Expand Up @@ -245,27 +248,27 @@ public class BadFixture
[Test]
public void SomeTest() { }
}

[TestFixture]
public class FixtureWithTestCases
{
public const int Tests = 4;
public const int Suites = 3;

[TestCase(2, 2, ExpectedResult=4)]
[TestCase(9, 11, ExpectedResult=20)]
public int MethodWithParameters(int x, int y)
{
return x+y;
}

[TestCase(2, 4)]
[TestCase(9.2, 11.7)]
public void GenericMethod<T>(T x, T y)
{
}
}

[TestFixture(5)]
[TestFixture(42)]
public class ParameterizedFixture
Expand All @@ -274,29 +277,29 @@ public class ParameterizedFixture
public const int Suites = 3;

public ParameterizedFixture(int num) { }

[Test]
public void Test1() { }

[Test]
public void Test2() { }
}

public class GenericFixtureConstants
{
public const int Tests = 4;
public const int Suites = 3;
}

[TestFixture(5)]
[TestFixture(11.5)]
public class GenericFixture<T>
{
public GenericFixture(T num){ }

[Test]
public void Test1() { }

[Test]
public void Test2() { }
}
Expand Down
1 change: 1 addition & 0 deletions src/NUnitEngine/mock-assembly/mock-assembly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AccessesCurrentTestContextDuringDiscovery.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
21 changes: 21 additions & 0 deletions src/NUnitEngine/nunit.engine.tests/Helpers/On.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Threading;

namespace NUnit.Engine.Tests.Helpers
{
public static class On
{
public static IDisposable Dispose(Action action) => new OnDisposeAction(action);
private sealed class OnDisposeAction : IDisposable
{
private Action action;

public OnDisposeAction(Action action)
{
this.action = action;
}

public void Dispose() => Interlocked.Exchange(ref action, null)?.Invoke();
}
}
}
116 changes: 116 additions & 0 deletions src/NUnitEngine/nunit.engine.tests/Helpers/ProcessUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace NUnit.Engine.Tests.Helpers
{
public static class ProcessUtils
{
public static ProcessResult Run(ProcessStartInfo startInfo)
{
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;

using (var process = new Process { StartInfo = startInfo })
{
var standardStreamData = new List<StandardStreamData>();
var currentData = new StringBuilder();
var currentDataIsError = false;

process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null) return;
if (currentDataIsError)
{
if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));
currentData = new StringBuilder();
currentDataIsError = false;
}
currentData.AppendLine(e.Data);
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null) return;
if (!currentDataIsError)
{
if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));
currentData = new StringBuilder();
currentDataIsError = true;
}
currentData.AppendLine(e.Data);
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();

if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));

return new ProcessResult(process.ExitCode, standardStreamData.ToArray());
}
}

[DebuggerDisplay("{ToString(),nq}")]
public struct ProcessResult
{
public ProcessResult(int exitCode, StandardStreamData[] standardStreamData)
{
ExitCode = exitCode;
StandardStreamData = standardStreamData;
}

public int ExitCode { get; }
public StandardStreamData[] StandardStreamData { get; }

public override string ToString() => ToString(true);

/// <param name="showStreamSource">If true, appends "[stdout] " or "[stderr] " to the beginning of each line.</param>
public string ToString(bool showStreamSource)
{
var r = new StringBuilder("Exit code ").Append(ExitCode);

if (StandardStreamData.Length != 0) r.AppendLine();

foreach (var data in StandardStreamData)
{
if (showStreamSource)
{
var lines = data.Data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

// StandardStreamData.Data always ends with a blank line, so skip that
for (var i = 0; i < lines.Length - 1; i++)
r.Append(data.IsError ? "[stderr] " : "[stdout] ").AppendLine(lines[i]);
}
else
{
r.Append(data.Data);
}
}

return r.ToString();
}
}

[DebuggerDisplay("{ToString(),nq}")]
public struct StandardStreamData
{
public StandardStreamData(bool isError, string data)
{
IsError = isError;
Data = data;
}

public bool IsError { get; }
public string Data { get; }

public override string ToString() => (IsError ? "[stderr] " : "[stdout] ") + Data;
}
}
}
36 changes: 36 additions & 0 deletions src/NUnitEngine/nunit.engine.tests/Helpers/ShadowCopyUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace NUnit.Engine.Tests.Helpers
{
public static class ShadowCopyUtils
{
/// <summary>
/// Returns the transitive closure of assemblies needed to copy.
/// Deals with assembly names rather than paths to work with runners that shadow copy.
/// </summary>
public static ICollection<string> GetAllNeededAssemblyPaths(params string[] assemblyNames)
{
var r = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

var dependencies = StackEnumerator.Create(
from assemblyName in assemblyNames
select new AssemblyName(assemblyName));

foreach (var dependencyName in dependencies)
{
var dependency = Assembly.ReflectionOnlyLoad(dependencyName.FullName);

if (!dependency.GlobalAssemblyCache && r.Add(Path.GetFullPath(dependency.Location)))
{
dependencies.Recurse(dependency.GetReferencedAssemblies());
}
}

return r;
}
}
}
Loading

0 comments on commit 9240f62

Please sign in to comment.