From b68d5a7ce974a47d37c641d4428b5f22bf90fd5f Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 26 Feb 2018 20:03:02 +0000 Subject: [PATCH 1/4] make it possible to use relative path for -Settings switch --- Engine/Commands/InvokeScriptAnalyzerCommand.cs | 3 ++- Engine/Settings.cs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Engine/Commands/InvokeScriptAnalyzerCommand.cs b/Engine/Commands/InvokeScriptAnalyzerCommand.cs index 7f7a76a41..16890116e 100644 --- a/Engine/Commands/InvokeScriptAnalyzerCommand.cs +++ b/Engine/Commands/InvokeScriptAnalyzerCommand.cs @@ -293,9 +293,10 @@ protected override void BeginProcessing() var combIncludeDefaultRules = IncludeDefaultRules.IsPresent; try { + var currentWorkingDirectory = CurrentProviderLocation("FileSystem").ProviderPath; var settingsObj = PSSASettings.Create( settings, - processedPaths == null || processedPaths.Count == 0 ? null : processedPaths[0], + processedPaths == null || processedPaths.Count == 0 ? currentWorkingDirectory : processedPaths[0], this); if (settingsObj != null) { diff --git a/Engine/Settings.cs b/Engine/Settings.cs index c34631183..2b05cf4c9 100644 --- a/Engine/Settings.cs +++ b/Engine/Settings.cs @@ -206,11 +206,12 @@ public static Settings Create(object settingsObj, string cwd, IOutputWriter outp case SettingsMode.Preset: case SettingsMode.File: + settingsFound = Path.Combine(cwd, settingsFound.ToString()); outputWriter?.WriteVerbose( String.Format( CultureInfo.CurrentCulture, Strings.SettingsUsingFile, - (string)settingsFound)); + settingsFound.ToString())); break; case SettingsMode.Hashtable: From c0c61b53231cc3d7608677530bc5b39c666acba0 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 26 Feb 2018 20:24:07 +0000 Subject: [PATCH 2/4] add test for using relative settings path --- Tests/Engine/InvokeScriptAnalyzer.tests.ps1 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index 2b03bc5e1..1be59e7bf 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -385,6 +385,18 @@ Describe "Test CustomizedRulePath" { if (!$testingLibraryUsage) { Context "When used from settings file" { + It "Should process relative settings path" { + try { + $initialLocation = Get-Location + Set-Location $PSScriptRoot + $warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -Settings .\SettingsTest\..\SettingsTest\Project1\PSScriptAnalyzerSettings.psd1 + $warnings.Count | Should -Be 1 + } + finally { + Set-Location $initialLocation + } + } + It "Should use the CustomRulePath parameter" { $settings = @{ CustomRulePath = "$directory\CommunityAnalyzerRules" From 76d69d891063a6525bca38701a3df6f6b0e18a6c Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 27 Feb 2018 23:09:39 +0000 Subject: [PATCH 3/4] use improved path resolving approach in settings class --- Engine/Commands/InvokeFormatterCommand.cs | 16 ++-------- .../Commands/InvokeScriptAnalyzerCommand.cs | 31 +++++-------------- Engine/Generic/PathResolver.cs | 19 ++++++++++++ Engine/Settings.cs | 26 +++++++--------- 4 files changed, 41 insertions(+), 51 deletions(-) create mode 100644 Engine/Generic/PathResolver.cs diff --git a/Engine/Commands/InvokeFormatterCommand.cs b/Engine/Commands/InvokeFormatterCommand.cs index 5d9723df0..23d19284c 100644 --- a/Engine/Commands/InvokeFormatterCommand.cs +++ b/Engine/Commands/InvokeFormatterCommand.cs @@ -1,18 +1,8 @@ -// -// Copyright (c) Microsoft Corporation. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. using System; using System.Globalization; -using System.Linq; using System.Management.Automation; namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands @@ -91,7 +81,7 @@ protected override void BeginProcessing() this.range = Range == null ? null : new Range(Range[0], Range[1], Range[2], Range[3]); try { - inputSettings = PSSASettings.Create(Settings, this.MyInvocation.PSScriptRoot, this); + inputSettings = PSSASettings.Create(Settings, this.MyInvocation.PSScriptRoot, this, GetResolvedProviderPathFromPSPath); } catch (Exception e) { diff --git a/Engine/Commands/InvokeScriptAnalyzerCommand.cs b/Engine/Commands/InvokeScriptAnalyzerCommand.cs index 16890116e..595462a9f 100644 --- a/Engine/Commands/InvokeScriptAnalyzerCommand.cs +++ b/Engine/Commands/InvokeScriptAnalyzerCommand.cs @@ -1,32 +1,16 @@ -// -// Copyright (c) Microsoft Corporation. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// - -using System.Text.RegularExpressions; +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; using System; -using System.ComponentModel; +using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Management.Automation; -using System.Management.Automation.Language; -using System.IO; -using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; -using System.Threading.Tasks; -using System.Collections.Concurrent; -using System.Threading; using System.Management.Automation.Runspaces; -using System.Collections; namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands { @@ -293,11 +277,10 @@ protected override void BeginProcessing() var combIncludeDefaultRules = IncludeDefaultRules.IsPresent; try { - var currentWorkingDirectory = CurrentProviderLocation("FileSystem").ProviderPath; var settingsObj = PSSASettings.Create( settings, - processedPaths == null || processedPaths.Count == 0 ? currentWorkingDirectory : processedPaths[0], - this); + processedPaths == null || processedPaths.Count == 0 ? null : processedPaths[0], + this, GetResolvedProviderPathFromPSPath); if (settingsObj != null) { ScriptAnalyzer.Instance.UpdateSettings(settingsObj); diff --git a/Engine/Generic/PathResolver.cs b/Engine/Generic/PathResolver.cs new file mode 100644 index 000000000..86e2b7ab4 --- /dev/null +++ b/Engine/Generic/PathResolver.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic +{ + internal static class PathResolver + { + /// + /// A shim around the GetResolvedProviderPathFromPSPath method from PSCmdlet to resolve relative path including wildcard support. + /// + /// + /// + /// + /// + /// + /// + internal delegate GetResolvedProviderPathFromPSPathDelegate GetResolvedProviderPathFromPSPath(@string input, out ProviderInfo output); + } +} diff --git a/Engine/Settings.cs b/Engine/Settings.cs index 2b05cf4c9..3c8d39cc7 100644 --- a/Engine/Settings.cs +++ b/Engine/Settings.cs @@ -1,21 +1,15 @@ -// -// Copyright (c) Microsoft Corporation. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; using System; using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Globalization; using System.IO; using System.Linq; +using System.Management.Automation; using System.Management.Automation.Language; using System.Reflection; @@ -183,8 +177,10 @@ public static string GetSettingPresetFilePath(string settingPreset) /// An input object of type Hashtable or string. /// The path in which to search for a settings file. /// An output writer. + /// The GetResolvedProviderPathFromPSPath method from PSCmdlet to resolve relative path including wildcard support. /// An object of Settings type. - public static Settings Create(object settingsObj, string cwd, IOutputWriter outputWriter) + internal static Settings Create(object settingsObj, string cwd, IOutputWriter outputWriter, + PathResolver.GetResolvedProviderPathFromPSPath> getResolvedProviderPathFromPSPathDelegate) { object settingsFound; var settingsMode = FindSettingsMode(settingsObj, cwd, out settingsFound); @@ -206,12 +202,14 @@ public static Settings Create(object settingsObj, string cwd, IOutputWriter outp case SettingsMode.Preset: case SettingsMode.File: - settingsFound = Path.Combine(cwd, settingsFound.ToString()); + var resolvedPath = getResolvedProviderPathFromPSPathDelegate(settingsFound.ToString(), out ProviderInfo providerInfo).Single(); + //var resolvedPath = pathResolver(settingsFound.ToString()); + settingsFound = resolvedPath; outputWriter?.WriteVerbose( String.Format( CultureInfo.CurrentCulture, Strings.SettingsUsingFile, - settingsFound.ToString())); + resolvedPath)); break; case SettingsMode.Hashtable: From 20084ee2764d5c8d70d177051299aacc51ff1188 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 27 Feb 2018 23:20:19 +0000 Subject: [PATCH 4/4] tidy up of comments/space --- Engine/Commands/InvokeScriptAnalyzerCommand.cs | 3 ++- Engine/Settings.cs | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/Commands/InvokeScriptAnalyzerCommand.cs b/Engine/Commands/InvokeScriptAnalyzerCommand.cs index 595462a9f..bef7a7b78 100644 --- a/Engine/Commands/InvokeScriptAnalyzerCommand.cs +++ b/Engine/Commands/InvokeScriptAnalyzerCommand.cs @@ -280,7 +280,8 @@ protected override void BeginProcessing() var settingsObj = PSSASettings.Create( settings, processedPaths == null || processedPaths.Count == 0 ? null : processedPaths[0], - this, GetResolvedProviderPathFromPSPath); + this, + GetResolvedProviderPathFromPSPath); if (settingsObj != null) { ScriptAnalyzer.Instance.UpdateSettings(settingsObj); diff --git a/Engine/Settings.cs b/Engine/Settings.cs index 3c8d39cc7..0947b77d8 100644 --- a/Engine/Settings.cs +++ b/Engine/Settings.cs @@ -203,7 +203,6 @@ internal static Settings Create(object settingsObj, string cwd, IOutputWriter ou case SettingsMode.Preset: case SettingsMode.File: var resolvedPath = getResolvedProviderPathFromPSPathDelegate(settingsFound.ToString(), out ProviderInfo providerInfo).Single(); - //var resolvedPath = pathResolver(settingsFound.ToString()); settingsFound = resolvedPath; outputWriter?.WriteVerbose( String.Format(