diff --git a/docs/CHANGELOG-v3.md b/docs/CHANGELOG-v3.md index e7ce3288f7..111ae36c54 100644 --- a/docs/CHANGELOG-v3.md +++ b/docs/CHANGELOG-v3.md @@ -38,8 +38,11 @@ What's changed since pre-release v3.0.0-B0203: - If a rule fails with a severity level equal or higher than the configured level the pipeline will break. - General improvements: - **Breaking change**: Improve scope handling for correctly handling cases with multiple module by @BernieWhite. - - As a result of this change the `binding` property can no longer be used within baselines. - - Use module configuration or workspace options instead. + [#1215](https://github.com/microsoft/PSRule/issues/1215) + - As a result of this change: + - The `binding` property can no longer be used within baselines. + - Custom inline script blocks can no longer be used for custom binding. + - Use module configuration or workspace to configure binding options instead. - Added support for native logging within emitters by @BernieWhite. [#1837](https://github.com/microsoft/PSRule/issues/1837) - Engineering: diff --git a/docs/commands/PSRule/en-US/New-PSRuleOption.md b/docs/commands/PSRule/en-US/New-PSRuleOption.md index d00cfc6ab0..821e623177 100644 --- a/docs/commands/PSRule/en-US/New-PSRuleOption.md +++ b/docs/commands/PSRule/en-US/New-PSRuleOption.md @@ -17,8 +17,8 @@ Create options to configure PSRule execution. ```text New-PSRuleOption [[-Path] ] [-Configuration ] - [-SuppressTargetName ] [-BindTargetName ] - [-BindTargetType ] [-BaselineGroup ] [-BindingIgnoreCase ] + [-SuppressTargetName ] + [-BaselineGroup ] [-BindingIgnoreCase ] [-BindingField ] [-BindingNameSeparator ] [-BindingPreferTargetInfo ] [-TargetName ] [-TargetType ] [-BindingUseQualifiedName ] [-Convention ] [-DuplicateResourceId ] @@ -44,8 +44,8 @@ New-PSRuleOption [[-Path] ] [-Configuration ] ```text New-PSRuleOption [-Option] [-Configuration ] - [-SuppressTargetName ] [-BindTargetName ] - [-BindTargetType ] [-BaselineGroup ] [-BindingIgnoreCase ] + [-SuppressTargetName ] + [-BaselineGroup ] [-BindingIgnoreCase ] [-BindingField ] [-BindingNameSeparator ] [-BindingPreferTargetInfo ] [-TargetName ] [-TargetType ] [-BindingUseQualifiedName ] [-Convention ] [-DuplicateResourceId ] @@ -71,7 +71,7 @@ New-PSRuleOption [-Option] [-Configuration ] ```text New-PSRuleOption [-Default] [-Configuration ] [-SuppressTargetName ] - [-BindTargetName ] [-BindTargetType ] [-BaselineGroup ] + [-BaselineGroup ] [-BindingIgnoreCase ] [-BindingField ] [-BindingNameSeparator ] [-BindingPreferTargetInfo ] [-TargetName ] [-TargetType ] [-BindingUseQualifiedName ] [-Convention ] @@ -118,28 +118,6 @@ Create an options object that suppresses `TestObject1` and `TestObject3` for a r ### Example 3 -```powershell -# Create a custom function that returns a TargetName string -$bindFn = { - param ($TargetObject) - - $otherName = $TargetObject.PSObject.Properties['OtherName']; - - if ($otherName -eq $Null) { - return $Null - } - - return $otherName.Value; -} - -# Specify the binding function script block code to execute -$option = New-PSRuleOption -BindTargetName $bindFn; -``` - -Creates an options object that uses a custom function to bind the _TargetName_ of an object. - -### Example 4 - ```powershell $option = New-PSRuleOption -Configuration @{ 'appServiceMinInstanceCount' = 2 }; ``` @@ -221,23 +199,6 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -BindTargetName - -Configures a custom function to use to bind TargetName of an object. -See about_PSRule_Options for more information. - -```yaml -Type: BindTargetName[] -Parameter Sets: (All) -Aliases: - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - ### -Configuration Configures a set of baseline configuration values that can be used in rule definitions instead of using hard coded values. @@ -274,23 +235,6 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -BindTargetType - -Configures a custom function to use to bind TargetType of an object. -See about_PSRule_Options for more information. - -```yaml -Type: BindTargetName[] -Parameter Sets: (All) -Aliases: - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - ### -BindingIgnoreCase Sets the option `Binding.IgnoreCase`. diff --git a/docs/deprecations.md b/docs/deprecations.md index 29a35234ad..b983ee0f29 100644 --- a/docs/deprecations.md +++ b/docs/deprecations.md @@ -106,6 +106,13 @@ This existed to support scenarios before a module configuration and language sco Configuring binding configuration on a baseline is removed from PSRule v3. +### Binding hooks + +Prior to v3, a custom binding PowerShell script block could be used to perform custom binding inline. +This feature was hard to use and obsolete for most common use cases. + +Alternatively, configure `Binding.TargetName` and `Binding.TargetType` options to use the built-in binder. + ## Deprecations for v2 ### Default baseline by module manifest diff --git a/src/PSRule/Configuration/PSRuleOption.cs b/src/PSRule/Configuration/PSRuleOption.cs index e31d962df3..17e86ecc07 100644 --- a/src/PSRule/Configuration/PSRuleOption.cs +++ b/src/PSRule/Configuration/PSRuleOption.cs @@ -58,7 +58,6 @@ public PSRuleOption() Input = new InputOption(); Logging = new LoggingOption(); Output = new OutputOption(); - Pipeline = new PipelineHook(); Repository = new RepositoryOption(); Requires = new RequiresOption(); Rule = new RuleOption(); @@ -79,7 +78,6 @@ private PSRuleOption(string sourcePath, PSRuleOption option) Input = new InputOption(option?.Input); Logging = new LoggingOption(option?.Logging); Output = new OutputOption(option?.Output); - Pipeline = new PipelineHook(option?.Pipeline); Repository = new RepositoryOption(option?.Repository); Requires = new RequiresOption(option?.Requires); Rule = new RuleOption(option?.Rule); @@ -131,13 +129,6 @@ private PSRuleOption(string sourcePath, PSRuleOption option) /// public OutputOption Output { get; set; } - /// - /// Configures pipeline hooks. - /// - [YamlIgnore] - [JsonIgnore] - public PipelineHook Pipeline { get; set; } - /// /// Options for repository properties that are used by PSRule. /// @@ -438,7 +429,6 @@ public bool Equals(PSRuleOption other) Logging == other.Logging && Output == other.Output && Suppression == other.Suppression && - Pipeline == other.Pipeline && Repository == other.Repository && Rule == other.Rule; } @@ -459,7 +449,6 @@ public override int GetHashCode() hash = hash * 23 + (Logging != null ? Logging.GetHashCode() : 0); hash = hash * 23 + (Output != null ? Output.GetHashCode() : 0); hash = hash * 23 + (Suppression != null ? Suppression.GetHashCode() : 0); - hash = hash * 23 + (Pipeline != null ? Pipeline.GetHashCode() : 0); hash = hash * 23 + (Repository != null ? Repository.GetHashCode() : 0); hash = hash * 23 + (Rule != null ? Rule.GetHashCode() : 0); return hash; diff --git a/src/PSRule/Configuration/PipelineHook.cs b/src/PSRule/Configuration/PipelineHook.cs index 249166458b..b649346a5f 100644 --- a/src/PSRule/Configuration/PipelineHook.cs +++ b/src/PSRule/Configuration/PipelineHook.cs @@ -10,38 +10,3 @@ namespace PSRule.Configuration; internal delegate string BindTargetMethod(string[] propertyNames, bool caseSensitive, bool preferTargetInfo, object targetObject, out string path); internal delegate string BindTargetFunc(string[] propertyNames, bool caseSensitive, bool preferTargetInfo, object targetObject, BindTargetMethod next, out string path); - -/// -/// Hooks that provide customize pipeline execution. -/// -public sealed class PipelineHook -{ - /// - /// Create an empty set of pipeline hooks. - /// - public PipelineHook() - { - BindTargetName = new List(); - BindTargetType = new List(); - } - - /// - /// Create pipeline hooks based on an existing option instance. - /// - /// An existing pipeline hook option. - public PipelineHook(PipelineHook option) - { - BindTargetName = option?.BindTargetName ?? new List(); - BindTargetType = option?.BindTargetType ?? new List(); - } - - /// - /// One or more custom functions to use to bind TargetName of a pipeline object. - /// - public List BindTargetName { get; set; } - - /// - /// One or more custom functions to use to bind TargetType of a pipeline object. - /// - public List BindTargetType { get; set; } -} diff --git a/src/PSRule/PSRule.psm1 b/src/PSRule/PSRule.psm1 index 7d2494f9a9..6c4f49a547 100644 --- a/src/PSRule/PSRule.psm1 +++ b/src/PSRule/PSRule.psm1 @@ -1116,12 +1116,6 @@ function New-PSRuleOption { [Parameter(Mandatory = $False)] [PSRule.Configuration.SuppressionOption]$SuppressTargetName, - [Parameter(Mandatory = $False)] - [PSRule.Configuration.BindTargetName[]]$BindTargetName, - - [Parameter(Mandatory = $False)] - [PSRule.Configuration.BindTargetName[]]$BindTargetType, - # Options # Sets the Baseline.Group option @@ -1371,12 +1365,6 @@ function New-PSRuleOption { if ($optionParams.ContainsKey('SuppressTargetName')) { $optionParams.Remove('SuppressTargetName'); } - if ($optionParams.ContainsKey('BindTargetName')) { - $optionParams.Remove('BindTargetName'); - } - if ($optionParams.ContainsKey('BindTargetType')) { - $optionParams.Remove('BindTargetType'); - } if ($PSBoundParameters.ContainsKey('Option')) { $Option = [PSRule.Configuration.PSRuleOption]::FromFileOrEmpty($Option, $Path); } @@ -1400,14 +1388,6 @@ function New-PSRuleOption { if ($PSBoundParameters.ContainsKey('SuppressTargetName')) { $Option.Suppression = $SuppressTargetName; } - if ($PSBoundParameters.ContainsKey('BindTargetName')) { - Write-Verbose -Message 'Set BindTargetName pipeline hook'; - $Option.Pipeline.BindTargetName.AddRange($BindTargetName); - } - if ($PSBoundParameters.ContainsKey('BindTargetType')) { - Write-Verbose -Message 'Set BindTargetType pipeline hook'; - $Option.Pipeline.BindTargetType.AddRange($BindTargetType); - } # Options $Option | SetOptions @optionParams -Verbose:$VerbosePreference; diff --git a/src/PSRule/Pipeline/AssertPipelineBuilder.cs b/src/PSRule/Pipeline/AssertPipelineBuilder.cs index de0a7999a4..4bfe3eb804 100644 --- a/src/PSRule/Pipeline/AssertPipelineBuilder.cs +++ b/src/PSRule/Pipeline/AssertPipelineBuilder.cs @@ -201,10 +201,7 @@ public sealed override IPipeline Build(IPipelineWriter writer = null) return !RequireModules() || !RequireSources() ? null : (IPipeline)new InvokeRulePipeline( - context: PrepareContext( - bindTargetName: BindTargetNameHook, - bindTargetType: BindTargetTypeHook, - bindField: BindFieldHook), + context: PrepareContext(PipelineHookActions.Default), source: Source, writer: HandleJobSummary(writer ?? PrepareWriter()), outcome: RuleOutcome.Processed); diff --git a/src/PSRule/Pipeline/ExportBaselinePipelineBuilder.cs b/src/PSRule/Pipeline/ExportBaselinePipelineBuilder.cs index de5a805b81..65ed92f87b 100644 --- a/src/PSRule/Pipeline/ExportBaselinePipelineBuilder.cs +++ b/src/PSRule/Pipeline/ExportBaselinePipelineBuilder.cs @@ -43,11 +43,7 @@ public override IPipeline Build(IPipelineWriter writer = null) { var filter = new BaselineFilter(_Name); return new GetBaselinePipeline( - pipeline: PrepareContext( - bindTargetName: null, - bindTargetType: null, - bindField: null - ), + pipeline: PrepareContext(PipelineHookActions.Empty), source: Source, reader: PrepareReader(), writer: writer ?? PrepareWriter(), diff --git a/src/PSRule/Pipeline/GetBaselinePipelineBuilder.cs b/src/PSRule/Pipeline/GetBaselinePipelineBuilder.cs index a8636ced86..191e2cf3d5 100644 --- a/src/PSRule/Pipeline/GetBaselinePipelineBuilder.cs +++ b/src/PSRule/Pipeline/GetBaselinePipelineBuilder.cs @@ -41,11 +41,7 @@ public override IPipeline Build(IPipelineWriter writer = null) { var filter = new BaselineFilter(ResolveBaselineGroup(_Name)); return new GetBaselinePipeline( - pipeline: PrepareContext( - bindTargetName: null, - bindTargetType: null, - bindField: null - ), + pipeline: PrepareContext(PipelineHookActions.Empty), source: Source, reader: PrepareReader(), writer: writer ?? PrepareWriter(), diff --git a/src/PSRule/Pipeline/GetRuleHelpPipeline.cs b/src/PSRule/Pipeline/GetRuleHelpPipeline.cs index b0dbdef933..b89a30ab9e 100644 --- a/src/PSRule/Pipeline/GetRuleHelpPipeline.cs +++ b/src/PSRule/Pipeline/GetRuleHelpPipeline.cs @@ -50,10 +50,7 @@ public void Online() public override IPipeline Build(IPipelineWriter writer = null) { return new GetRuleHelpPipeline( - pipeline: PrepareContext( - bindTargetName: null, - bindTargetType: null, - bindField: null), + pipeline: PrepareContext(PipelineHookActions.Empty), source: Source, reader: PrepareReader(), writer: writer ?? PrepareWriter()); diff --git a/src/PSRule/Pipeline/GetRulePipelineBuilder.cs b/src/PSRule/Pipeline/GetRulePipelineBuilder.cs index 3e8f6c8079..cc7b423d59 100644 --- a/src/PSRule/Pipeline/GetRulePipelineBuilder.cs +++ b/src/PSRule/Pipeline/GetRulePipelineBuilder.cs @@ -45,11 +45,7 @@ public override IPipeline Build(IPipelineWriter writer = null) return !RequireModules() || !RequireSources() ? null : (IPipeline)new GetRulePipeline( - pipeline: PrepareContext( - bindTargetName: null, - bindTargetType: null, - bindField: null - ), + pipeline: PrepareContext(PipelineHookActions.Empty), source: Source, reader: PrepareReader(), writer: writer ?? PrepareWriter(), diff --git a/src/PSRule/Pipeline/GetTargetPipelineBuilder.cs b/src/PSRule/Pipeline/GetTargetPipelineBuilder.cs index 382f1df9c5..9692883336 100644 --- a/src/PSRule/Pipeline/GetTargetPipelineBuilder.cs +++ b/src/PSRule/Pipeline/GetTargetPipelineBuilder.cs @@ -57,7 +57,7 @@ public void InputPath(string[] path) /// public override IPipeline Build(IPipelineWriter writer = null) { - return new GetTargetPipeline(PrepareContext(null, null, null), PrepareReader(), writer ?? PrepareWriter()); + return new GetTargetPipeline(PrepareContext(PipelineHookActions.Empty), PrepareReader(), writer ?? PrepareWriter()); } /// diff --git a/src/PSRule/Pipeline/InvokePipelineBuilderBase.cs b/src/PSRule/Pipeline/InvokePipelineBuilderBase.cs index 9d72d60609..cd7d3a2bcf 100644 --- a/src/PSRule/Pipeline/InvokePipelineBuilderBase.cs +++ b/src/PSRule/Pipeline/InvokePipelineBuilderBase.cs @@ -4,10 +4,12 @@ using PSRule.Configuration; using PSRule.Definitions; using PSRule.Host; -using PSRule.Options; namespace PSRule.Pipeline; +/// +/// A pipeline builder for any pipelines that test objects against rules. +/// internal abstract class InvokePipelineBuilderBase : PipelineBuilderBase, IInvokePipelineBuilder { protected InputPathBuilder _InputPath; @@ -91,7 +93,7 @@ public override IPipeline Build(IPipelineWriter writer = null) Unblock(writer); return !RequireModules() || !RequireSources() ? null - : (IPipeline)new InvokeRulePipeline(PrepareContext(BindTargetNameHook, BindTargetTypeHook, BindFieldHook), Source, writer, Option.Output.Outcome.Value); + : (IPipeline)new InvokeRulePipeline(PrepareContext(PipelineHookActions.Default), Source, writer, Option.Output.Outcome.Value); } protected void Unblock(IPipelineWriter writer) diff --git a/src/PSRule/Pipeline/PipelineBuilderBase.cs b/src/PSRule/Pipeline/PipelineBuilderBase.cs index 570c337e1f..4f4a0b2903 100644 --- a/src/PSRule/Pipeline/PipelineBuilderBase.cs +++ b/src/PSRule/Pipeline/PipelineBuilderBase.cs @@ -13,6 +13,9 @@ namespace PSRule.Pipeline; +/// +/// A base instance for a pipeline builder. +/// internal abstract class PipelineBuilderBase : IPipelineBuilder { private const string ENGINE_MODULE_NAME = "PSRule"; @@ -20,10 +23,6 @@ internal abstract class PipelineBuilderBase : IPipelineBuilder protected readonly PSRuleOption Option; protected readonly Source[] Source; protected readonly IHostContext HostContext; - protected BindTargetMethod BindTargetNameHook; - protected BindTargetMethod BindTargetTypeHook; - protected BindTargetMethod BindFieldHook; - protected VisitTargetObject VisitTargetObject; private string[] _Include; private Hashtable _Tag; @@ -43,9 +42,6 @@ protected PipelineBuilderBase(Source[] source, IHostContext hostContext) Source = source; _Output = new HostPipelineWriter(hostContext, Option, ShouldProcess); HostContext = hostContext; - BindTargetNameHook = PipelineHookActions.BindTargetName; - BindTargetTypeHook = PipelineHookActions.BindTargetType; - BindFieldHook = PipelineHookActions.BindField; } /// @@ -167,7 +163,10 @@ private static bool TryModuleVersion(string moduleVersion, string requiredVersio constraint.Equals(version); } - protected PipelineContext PrepareContext(BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField) + /// + /// Create a pipeline context. + /// + protected PipelineContext PrepareContext((BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField) binding) { var unresolved = new List(); if (_Baseline is Configuration.BaselineOption.BaselineRef baselineRef) @@ -177,10 +176,10 @@ protected PipelineContext PrepareContext(BindTargetMethod bindTargetName, BindTa option: Option, hostContext: HostContext, reader: PrepareReader(), - bindTargetName: bindTargetName, - bindTargetType: bindTargetType, - bindField: bindField, - optionBuilder: GetOptionBuilder(bindTargetName, bindTargetType, bindField), + bindTargetName: binding.bindTargetName, + bindTargetType: binding.bindTargetType, + bindField: binding.bindField, + optionBuilder: GetOptionBuilder(binding), unresolved: unresolved ); } @@ -328,60 +327,14 @@ protected PathFilter GetInputFilter() return _InputFilter; } - private OptionContextBuilder GetOptionBuilder(BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField) - { - return new OptionContextBuilder(Option, _Include, _Tag, _Convention, bindTargetName, bindTargetType, bindField); - } - protected void ConfigureBinding(PSRuleOption option) { - if (option.Pipeline.BindTargetName != null && option.Pipeline.BindTargetName.Count > 0) - { - // Do not allow custom binding functions to be used with constrained language mode - if (Option.Execution.LanguageMode == LanguageMode.ConstrainedLanguage) - throw new PipelineConfigurationException(optionName: "BindTargetName", message: PSRuleResources.ConstrainedTargetBinding); - - foreach (var action in option.Pipeline.BindTargetName) - BindTargetNameHook = AddBindTargetAction(action, BindTargetNameHook); - } - - if (option.Pipeline.BindTargetType != null && option.Pipeline.BindTargetType.Count > 0) - { - // Do not allow custom binding functions to be used with constrained language mode - if (Option.Execution.LanguageMode == LanguageMode.ConstrainedLanguage) - throw new PipelineConfigurationException(optionName: "BindTargetType", message: PSRuleResources.ConstrainedTargetBinding); - - foreach (var action in option.Pipeline.BindTargetType) - BindTargetTypeHook = AddBindTargetAction(action, BindTargetTypeHook); - } - } - - private static BindTargetMethod AddBindTargetAction(BindTargetFunc action, BindTargetMethod previous) - { - // Nest the previous write action in the new supplied action - // Execution chain will be: action -> previous -> previous..n - return (string[] propertyNames, bool caseSensitive, bool preferTargetInfo, object targetObject, out string path) => - { - return action(propertyNames, caseSensitive, preferTargetInfo, targetObject, previous, out path); - }; - } - - private static BindTargetMethod AddBindTargetAction(BindTargetName action, BindTargetMethod previous) - { - return AddBindTargetAction((string[] propertyNames, bool caseSensitive, bool preferTargetInfo, object targetObject, BindTargetMethod next, out string path) => - { - path = null; - var targetType = action(targetObject); - return string.IsNullOrEmpty(targetType) ? next(propertyNames, caseSensitive, preferTargetInfo, targetObject, out path) : targetType; - }, previous); } - protected void AddVisitTargetObjectAction(VisitTargetObjectAction action) + private OptionContextBuilder GetOptionBuilder((BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField) binding) { - // Nest the previous write action in the new supplied action - // Execution chain will be: action -> previous -> previous..n - var previous = VisitTargetObject; - VisitTargetObject = (targetObject) => action(targetObject, previous); + var builder = new OptionContextBuilder(Option, _Include, _Tag, _Convention, binding.bindTargetName, binding.bindTargetType, binding.bindField); + return builder; } /// diff --git a/src/PSRule/Pipeline/PipelineHookActions.cs b/src/PSRule/Pipeline/PipelineHookActions.cs index 82e0e1d608..d3702e11fb 100644 --- a/src/PSRule/Pipeline/PipelineHookActions.cs +++ b/src/PSRule/Pipeline/PipelineHookActions.cs @@ -18,6 +18,9 @@ namespace PSRule.Pipeline; /// internal static class PipelineHookActions { + internal static readonly (BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField) Default = (PipelineHookActions.BindTargetName, PipelineHookActions.BindTargetType, PipelineHookActions.BindField); + internal static readonly (BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField) Empty = (null, null, null); + private const string Property_TargetName = "TargetName"; private const string Property_Name = "Name"; diff --git a/src/PSRule/Pipeline/RulePipeline.cs b/src/PSRule/Pipeline/RulePipeline.cs index afc93a9312..2c6b1c4f7d 100644 --- a/src/PSRule/Pipeline/RulePipeline.cs +++ b/src/PSRule/Pipeline/RulePipeline.cs @@ -12,16 +12,16 @@ internal abstract class RulePipeline : IPipeline protected readonly PipelineContext Pipeline; protected readonly RunspaceContext Context; protected readonly Source[] Source; - protected readonly PipelineInputStream Reader; + protected readonly IPipelineReader Reader; protected readonly IPipelineWriter Writer; // Track whether Dispose has been called. private bool _Disposed; - protected RulePipeline(PipelineContext context, Source[] source, PipelineInputStream reader, IPipelineWriter writer) + protected RulePipeline(PipelineContext pipelineContext, Source[] source, IPipelineReader reader, IPipelineWriter writer) { - Result = new DefaultPipelineResult(writer, context.Option.Execution.Break.GetValueOrDefault(ExecutionOption.Default.Break.Value)); - Pipeline = context; + Result = new DefaultPipelineResult(writer, pipelineContext.Option.Execution.Break.GetValueOrDefault(ExecutionOption.Default.Break.Value)); + Pipeline = pipelineContext; Context = new RunspaceContext(Pipeline, writer); Source = source; Reader = reader; diff --git a/src/PSRule/Runtime/RunspaceContext.cs b/src/PSRule/Runtime/RunspaceContext.cs index 3dda929de2..d9bb074a8f 100644 --- a/src/PSRule/Runtime/RunspaceContext.cs +++ b/src/PSRule/Runtime/RunspaceContext.cs @@ -18,7 +18,7 @@ namespace PSRule.Runtime; #nullable enable /// -/// A context for a PSRule runspace. +/// A context applicable to rule execution. /// internal sealed class RunspaceContext : IDisposable, ILogger { diff --git a/tests/PSRule.Tests/PSRule.Common.Tests.ps1 b/tests/PSRule.Tests/PSRule.Common.Tests.ps1 index be92f0d5ab..e9e726987e 100644 --- a/tests/PSRule.Tests/PSRule.Common.Tests.ps1 +++ b/tests/PSRule.Tests/PSRule.Common.Tests.ps1 @@ -1030,15 +1030,6 @@ Describe 'Invoke-PSRule' -Tag 'Invoke-PSRule','Common' { $option = @{ 'execution.mode' = 'ConstrainedLanguage' }; { $Null = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'ConstrainedTest2' -Option $option -ErrorAction Stop } | Should -Throw 'Cannot invoke method. Method invocation is supported only on core types in this language mode.'; { $Null = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'ConstrainedTest3' -Option $option -ErrorAction Stop } | Should -Throw 'Cannot invoke method. Method invocation is supported only on core types in this language mode.'; - - $bindFn = { - param ($TargetObject) - $Null = [Console]::WriteLine('Should fail'); - return 'BadName'; - } - - $option = New-PSRuleOption -Option @{ 'execution.mode' = 'ConstrainedLanguage' } -BindTargetName $bindFn; - { $Null = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'ConstrainedTest1' -Option $option -ErrorAction Stop } | Should -Throw 'Exception calling "Invoke" with "3" argument(s): "Binding functions are not supported in this language mode."'; } } @@ -1545,15 +1536,6 @@ Describe 'Test-PSRuleTarget' -Tag 'Test-PSRuleTarget','Common' { $option = @{ 'execution.mode' = 'ConstrainedLanguage' }; { $Null = $testObject | Test-PSRuleTarget -Path $ruleFilePath -Name 'ConstrainedTest2' -Option $option -ErrorAction Stop } | Should -Throw 'Cannot invoke method. Method invocation is supported only on core types in this language mode.'; { $Null = $testObject | Test-PSRuleTarget -Path $ruleFilePath -Name 'ConstrainedTest3' -Option $option -ErrorAction Stop } | Should -Throw 'Cannot invoke method. Method invocation is supported only on core types in this language mode.'; - - $bindFn = { - param ($TargetObject) - $Null = [Console]::WriteLine('Should fail'); - return 'BadName'; - } - - $option = New-PSRuleOption -Option @{ 'execution.mode' = 'ConstrainedLanguage' } -BindTargetName $bindFn; - { $Null = $testObject | Test-PSRuleTarget -Path $ruleFilePath -Name 'ConstrainedTest1' -Option $option -ErrorAction Stop } | Should -Throw 'Exception calling "Test" with "3" argument(s): "Binding functions are not supported in this language mode."'; } } } @@ -1803,15 +1785,6 @@ Describe 'Assert-PSRule' -Tag 'Assert-PSRule','Common' { $option = @{ 'execution.mode' = 'ConstrainedLanguage' }; { $Null = $testObject | Assert-PSRule -Path $ruleFilePath -Name 'ConstrainedTest2' -Option $option -ErrorAction Stop 6>&1 } | Should -Throw 'Cannot invoke method. Method invocation is supported only on core types in this language mode.'; { $Null = $testObject | Assert-PSRule -Path $ruleFilePath -Name 'ConstrainedTest3' -Option $option -ErrorAction Stop 6>&1 } | Should -Throw 'Cannot invoke method. Method invocation is supported only on core types in this language mode.'; - - $bindFn = { - param ($TargetObject) - $Null = [Console]::WriteLine('Should fail'); - return 'BadName'; - } - - $option = New-PSRuleOption -Option @{ 'execution.mode' = 'ConstrainedLanguage' } -BindTargetName $bindFn; - { $Null = $testObject | Assert-PSRule -Path $ruleFilePath -Name 'ConstrainedTest1' -Option $option -ErrorAction Stop 6>&1 } | Should -Throw 'Exception calling "Assert" with "3" argument(s): "Binding functions are not supported in this language mode."'; } } } @@ -2814,12 +2787,6 @@ Describe 'Binding' -Tag Common, Binding { [PSCustomObject]@{ TargetName = 'TargetName' } - [PSCustomObject]@{ - OtherName = 'OtherName' - resourceName = 'ResourceName' - AlternateName = 'AlternateName' - TargetName = 'TargetName' - } [PSCustomObject]@{ Metadata = @{ Name = 'MetadataName' @@ -2827,26 +2794,16 @@ Describe 'Binding' -Tag Common, Binding { } ) - $bindFn = { - param ($TargetObject) - $otherName = $TargetObject.PSObject.Properties['OtherName']; - if ($otherName -eq $Null) { - return $Null - } - return $otherName.Value; - } - - $option = New-PSRuleOption -Option @{ 'Binding.TargetName' = 'ResourceName', 'AlternateName', 'Metadata.Name'; 'Binding.IgnoreCase' = $True } -BindTargetName $bindFn; + $option = New-PSRuleOption -Option @{ 'Binding.TargetName' = 'ResourceName', 'AlternateName', 'Metadata.Name'; 'Binding.IgnoreCase' = $True }; $result = $testObject | Invoke-PSRule -Option $option -Path $ruleFilePath -Name 'FromFile1'; $result | Should -Not -BeNullOrEmpty; - $result.Count | Should -Be 5; + $result.Count | Should -Be 4; $result[0].TargetName | Should -Be 'ResourceName'; $result[1].TargetName | Should -Be 'AlternateName'; $result[2].TargetName | Should -Be 'TargetName'; - $result[3].TargetName | Should -Be 'OtherName'; - $result[4].TargetName | Should -Be 'MetadataName'; + $result[3].TargetName | Should -Be 'MetadataName'; - $option = New-PSRuleOption -Option @{ 'Binding.TargetName' = 'ResourceName', 'AlternateName'; 'Binding.IgnoreCase' = $False } -BindTargetName $bindFn; + $option = New-PSRuleOption -Option @{ 'Binding.TargetName' = 'ResourceName', 'AlternateName'; 'Binding.IgnoreCase' = $False }; $result = $testObject[0..1] | Invoke-PSRule -Option $option -Path $ruleFilePath -Name 'FromFile1'; $result | Should -Not -BeNullOrEmpty; $result.Count | Should -Be 2; @@ -2919,25 +2876,6 @@ Describe 'Binding' -Tag Common, Binding { $result | Should -Not -BeNullOrEmpty; $result.TargetType | Should -Be 'TestType'; } - - It 'Binds to custom type by script' { - $bindFn = { - param ($TargetObject) - - $otherType = $TargetObject.PSObject.Properties['OtherType']; - - if ($otherType -eq $Null) { - return $Null - } - - return $otherType.Value; - } - - $option = New-PSRuleOption -Option @{ 'Binding.TargetType' = 'kind' } -BindTargetType $bindFn; - $result = $testObject | Invoke-PSRule -Option $option -Path $ruleFilePath -Name 'FromFile1'; - $result | Should -Not -BeNullOrEmpty; - $result.TargetType | Should -Be 'OtherType'; - } } }