Skip to content

Commit

Permalink
Fixed piped input with ignored paths #1114 (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Jun 18, 2022
1 parent f2067c9 commit ceec946
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ The following conceptual topics exist in the `PSRule` module:
- [Include.Path](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#includepath)
- [Input.Format](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputformat)
- [Input.IgnoreGitPath](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignoregitpath)
- [Input.IgnoreObjectSource](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignoreobjectsource)
- [Input.IgnoreRepositoryCommon](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignorerepositorycommon)
- [Input.ObjectPath](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputobjectpath)
- [Input.PathIgnore](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputpathignore)
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ What's changed since pre-release v2.2.0-B0021:
- Bug fixes:
- Fixed output of reason with wide format by @BernieWhite.
[#1117](https://github.com/microsoft/PSRule/issues/1117)
- Fixed piped input does not respect excluded paths by @BernieWhite.
[#1114](https://github.com/microsoft/PSRule/issues/1114)

## v2.2.0-B0021 (pre-release)

Expand Down
54 changes: 54 additions & 0 deletions docs/concepts/PSRule/en-US/about_PSRule_Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The following workspace options are available for use:
- [Include.Path](#includepath)
- [Input.Format](#inputformat)
- [Input.IgnoreGitPath](#inputignoregitpath)
- [Input.IgnoreObjectSource](#inputignoreobjectsource)
- [Input.IgnoreRepositoryCommon](#inputignorerepositorycommon)
- [Input.ObjectPath](#inputobjectpath)
- [Input.PathIgnore](#inputpathignore)
Expand Down Expand Up @@ -1208,6 +1209,55 @@ variables:
value: false
```

### Input.IgnoreObjectSource

By default, objects read from file using `inputPath` will be skipped if the file path has been ignored.
When set to true, additionally objects with a source path that has been ignored will be skipped.
This will include `FileInfo` objects, and objects with a source set using the `_PSRule.source` property.

File paths to ignore are set by `Input.PathIgnore`, `Input.IgnoreGitPath`, and `Input.IgnoreRepositoryCommon`.

This option can be specified using:

```powershell
# PowerShell: Using the InputIgnoreObjectSource parameter
$option = New-PSRuleOption -InputIgnoreObjectSource $True;
```

```powershell
# PowerShell: Using the Input.IgnoreObjectSource hashtable key
$option = New-PSRuleOption -Option @{ 'Input.IgnoreObjectSource' = $True };
```

```powershell
# PowerShell: Using the InputIgnoreObjectSource parameter to set YAML
Set-PSRuleOption -InputIgnoreObjectSource $True;
```

```yaml
# YAML: Using the input/ignoreObjectSource property
input:
ignoreObjectSource: true
```

```bash
# Bash: Using environment variable
export PSRULE_INPUT_IGNOREOBJECTSOURCE=true
```

```yaml
# GitHub Actions: Using environment variable
env:
PSRULE_INPUT_IGNOREOBJECTSOURCE: true
```

```yaml
# Azure Pipelines: Using environment variable
variables:
- name: PSRULE_INPUT_IGNOREOBJECTSOURCE
value: true
```

### Input.IgnoreRepositoryCommon

When reading files from an input path, files are discovered recursively.
Expand Down Expand Up @@ -2638,6 +2688,8 @@ include:
input:
format: Yaml
ignoreGitPath: false
ignoreObjectSource: true
ignoreRepositoryCommon: false
objectPath: items
pathIgnore:
- '*.Designer.cs'
Expand Down Expand Up @@ -2743,6 +2795,8 @@ include:
input:
format: Detect
ignoreGitPath: true
ignoreObjectSource: false
ignoreRepositoryCommon: true
objectPath: null
pathIgnore: [ ]
targetType: [ ]
Expand Down
20 changes: 14 additions & 6 deletions schemas/PSRule-options.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"type": "object",
"title": "Input options",
"description": "Options that affect how input types are processed.",
"markdownDescription": "Options that affect how input types are processed.",
"properties": {
"format": {
"type": "string",
Expand All @@ -218,17 +219,24 @@
"ignoreGitPath": {
"type": "boolean",
"title": "Ignore .git path",
"description": "Determine if files within the .git path are ignored when the -InputPath parameter is used.",
"markdownDescription": "Determine if files within the `.git` path are ignored when the `-InputPath` parameter is used. [See help](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignoregitpath)",
"description": "Determine if files within the .git path are ignored when the -InputPath parameter is used. This is enabled by default.",
"markdownDescription": "Determine if files within the `.git` path are ignored when the `-InputPath` parameter is used. This is enabled by default. [See help](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignoregitpath)",
"default": true
},
"ignoreRepositoryCommon": {
"type": "boolean",
"title": "Ignore common files",
"description": "Determine if common repository files are ignored when the -InputPath parameter is used.",
"markdownDescription": "Determine if common repository files are ignored when the `-InputPath` parameter is used. [See help](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignorerepositorycommon)",
"description": "Determine if common repository files are ignored when the -InputPath parameter is used. This is enabled by default.",
"markdownDescription": "Determine if common repository files are ignored when the `-InputPath` parameter is used. This is enabled by default. [See help](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignorerepositorycommon)",
"default": true
},
"ignoreObjectSource": {
"type": "boolean",
"title": "Ignore object source",
"description": "Determines if objects are ignore based on their file source path. When set, objects from the pipeline or read from files will be exclude based on their source path and the configuration of pathIgnore, ignoreGitPath, and ignoreRepositoryCommon options.",
"markdownDescription": "Determines if objects are ignore based on their file source path. When set, objects from the pipeline or read from files will be exclude based on their source path and the configuration of `pathIgnore`, `ignoreGitPath`, and `ignoreRepositoryCommon` options. [See help](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputignoreobjectsource)",
"default": false
},
"objectPath": {
"type": "string",
"title": "Object path",
Expand All @@ -238,8 +246,8 @@
"pathIgnore": {
"type": "array",
"title": "Path ignore",
"description": "Ignores input files that match the path spec.",
"markdownDescription": "Ignores input files that match the path spec. [See help](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputpathignore)",
"description": "Exclude input files that match the path spec. To exclude a file specify the path prefix or file name. To re-include previously excluded paths use ! at the start of the entry. Wildcards are supported.",
"markdownDescription": "Exclude input files that match the path spec. To exclude a file specify the path prefix or file name. To re-include previously excluded paths use `!` at the start of the entry. Wildcards are supported. [See help](https://microsoft.github.io/PSRule/v2/concepts/PSRule/en-US/about_PSRule_Options/#inputpathignore)",
"items": {
"type": "string"
},
Expand Down
16 changes: 16 additions & 0 deletions src/PSRule/Common/PSObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Threading;
using Newtonsoft.Json;
Expand Down Expand Up @@ -138,6 +139,21 @@ public static void ConvertTargetInfoProperty(this PSObject o)
}
}

public static void ConvertTargetInfoType(this PSObject o)
{
var info = o?.BaseObject;
if (info is FileInfo fileInfo)
{
UseTargetInfo(o, out var targetInfo);
targetInfo.WithSource(new TargetSourceInfo(fileInfo));
}
if (info is InputFileInfo inputFileInfo)
{
UseTargetInfo(o, out var targetInfo);
targetInfo.WithSource(new TargetSourceInfo(inputFileInfo));
}
}

private static T ConvertValue<T>(object value)
{
if (value == null)
Expand Down
23 changes: 21 additions & 2 deletions src/PSRule/Configuration/InputOption.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand All @@ -12,8 +12,9 @@ namespace PSRule.Configuration
/// </summary>
public sealed class InputOption : IEquatable<InputOption>
{
private const InputFormat DEFAULT_FORMAT = PSRule.Configuration.InputFormat.Detect;
private const InputFormat DEFAULT_FORMAT = InputFormat.Detect;
private const bool DEFAULT_IGNOREGITPATH = true;
private const bool DEFAULT_IGNOREOBJECTSOURCE = false;
private const bool DEFAULT_IGNOREREPOSITORYCOMMON = true;
private const string DEFAULT_OBJECTPATH = null;
private const string[] DEFAULT_PATHIGNORE = null;
Expand All @@ -23,6 +24,7 @@ public sealed class InputOption : IEquatable<InputOption>
{
Format = DEFAULT_FORMAT,
IgnoreGitPath = DEFAULT_IGNOREGITPATH,
IgnoreObjectSource = DEFAULT_IGNOREOBJECTSOURCE,
IgnoreRepositoryCommon = DEFAULT_IGNOREREPOSITORYCOMMON,
ObjectPath = DEFAULT_OBJECTPATH,
PathIgnore = DEFAULT_PATHIGNORE,
Expand All @@ -33,6 +35,7 @@ public InputOption()
{
Format = null;
IgnoreGitPath = null;
IgnoreObjectSource = null;
IgnoreRepositoryCommon = null;
ObjectPath = null;
PathIgnore = null;
Expand All @@ -46,6 +49,7 @@ public InputOption(InputOption option)

Format = option.Format;
IgnoreGitPath = option.IgnoreGitPath;
IgnoreObjectSource = option.IgnoreObjectSource;
IgnoreRepositoryCommon = option.IgnoreRepositoryCommon;
ObjectPath = option.ObjectPath;
PathIgnore = option.PathIgnore;
Expand All @@ -62,6 +66,7 @@ public bool Equals(InputOption other)
return other != null &&
Format == other.Format &&
IgnoreGitPath == other.IgnoreGitPath &&
IgnoreObjectSource == other.IgnoreObjectSource &&
IgnoreRepositoryCommon == other.IgnoreRepositoryCommon &&
ObjectPath == other.ObjectPath &&
PathIgnore == other.PathIgnore &&
Expand All @@ -75,6 +80,7 @@ public override int GetHashCode()
var hash = 17;
hash = hash * 23 + (Format.HasValue ? Format.Value.GetHashCode() : 0);
hash = hash * 23 + (IgnoreGitPath.HasValue ? IgnoreGitPath.Value.GetHashCode() : 0);
hash = hash * 23 + (IgnoreObjectSource.HasValue ? IgnoreObjectSource.Value.GetHashCode() : 0);
hash = hash * 23 + (IgnoreRepositoryCommon.HasValue ? IgnoreRepositoryCommon.Value.GetHashCode() : 0);
hash = hash * 23 + (ObjectPath != null ? ObjectPath.GetHashCode() : 0);
hash = hash * 23 + (PathIgnore != null ? PathIgnore.GetHashCode() : 0);
Expand All @@ -89,6 +95,7 @@ internal static InputOption Combine(InputOption o1, InputOption o2)
{
Format = o1.Format ?? o2.Format,
IgnoreGitPath = o1.IgnoreGitPath ?? o2.IgnoreGitPath,
IgnoreObjectSource = o1.IgnoreObjectSource ?? o2.IgnoreObjectSource,
IgnoreRepositoryCommon = o1.IgnoreRepositoryCommon ?? o2.IgnoreRepositoryCommon,
ObjectPath = o1.ObjectPath ?? o2.ObjectPath,
PathIgnore = o1.PathIgnore ?? o2.PathIgnore,
Expand All @@ -109,6 +116,12 @@ internal static InputOption Combine(InputOption o1, InputOption o2)
[DefaultValue(null)]
public bool? IgnoreGitPath { get; set; }

/// <summary>
/// Determines if objects are ignore based on their file source path.
/// </summary>
[DefaultValue(null)]
public bool? IgnoreObjectSource { get; set; }

/// <summary>
/// Determine if common repository files are ignored.
/// </summary>
Expand Down Expand Up @@ -141,6 +154,9 @@ internal void Load(EnvironmentHelper env)
if (env.TryBool("PSRULE_INPUT_IGNOREGITPATH", out var ignoreGitPath))
IgnoreGitPath = ignoreGitPath;

if (env.TryBool("PSRULE_INPUT_IGNOREOBJECTSOURCE", out var ignoreObjectSource))
IgnoreObjectSource = ignoreObjectSource;

if (env.TryBool("PSRULE_INPUT_IGNOREREPOSITORYCOMMON", out var ignoreRepositoryCommon))
IgnoreRepositoryCommon = ignoreRepositoryCommon;

Expand All @@ -162,6 +178,9 @@ internal void Load(Dictionary<string, object> index)
if (index.TryPopBool("Input.IgnoreGitPath", out var ignoreGitPath))
IgnoreGitPath = ignoreGitPath;

if (index.TryPopBool("Input.IgnoreObjectSource", out var ignoreObjectSource))
IgnoreObjectSource = ignoreObjectSource;

if (index.TryPopBool("Input.IgnoreRepositoryCommon", out var ignoreRepositoryCommon))
IgnoreRepositoryCommon = ignoreRepositoryCommon;

Expand Down
2 changes: 2 additions & 0 deletions src/PSRule/Data/ITargetSourceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal sealed class TargetSourceCollection : ITargetSourceCollection

internal TargetSourceCollection() { }

public int Count => _Items == null ? 0 : _Items.Count;

public TargetSourceInfo this[string type]
{
get
Expand Down
17 changes: 17 additions & 0 deletions src/PSRule/PSRule.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,10 @@ function New-PSRuleOption {
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreRepositoryCommon = $True,

# Sets the Input.IgnoreObjectSource option
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreObjectSource = $False,

# Sets the Input.ObjectPath option
[Parameter(Mandatory = $False)]
[Alias('InputObjectPath')]
Expand Down Expand Up @@ -1469,6 +1473,10 @@ function Set-PSRuleOption {
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreGitPath = $True,

# Sets the Input.IgnoreObjectSource option
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreObjectSource = $False,

# Sets the Input.IgnoreRepositoryCommon option
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreRepositoryCommon = $True,
Expand Down Expand Up @@ -2181,6 +2189,10 @@ function SetOptions {
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreGitPath = $True,

# Sets the Input.IgnoreObjectSource option
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreObjectSource = $False,

# Sets the Input.IgnoreRepositoryCommon option
[Parameter(Mandatory = $False)]
[System.Boolean]$InputIgnoreRepositoryCommon = $True,
Expand Down Expand Up @@ -2364,6 +2376,11 @@ function SetOptions {
$Option.Input.IgnoreGitPath = $InputIgnoreGitPath;
}

# Sets option Input.IgnoreObjectSource
if ($PSBoundParameters.ContainsKey('InputIgnoreObjectSource')) {
$Option.Input.IgnoreObjectSource = $InputIgnoreObjectSource;
}

# Sets option Input.IgnoreRepositoryCommon
if ($PSBoundParameters.ContainsKey('InputIgnoreRepositoryCommon')) {
$Option.Input.IgnoreRepositoryCommon = $InputIgnoreRepositoryCommon;
Expand Down
11 changes: 2 additions & 9 deletions src/PSRule/Pipeline/GetTargetPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ public void InputPath(string[] path)
if (path == null || path.Length == 0)
return;

var basePath = PSRuleOption.GetWorkingPath();
var ignoreGitPath = Option.Input.IgnoreGitPath ?? InputOption.Default.IgnoreGitPath.Value;
var ignoreRepositoryCommon = Option.Input.IgnoreRepositoryCommon ?? InputOption.Default.IgnoreRepositoryCommon.Value;
var filter = PathFilterBuilder.Create(basePath, Option.Input.PathIgnore, ignoreGitPath, ignoreRepositoryCommon);
if (Option.Input.Format == InputFormat.File)
filter.UseGitIgnore();

var builder = new InputPathBuilder(GetOutput(), basePath, "*", filter.Build());
var builder = new InputPathBuilder(GetOutput(), PSRuleOption.GetWorkingPath(), "*", GetInputFilter());
builder.Add(path);
_InputPath = builder.Build();
}
Expand Down Expand Up @@ -116,7 +109,7 @@ protected override PipelineReader PrepareReader()
return PipelineReceiverActions.DetectInputFormat(sourceObject, next);
});
}
return new PipelineReader(VisitTargetObject, _InputPath);
return new PipelineReader(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
}
}

Expand Down
11 changes: 2 additions & 9 deletions src/PSRule/Pipeline/InvokeRulePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ public void InputPath(string[] path)
if (path == null || path.Length == 0)
return;

var basePath = PSRuleOption.GetWorkingPath();
var ignoreGitPath = Option.Input.IgnoreGitPath ?? InputOption.Default.IgnoreGitPath.Value;
var ignoreRepositoryCommon = Option.Input.IgnoreRepositoryCommon ?? InputOption.Default.IgnoreRepositoryCommon.Value;
var filter = PathFilterBuilder.Create(basePath, Option.Input.PathIgnore, ignoreGitPath, ignoreRepositoryCommon);
if (Option.Input.Format == InputFormat.File)
filter.UseGitIgnore();

var builder = new InputPathBuilder(GetOutput(), basePath, "*", filter.Build());
var builder = new InputPathBuilder(GetOutput(), PSRuleOption.GetWorkingPath(), "*", GetInputFilter());
builder.Add(path);
_InputPath = builder.Build();
}
Expand Down Expand Up @@ -150,7 +143,7 @@ protected override PipelineReader PrepareReader()
return PipelineReceiverActions.DetectInputFormat(sourceObject, next);
});
}
return new PipelineReader(VisitTargetObject, _InputPath);
return new PipelineReader(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
}
}

Expand Down
Loading

0 comments on commit ceec946

Please sign in to comment.