From 36a26b0c92ab963a99a2e95bae951ccd2facdd3e Mon Sep 17 00:00:00 2001 From: Justin Chung Date: Tue, 8 Oct 2024 11:21:39 -0500 Subject: [PATCH 1/2] relative path fix --- src/code/PublishHelper.cs | 5 ++ .../CompressPSResource.Tests.ps1 | 89 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/src/code/PublishHelper.cs b/src/code/PublishHelper.cs index 8fc2575ad..ce7791a10 100644 --- a/src/code/PublishHelper.cs +++ b/src/code/PublishHelper.cs @@ -294,6 +294,11 @@ out string[] _ } } + if (_callerCmdlet == CallerCmdlet.CompressPSResource) + { + outputNupkgDir = DestinationPath; + } + // pack into .nupkg if (!PackNupkg(outputDir, outputNupkgDir, nuspec, out ErrorRecord packNupkgError)) { diff --git a/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 b/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 index cdccee7f6..c99af8660 100644 --- a/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 +++ b/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 @@ -46,6 +46,44 @@ Describe "Test Compress-PSResource" -tags 'CI' { BeforeAll { Get-NewPSResourceRepositoryFile + $testDir = (get-item $psscriptroot).parent.FullName + +function CreateTestModule +{ + param ( + [string] $Path = "$TestDrive", + [string] $ModuleName = 'TestModule' + ) + + $modulePath = Join-Path -Path $Path -ChildPath $ModuleName + $moduleMan = Join-Path $modulePath -ChildPath ($ModuleName + '.psd1') + $moduleSrc = Join-Path $modulePath -ChildPath ($ModuleName + '.psm1') + + if ( Test-Path -Path $modulePath) { + Remove-Item -Path $modulePath -Recurse -Force + } + + $null = New-Item -Path $modulePath -ItemType Directory -Force + + @' + @{{ + RootModule = "{0}.psm1" + ModuleVersion = '1.0.0' + Author = 'None' + Description = 'None' + GUID = '0c2829fc-b165-4d72-9038-ae3a71a755c1' + FunctionsToExport = @('Test1') + RequiredModules = @('NonExistentModule') + }} +'@ -f $ModuleName | Out-File -FilePath $moduleMan + + @' + function Test1 { + Write-Output 'Hello from Test1' + } +'@ | Out-File -FilePath $moduleSrc +} + # Register temporary repositories $tmpRepoPath = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath" New-Item $tmpRepoPath -Itemtype directory -Force @@ -153,6 +191,57 @@ Describe "Test Compress-PSResource" -tags 'CI' { Test-Path -Path (Join-Path -Path $unzippedPath -ChildPath $testFile) | Should -Be $True } + + It "Compresses a script" { + $scriptName = "PSGetTestScript" + $scriptVersion = "1.0.0" + + $params = @{ + Version = $scriptVersion + GUID = [guid]::NewGuid() + Author = 'Jane' + CompanyName = 'Microsoft Corporation' + Copyright = '(c) 2020 Microsoft Corporation. All rights reserved.' + Description = "Description for the $scriptName script" + LicenseUri = "https://$scriptName.com/license" + IconUri = "https://$scriptName.com/icon" + ProjectUri = "https://$scriptName.com" + Tags = @('Tag1','Tag2', "Tag-$scriptName-$scriptVersion") + ReleaseNotes = "$scriptName release notes" + } + + $scriptPath = (Join-Path -Path $script:tmpScriptsFolderPath -ChildPath "$scriptName.ps1") + New-PSScriptFileInfo @params -Path $scriptPath + + Compress-PSResource -Path $scriptPath -DestinationPath $script:repositoryPath + + $expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg" + (Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath + } + + It "Compress-PSResource -DestinationPath works for relative paths" { + $version = "1.0.0" + $relativePath = ".\RelativeTestModule" + $relativeDestination = ".\RelativeDestination" + + # Create relative paths + New-Item -Path $relativePath -ItemType Directory -Force + New-Item -Path $relativeDestination -ItemType Directory -Force + + # Create module manifest in the relative path + New-ModuleManifest -Path (Join-Path -Path $relativePath -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" + + # Compress using relative paths + Compress-PSResource -Path $relativePath -DestinationPath $relativeDestination + + $expectedPath = Join-Path -Path $relativeDestination -ChildPath "$script:PublishModuleName.$version.nupkg" + $fileExists = Test-Path -Path $expectedPath + $fileExists | Should -Be $True + + # Cleanup + Remove-Item -Path $relativePath -Recurse -Force + Remove-Item -Path $relativeDestination -Recurse -Force + } <# Test for Signing the nupkg. Signing doesn't work It "Compressed Module is able to be signed with a certificate" { $version = "1.0.0" From acae208e0fcc59ee15027c10b73927331721e7e2 Mon Sep 17 00:00:00 2001 From: Justin Chung Date: Tue, 8 Oct 2024 11:25:48 -0500 Subject: [PATCH 2/2] removed pester edit --- .../CompressPSResource.Tests.ps1 | 38 ------------------- 1 file changed, 38 deletions(-) diff --git a/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 b/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 index c99af8660..33f1016f8 100644 --- a/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 +++ b/test/PublishPSResourceTests/CompressPSResource.Tests.ps1 @@ -46,44 +46,6 @@ Describe "Test Compress-PSResource" -tags 'CI' { BeforeAll { Get-NewPSResourceRepositoryFile - $testDir = (get-item $psscriptroot).parent.FullName - -function CreateTestModule -{ - param ( - [string] $Path = "$TestDrive", - [string] $ModuleName = 'TestModule' - ) - - $modulePath = Join-Path -Path $Path -ChildPath $ModuleName - $moduleMan = Join-Path $modulePath -ChildPath ($ModuleName + '.psd1') - $moduleSrc = Join-Path $modulePath -ChildPath ($ModuleName + '.psm1') - - if ( Test-Path -Path $modulePath) { - Remove-Item -Path $modulePath -Recurse -Force - } - - $null = New-Item -Path $modulePath -ItemType Directory -Force - - @' - @{{ - RootModule = "{0}.psm1" - ModuleVersion = '1.0.0' - Author = 'None' - Description = 'None' - GUID = '0c2829fc-b165-4d72-9038-ae3a71a755c1' - FunctionsToExport = @('Test1') - RequiredModules = @('NonExistentModule') - }} -'@ -f $ModuleName | Out-File -FilePath $moduleMan - - @' - function Test1 { - Write-Output 'Hello from Test1' - } -'@ | Out-File -FilePath $moduleSrc -} - # Register temporary repositories $tmpRepoPath = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath" New-Item $tmpRepoPath -Itemtype directory -Force