From 2b2c4f3c6bd48d1e7c73753aeffa678b73c1105d Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 30 Jan 2017 13:57:58 +0300 Subject: [PATCH 1/2] Add ShouldProcess to New-FileCatalog and Test-FileCatalog Close #3068 Add support `-WhatIf` and `-Confirm` to `New-FileCatalog` and add a test. `Test-FileCatalog` has a common code base with `New-FileCatalog` so it automatically get the same. I believe that adding a separate test in this case doesn't make sense. --- .../security/CatalogCommands.cs | 25 +++++++++++++------ .../FileCatalog.Tests.ps1 | 19 ++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs b/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs index e894ec0119b..8de0aa95dbb 100644 --- a/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs +++ b/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs @@ -89,26 +89,35 @@ protected override void ProcessRecord() Collection paths = new Collection(); + bool _ShouldProcess = false; + if (Path != null) { foreach (string p in Path) { foreach (PathInfo tempPath in SessionState.Path.GetResolvedPSPathFromPSPath(p)) { - paths.Add(tempPath.ProviderPath); + if (ShouldProcess(tempPath.ProviderPath)) + { + paths.Add(tempPath.ProviderPath); + _ShouldProcess = true; + } } } } - string drive = null; - - // resolve catalog destination Path - if (!SessionState.Path.IsPSAbsolute(catalogFilePath, out drive) && !System.IO.Path.IsPathRooted(catalogFilePath)) + if (_ShouldProcess) { - catalogFilePath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(catalogFilePath); - } + string drive = null; - PerformAction(paths, catalogFilePath); + // resolve catalog destination Path + if (!SessionState.Path.IsPSAbsolute(catalogFilePath, out drive) && !System.IO.Path.IsPathRooted(catalogFilePath)) + { + catalogFilePath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(catalogFilePath); + } + + PerformAction(paths, catalogFilePath); + } } /// diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 index f89d32f8dae..38acb5f8638 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 @@ -42,6 +42,25 @@ Describe "Test suite for NewFileCatalogAndTestFileCatalogCmdlets" -Tags "CI" { Context "NewAndTestCatalogTests PositiveTestCases when validation Succeeds" { + It "NewFileCatalogWithSingleFile with WhatIf" { + + $sourcePath = Join-Path $testDataPath '\CatalogTestFile1.mof' + # use existant Path for the directory when .cat file name is not specified + $catalogPath = $testDataPath + try + { + $null = New-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath -WhatIf + $result = Test-Path -Path ($catalogPath + "\catalog.cat") + } + finally + { + Remove-Item "$catalogPath\catalog.cat" -Force -ErrorAction SilentlyContinue + } + + # Validate result properties + $result | Should Be $false + } + It "NewFileCatalogFolder" { $sourcePath = Join-Path $testDataPath 'UserConfigProv\DSCResources\scriptdsc' From b7a02e63c046f1ca746e3681d1972dfecf3e67d5 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 16 Feb 2017 15:35:40 +0300 Subject: [PATCH 2/2] Fiz after code review Remove _ShouldProcess Add var in test --- .../security/CatalogCommands.cs | 6 ++---- .../Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 | 6 ++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs b/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs index 8de0aa95dbb..aa6c222fdf6 100644 --- a/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs +++ b/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs @@ -89,8 +89,6 @@ protected override void ProcessRecord() Collection paths = new Collection(); - bool _ShouldProcess = false; - if (Path != null) { foreach (string p in Path) @@ -100,13 +98,13 @@ protected override void ProcessRecord() if (ShouldProcess(tempPath.ProviderPath)) { paths.Add(tempPath.ProviderPath); - _ShouldProcess = true; } } } } - if (_ShouldProcess) + // We add 'paths.Count > 0' to support 'ShouldProcess()' + if (paths.Count > 0 ) { string drive = null; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 index 38acb5f8638..daa91ddb55a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 @@ -47,14 +47,16 @@ Describe "Test suite for NewFileCatalogAndTestFileCatalogCmdlets" -Tags "CI" { $sourcePath = Join-Path $testDataPath '\CatalogTestFile1.mof' # use existant Path for the directory when .cat file name is not specified $catalogPath = $testDataPath + $catalogFile = $catalogPath + "\catalog.cat" + try { $null = New-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath -WhatIf - $result = Test-Path -Path ($catalogPath + "\catalog.cat") + $result = Test-Path -Path $catalogFile } finally { - Remove-Item "$catalogPath\catalog.cat" -Force -ErrorAction SilentlyContinue + Remove-Item $catalogFile -Force -ErrorAction SilentlyContinue } # Validate result properties