Fix tests and fix exception throwing

This commit is contained in:
Andreia Gaita 2019-07-01 21:03:32 +02:00
Родитель a9cabd1da7
Коммит bb7250bbe9
13 изменённых файлов: 62 добавлений и 25 удалений

Просмотреть файл

@ -1,9 +1,11 @@
using System;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
namespace Unity.Git
{
static class ExceptionExtensions
public static class ExceptionExtensions
{
/// <summary>
/// Represents exceptions we should never attempt to catch and ignore.
@ -50,5 +52,32 @@ namespace Unity.Git
return !exception.IsCriticalException()
&& !(exception is ObjectDisposedException);
}
public static void Rethrow(this Exception exception)
{
#if NET35
SaveStackTraceForThrowing(exception);
throw exception;
#else
ExceptionDispatchInfo.Capture(exception).Throw();
#endif
}
private static Action<Exception> saveStackTraceForThrowing;
private static Action<Exception> SaveStackTraceForThrowing {
get {
if (saveStackTraceForThrowing == null)
{
// in mono, FixRemotingException saves the original stacktrace
// in .net < 4.0, InternalPreserveStackTrace saves it
// but .net also has a FixRemotingException method, so try InternalPreserveStackTrace first
var method = typeof(Exception).GetMethod( "InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic );
if (method == null) // maybe it's mono
typeof(Exception).GetMethod( "FixRemotingException", BindingFlags.Instance | BindingFlags.NonPublic );
saveStackTraceForThrowing = (Action<Exception>)Delegate.CreateDelegate(typeof(Action<Exception>), method);
}
return saveStackTraceForThrowing;
}
}
}
}

Просмотреть файл

@ -36,7 +36,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
return ret;
}

Просмотреть файл

@ -229,7 +229,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
Token.ThrowIfCancellationRequested();
}
}
@ -273,7 +273,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
Token.ThrowIfCancellationRequested();
}
return ret;
@ -324,7 +324,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
}
}
@ -408,7 +408,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
}
@ -462,7 +462,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
return result;
}
@ -507,7 +507,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
return result;
}
@ -562,7 +562,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
finally
{
@ -610,7 +610,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
return result;
}

Просмотреть файл

@ -20,7 +20,7 @@ namespace Unity.Git
return e.Response;
}
throw e;
throw;
}
}
}
@ -59,7 +59,7 @@ namespace Unity.Git
catch (Exception ex)
{
if (!RaiseFaultHandlers(ex))
throw;
ThrownException.Rethrow();
}
return result;
}

Просмотреть файл

@ -193,6 +193,7 @@ namespace Unity.Git
sb.AppendLine($"'{Process.StartInfo.FileName} {Process.StartInfo.Arguments}'");
if (errorCode == 2)
sb.AppendLine("The system cannot find the file specified.");
sb.AppendLine($"Working directory: {Process.StartInfo.WorkingDirectory}");
foreach (string env in Process.StartInfo.EnvironmentVariables.Keys)
{
sb.AppendFormat("{0}:{1}", env, Process.StartInfo.EnvironmentVariables[env]);
@ -375,7 +376,7 @@ namespace Unity.Git
}
if (thrownException != null && !RaiseFaultHandlers(thrownException))
throw thrownException;
ThrownException.Rethrow();
},
(ex, error) =>
{
@ -502,7 +503,7 @@ namespace Unity.Git
}
if (thrownException != null && !RaiseFaultHandlers(thrownException))
throw thrownException;
ThrownException.Rethrow();
},
(ex, error) =>
{
@ -546,7 +547,7 @@ namespace Unity.Git
this.arguments = arguments;
}
public override string ProcessName => fullPathToExecutable?.FileName;
public override string ProcessName => fullPathToExecutable;
public override string ProcessArguments => arguments;
}

Просмотреть файл

@ -1,6 +1,7 @@
using Unity.Git;
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
@ -238,7 +239,7 @@ namespace Unity.Git
{
actionToContinueWith(s, ex);
if (!s)
throw ex;
ex.Rethrow();
})
{ Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways)
.CatchInternal(_ => true);
@ -641,7 +642,7 @@ namespace Unity.Git
{
continuation(s, ex, res);
if (!s)
throw ex;
ex.Rethrow();
})
{ Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways)
.CatchInternal(_ => true);

Просмотреть файл

@ -210,7 +210,7 @@ namespace Unity.Git
leftRight[1].IsInitialized ? leftRight[1].MakeAbsolute().ToString() : null,
null, null);
else
throw ex;
ex.Rethrow();
})
.Start();
});

Просмотреть файл

@ -7,6 +7,9 @@
<RootNamespace>Test.CommandLine</RootNamespace>
<AssemblyName>CommandLine</AssemblyName>
<OutputPath>$(SolutionDir)\build\tests\</OutputPath>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>
<Import Project="$(SolutionDir)\common\properties.props" />
<ItemGroup>

Просмотреть файл

@ -60,7 +60,8 @@ namespace IntegrationTests.Download
var task = await Task.WhenAny(downloader.Start().Task, Task.Delay(Timeout));
StopTrackTimeAndLog(watch, logger);
Assert.AreEqual(downloader.Task, task);
Assert.Throws(typeof(DownloadException), async () => await downloader.Task);
Func<Task> act = async () => await downloader.Task;
await act.Should().ThrowAsync<DownloadException>();
}
[Test]

Просмотреть файл

@ -30,7 +30,7 @@ namespace IntegrationTests
UserCachePath = environmentPath.Value.Combine("User");
SystemCachePath = environmentPath.Value.Combine("System");
var installPath = solutionDirectory.Parent.Parent.Combine("src", "Git.Api");
var installPath = solutionDirectory.Parent.Parent.Parent.Combine("src", "api", "Api");
Initialize(UnityVersion, installPath, solutionDirectory, NPath.Default, repoPath.Combine("Assets"));

Просмотреть файл

@ -7,7 +7,7 @@ namespace IntegrationTests
[SetUpFixture]
public class SetUpFixture
{
[SetUp]
[OneTimeSetUp]
public void Setup()
{
LogHelper.TracingEnabled = true;

Просмотреть файл

@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\src\api\Api\Api.csproj" />
<ProjectReference Include="..\CommandLine\CommandLine.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs" />

Просмотреть файл

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using System.Threading.Tasks.Schedulers;
using FluentAssertions;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NUnit.Framework;
using Unity.Git;
@ -333,7 +334,7 @@ namespace IntegrationTests
}
[Test]
public async void NonUITasksAlwaysRunOnDifferentThreadFromUITasks()
public async Task NonUITasksAlwaysRunOnDifferentThreadFromUITasks()
{
var output = new Dictionary<int, int>();
var tasks = new List<ITask>();
@ -355,7 +356,7 @@ namespace IntegrationTests
[Test]
public async void ChainingOnDifferentSchedulers()
public async Task ChainingOnDifferentSchedulers()
{
var output = new Dictionary<int, KeyValuePair<int, int>>();
var tasks = new List<ITask>();
@ -1007,10 +1008,10 @@ namespace IntegrationTests
public void FailingTasksThrowCorrectlyEvenIfFinallyIsPresent()
{
var queue = new TaskQueue();
var task = new ActionTask(Token, () => { throw new Exception(); })
var task = new ActionTask(Token, () => throw new InvalidOperationException())
.Finally((s, e) => { });
queue.Queue(task);
Assert.Throws<Exception>(() => queue.RunSynchronously());
Assert.Throws<InvalidOperationException>(() => queue.RunSynchronously());
}
[Test]