Skip to content

Commit

Permalink
Fixes #1131 (#1132)
Browse files Browse the repository at this point in the history
* Fixes #1131

* Test fails on builder.

* Try fix test again.
  • Loading branch information
adamdriscoll authored Sep 18, 2019
1 parent c684214 commit d601962
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}
}
}

Pop-Location
7 changes: 6 additions & 1 deletion src/UniversalDashboard/Cmdlets/NewDashboardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/UniversalDashboard/Cmdlets/StartRestApiCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Security.Cryptography.X509Certificates;
using System.Management.Automation.Runspaces;
using System.Linq;
using UniversalDashboard.Services;

namespace UniversalDashboard.Cmdlets
{
Expand Down Expand Up @@ -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; }
Expand All @@ -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.");
Expand Down
2 changes: 1 addition & 1 deletion src/UniversalDashboard/Server/AutoReloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down
23 changes: 23 additions & 0 deletions src/UniversalDashboard/Services/UDRunspaceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +74,28 @@ public UDRunspaceFactory(IDashboardService dashboardService, InitialSessionState
_runspacePool = new ObjectPool<Runspace>(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<PSModuleInfo>().Select(m => m.Path).ToArray();
initialSessionState.ImportPSModule(modulePaths);

var commands = sessionState.InvokeCommand.InvokeScript("Get-ChildItem -Path Function:\\").Select(m => m.BaseObject).Cast<FunctionInfo>();
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<PSVariable>();
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);
Expand Down

0 comments on commit d601962

Please sign in to comment.