Skip to content

Commit

Permalink
AutomationSandbox API Enhancements (#6)
Browse files Browse the repository at this point in the history
* AutomationSandbox API changes

* Upgrade sample with new PowerShell Tools APIs
  • Loading branch information
kenswan committed Nov 3, 2023
1 parent 010b16a commit 9ea6fe8
Show file tree
Hide file tree
Showing 46 changed files with 827 additions and 355 deletions.
10 changes: 5 additions & 5 deletions samples/BlazorFocused.Automation.Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RuntimeSample", "RuntimeSam
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Configuration", "_Configuration", "{29F3FEAB-CFED-467D-8953-8FBD56707CFF}"
ProjectSection(SolutionItems) = preProject
Directory.Build.targets = Directory.Build.targets
Directory.Packages.props = Directory.Packages.props
Directory.Build.props = Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4FF3865D-3154-49E9-8B12-1A5A92FC4742}"
Expand All @@ -35,6 +35,10 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{949472D6-391D-4D86-A709-79FC56D89210}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{949472D6-391D-4D86-A709-79FC56D89210}.Debug|Any CPU.Build.0 = Debug|Any CPU
{949472D6-391D-4D86-A709-79FC56D89210}.Release|Any CPU.ActiveCfg = Release|Any CPU
{949472D6-391D-4D86-A709-79FC56D89210}.Release|Any CPU.Build.0 = Release|Any CPU
{9D7EBFE3-2BB1-4692-828D-EFC5443FB676}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D7EBFE3-2BB1-4692-828D-EFC5443FB676}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D7EBFE3-2BB1-4692-828D-EFC5443FB676}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -67,10 +71,6 @@ Global
{4B2B49BD-445C-472E-93F6-FCFAC301EE44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B2B49BD-445C-472E-93F6-FCFAC301EE44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B2B49BD-445C-472E-93F6-FCFAC301EE44}.Release|Any CPU.Build.0 = Release|Any CPU
{949472D6-391D-4D86-A709-79FC56D89210}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{949472D6-391D-4D86-A709-79FC56D89210}.Debug|Any CPU.Build.0 = Debug|Any CPU
{949472D6-391D-4D86-A709-79FC56D89210}.Release|Any CPU.ActiveCfg = Release|Any CPU
{949472D6-391D-4D86-A709-79FC56D89210}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion samples/CommandLineSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License
// -------------------------------------------------------

using CommandLineSample;
using BlazorFocused.Automation.CommandLine;
using CommandLineSample;

IAutomationConsoleBuilder builder =
AutomationConsole.CreateDefaultBuilder("Sample CommandLine Example")
Expand Down
2 changes: 1 addition & 1 deletion samples/CommandLineSample/ReportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

using AutomationSamples.Shared.Models;
using AutomationSamples.Shared.Services;
using BlazorFocused.Automation.CommandLine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.CommandLine;
using BlazorFocused.Automation.CommandLine;

namespace CommandLineSample;

Expand Down
23 changes: 23 additions & 0 deletions samples/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>

<PropertyGroup Label="Documentation Settings">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!--
Make sure any documentation comments which are included in code get checked for syntax during the build, but do
not report warnings for missing comments.
CS1591: Missing XML comment for publicly visible type or member 'Type_or_Member'
-->
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<PropertyGroup Label="Build Settings">
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Label="Compile Settings">
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable>
</PropertyGroup>

</Project>
20 changes: 0 additions & 20 deletions samples/Directory.Build.targets

This file was deleted.

2 changes: 1 addition & 1 deletion samples/PowerShellSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

using AutomationSamples.Shared.Models;
using AutomationSamples.Shared.Services;
using BlazorFocused.Automation.Runtime;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorFocused.Automation.Runtime;

namespace PowerShellSample;

Expand Down
1 change: 1 addition & 0 deletions samples/PowerShellToolsSample/PowerShellToolsSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
48 changes: 44 additions & 4 deletions samples/PowerShellToolsSample/WriteReportCmdletTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// Licensed under the MIT License
// -------------------------------------------------------

using AutomationSamples.Shared.Services;
using BlazorFocused.Automation.PowerShell.Tools;
using BlazorFocused.Automation.Runtime;
using Bogus;
using Moq;
using PowerShellSample;
using System.Management.Automation;

