Skip to content
Open
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
2 changes: 1 addition & 1 deletion ExcelDna.Testing/Examples/InProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void AsyncFunctionTest()
Assert.Equal("Completed", targetRange.Value);
}

[ExcelFact(Workbook = "", AddIn = @"..\..\..\..\ExampleAddinNET6\bin\Debug\net6.0-windows\ExampleAddinNET6-AddIn")]
[ExcelFact(Workbook = "", AddIn = @"..\..\..\..\ExampleAddinNET6\bin\Debug\net6.0-windows\ExampleAddinNET6-AddIn", SafeMode = true)]
public void FunctionTestNET6()
{
Range targetRange = (ExcelDna.Testing.Util.Workbook.Sheets[1] as Worksheet).Range["A1"];
Expand Down
3 changes: 3 additions & 0 deletions ExcelDna.Testing/ExcelDna.Testing/ExcelFactAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public class ExcelFactAttribute : FactAttribute, ITestSettings

/// <inheritdoc />
public string AddIn { get; set; }

/// <inheritdoc />
public bool SafeMode { get; set; }
}
}
3 changes: 2 additions & 1 deletion ExcelDna.Testing/ExcelDna.Testing/ExcelFactDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ private static ExcelTestSettings GetSettings(ITestMethod testMethod)
return new ExcelTestSettings(
GetSetting<bool>(testMethod, nameof(ExcelFactAttribute.OutOfProcess)),
GetSetting<string>(testMethod, nameof(ExcelFactAttribute.Workbook)),
GetSetting<string>(testMethod, nameof(ExcelFactAttribute.AddIn)));
GetSetting<string>(testMethod, nameof(ExcelFactAttribute.AddIn)),
GetSetting<bool>(testMethod, nameof(ExcelFactAttribute.SafeMode)));
}

private static T GetSetting<T>(ITestMethod testMethod, string name)
Expand Down
8 changes: 7 additions & 1 deletion ExcelDna.Testing/ExcelDna.Testing/ExcelRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ public ExcelRunner()
excelDetected = excelDetector.TryFindLatestExcel(out excelExePath) && excelDetector.TryFindExcelBitness(excelExePath, out bitness);
}

