Skip to content

Commit

Permalink
review & fix mono compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Jul 2, 2010
1 parent da7abf1 commit 988ff7c
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 107 deletions.
28 changes: 0 additions & 28 deletions Reporting/ReflectionHelper.cs

This file was deleted.

13 changes: 13 additions & 0 deletions Reporting/ReflectionHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Reflection;

namespace TechTalk.SpecFlow.Reporting
{
static class ReflectionHelperExtensions
{
public static T GetProperty<T>(this object source, string propertyName)
{
return (T)source.GetType().GetProperty(propertyName).GetValue(source, null);
}
}
}
3 changes: 3 additions & 0 deletions Reporting/ResourceXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace TechTalk.SpecFlow.Reporting
{
/// <summary>
/// XML reader for an embedded resource that uses the resource://assembly/resource base URI.
/// </summary>
internal class ResourceXmlReader : XmlTextReader
{
private readonly string baseUri;
Expand Down
8 changes: 7 additions & 1 deletion Reporting/TechTalk.SpecFlow.Reporting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Runtime\Compatibility\ExceptionHelper.cs">
<Link>Compatibility\ExceptionHelper.cs</Link>
</Compile>
<Compile Include="..\Runtime\Compatibility\MonoHelper.cs">
<Link>Compatibility\MonoHelper.cs</Link>
</Compile>
<Compile Include="..\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
Expand All @@ -63,7 +69,7 @@
<Compile Include="ParserHelper.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionHelper.cs" />
<Compile Include="ReflectionHelperExtensions.cs" />
<Compile Include="StepDefinitionReport\ReportElements\Binding.cs" />
<Compile Include="StepDefinitionReport\ReportElements\FeatureRef.cs" />
<Compile Include="StepDefinitionReport\ReportElements\Instance.cs" />
Expand Down
38 changes: 10 additions & 28 deletions Reporting/XmlResourceResolver.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Xml;
using TechTalk.SpecFlow.Compatibility;

namespace TechTalk.SpecFlow.Reporting
{
Expand All @@ -14,38 +15,19 @@ public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToRe
return base.GetEntity(absoluteUri, role, ofObjectToReturn);

string resourceName = absoluteUri.AbsolutePath.TrimStart(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Type.Delimiter);

Assembly assembly = GetAssemblyFor(absoluteUri.Host);

if (ofObjectToReturn != null)
{
if (ofObjectToReturn == typeof(ResourceXmlReader) || ofObjectToReturn == typeof(XmlTextReader))
{
return new ResourceXmlReader(assembly, resourceName);
}
else if (ofObjectToReturn == typeof(Stream))
{
return assembly.GetManifestResourceStream(resourceName);
}
}

Debug.Assert(ofObjectToReturn == null || ofObjectToReturn == typeof(Stream));
return assembly.GetManifestResourceStream(resourceName);
}

private static Assembly GetAssemblyFor(string host)
private static Assembly GetAssemblyFor(string assemblyName)
{
Assembly locatedAssembly = null;

foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
AssemblyName assemblyName = assembly.GetName();

if (!assemblyName.Name.Equals(host, StringComparison.CurrentCultureIgnoreCase))
continue;

locatedAssembly = assembly;
}

return locatedAssembly;
if (MonoHelper.IsMono)
//TODO: find out why Assembly.Load does not work on Mono
return MonoHelper.GetLoadedAssembly(assemblyName);

return Assembly.Load(assemblyName);
}
}
}
3 changes: 1 addition & 2 deletions Reporting/XsltHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public static void TransformHtml(XmlSerializer serializer, object report, Type r
argumentList.AddParam("feature-language", "", generatorConfiguration.FeatureLanguage.Name);
argumentList.AddParam("tool-language", "", generatorConfiguration.ToolLanguage.Name);

using (var outFileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
using (var xmlTextWriter = new XmlTextWriter(outFileStream, Encoding.UTF8))
using (var xmlTextWriter = new XmlTextWriter(outputFilePath, Encoding.UTF8))
{
xslt.Transform(xmlOutputReader, argumentList, xmlTextWriter, resourceResolver);
}
Expand Down
12 changes: 12 additions & 0 deletions Runtime.Silverlight/Compatibility/ExceptionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace TechTalk.SpecFlow.Compatibility
{
internal static class ExceptionHelper
{
public static void PreserveStackTrace(this Exception ex)
{
//do nothing
}
}
}
1 change: 1 addition & 0 deletions Runtime.Silverlight/TechTalk.SpecFlow.Silverlight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<Compile Include="Compatibility\ConfigurationErrorsException.cs" />
<Compile Include="Compatibility\CultureInfoHelper.cs" />
<Compile Include="Compatibility\EnumHelper.cs" />
<Compile Include="Compatibility\ExceptionHelper.cs" />
<Compile Include="Compatibility\Stopwatch.cs" />
<Compile Include="Configuration\ConfigDefaults.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
17 changes: 1 addition & 16 deletions Runtime/Bindings/StepMethodBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public object InvokeAction(object[] arguments, ITestTracer testTracer, out TimeS
catch (TargetInvocationException invEx)
{
var ex = invEx.InnerException;
PreserveStackTrace(ex);
ex.PreserveStackTrace();
throw ex;
}
}
Expand All @@ -160,21 +160,6 @@ private CultureInfoScope CreateCultureInfoScope()
return new CultureInfoScope(cultureInfo);
}

internal void PreserveStackTrace(Exception ex)
{
Type exceptionType = typeof(Exception);

// Mono's implementation of System.Exception doesn't contain the method InternalPreserveStackTrace
if (Type.GetType("Mono.Runtime") != null)
{
exceptionType.GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ex, ex.StackTrace + Environment.NewLine);
}
else
{
exceptionType.GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(ex, new object[0]);
}
}

