Skip to content

Commit bfa8bb0

Browse files
add unit tests
1 parent eeded0d commit bfa8bb0

File tree

6 files changed

+100
-16
lines changed

6 files changed

+100
-16
lines changed

src/PowerShell/PowerShellManager.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal class PowerShellManager
2929

3030
// The path to the FunctionApp root. This is set at the first FunctionLoad message
3131
//and used for determining the path to the 'Profile.ps1' and 'Modules' folder.
32-
internal string FunctionAppRootLocation { get; set; }
32+
internal string FunctionAppRootLocation { get; set; } = AppDomain.CurrentDomain.BaseDirectory;
3333

3434
internal PowerShellManager(ILogger logger)
3535
{
@@ -55,6 +55,16 @@ internal PowerShellManager(ILogger logger)
5555
_pwsh.Streams.Warning.DataAdding += streamHandler.WarningDataAdding;
5656
}
5757

58+
internal void InitializeRunspace()
59+
{
60+
// Add HttpResponseContext namespace so users can reference
61+
// HttpResponseContext without needing to specify the full namespace
62+
_pwsh.AddScript($"using namespace {typeof(HttpResponseContext).Namespace}").InvokeAndClearCommands();
63+
64+
// Set the PSModulePath
65+
Environment.SetEnvironmentVariable("PSModulePath", Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules"));
66+
}
67+
5868
internal void InvokeProfile()
5969
{
6070
IEnumerable<string> profiles = Directory.EnumerateFiles(FunctionAppRootLocation, PROFILE_FILENAME);
@@ -65,13 +75,27 @@ internal void InvokeProfile()
6575
}
6676

6777
var dotSourced = new StringBuilder(". ").Append(QuoteEscapeString(profiles.First()));
68-
_pwsh.AddScript(dotSourced.ToString()).InvokeAndClearCommands();
78+
79+
try
80+
{
81+
_pwsh.AddScript(dotSourced.ToString()).InvokeAndClearCommands();
82+
}
83+
catch (RuntimeException e)
84+
{
85+
_logger.Log(
86+
LogLevel.Error,
87+
$"Invoking the Profile had errors. See logs for details. Profile location: {FunctionAppRootLocation}",
88+
e,
89+
isUserLog: true);
90+
throw;
91+
}
6992

7093
if (_pwsh.HadErrors)
7194
{
72-
var logMessage = $"Invoking the Profile had errors. See logs for details. Profile location: {FunctionAppRootLocation}";
73-
_logger.Log(LogLevel.Error, logMessage, isUserLog: true);
74-
throw new InvalidOperationException(logMessage);
95+
_logger.Log(
96+
LogLevel.Error,
97+
$"Invoking the Profile had errors. See logs for details. Profile location: {FunctionAppRootLocation}",
98+
isUserLog: true);
7599
}
76100
}
77101

@@ -105,16 +129,6 @@ private static StringBuilder QuoteEscapeString(string path)
105129
return sb;
106130
}
107131

108-
internal void InitializeRunspace()
109-
{
110-
// Add HttpResponseContext namespace so users can reference
111-
// HttpResponseContext without needing to specify the full namespace
112-
_pwsh.AddScript($"using namespace {typeof(HttpResponseContext).Namespace}").InvokeAndClearCommands();
113-
114-
// Set the PSModulePath
115-
Environment.SetEnvironmentVariable("PSModulePath", Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules"));
116-
}
117-
118132
/// <summary>
119133
/// Execution a function fired by a trigger or an activity function scheduled by an orchestration.
120134
/// </summary>

test/Microsoft.Azure.Functions.PowerShellWorker.Test.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@
3131
<Content Include="Unit\PowerShell\TestScripts\testFunctionCleanup.ps1">
3232
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3333
</Content>
34+
<Content Include="Unit\PowerShell\TestScripts\ProfileBasic\Profile.ps1">
35+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
36+
</Content>
37+
<Content Include="Unit\PowerShell\TestScripts\ProfileWithTerminatingError\Profile.ps1">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</Content>
40+
<Content Include="Unit\PowerShell\TestScripts\ProfileWithNonTerminatingError\Profile.ps1">
41+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
42+
</Content>
3443
</ItemGroup>
3544
</Project>

test/Unit/PowerShell/PowerShellManagerTests.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System;
77
using System.Collections;
88
using System.Collections.Generic;
9-
9+
using System.Management.Automation;
1010
using Microsoft.Azure.Functions.PowerShellWorker.PowerShell;
1111
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
1212
using Xunit;
@@ -159,5 +159,63 @@ public void RegisterAndUnregisterFunctionMetadataShouldWork()
159159
manager.UnregisterFunctionMetadata();
160160
Assert.Empty(FunctionMetadata.OutputBindingCache);
161161
}
162+
163+
[Fact]
164+
public void ProfileShouldWork()
165+
{
166+
var logger = new ConsoleLogger();
167+
var manager = new PowerShellManager(logger);
168+
manager.FunctionAppRootLocation = System.IO.Path.Join(
169+
AppDomain.CurrentDomain.BaseDirectory,
170+
"Unit/PowerShell/TestScripts/ProfileBasic");
171+
172+
manager.InvokeProfile();
173+
174+
Assert.Single(logger.FullLog);
175+
Assert.Equal("Information: INFORMATION: Hello PROFILE", logger.FullLog[0]);
176+
}
177+
178+
[Fact]
179+
public void ProfileDoesNotExist()
180+
{
181+
var logger = new ConsoleLogger();
182+
var manager = new PowerShellManager(logger);
183+
manager.FunctionAppRootLocation = AppDomain.CurrentDomain.BaseDirectory;
184+
185+
manager.InvokeProfile();
186+
187+
Assert.Single(logger.FullLog);
188+
Assert.Matches("Trace: No 'Profile.ps1' found at: ", logger.FullLog[0]);
189+
}
190+
191+
[Fact]
192+
public void ProfileWithTerminatingError()
193+
{
194+
var logger = new ConsoleLogger();
195+
var manager = new PowerShellManager(logger);
196+
manager.FunctionAppRootLocation = System.IO.Path.Join(
197+
AppDomain.CurrentDomain.BaseDirectory,
198+
"Unit/PowerShell/TestScripts/ProfileWithTerminatingError");
199+
200+
Assert.Throws(typeof(RuntimeException), () => manager.InvokeProfile());
201+
Assert.Single(logger.FullLog);
202+
Assert.Matches("Error: Invoking the Profile had errors. See logs for details. Profile location: ", logger.FullLog[0]);
203+
}
204+
205+
[Fact]
206+
public void ProfileWithNonTerminatingError()
207+
{
208+
var logger = new ConsoleLogger();
209+
var manager = new PowerShellManager(logger);
210+
manager.FunctionAppRootLocation = System.IO.Path.Join(
211+
AppDomain.CurrentDomain.BaseDirectory,
212+
"Unit/PowerShell/TestScripts/ProfileWithNonTerminatingError");
213+
214+
manager.InvokeProfile();
215+
216+
Assert.Equal(2, logger.FullLog.Count);
217+
Assert.Equal("Error: ERROR: help me!", logger.FullLog[0]);
218+
Assert.Matches("Error: Invoking the Profile had errors. See logs for details. Profile location: ", logger.FullLog[1]);
219+
}
162220
}
163221
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Host "Hello PROFILE"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Error "help me!"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw "help me!"

0 commit comments

Comments
 (0)