From c658a8be89a85235c80909631db0c2896a039c78 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 12 Apr 2024 14:44:19 +0200 Subject: [PATCH 1/2] allow new moduleV3 on existing folders --- .../New-ModuleV3-Add-ModuleV3.Tests.ps1 | 24 +++++++++++++++++++ private/New-ModuleV3.Helper.ps1 | 18 +++++++++----- public/Add-ModuleV3.ps1 | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/TestingHelperTest/public/New-ModuleV3-Add-ModuleV3.Tests.ps1 b/TestingHelperTest/public/New-ModuleV3-Add-ModuleV3.Tests.ps1 index bd87b8b..a29c865 100644 --- a/TestingHelperTest/public/New-ModuleV3-Add-ModuleV3.Tests.ps1 +++ b/TestingHelperTest/public/New-ModuleV3-Add-ModuleV3.Tests.ps1 @@ -37,6 +37,30 @@ function TestingHelperTest_NewModuleV3_AddModule_FailCall_NewModuleManifest { } +function TestingHelperTest_NewModuleV3_AddModule_FolderExists { + + $moduleName = "MyModule" + + New-TestingFolder -Name $moduleName + + $result = Add-TT_ModuleV3 -Name $moduleName + + Assert-AddModuleV3 -Path $result +} + + +function TestingHelperTest_NewModuleV3_AddModule_ModuleExistInFolder { + + $moduleName = "MyModule" + + $result = Add-TT_ModuleV3 -Name $moduleName + + $result = Add-TT_ModuleV3 -Name $moduleName @ErrorParameters + + Assert-IsNull -Object $result + Assert-Contains -Expected "Module already exists." -Presented ($errorVar.Exception.Message) +} + function TestingHelperTest_NewModuleV3_AddModule_DefaultManifest { $moduleName = "MyModule" diff --git a/private/New-ModuleV3.Helper.ps1 b/private/New-ModuleV3.Helper.ps1 index ed46f05..173e3b8 100644 --- a/private/New-ModuleV3.Helper.ps1 +++ b/private/New-ModuleV3.Helper.ps1 @@ -61,7 +61,7 @@ function Get-TestModuleName { return $name } -function New-Folder{ +function New-ModuleFolder{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,ValueFromPipeline)][string]$Path @@ -71,17 +71,23 @@ function New-Folder{ try { #test if path exists - if($Path | Test-Path){ - Write-Error "Path already exists." - return $null - } else { + if(!($Path | Test-Path)){ if ($PSCmdlet.ShouldProcess($Path, "New-Item -ItemType Directory")) { $null = New-Item -ItemType Directory -Path $Path } - # Converting to Provider path return $Path | Convert-Path + } + + # Folder exists. Check if the psd1 file exists + $psd1Path = ($Path | Join-Path -ChildPath "$($Path | Split-Path -LeafBase).psd1") + if($psd1Path | Test-Path){ + Write-Error -Message "Module already exists." + return $null } + + return $Path | Convert-Path + } catch { Write-Error -Message "Failed to add path." diff --git a/public/Add-ModuleV3.ps1 b/public/Add-ModuleV3.ps1 index ce64dae..4121cc7 100644 --- a/public/Add-ModuleV3.ps1 +++ b/public/Add-ModuleV3.ps1 @@ -22,7 +22,7 @@ function Add-ModuleV3 { # Create the module folder. Fail if exists # This will filter if Path already exist to avoid overwriting an existing module - $modulePath = $modulePathString | New-Folder + $modulePath = $modulePathString | New-ModuleFolder if( !$modulePath ){ return $null } $moduleName = Get-ModuleName -Path $modulePath From 3356f9c64f7dd6a5e8d0e476fcbab35cdc7492d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Fri, 12 Apr 2024 14:56:08 +0200 Subject: [PATCH 2/2] smells --- private/New-ModuleV3.Helper.ps1 | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/private/New-ModuleV3.Helper.ps1 b/private/New-ModuleV3.Helper.ps1 index 173e3b8..679ce15 100644 --- a/private/New-ModuleV3.Helper.ps1 +++ b/private/New-ModuleV3.Helper.ps1 @@ -77,8 +77,8 @@ function New-ModuleFolder{ } # Converting to Provider path return $Path | Convert-Path - } - + } + # Folder exists. Check if the psd1 file exists $psd1Path = ($Path | Join-Path -ChildPath "$($Path | Split-Path -LeafBase).psd1") if($psd1Path | Test-Path){ @@ -88,12 +88,11 @@ function New-ModuleFolder{ return $Path | Convert-Path - } + } catch { Write-Error -Message "Failed to add path." return $null } } - }