Skip to content

Commit 73d63ee

Browse files
author
Kapil Borle
committed
Use IDisposable pattern for ModuleDependencyHandler
1 parent ccdb0e0 commit 73d63ee

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

Engine/Generic/ModuleDependencyHandler.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -238,30 +238,20 @@ private void RestorePSModulePath()
238238
/// <param name="tempPath">Path to the user scoped temporary directory</param>
239239
/// <param name="localAppDataPath">Path to the local app data directory</param>
240240
public ModuleDependencyHandler(
241-
Runspace runspace = null,
241+
Runspace runspace,
242242
string moduleRepository = null,
243243
string tempPath = null,
244244
string localAppDataPath = null)
245245
{
246-
if (runspace == null)
247-
{
248-
Runspace = RunspaceFactory.CreateRunspace();
249-
}
250-
else
251-
{
252-
Runspace = runspace;
253-
}
254-
255-
if (Runspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen)
256-
{
257-
Runspace.Open();
258-
}
259-
else if (Runspace.RunspaceStateInfo.State != RunspaceState.Opened)
246+
247+
ThrowIfNull(runspace, "runspace");
248+
if (runspace.RunspaceStateInfo.State != RunspaceState.Opened)
260249
{
261250
throw new ArgumentException(string.Format(
262-
"Runspace state cannot be {0}",
251+
"Runspace state cannot be in {0} state. It must be in Opened state",
263252
runspace.RunspaceStateInfo.State.ToString()));
264253
}
254+
Runspace = runspace;
265255

266256
// TODO should set PSSA environment variables outside this class
267257
// Should be set in ScriptAnalyzer class
@@ -416,7 +406,6 @@ public static string GetModuleNameFromErrorExtent(ParseError error, ScriptBlockA
416406
/// </summary>
417407
public void Dispose()
418408
{
419-
runspace.Dispose();
420409
RestorePSModulePath();
421410
}
422411

Engine/ScriptAnalyzer.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public sealed class ScriptAnalyzer
4545
List<Regex> includeRegexList;
4646
List<Regex> excludeRegexList;
4747
bool suppressedOnly;
48-
Runspace runspace;
49-
ModuleDependencyHandler moduleHandler;
50-
5148
#endregion
5249

5350
#region Singleton
@@ -170,10 +167,6 @@ public void CleanUp()
170167
includeRegexList = null;
171168
excludeRegexList = null;
172169
suppressedOnly = false;
173-
if (moduleHandler != null)
174-
{
175-
moduleHandler.Dispose();
176-
}
177170
}
178171

179172
internal bool ParseProfile(object profileObject, PathIntrinsics path, IOutputWriter writer)
@@ -486,10 +479,6 @@ private void Initialize(
486479

487480
this.outputWriter = outputWriter;
488481

489-
// TODO Create a runspace pool
490-
runspace = RunspaceFactory.CreateRunspace();
491-
moduleHandler = resolveDSCResourceDependency ? new ModuleDependencyHandler(runspace) : null;
492-
493482
#region Verifies rule extensions and loggers path
494483

495484
List<string> paths = this.GetValidCustomRulePaths(customizedRulePath, path);
@@ -1176,16 +1165,22 @@ public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursi
11761165
// is an optimization over doing the whole operation at once
11771166
// and calling .Concat on IEnumerables to join results.
11781167
this.BuildScriptPathList(path, searchRecursively, scriptFilePaths);
1179-
1180-
foreach (string scriptFilePath in scriptFilePaths)
1168+
using (var rsp = RunspaceFactory.CreateRunspace())
11811169
{
1182-
// Yield each record in the result so that the
1183-
// caller can pull them one at a time
1184-
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
1170+
rsp.Open();
1171+
using (var moduleHandler = new ModuleDependencyHandler(rsp))
11851172
{
1186-
yield return diagnosticRecord;
1173+
foreach (string scriptFilePath in scriptFilePaths)
1174+
{
1175+
// Yield each record in the result so that the
1176+
// caller can pull them one at a time
1177+
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath, moduleHandler))
1178+
{
1179+
yield return diagnosticRecord;
1180+
}
1181+
}
11871182
}
1188-
}
1183+
} // disposing the runspace also closes it if it not already closed
11891184
}
11901185

11911186
/// <summary>
@@ -1287,7 +1282,9 @@ private void BuildScriptPathList(
12871282
}
12881283

12891284

1290-
private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
1285+
private IEnumerable<DiagnosticRecord> AnalyzeFile(
1286+
string filePath,
1287+
ModuleDependencyHandler moduleHandler = null)
12911288
{
12921289
ScriptBlockAst scriptAst = null;
12931290
Token[] scriptTokens = null;

Tests/Engine/ModuleDependencyHandler.tests.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ Describe "Resolve DSC Resource Dependency" {
2626
}
2727

2828
Context "Module handler class" {
29+
$moduleHandlerType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]
2930
$oldEnvVars = Get-Item Env:\* | Sort-Object -Property Key
3031
$oldPSModulePath = $env:PSModulePath
3132
It "Sets defaults correctly" {
32-
$depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]::new()
33+
$rsp = [runspacefactory]::CreateRunspace()
34+
$rsp.Open()
35+
$depHandler = $moduleHandlerType::new($rsp)
3336

3437
$expectedPath = [System.IO.Path]::GetTempPath()
3538
$depHandler.TempPath | Should Be $expectedPath
@@ -47,11 +50,22 @@ Describe "Resolve DSC Resource Dependency" {
4750
$env:PSModulePath | Should Be $expectedPSModulePath
4851

4952
$depHandler.Dispose()
53+
$rsp.Dispose()
5054
}
5155

5256
It "Keeps the environment variables unchanged" {
5357
Test-EnvironmentVariables($oldEnvVars)
5458
}
59+
60+
It "Throws if runspace is null" {
61+
{$moduleHandlerType::new($null)} | Should Throw
62+
}
63+
64+
It "Throws if runspace is not opened" {
65+
$rsp = [runspacefactory]::CreateRunspace()
66+
{$moduleHandlerType::new($rsp)} | Should Throw
67+
$rsp.Dispose()
68+
}
5569
}
5670

5771
Context "Invoke-ScriptAnalyzer without switch" {
@@ -82,10 +96,13 @@ Describe "Resolve DSC Resource Dependency" {
8296
New-Item -Type Directory -Path $newTempPath
8397

8498
# create and dispose module dependency handler object
85-
# to setup the temporary module location
86-
$depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]::new()
99+
# to setup the temporary module
100+
$rsp = [runspacefactory]::CreateRunspace()
101+
$rsp.Open()
102+
$depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]::new($rsp)
87103
$pssaAppDataPath = $depHandler.PSSAAppDataPath
88104
$tempModulePath = $depHandler.TempModulePath
105+
$rsp.Dispose()
89106
$depHandler.Dispose()
90107

91108
# copy myresource module to the temporary location

0 commit comments

Comments
 (0)