Expand All @@ -14,12 +18,48 @@ public class WriteReportCmdletTests
[Fact]
public void RunCommand_ShouldGenerateReportDetails()
{
using IAutomationCommand<WriteReportCmdlet> context =
AutomationSandbox.CreateContext<WriteReportCmdlet, Startup>((services) =>
{ });
using IPowerShellAutomation<Startup> powerShellAutomation = AutomationSandbox.CreateSession<Startup>();

ICollection<PSObject> results = context.RunCommand();
ICollection<PSObject> results = powerShellAutomation.RunAutomationCommand<WriteReportCmdlet>();

Assert.Equal(3, results.Count);
}

[Fact]
public void RunCommand_ShouldPrint_Header_Data_Disclaimer()
{
string inputHeader = new Faker().Lorem.Sentence();
string expectedHeader = new Faker().Lorem.Sentence();
string expectedData = new Faker().Random.AlphaNumeric(20);
string expectedDisclaimer = new Faker().Lorem.Sentence();

var reportServiceMock = new Mock<IReportService>();

reportServiceMock.Setup(service => service.GenerateReportHeader(inputHeader))
.Returns(expectedHeader);

reportServiceMock.Setup(service => service.GenerateReportData())
.Returns(expectedData);

reportServiceMock.Setup(service => service.GenerateReportDisclaimer())
.Returns(expectedDisclaimer);

using IPowerShellAutomation<Startup> powerShellAutomation =
AutomationSandbox.CreateSession<Startup>(services =>
// Replace the registered service with the mock
services.ReplaceRegisteredService<IReportService>((_) => reportServiceMock.Object));

ICollection<string> actualResults =
powerShellAutomation.RunAutomationCommand<WriteReportCmdlet, string>(command =>
{
command.AddParameter("Header", inputHeader);
});

Assert.Equal(3, actualResults.Count);

Assert.Collection(actualResults,
actualHeader => Assert.Equal(expectedHeader, actualHeader),
actualData => Assert.Equal(expectedData, actualData),
actualDisclaimer => Assert.Equal(expectedDisclaimer, actualDisclaimer));
}
}
2 changes: 1 addition & 1 deletion samples/RuntimeSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// Licensed under the MIT License
// -------------------------------------------------------

using BlazorFocused.Automation.Runtime;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RuntimeSample.Attributes;
using RuntimeSample.Services;
using RuntimeSample.Session;
using BlazorFocused.Automation.Runtime;

namespace RuntimeSample;

Expand Down
2 changes: 1 addition & 1 deletion samples/RuntimeSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

using AutomationSamples.Shared.Models;
using AutomationSamples.Shared.Services;
using BlazorFocused.Automation.Runtime;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RuntimeSample.Services;
using BlazorFocused.Automation.Runtime;

namespace RuntimeSample;

Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/src/AutomationConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License
// -------------------------------------------------------

using System.CommandLine;
using BlazorFocused.Automation.CommandLine.Builder;
using System.CommandLine;

namespace BlazorFocused.Automation.CommandLine;

Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/src/Binder/ServiceBinderActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Licensed under the MIT License
// -------------------------------------------------------

using BlazorFocused.Automation.Runtime;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorFocused.Automation.Runtime;

namespace BlazorFocused.Automation.CommandLine.Binder;

Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/src/Builder/AutomationConsoleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License
// -------------------------------------------------------

using System.CommandLine;
using BlazorFocused.Automation.CommandLine.Application;
using System.CommandLine;

namespace BlazorFocused.Automation.CommandLine.Builder;

Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/src/Builder/ConsoleCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License
// -------------------------------------------------------

using System.CommandLine;
using BlazorFocused.Automation.CommandLine.Binder;
using System.CommandLine;

namespace BlazorFocused.Automation.CommandLine.Builder;

Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/src/IConsoleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License
// -------------------------------------------------------

using System.CommandLine;
using BlazorFocused.Automation.Runtime;
using System.CommandLine;

namespace BlazorFocused.Automation.CommandLine;

Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/src/StandardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License
// -------------------------------------------------------

