Skip to content

Commit a470e16

Browse files
author
Kapil Borle
committed
Remove redundancies from ModuleDependencyHandler
1 parent 0d984bd commit a470e16

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

Engine/Generic/ModuleDependencyHandler.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ public string TempPath
5050
private set
5151
{
5252
tempPath
53-
= (string.IsNullOrEmpty(value)
54-
|| string.IsNullOrWhiteSpace(value))
55-
? Environment.GetEnvironmentVariable("TEMP")
53+
= string.IsNullOrWhiteSpace(value)
54+
? Path.GetTempPath()
5655
: value;
5756
}
5857

@@ -69,10 +68,9 @@ public string LocalAppDataPath
6968
}
7069
private set
7170
{
72-
localAppdataPath
73-
= (string.IsNullOrEmpty(value)
74-
|| string.IsNullOrWhiteSpace(value))
75-
? Environment.GetEnvironmentVariable("LOCALAPPDATA")
71+
localAppdataPath
72+
= string.IsNullOrWhiteSpace(value)
73+
? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
7674
: value;
7775
}
7876
}
@@ -90,8 +88,7 @@ public string ModuleRepository
9088
set
9189
{
9290
moduleRepository
93-
= (string.IsNullOrEmpty(value)
94-
|| string.IsNullOrWhiteSpace(value))
91+
= string.IsNullOrWhiteSpace(value)
9592
? "PSGallery"
9693
: value;
9794
}
@@ -106,14 +103,6 @@ public string PSSAAppDataPath
106103
{
107104
return pssaAppDataPath;
108105
}
109-
private set
110-
{
111-
var leaf
112-
= (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
113-
? "PSScriptAnalyzer"
114-
: value;
115-
pssaAppDataPath = Path.Combine(LocalAppDataPath, leaf);
116-
}
117106
}
118107

119108
/// <summary>
@@ -156,8 +145,7 @@ private void SetupPSSAAppData()
156145
tempModulePath = GetTempModulePath(symLinkPath);
157146

158147
// check if the temp dir exists
159-
if (tempModulePath != null
160-
&& Directory.Exists(tempModulePath))
148+
if (Directory.Exists(tempModulePath))
161149
{
162150
return;
163151
}
@@ -204,12 +192,12 @@ private string GetPSSATempDirPath()
204192
// Return the first line of the file
205193
private string GetTempModulePath(string symLinkPath)
206194
{
207-
var symLinkLines = File.ReadAllLines(symLinkPath);
208-
if(symLinkLines.Length != 1)
195+
string line;
196+
using (var fileStream = new StreamReader(symLinkPath))
209197
{
210-
return null;
198+
line = fileStream.ReadLine();
211199
}
212-
return symLinkLines[0];
200+
return line;
213201
}
214202

215203
private void SaveModule(PSObject module)
@@ -278,9 +266,13 @@ public ModuleDependencyHandler(
278266
// and then passed into modulehandler
279267
TempPath = tempPath;
280268
LocalAppDataPath = localAppDataPath;
281-
PSSAAppDataPath = pssaAppDataPath;
282269
ModuleRepository = moduleRepository;
283-
270+
pssaAppDataPath = Path.Combine(
271+
LocalAppDataPath,
272+
string.IsNullOrWhiteSpace(pssaAppDataPath)
273+
? "PSScriptAnalyzer"
274+
: pssaAppDataPath);
275+
284276
modulesFound = new Dictionary<string, PSObject>();
285277

286278
// TODO Add PSSA Version in the path

Tests/Engine/ModuleDependencyHandler.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Describe "Resolve DSC Resource Dependency" {
3131
It "Sets defaults correctly" {
3232
$depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]::new()
3333

34-
$expectedPath = [System.Environment]::GetEnvironmentVariable("TEMP");
34+
$expectedPath = [System.IO.Path]::GetTempPath()
3535
$depHandler.TempPath | Should Be $expectedPath
3636

3737
$expectedLocalAppDataPath = [System.Environment]::GetEnvironmentVariable("LOCALAPPDATA");
@@ -105,8 +105,8 @@ Describe "Resolve DSC Resource Dependency" {
105105
#restore environment variables and clean up temporary location
106106
$env:LOCALAPPDATA = $oldLocalAppDataPath
107107
$env:TEMP = $oldTempPath
108-
Remove-Item -Recurse -Path $tempModulePath
109-
Remove-Item -Recurse -Path $tempPath
108+
Remove-Item -Recurse -Path $tempModulePath -Force
109+
Remove-Item -Recurse -Path $tempPath -Force
110110

111111
It "Keeps the environment variables unchanged" {
112112
Test-EnvironmentVariables($oldEnvVars)

0 commit comments

Comments
 (0)