#region extended action types
static readonly Type[] actionTypes = new Type[] { typeof(Action), typeof(Action<>), typeof(Action<,>), typeof(Action<,,>), typeof(Action<,,,>),
typeof(ExtendedAction<,,,,>), typeof(ExtendedAction<,,,,,>), typeof(ExtendedAction<,,,,,,>), typeof(ExtendedAction<,,,,,,,>), typeof(ExtendedAction<,,,,,,,,>), typeof(ExtendedAction<,,,,,,,,,>)
Expand Down
21 changes: 21 additions & 0 deletions Runtime/Compatibility/ExceptionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace TechTalk.SpecFlow.Compatibility
{
internal static class ExceptionHelper
{
public static void PreserveStackTrace(this Exception ex)
{
// Mono's implementation of System.Exception doesn't contain the method InternalPreserveStackTrace
if (MonoHelper.IsMono)
MonoHelper.PreserveStackTrace(ex);
else
typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(ex, new object[0]);
}

}
}
40 changes: 40 additions & 0 deletions Runtime/Compatibility/MonoHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace TechTalk.SpecFlow.Compatibility
{
internal class MonoHelper
{
public static bool IsMono { get; private set; }

static MonoHelper()
{
IsMono = Type.GetType("Mono.Runtime") != null;
}

public static void PreserveStackTrace(Exception ex)
{
typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ex, ex.StackTrace + Environment.NewLine);
}

public static Assembly GetLoadedAssembly(string assemblyName)
{
Assembly locatedAssembly = null;

foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
AssemblyName loadedAssemblyName = assembly.GetName();

if (!loadedAssemblyName.Name.Equals(assemblyName, StringComparison.CurrentCultureIgnoreCase))
continue;

locatedAssembly = assembly;
}

return locatedAssembly;
}
}
}
2 changes: 2 additions & 0 deletions Runtime/TechTalk.SpecFlow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
<Compile Include="Bindings\StepMethodBinding.cs" />
<Compile Include="Compatibility\CultureInfoHelper.cs" />
<Compile Include="Compatibility\EnumHelper.cs" />
<Compile Include="Compatibility\ExceptionHelper.cs" />
<Compile Include="Compatibility\MonoHelper.cs" />
<Compile Include="Configuration\ConfigDefaults.cs" />
<Compile Include="Configuration\ConfigurationServices.cs" />
<Compile Include="Configuration\MissingOrPendingStepsOutcome.cs" />
Expand Down
18 changes: 2 additions & 16 deletions Runtime/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Text.RegularExpressions;
using TechTalk.SpecFlow.Bindings;
using TechTalk.SpecFlow.Compatibility;
using TechTalk.SpecFlow.Configuration;
using TechTalk.SpecFlow.ErrorHandling;
using TechTalk.SpecFlow.Tracing;
Expand Down Expand Up @@ -146,7 +147,7 @@ public void CollectScenarioErrors()
if (ObjectContainer.ScenarioContext.TestError == null)
throw new InvalidOperationException("test failed with an unknown error");

PreserveStackTrace(ObjectContainer.ScenarioContext.TestError);
ObjectContainer.ScenarioContext.TestError.PreserveStackTrace();
throw ObjectContainer.ScenarioContext.TestError;
}

Expand Down Expand Up @@ -397,21 +398,6 @@ private List<BindingMatch> GetMatchesWithoutParamCheck(StepArgs stepArgs)
return matches;
}

internal void PreserveStackTrace(Exception ex)
{
Type exceptionType = typeof(Exception);

// Mono's implementation of System.Exception doesn't contain the method InternalPreserveStackTrace
if (Type.GetType("Mono.Runtime") != null)
{
exceptionType.GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ex, ex.StackTrace + Environment.NewLine);
}
else
{
exceptionType.GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(ex, new object[0]);
}
}

private TimeSpan ExecuteStepMatch(BindingMatch match, object[] arguments)
{
OnStepStart();
Expand Down
18 changes: 2 additions & 16 deletions Tests/RuntimeTests/NUnitTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using TechTalk.SpecFlow.Compatibility;

namespace TechTalk.SpecFlow.RuntimeTests
{
Expand Down Expand Up @@ -48,26 +49,11 @@ private static void InvokeMethod(object test, MethodInfo testMethod)
catch (TargetInvocationException invEx)
{
var ex = invEx.InnerException;
PreserveStackTrace(ex);
ex.PreserveStackTrace();
throw ex;
}
}

internal static void PreserveStackTrace(Exception ex)
{
Type exceptionType = typeof(Exception);

// Mono's implementation of System.Exception doesn't contain the method InternalPreserveStackTrace
if (Type.GetType("Mono.Runtime") != null)
{
exceptionType.GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ex, ex.StackTrace + Environment.NewLine);
}
else
{
exceptionType.GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(ex, new object[0]);
}
}

private static void ExecuteWithAttribute(object test, Type attributeType)
{
foreach (var methodInfo in GetMethodsWithAttribute(test, attributeType))
Expand Down

0 comments on commit 988ff7c

Please sign in to comment.