Skip to content

Commit d4ba947

Browse files
bergmeisterJamesWTruher
authored andcommitted
Allow relative settings path (#909)
* make it possible to use relative path for -Settings switch * add test for using relative settings path * use improved path resolving approach in settings class * tidy up of comments/space
1 parent cad6f47 commit d4ba947

File tree

5 files changed

+52
-48
lines changed

5 files changed

+52
-48
lines changed

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
//
2-
// Copyright (c) Microsoft Corporation.
3-
//
4-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10-
// THE SOFTWARE.
11-
//
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
123

134
using System;
145
using System.Globalization;
15-
using System.Linq;
166
using System.Management.Automation;
177

188
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
@@ -91,7 +81,7 @@ protected override void BeginProcessing()
9181
this.range = Range == null ? null : new Range(Range[0], Range[1], Range[2], Range[3]);
9282
try
9383
{
94-
inputSettings = PSSASettings.Create(Settings, this.MyInvocation.PSScriptRoot, this);
84+
inputSettings = PSSASettings.Create(Settings, this.MyInvocation.PSScriptRoot, this, GetResolvedProviderPathFromPSPath);
9585
}
9686
catch (Exception e)
9787
{

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
1-
//
2-
// Copyright (c) Microsoft Corporation.
3-
//
4-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10-
// THE SOFTWARE.
11-
//
12-
13-
using System.Text.RegularExpressions;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
145
using System;
15-
using System.ComponentModel;
6+
using System.Collections;
167
using System.Collections.Generic;
178
using System.Collections.ObjectModel;
189
using System.Diagnostics.CodeAnalysis;
1910
using System.Globalization;
2011
using System.Linq;
2112
using System.Management.Automation;
22-
using System.Management.Automation.Language;
23-
using System.IO;
24-
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
25-
using System.Threading.Tasks;
26-
using System.Collections.Concurrent;
27-
using System.Threading;
2813
using System.Management.Automation.Runspaces;
29-
using System.Collections;
3014

3115
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
3216
{
@@ -296,7 +280,8 @@ protected override void BeginProcessing()
296280
var settingsObj = PSSASettings.Create(
297281
settings,
298282
processedPaths == null || processedPaths.Count == 0 ? null : processedPaths[0],
299-
this);
283+
this,
284+
GetResolvedProviderPathFromPSPath);
300285
if (settingsObj != null)
301286
{
302287
ScriptAnalyzer.Instance.UpdateSettings(settingsObj);

Engine/Generic/PathResolver.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
5+
{
6+
internal static class PathResolver
7+
{
8+
/// <summary>
9+
/// A shim around the GetResolvedProviderPathFromPSPath method from PSCmdlet to resolve relative path including wildcard support.
10+
/// </summary>
11+
/// <typeparam name="string"></typeparam>
12+
/// <typeparam name="ProviderInfo"></typeparam>
13+
/// <typeparam name="GetResolvedProviderPathFromPSPathDelegate"></typeparam>
14+
/// <param name="input"></param>
15+
/// <param name="output"></param>
16+
/// <returns></returns>
17+
internal delegate GetResolvedProviderPathFromPSPathDelegate GetResolvedProviderPathFromPSPath<in @string, ProviderInfo, out GetResolvedProviderPathFromPSPathDelegate>(@string input, out ProviderInfo output);
18+
}
19+
}

Engine/Settings.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
//
2-
// Copyright (c) Microsoft Corporation.
3-
//
4-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10-
// THE SOFTWARE.
11-
//
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
123

4+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
135
using System;
146
using System.Collections;
157
using System.Collections.Generic;
8+
using System.Collections.ObjectModel;
169
using System.Globalization;
1710
using System.IO;
1811
using System.Linq;
12+
using System.Management.Automation;
1913
using System.Management.Automation.Language;
2014
using System.Reflection;
2115

@@ -183,8 +177,10 @@ public static string GetSettingPresetFilePath(string settingPreset)
183177
/// <param name="settingsObj">An input object of type Hashtable or string.</param>
184178
/// <param name="cwd">The path in which to search for a settings file.</param>
185179
/// <param name="outputWriter">An output writer.</param>
180+
/// <param name="getResolvedProviderPathFromPSPathDelegate">The GetResolvedProviderPathFromPSPath method from PSCmdlet to resolve relative path including wildcard support.</param>
186181
/// <returns>An object of Settings type.</returns>
187-
public static Settings Create(object settingsObj, string cwd, IOutputWriter outputWriter)
182+
internal static Settings Create(object settingsObj, string cwd, IOutputWriter outputWriter,
183+
PathResolver.GetResolvedProviderPathFromPSPath<string, ProviderInfo, Collection<string>> getResolvedProviderPathFromPSPathDelegate)
188184
{
189185
object settingsFound;
190186
var settingsMode = FindSettingsMode(settingsObj, cwd, out settingsFound);
@@ -206,11 +202,13 @@ public static Settings Create(object settingsObj, string cwd, IOutputWriter outp
206202

207203
case SettingsMode.Preset:
208204
case SettingsMode.File:
205+
var resolvedPath = getResolvedProviderPathFromPSPathDelegate(settingsFound.ToString(), out ProviderInfo providerInfo).Single();
206+
settingsFound = resolvedPath;
209207
outputWriter?.WriteVerbose(
210208
String.Format(
211209
CultureInfo.CurrentCulture,
212210
Strings.SettingsUsingFile,
213-
(string)settingsFound));
211+
resolvedPath));
214212
break;
215213

216214
case SettingsMode.Hashtable:

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,18 @@ Describe "Test CustomizedRulePath" {
385385
if (!$testingLibraryUsage)
386386
{
387387
Context "When used from settings file" {
388+
It "Should process relative settings path" {
389+
try {
390+
$initialLocation = Get-Location
391+
Set-Location $PSScriptRoot
392+
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -Settings .\SettingsTest\..\SettingsTest\Project1\PSScriptAnalyzerSettings.psd1
393+
$warnings.Count | Should -Be 1
394+
}
395+
finally {
396+
Set-Location $initialLocation
397+
}
398+
}
399+
388400
It "Should use the CustomRulePath parameter" {
389401
$settings = @{
390402
CustomRulePath = "$directory\CommunityAnalyzerRules"

0 commit comments

Comments
 (0)