Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/nuget/Microsoft.NET.Test.Sdk-17.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Jun 6, 2023
2 parents 3f6f2c7 + 54bfb2d commit 5336e8a
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 52 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ What's changed since pre-release v2.9.0-B0033:
[#1540](https://github.com/microsoft/PSRule/pull/1540)
- Bump Microsoft.CodeAnalysis.Common to v4.6.0.
[#1534](https://github.com/microsoft/PSRule/pull/1534)
- Bug fixes:
- Fixed include local not automatically being enabled for default module baseline by @BernieWhite.
[#1506](https://github.com/microsoft/PSRule/issues/1506)

## v2.9.0-B0033 (pre-release)

Expand Down
48 changes: 24 additions & 24 deletions src/PSRule/Definitions/Rules/RuleFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace PSRule.Definitions.Rules
/// </summary>
internal sealed class RuleFilter : IResourceFilter
{
private readonly string[] _Include;
private readonly string[] _Excluded;
private readonly Hashtable _Tag;
private readonly ResourceLabels _Labels;
private readonly bool _IncludeLocal;
private readonly WildcardPattern _WildcardMatch;
internal readonly string[] Include;
internal readonly string[] Excluded;
internal readonly Hashtable Tag;
internal readonly ResourceLabels Labels;
internal readonly bool IncludeLocal;
internal readonly WildcardPattern WildcardMatch;

/// <summary>
/// Filter rules by id or tag.
Expand All @@ -32,19 +32,19 @@ internal sealed class RuleFilter : IResourceFilter
/// <param name="labels">Only accept rules that have these labels.</param>
public RuleFilter(string[] include, Hashtable tag, string[] exclude, bool? includeLocal, ResourceLabels labels)
{
_Include = include == null || include.Length == 0 ? null : include;
_Excluded = exclude == null || exclude.Length == 0 ? null : exclude;
_Tag = tag ?? null;
_Labels = labels ?? null;
_IncludeLocal = includeLocal ?? RuleOption.Default.IncludeLocal.Value;
_WildcardMatch = null;
Include = include == null || include.Length == 0 ? null : include;
Excluded = exclude == null || exclude.Length == 0 ? null : exclude;
Tag = tag ?? null;
Labels = labels ?? null;
IncludeLocal = includeLocal ?? RuleOption.Default.IncludeLocal.Value;
WildcardMatch = null;

if (include != null && include.Length > 0 && WildcardPattern.ContainsWildcardCharacters(include[0]))
{
if (include.Length > 1)
throw new NotSupportedException(PSRuleResources.MatchSingleName);

_WildcardMatch = new WildcardPattern(include[0]);
WildcardMatch = new WildcardPattern(include[0]);
}
}

Expand All @@ -63,17 +63,17 @@ internal bool Match(string name, ResourceTags tag, ResourceLabels labels)
public bool Match(IResource resource)
{
var ids = resource.GetIds();
return !IsExcluded(ids) && (_IncludeLocal && resource.IsLocalScope() || IsIncluded(ids, resource.Tags, resource.Labels));
return !IsExcluded(ids) && (IncludeLocal && resource.IsLocalScope() || IsIncluded(ids, resource.Tags, resource.Labels));
}

private bool IsExcluded(IEnumerable<ResourceId> ids)
{
if (_Excluded == null)
if (Excluded == null)
return false;

foreach (var id in ids)
{
if (Contains(id, _Excluded))
if (Contains(id, Excluded))
return true;
}
return false;
Expand All @@ -83,21 +83,21 @@ private bool IsIncluded(IEnumerable<ResourceId> ids, ResourceTags tag, ResourceL
{
foreach (var id in ids)
{
if (_Include == null || Contains(id, _Include) || MatchWildcard(id.Name))
if (Include == null || Contains(id, Include) || MatchWildcard(id.Name))
return TagEquals(tag) && LabelEquals(labels);
}
return false;
}

private bool TagEquals(ResourceTags tag)
{
if (_Tag == null)
if (Tag == null)
return true;

if (tag == null || _Tag.Count > tag.Count)
if (tag == null || Tag.Count > tag.Count)
return false;

foreach (DictionaryEntry entry in _Tag)
foreach (DictionaryEntry entry in Tag)
{
if (!tag.Contains(entry.Key, entry.Value))
return false;
Expand All @@ -107,13 +107,13 @@ private bool TagEquals(ResourceTags tag)

private bool LabelEquals(ResourceLabels labels)
{
if (_Labels == null)
if (Labels == null)
return true;

if (labels == null || _Labels.Count > labels.Count)
if (labels == null || Labels.Count > labels.Count)
return false;

foreach (var taxon in _Labels)
foreach (var taxon in Labels)
{
if (!labels.Contains(taxon.Key, taxon.Value))
return false;
Expand All @@ -133,7 +133,7 @@ private static bool Contains(ResourceId id, string[] set)

private bool MatchWildcard(string name)
{
return _WildcardMatch != null && _WildcardMatch.IsMatch(name);
return WildcardMatch != null && WildcardMatch.IsMatch(name);
}
}
}
9 changes: 3 additions & 6 deletions src/PSRule/PSRule.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ function Get-PSRuleHelp {
[Parameter(Position = 0, Mandatory = $False)]
[Alias('n')]
[SupportsWildcards()]
[String]$Name,
[String[]]$Name,

# A path to check documentation for.
[Parameter(Mandatory = $False)]
Expand Down Expand Up @@ -1053,16 +1053,13 @@ function Get-PSRuleHelp {
if ($isDeviceGuard) {
$Option.Execution.LanguageMode = [PSRule.Configuration.LanguageMode]::ConstrainedLanguage;
}
if ($PSBoundParameters.ContainsKey('Name')) {
$Option.Rule.Include = $Name;
}
if ($PSBoundParameters.ContainsKey('Culture')) {
$Option.Output.Culture = $Culture;
}

$hostContext = [PSRule.Pipeline.PSHostContext]::new($PSCmdlet, $ExecutionContext);
$builder = [PSRule.Pipeline.PipelineBuilder]::GetHelp($sourceFiles, $Option, $hostContext);;

$builder = [PSRule.Pipeline.PipelineBuilder]::GetHelp($sourceFiles, $Option, $hostContext);
$builder.Name($Name);
if ($Online) {
$builder.Online();
}
Expand Down
5 changes: 5 additions & 0 deletions src/PSRule/Pipeline/GetRuleHelpPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public interface IHelpPipelineBuilder : IPipelineBuilder
/// Open or show online help for a rule if it exists.
/// </summary>
void Online();

/// <summary>
/// Filter by name.
/// </summary>
void Name(string[] name);
}

internal sealed class GetRuleHelpPipelineBuilder : PipelineBuilderBase, IHelpPipelineBuilder
Expand Down
48 changes: 27 additions & 21 deletions src/PSRule/Pipeline/OptionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ internal sealed class OptionContext
private readonly List<string> _ConventionOrder;
private readonly string[] _DefaultCulture;

/// <summary>
/// Used when options are passed in by command line parameters.
/// </summary>
private BaselineScope _Parameter;

/// <summary>
/// Used when a baseline is explictly set by name.
/// </summary>
private BaselineScope _Explicit;

private BaselineScope _WorkspaceBaseline;
Expand All @@ -33,12 +40,6 @@ internal sealed class OptionContext
private BaselineScope _ModuleBaseline;
private ConfigScope _ModuleConfig;

//private BindingOption _Binding;
//private RuleFilter _Filter;
//private Dictionary<string, object> _Configuration;
//private string[] _Culture;
//private ConventionFilter _ConventionFilter;

internal OptionContext()
{
_ModuleBaselineScope = new Dictionary<string, BaselineScope>();
Expand All @@ -51,9 +52,24 @@ internal OptionContext()

internal enum ScopeType
{
/// <summary>
/// Used when options are passed in by command line parameters.
/// </summary>
Parameter = 0,

/// <summary>
/// Used when a baseline is explictly set by name.
/// </summary>
Explicit = 1,

/// <summary>
/// Used when options are set within the PSRule options from the workspace or an options object.
/// </summary>
Workspace = 2,

/// <summary>
/// Used for options that are inherited from module configuration.
/// </summary>
Module = 3
}

Expand Down Expand Up @@ -358,31 +374,24 @@ private void ResetScope(string moduleName)
{
_ModuleConfig = !string.IsNullOrEmpty(moduleName) && _ModuleConfigScope.TryGetValue(moduleName, out var configScope) ? configScope : null;
_ModuleBaseline = !string.IsNullOrEmpty(moduleName) && _ModuleBaselineScope.TryGetValue(moduleName, out var baselineScope) ? baselineScope : null;
//_Binding = null;
//_Configuration = null;
//_Filter = null;
//_Culture = null;
//_ConventionFilter = null;
}

private IResourceFilter GetRuleFilter()
{
// if (_Filter != null)
// return _Filter;

var include = _Parameter?.Include ?? _Explicit?.Include ?? _WorkspaceBaseline?.Include ?? _ModuleBaseline?.Include;
var exclude = _Explicit?.Exclude ?? _WorkspaceBaseline?.Exclude ?? _ModuleBaseline?.Exclude;
var tag = _Parameter?.Tag ?? _Explicit?.Tag ?? _WorkspaceBaseline?.Tag ?? _ModuleBaseline?.Tag;
var labels = _Parameter?.Labels ?? _Explicit?.Labels ?? _WorkspaceBaseline?.Labels ?? _ModuleBaseline?.Labels;
var includeLocal = _Explicit?.IncludeLocal ?? _WorkspaceBaseline?.IncludeLocal ?? _ModuleBaseline?.IncludeLocal;
var includeLocal = _Explicit == null &&
_Parameter?.Include == null &&
_Parameter?.Tag == null &&
_Parameter?.Labels == null &&
(_WorkspaceBaseline == null || !_WorkspaceBaseline.IncludeLocal.HasValue) ? true : _WorkspaceBaseline?.IncludeLocal;
return new RuleFilter(include, tag, exclude, includeLocal, labels);
}

private IResourceFilter GetConventionFilter()
{
// if (_ConventionFilter != null)
// return _ConventionFilter;

var include = new List<string>();
for (var i = 0; _Parameter?.Convention?.Include != null && i < _Parameter.Convention.Include.Length; i++)
include.Add(_Parameter.Convention.Include[i]);
Expand All @@ -398,9 +407,6 @@ private IResourceFilter GetConventionFilter()

private IBindingOption GetTargetBinding()
{
// if (_Binding != null)
// return _Binding;

var field = new FieldMap[] { _Explicit?.Field, _WorkspaceBaseline?.Field, _ModuleBaseline?.Field, _ModuleConfig?.Field };
var ignoreCase = _Explicit?.IgnoreCase ?? _WorkspaceBaseline?.IgnoreCase ?? _ModuleBaseline?.IgnoreCase ?? _ModuleConfig?.IgnoreCase ?? Configuration.BindingOption.Default.IgnoreCase.Value;
var nameSeparator = _Explicit?.NameSeparator ?? _WorkspaceBaseline?.NameSeparator ?? _ModuleBaseline?.NameSeparator ?? _ModuleConfig?.NameSeparator ?? Configuration.BindingOption.Default.NameSeparator;
Expand Down
Loading

0 comments on commit 5336e8a

Please sign in to comment.