Skip to content

Allow relative settings path #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions Engine/Commands/InvokeFormatterCommand.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
29 changes: 7 additions & 22 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -296,7 +280,8 @@ protected override void BeginProcessing()
var settingsObj = PSSASettings.Create(
settings,
processedPaths == null || processedPaths.Count == 0 ? null : processedPaths[0],
this);
this,
GetResolvedProviderPathFromPSPath);
if (settingsObj != null)
{
ScriptAnalyzer.Instance.UpdateSettings(settingsObj);
Expand Down
19 changes: 19 additions & 0 deletions Engine/Generic/PathResolver.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A shim around the GetResolvedProviderPathFromPSPath method from PSCmdlet to resolve relative path including wildcard support.
/// </summary>
/// <typeparam name="string"></typeparam>
/// <typeparam name="ProviderInfo"></typeparam>
/// <typeparam name="GetResolvedProviderPathFromPSPathDelegate"></typeparam>
/// <param name="input"></param>
/// <param name="output"></param>
/// <returns></returns>
internal delegate GetResolvedProviderPathFromPSPathDelegate GetResolvedProviderPathFromPSPath<in @string, ProviderInfo, out GetResolvedProviderPathFromPSPathDelegate>(@string input, out ProviderInfo output);
}
}
24 changes: 11 additions & 13 deletions Engine/Settings.cs
Original file line number Diff line number Diff line change
@@ -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;

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

case SettingsMode.Preset:
case SettingsMode.File:
var resolvedPath = getResolvedProviderPathFromPSPathDelegate(settingsFound.ToString(), out ProviderInfo providerInfo).Single();
settingsFound = resolvedPath;
outputWriter?.WriteVerbose(
String.Format(
CultureInfo.CurrentCulture,
Strings.SettingsUsingFile,
(string)settingsFound));
resolvedPath));
break;

case SettingsMode.Hashtable:
Expand Down
12 changes: 12 additions & 0 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down