Skip to content

Commit

Permalink
Import Pester separately
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleejordan committed Jun 30, 2022
1 parent a1aa699 commit d483736
Showing 1 changed file with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -144,7 +144,8 @@ private string GenerateScriptFromLoggingStatements(params string[] logStatements

private static async Task<string[]> GetLog()
{
while (!File.Exists(s_testOutputPath))
using CancellationTokenSource cts = new(30000);
while (!File.Exists(s_testOutputPath) && !cts.Token.IsCancellationRequested)
{
await Task.Delay(1000).ConfigureAwait(true);
}
Expand Down Expand Up @@ -294,25 +295,40 @@ public async Task CanLaunchScriptWithCommentedLastLineAsync()
[Fact]
public async Task CanRunPesterTestFile()
{
string logStatement = GenerateScriptFromLoggingStatements("pester");
string filePath = NewTestFile(@"
Install-Module -Name Pester -Scope CurrentUser -Force | Import-Module -Force
string pesterTest = NewTestFile(@"
Describe 'A' {
Context 'B' {
It 'C' {
" + logStatement + @"
{ throw 'error' } | Should -Throw
}
It 'D' {
" + GenerateScriptFromLoggingStatements("pester") + @"
}
}
}
");
}", isPester: true);

await PsesDebugAdapterClient.LaunchScript(filePath, Started).ConfigureAwait(true);
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
Assert.NotNull(configDoneResponse);
string pesterLog = Path.Combine(s_binDir, Path.GetRandomFileName() + ".log");

Assert.Collection(await GetLog().ConfigureAwait(true),
(i) => Assert.Equal("pester", i));
string testCommand = @"
Start-Transcript -Path '" + pesterLog + @"'
Install-Module -Name Pester -RequiredVersion 5.3.3 -Force -PassThru | Write-Host
Import-Module -Name Pester -RequiredVersion 5.3.3 -PassThru | Write-Host
Invoke-Pester -Script '" + pesterTest + @"'
Stop-Transcript";

string testFile = NewTestFile(testCommand);
await PsesDebugAdapterClient.LaunchScript(testFile, Started).ConfigureAwait(true);
await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
await Task.Delay(10000).ConfigureAwait(true);

using CancellationTokenSource cts = new(5000);
while (!File.Exists(pesterLog) && !cts.Token.IsCancellationRequested)
{
await Task.Delay(1000).ConfigureAwait(true);
}
_output.WriteLine(File.ReadAllText(pesterLog));

Assert.Collection(await GetLog().ConfigureAwait(true), (i) => Assert.Equal("pester", i));
}
}
}

0 comments on commit d483736

Please sign in to comment.