Skip to content
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

Make Measure-Object ignore missing properties unless running in strict mode #16589

Merged
merged 4 commits into from
Jan 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,9 @@ private void WritePropertyNotFoundError(string propertyName, string errorId)
{
Diagnostics.Assert(Property != null, "no property and no InputObject should have been addressed");
ErrorRecord errorRecord = new(
PSTraceSource.NewArgumentException("Property"),
PSTraceSource.NewArgumentException(propertyName),
errorId,
ErrorCategory.InvalidArgument,
ErrorCategory.ObjectNotFound,
null);
errorRecord.ErrorDetails = new ErrorDetails(
this, "MeasureObjectStrings", "PropertyNotFound", propertyName);
Expand All @@ -820,9 +820,12 @@ protected override void EndProcessing()
Statistics stat = _statistics[propertyName];
if (stat.count == 0 && Property != null)
{
// Why are there two different ids for this error?
string errorId = (IsMeasuringGeneric) ? "GenericMeasurePropertyNotFound" : "TextMeasurePropertyNotFound";
WritePropertyNotFoundError(propertyName, errorId);
if (Context.IsStrictVersion(2))
{
string errorId = (IsMeasuringGeneric) ? "GenericMeasurePropertyNotFound" : "TextMeasurePropertyNotFound";
WritePropertyNotFoundError(propertyName, errorId);
}

continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ Describe "Measure-Object" -Tags "CI" {
}
}

Context "Empty folder tests" {
BeforeAll {
$repoPath = Join-Path -Path $TESTDRIVE -ChildPath "my_folder"
$testEmptyFolder = New-Item $repoPath -ItemType "directory"
$propertyName = "Length"
}

AfterAll {
Set-StrictMode -off
}

It "Should not throw an invalid property error when not in StrictMode" {
{ Set-StrictMode -off; Get-Item $testEmptyFolder | Measure-Object $propertyName -ErrorAction Stop -sum } | Should -Not -Throw
}

It "Should throw an invalid property error when in StrictMode" {
{ Set-StrictMode -version 3.0; Get-Item $testEmptyFolder | Measure-Object $propertyName -ErrorAction Stop -sum } | Should -Throw -ErrorId 'GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand'
}
}

Context "String tests" {
BeforeAll {
$nl = [Environment]::NewLine
Expand Down