From d601962bcd1e830972edb14faa508de76fb24810 Mon Sep 17 00:00:00 2001 From: Adam Driscoll Date: Wed, 18 Sep 2019 09:08:21 +0530 Subject: [PATCH] Fixes #1131 (#1132) * Fixes #1131 * Test fails on builder. * Try fix test again. --- .../EndpointInitialization.Tests.ps1 | 117 +++++++++++++----- .../Cmdlets/NewDashboardCommand.cs | 7 +- .../Cmdlets/StartRestApiCommand.cs | 8 +- src/UniversalDashboard/Server/AutoReloader.cs | 2 +- .../Services/UDRunspaceFactory.cs | 23 ++++ 5 files changed, 125 insertions(+), 32 deletions(-) diff --git a/src/UniversalDashboard.UITest/Integration/EndpointInitialization.Tests.ps1 b/src/UniversalDashboard.UITest/Integration/EndpointInitialization.Tests.ps1 index 373949c9..6c49f378 100644 --- a/src/UniversalDashboard.UITest/Integration/EndpointInitialization.Tests.ps1 +++ b/src/UniversalDashboard.UITest/Integration/EndpointInitialization.Tests.ps1 @@ -2,19 +2,21 @@ param([Switch]$Release) Import-Module "$PSScriptRoot\..\TestFramework.psm1" -Force $ModulePath = Get-ModulePath -Release:$Release -$BrowserPort = Get-BrowserPort -Release:$Release - Import-Module $ModulePath -Force +Push-Location $PSScriptRoot + Get-UDDashboard | Stop-UDDashboard Describe "EndpointInitialization" { - Context "Variables" { + + Context "Autoload" { + function Get-Stuff { 999 } - $SomeVar = "This is a value" + $SomeVar123 = "This is a value" $tempModule = [IO.Path]::GetTempFileName() + ".psm1" @@ -24,55 +26,112 @@ Describe "EndpointInitialization" { } }.ToString() | Out-File -FilePath $tempModule - $Initialization = New-UDEndpointInitialization -Module @($tempModule, ".\TestModule.psm1") -Variable "SomeVar" -Function 'Get-Stuff' + Import-Module $tempModule + Import-Module ".\TestModule.psm1" - $dashboard = New-UDDashboard -Title "Test" -Content { - New-UDCounter -Title "Counter" -Id "Counter" -Endpoint { + $dashboard = New-UDDashboard -Title "Test" -Content {} + $Server = Start-UDDashboard -Port 10001 -Dashboard $dashboard -Endpoint @( + New-UDEndpoint -Url '/module1_a' -Endpoint { Get-Number } - - New-UDElement -tag "div" -Endpoint { - New-UDElement -tag "div" -Id "variable" -Content { $SomeVar } + New-UDEndpoint -Url "/variable_a" -Endpoint { + $SomeVar123 } - - New-UDElement -tag "div" -Id "function" -Endpoint { + New-UDEndpoint -Url "/function_a" -Endpoint { Get-Stuff } - - New-UDElement -tag "div" -Id "othermodule" -Endpoint { + New-UDEndpoint -Url "/module2_a" -Endpoint { Get-TheMeaningOfLife } + ) + + It "should load module from temp dir" { + Invoke-RestMethod "http://localhost:10001/api/module1_a" | Should be "10" + } + + It "should have variable defined" { + Invoke-RestMethod "http://localhost:10001/api/variable_a" | Should be "This is a value" + } + + It "should have function defined" { + Invoke-RestMethod "http://localhost:10001/api/function_a" | Should be "999" + } + + It "should load module from relative path" { + Invoke-RestMethod "http://localhost:10001/api/module2_a" | Should be "42" + } + + Remove-Item $tempModule -Force + Stop-UDDashboard -Server $Server - } -EndpointInitialization $Initialization - $Server = Start-UDDashboard -Port 10001 -Dashboard $dashboard - $Driver = Start-SeFirefox - Enter-SeUrl -Driver $Driver -Url "http://localhost:$BrowserPort" + } + + Context "Variables" { + + function Get-Stuff { + 999 + } + + function Get-NoStuff { + 101 + } + + $SomeVar123 = "This is a value" + + $tempModule = [IO.Path]::GetTempFileName() + ".psm1" + + { + function Get-Number { + 10 + } + }.ToString() | Out-File -FilePath $tempModule - Start-Sleep 3 + $Initialization = New-UDEndpointInitialization -Module @($tempModule, ".\TestModule.psm1") -Variable "SomeVar123" -Function 'Get-Stuff' + + $dashboard = New-UDDashboard -Title "Test" -Content {} -EndpointInitialization $Initialization + + $Server = Start-UDDashboard -Port 10001 -Dashboard $dashboard -Endpoint @( + New-UDEndpoint -Url '/module1_b' -Endpoint { + Get-Number + } + New-UDEndpoint -Url "/variable_b" -Endpoint { + $SomeVar123 + } + New-UDEndpoint -Url "/function_b" -Endpoint { + Get-Stuff + } + New-UDEndpoint -Url "/module2_b" -Endpoint { + Get-TheMeaningOfLife + } + New-UDEndpoint -Url "/notfound_b" -Endpoint { + Get-NoStuff + } + ) It "should load module from temp dir" { - $Target = Find-SeElement -Driver $Driver -Id "Counter" - $Target.Text | Should be "Counter`r`n10" + Invoke-RestMethod "http://localhost:10001/api/module1_b" | Should be "10" } It "should have variable defined" { - $Target = Find-SeElement -Driver $Driver -Id "variable" - $Target.Text | Should be "This is a value" + Invoke-RestMethod "http://localhost:10001/api/variable_b" | Should be "This is a value" } It "should have function defined" { - $Target = Find-SeElement -Driver $Driver -Id "function" - $Target.Text | Should be "999" + Invoke-RestMethod "http://localhost:10001/api/function_b" | Should be "999" } It "should load module from relative path" { - $Target = Find-SeElement -Driver $Driver -Id "othermodule" - $Target.Text | Should be "42" + Invoke-RestMethod "http://localhost:10001/api/module2_b" | Should be "42" + } + + It "should not load function" { + Invoke-RestMethod "http://localhost:10001/api/notfound_b" | Should not be 101 } Remove-Item $tempModule -Force - Stop-SeDriver $Driver Stop-UDDashboard -Server $Server } -} \ No newline at end of file +} + +Pop-Location \ No newline at end of file diff --git a/src/UniversalDashboard/Cmdlets/NewDashboardCommand.cs b/src/UniversalDashboard/Cmdlets/NewDashboardCommand.cs index ad69441f..16d2fac1 100644 --- a/src/UniversalDashboard/Cmdlets/NewDashboardCommand.cs +++ b/src/UniversalDashboard/Cmdlets/NewDashboardCommand.cs @@ -60,7 +60,7 @@ public class NewDashboardCommand : PSCmdlet public Element NavBarLogo {get;set;} [Parameter] - public InitialSessionState EndpointInitialization { get; set; } = InitialSessionState.CreateDefault2(); + public InitialSessionState EndpointInitialization { get; set; } [Parameter] public Theme Theme { get; set; } @@ -78,6 +78,11 @@ public class NewDashboardCommand : PSCmdlet protected override void EndProcessing() { + if (EndpointInitialization == null) + { + EndpointInitialization = UDRunspaceFactory.GenerateInitialSessionState(SessionState); + } + var dashboard = new Dashboard(); dashboard.Title = Title; dashboard.NavBarColor = NavBarColor?.HtmlColor; diff --git a/src/UniversalDashboard/Cmdlets/StartRestApiCommand.cs b/src/UniversalDashboard/Cmdlets/StartRestApiCommand.cs index d9c2c235..2e0dbd0f 100644 --- a/src/UniversalDashboard/Cmdlets/StartRestApiCommand.cs +++ b/src/UniversalDashboard/Cmdlets/StartRestApiCommand.cs @@ -6,6 +6,7 @@ using System.Security.Cryptography.X509Certificates; using System.Management.Automation.Runspaces; using System.Linq; +using UniversalDashboard.Services; namespace UniversalDashboard.Cmdlets { @@ -36,7 +37,7 @@ public class StartRestApiCommand : PSCmdlet public SecureString CertificateFilePassword { get; set; } [Parameter] - public InitialSessionState EndpointInitialization { get; set; } = InitialSessionState.CreateDefault2(); + public InitialSessionState EndpointInitialization { get; set; } [Parameter] public SwitchParameter AutoReload { get; set; } @@ -51,6 +52,11 @@ protected override void EndProcessing() { Log.Info($"{Name} - {MyInvocation.ScriptName} - {AutoReload}"); + if (EndpointInitialization == null) + { + EndpointInitialization = UDRunspaceFactory.GenerateInitialSessionState(SessionState); + } + if (string.IsNullOrEmpty(MyInvocation.ScriptName) && AutoReload) { WriteWarning("AutoReload does not work on the command line. You must save your file as a script."); diff --git a/src/UniversalDashboard/Server/AutoReloader.cs b/src/UniversalDashboard/Server/AutoReloader.cs index 59be9f66..74bd68c9 100644 --- a/src/UniversalDashboard/Server/AutoReloader.cs +++ b/src/UniversalDashboard/Server/AutoReloader.cs @@ -68,7 +68,7 @@ public void OnFileChanged(object source, FileSystemEventArgs e) { var assemblyBasePath = Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location); - var tempPath = Path.Combine(assemblyBasePath, "..\\UniversalDashboard.psd1"); + var tempPath = Path.Combine(assemblyBasePath, "..", Constants.ModuleManifest); var initialSessionState = InitialSessionState.CreateDefault(); initialSessionState.ImportPSModule(new[] { tempPath }); diff --git a/src/UniversalDashboard/Services/UDRunspaceFactory.cs b/src/UniversalDashboard/Services/UDRunspaceFactory.cs index 8eda337a..b9fe844c 100644 --- a/src/UniversalDashboard/Services/UDRunspaceFactory.cs +++ b/src/UniversalDashboard/Services/UDRunspaceFactory.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Management.Automation; using System.Management.Automation.Host; using System.Management.Automation.Runspaces; @@ -73,6 +74,28 @@ public UDRunspaceFactory(IDashboardService dashboardService, InitialSessionState _runspacePool = new ObjectPool(CreateRunspace); } + public static InitialSessionState GenerateInitialSessionState(System.Management.Automation.SessionState sessionState) + { + var initialSessionState = InitialSessionState.CreateDefault2(); + + var modulePaths = sessionState.InvokeCommand.InvokeScript("Get-Module").Select(m => m.BaseObject).Cast().Select(m => m.Path).ToArray(); + initialSessionState.ImportPSModule(modulePaths); + + var commands = sessionState.InvokeCommand.InvokeScript("Get-ChildItem -Path Function:\\").Select(m => m.BaseObject).Cast(); + foreach (var command in commands) + { + initialSessionState.Commands.Add(new SessionStateFunctionEntry(command.Name, command.Definition)); + } + + var variables = sessionState.InvokeCommand.InvokeScript("Get-Variable").Select(m => m.BaseObject).Cast(); + foreach (var variable in variables) + { + initialSessionState.Variables.Add(new SessionStateVariableEntry(variable.Name, variable.Value, variable.Description)); + } + + return initialSessionState; + } + private Runspace CreateRunspace() { var assemblyBasePath = Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location); var tempPath = Path.Combine(assemblyBasePath, "..", Constants.ModuleManifest);