Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Handle error looking for init.ps1 in NuGet packages directory.
Browse files Browse the repository at this point in the history
If there is an exception finding init.ps1 files when the solution is
opened then the unhandled exception dialog is shown. This can happen
if a NuGet package requires NuGet 3.0. This results in an
NuGetVersionNotSatisfiedException being thrown. Now when this happens
the error message is reported in the PowerShell console if it is open.
  • Loading branch information
mrward committed Jan 23, 2016
1 parent 9c3fd24 commit 336b105
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Expand Up @@ -17,12 +17,13 @@
// DEALINGS IN THE SOFTWARE.

using System;
using ICSharpCode.Scripting;

namespace ICSharpCode.PackageManagement.Scripting
{
public class PackageInitializationScriptsConsole
{
IPackageManagementConsoleHost consoleHost;
readonly IPackageManagementConsoleHost consoleHost;

public PackageInitializationScriptsConsole(
IPackageManagementConsoleHost consoleHost)
Expand All @@ -36,5 +37,13 @@ public void ExecuteCommand(string command)
consoleHost.ExecuteCommand(command);
}
}

public void WriteError(string message)
{
if (consoleHost.IsRunning) {
consoleHost.ScriptingConsole.WriteLine(message, ScriptingStyle.Error);
consoleHost.WritePrompt();
}
}
}
}
Expand Up @@ -47,7 +47,11 @@ public class RunPackageInitializationScriptsOnSolutionOpen

void SolutionOpened(object sender, SolutionEventArgs e)
{
RunPackageInitializationScripts(e.Solution);
try {
RunPackageInitializationScripts(e.Solution);
} catch (Exception ex) {
scriptsConsole.WriteError(ex.Message);
}
}

void RunPackageInitializationScripts(ISolution solution)
Expand Down
Expand Up @@ -28,10 +28,15 @@ public class FakePackageInitializationScriptsFactory : IPackageInitializationScr
new FakePackageInitializationScripts();

public ISolution SolutionPassedToCreatePackageInitializationScripts;

public Exception ExceptionToThrow;

public IPackageInitializationScripts CreatePackageInitializationScripts(
ISolution solution)
{
if (ExceptionToThrow != null)
throw ExceptionToThrow;

SolutionPassedToCreatePackageInitializationScripts = solution;
return FakePackageInitializationScripts;
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
using System;
using ICSharpCode.PackageManagement.Design;
using ICSharpCode.PackageManagement.Scripting;
using ICSharpCode.Scripting;
using ICSharpCode.Scripting.Tests.Utils;
using ICSharpCode.SharpDevelop.Project;
using NUnit.Framework;
Expand Down Expand Up @@ -102,5 +103,33 @@ public void Instance_SolutionOpenedAndHasNoPackageInitializationScripts_InvokeIn

Assert.IsFalse(contains);
}

[Test]
public void Instance_ExceptionThrownByScriptsFactory_ErrorMessageLoggedToConsole()
{
CreateRunner();
SolutionHasPackageInitializationScripts();
fakeConsoleHost.IsRunning = true;
fakeScriptsFactory.ExceptionToThrow = new Exception("Error");

OpenSolution();

Assert.AreEqual("Error", fakeScriptingConsole.TextPassedToWriteLine);
Assert.AreEqual(ScriptingStyle.Error, fakeScriptingConsole.ScriptingStylePassedToWriteLine);
Assert.IsTrue(fakeConsoleHost.IsWritePromptCalled);
}

[Test]
public void Instance_ExceptionThrownByScriptsFactoryButConsoleIsNotRunning_NoErrorMessageLoggedToConsole()
{
CreateRunner();
SolutionHasPackageInitializationScripts();
fakeConsoleHost.IsRunning = false;
fakeScriptsFactory.ExceptionToThrow = new Exception("Error");

OpenSolution();

Assert.IsNull(fakeScriptingConsole.TextPassedToWriteLine);
}
}
}

0 comments on commit 336b105

Please sign in to comment.