public Process Start(string addinAssemblyPath, IEnumerable<string> addins)
public Process Start(string addinAssemblyPath, IEnumerable<string> addins, bool safeMode = false)
{
if (!excelDetected)
throw new ApplicationException("Can't find an installed version of Excel.");

string addinAssemblyDirectory = Path.GetDirectoryName(addinAssemblyPath);
string arguments = "";

if (safeMode)
{
arguments += "/safe ";
}

foreach (string externalAddinRelativePath in addins)
{
arguments += Quote(GetXllPath(addinAssemblyDirectory, externalAddinRelativePath, bitness)) + " ";
Expand Down
11 changes: 7 additions & 4 deletions ExcelDna.Testing/ExcelDna.Testing/ExcelTestAssemblyRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ public ExcelTestAssemblyRunner(ITestAssembly testAssembly, IEnumerable<IXunitTes
protected override async Task<RunSummary> RunTestCollectionsAsync(IMessageBus messageBus, CancellationTokenSource cancellationTokenSource)
{
IEnumerable<IXunitTestCase> localTestCases = TestCases.Except(TestCases.OfType<ExcelTestCase>());
IEnumerable<ExcelTestCase> excelInProcessTestCases = TestCases.OfType<ExcelTestCase>().Where(i => !i.Settings.OutOfProcess);
IEnumerable<ExcelTestCase> excelInProcessTestCases = TestCases.OfType<ExcelTestCase>().Where(i => !i.Settings.OutOfProcess && !i.Settings.SafeMode);
IEnumerable<ExcelTestCase> excelInProcessSafeTestCases = TestCases.OfType<ExcelTestCase>().Where(i => !i.Settings.OutOfProcess && i.Settings.SafeMode);
IEnumerable<ExcelTestCase> excelOutOfProcessTestCases = TestCases.OfType<ExcelTestCase>().Where(i => i.Settings.OutOfProcess);

var result = await LocalRunTestCasesAsync(localTestCases, messageBus, cancellationTokenSource);
if (excelOutOfProcessTestCases.Count() > 0)
result.Aggregate(await COMRunTestCasesAsync(excelOutOfProcessTestCases, messageBus, cancellationTokenSource));
if (excelInProcessTestCases.Count() > 0)
result.Aggregate(await RemoteRunTestCasesAsync(excelInProcessTestCases, messageBus, cancellationTokenSource));
result.Aggregate(await RemoteRunTestCasesAsync(excelInProcessTestCases, messageBus, cancellationTokenSource, false));
if (excelInProcessSafeTestCases.Count() > 0)
result.Aggregate(await RemoteRunTestCasesAsync(excelInProcessSafeTestCases, messageBus, cancellationTokenSource, true));

CleanupReferences();

Expand All @@ -58,13 +61,13 @@ private async Task<RunSummary> LocalRunTestCasesAsync(IEnumerable<IXunitTestCase
return result;
}

private async Task<RunSummary> RemoteRunTestCasesAsync(IEnumerable<ExcelTestCase> testCases, IMessageBus messageBus, CancellationTokenSource cancellationTokenSource)
private async Task<RunSummary> RemoteRunTestCasesAsync(IEnumerable<ExcelTestCase> testCases, IMessageBus messageBus, CancellationTokenSource cancellationTokenSource, bool safeMode)
{
RunSummary result = new RunSummary();
try
{
ExcelStartupEvent.Create();
Process excelProcess = excelRunner.Start(testAssembly.Assembly.AssemblyPath, GetAddins(testCases));
Process excelProcess = excelRunner.Start(testAssembly.Assembly.AssemblyPath, GetAddins(testCases), safeMode);
if (!ExcelStartupEvent.Wait(30000))
throw new System.ApplicationException("Excel startup failed.");

Expand Down
4 changes: 3 additions & 1 deletion ExcelDna.Testing/ExcelDna.Testing/ExcelTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public override void Serialize(IXunitSerializationInfo info)
info.AddValue(nameof(testSettings.OutOfProcess), testSettings.OutOfProcess);
info.AddValue(nameof(testSettings.Workbook), testSettings.Workbook);
info.AddValue(nameof(testSettings.AddIn), testSettings.AddIn);
info.AddValue(nameof(testSettings.SafeMode), testSettings.SafeMode);
}

public override void Deserialize(IXunitSerializationInfo info)
Expand All @@ -43,7 +44,8 @@ public override void Deserialize(IXunitSerializationInfo info)
testSettings = new ExcelTestSettings(
info.GetValue<bool>(nameof(testSettings.OutOfProcess)),
info.GetValue<string>(nameof(testSettings.Workbook)),
info.GetValue<string>(nameof(testSettings.AddIn)));
info.GetValue<string>(nameof(testSettings.AddIn)),
info.GetValue<bool>(nameof(testSettings.SafeMode)));
}

public string SerializeToString()
Expand Down
5 changes: 4 additions & 1 deletion ExcelDna.Testing/ExcelDna.Testing/ExcelTestSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
{
public class ExcelTestSettings : ITestSettings
{
public ExcelTestSettings(bool outOfProcess, string workbook, string addin)
public ExcelTestSettings(bool outOfProcess, string workbook, string addin, bool safeMode = false)
{
OutOfProcess = outOfProcess;
Workbook = workbook;
AddIn = addin;
SafeMode = safeMode;
}

public bool OutOfProcess { get; set; }

public string Workbook { get; set; }

public string AddIn { get; set; }

public bool SafeMode { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public class ExcelTestSettingsAttribute : Attribute, ITestSettings

/// <inheritdoc />
public string AddIn { get; set; }

/// <inheritdoc />
public bool SafeMode { get; set; }
}
}
5 changes: 5 additions & 0 deletions ExcelDna.Testing/ExcelDna.Testing/ITestSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ internal interface ITestSettings
/// A relative path to .xll add-in to load. Without bitness and .xll extension.
/// </summary>
string AddIn { get; set; }

/// <summary>
/// Whether to start Excel in safe mode using the /safe command-line switch.
/// </summary>
bool SafeMode { get; set; }
}
}