Skip to content
Merged
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
19 changes: 19 additions & 0 deletions src/ScriptCs.Core/Exceptions/ScriptPackException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Runtime.Serialization;

namespace ScriptCs.Exceptions
{
[Serializable]
public class ScriptPackException : Exception
{
public ScriptPackException(string message)
: base(message)
{
}

protected ScriptPackException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
1 change: 1 addition & 0 deletions src/ScriptCs.Core/ScriptCs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="AssemblyInfo.cs" />
<Compile Include="AssemblyResolver.cs" />
<Compile Include="AssemblyUtility.cs" />
<Compile Include="Exceptions\ScriptPackException.cs" />
<Compile Include="Extensions\EnumerableExtensions.cs" />
<Compile Include="Extensions\MethodInfoExtensions.cs" />
<Compile Include="FileSystemMigrator.cs" />
Expand Down
9 changes: 8 additions & 1 deletion src/ScriptCs.Core/ScriptPackManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using ScriptCs.Contracts;
using ScriptCs.Exceptions;

namespace ScriptCs
{
Expand All @@ -20,7 +21,13 @@ public ScriptPackManager(IEnumerable<IScriptPackContext> contexts)

public TContext Get<TContext>() where TContext : IScriptPackContext
{
return (TContext)_contexts[typeof(TContext)];
var key = typeof(TContext);
if (!_contexts.ContainsKey(key))
{
throw new ScriptPackException(string.Format("Tried to resolve a script pack '{0}', but such script pack is not available in the current execution context.", key));
}

return (TContext)_contexts[key];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
</None>
<None Include="xunit.runner.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ScriptCs.Core\ScriptCs.Core.csproj">
<Project>{E590E710-E159-48E6-A3E6-1A83D3FE732C}</Project>
<Name>ScriptCs.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="..\..\build\ScriptCs.Common.props" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
27 changes: 27 additions & 0 deletions test/ScriptCs.Tests.Acceptance/ScriptPacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using ScriptCs.Tests.Acceptance.Support;
using Should;
using Xbehave;
using System;
using Should.Core.Assertions;
using ScriptCs.Exceptions;

public static class ScriptPacks
{
Expand All @@ -25,5 +28,29 @@ public static void UsingAScriptPack(ScenarioDirectory directory, string output)
"Then I see 6912"
.x(() => output.ShouldContain("6912"));
}

[Scenario]
public static void UsingAnUnavailableScriptPack(ScenarioDirectory directory, string output, Exception exception)
{
var scenario = MethodBase.GetCurrentMethod().GetFullName();

"Given a script which uses a non-existing script pack 'Foo'"
.x(() => directory = ScenarioDirectory.Create(scenario)
.WriteLine("foo.csx", @"var fooContext = Require<Foo>();
class Foo : ScriptCs.Contracts.IScriptPackContext{}
Console.WriteLine(""hi"");"));

"When I execute the script"
.x(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", directory)));

"Then scriptcs fails"
.x(() => exception.ShouldBeType<ScriptCsException>());

"With a script pack exception"
.x(() =>
{
exception.Message.ShouldContain("Tried to resolve a script pack 'Submission#0+Foo', but such script pack is not available in the current execution context.");
});
}
}
}