using System.CommandLine;
using BlazorFocused.Automation.Runtime;
using System.CommandLine;

namespace BlazorFocused.Automation.CommandLine;

Expand Down
12 changes: 10 additions & 2 deletions src/PowerShell/PowerShell/src/AutomationShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ protected override void BeginProcessing()

base.BeginProcessing();

AutomationRuntime.BindServicesByAttribute<AutomationDependencyAttribute, TStartup>(
new AutomationSessionStateProxy(SessionState), this);
ISessionStorage sessionStorage = new AutomationSessionStateProxy(SessionState);

if (AutomationRuntime.HasRegisteredServiceProvider<TStartup>(sessionStorage, out IServiceProvider serviceProvider))
{
AutomationRuntime.BindServicesByAttribute<AutomationDependencyAttribute>(serviceProvider, this);
}
else
{
AutomationRuntime.BindServicesByAttribute<AutomationDependencyAttribute, TStartup>(sessionStorage, this);
}

WriteVerbose($"{CommandName} Context Initialized");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License
// -------------------------------------------------------

using System.Management.Automation;
using BlazorFocused.Automation.Runtime;
using System.Management.Automation;
using Runspace = System.Management.Automation.Runspaces;

namespace BlazorFocused.Automation.PowerShell.Session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// -------------------------------------------------------

using BlazorFocused.Automation.PowerShell.Test.TestBed.Commands;
using BlazorFocused.Automation.PowerShell.Test.TestBed.Startup;
using BlazorFocused.Automation.PowerShell.Tools;

namespace BlazorFocused.Automation.PowerShell.Test;
Expand All @@ -15,10 +16,9 @@ public void ShouldAddDependencies()
{
int expectedValue = 3;

using IAutomationCommand<TestPSCmdletCommand> context =
AutomationSandbox.CreateCommand<TestPSCmdletCommand>();
using IPowerShellAutomation<TestStartup> context = AutomationSandbox.CreateSession<TestStartup>();

int actualValue = context.RunCommand<int>().FirstOrDefault();
int actualValue = context.RunAutomationCommand<TestAutomationShellCommand, int>().FirstOrDefault();

Assert.Equal(expectedValue, actualValue);
}
Expand Down
12 changes: 6 additions & 6 deletions src/PowerShell/PowerShell/test/AutomationShellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// Licensed under the MIT License
// -------------------------------------------------------

using Microsoft.Extensions.DependencyInjection;
using Moq;
using System.Management.Automation;
using BlazorFocused.Automation.PowerShell.Test.TestBed.Services;
using BlazorFocused.Automation.PowerShell.Test.TestBed.Startup;
using BlazorFocused.Automation.PowerShell.Tools;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System.Management.Automation;

namespace BlazorFocused.Automation.PowerShell.Test;

Expand All @@ -19,14 +19,14 @@ public void ShouldLoadDependency()
{
var testRuntimeServiceMock = new Mock<ITestRuntimeService>();

using IAutomationCommand<TestIoCShell> context =
AutomationSandbox.CreateContext<TestIoCShell, TestStartup>(
using IPowerShellAutomation<TestStartup> context =
AutomationSandbox.CreateSession<TestStartup>(
serviceCollection =>
{
serviceCollection.AddTransient(_ => testRuntimeServiceMock.Object);
});

context.RunCommand();
context.RunAutomationCommand<TestIoCShell>();

testRuntimeServiceMock.Verify(service => service.RunMethod(), Times.Exactly(3));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
// Licensed under the MIT License
// -------------------------------------------------------

using System.Management.Automation;
using BlazorFocused.Automation.PowerShell.Test.Services;
using BlazorFocused.Automation.PowerShell.Test.TestBed.Startup;
using System.Management.Automation;

namespace BlazorFocused.Automation.PowerShell.Test.TestBed.Commands;

[Cmdlet(VerbsCommon.Get, "TestSDKCommand")]
public class TestPSCmdletCommand : AutomationShell<TestStartup>
public class TestAutomationShellCommand : AutomationShell<TestStartup>
{
[AutomationDependency]
protected readonly ITestService testService;
Expand Down

0 comments on commit 9ea6fe8

Please sign in to comment.