From 1cd8cda92d8a419ef1d09e6adab4a867d1708450 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 18 May 2022 14:39:50 -0700 Subject: [PATCH 1/5] Update tests for publish psresource --- test/PublishPSResource.Tests.ps1 | 34 +++-- .../incorrectdepmoduleversion.psd1 | 133 ++++++++++++++++++ .../incorrectmoduleversion.psd1 | 132 +++++++++++++++++ 3 files changed, 281 insertions(+), 18 deletions(-) create mode 100644 test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 create mode 100644 test/testFiles/testModules/incorrectmoduleversion/incorrectmoduleversion.psd1 diff --git a/test/PublishPSResource.Tests.ps1 b/test/PublishPSResource.Tests.ps1 index 604087dd7..af0246463 100644 --- a/test/PublishPSResource.Tests.ps1 +++ b/test/PublishPSResource.Tests.ps1 @@ -6,6 +6,7 @@ Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe "Test Publish-PSResource" { BeforeAll { Get-NewPSResourceRepositoryFile + $relativeCurrentPath = Get-Location # Register temporary repositories $tmpRepoPath = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath" @@ -286,26 +287,23 @@ Describe "Test Publish-PSResource" { (Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath } - <# The following tests required manual testing because New-ModuleManifest will not allow for incorrect syntax/formatting, - At the moment, 'Update-ModuleManifest' is not yet complete, but that too should call 'Test-ModuleManifest', which also - does not allow for incorrect syntax. - It's still important to validate these scenarios because users may alter the module manifest manually, or the file may - get corrupted. #> - <# - It "Publish a module with that has an invalid version format, should throw -- Requires Manual testing" { - $incorrectVersion = '1..0.0' - $correctVersion = '1.0.0.0' - New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $incorrectVersion -Description "$script:PublishModuleName module" -RequiredModules @(@{ModuleName = 'PackageManagement'; ModuleVersion = $correctVersion }) - - {Publish-PSResource -Path $script:PublishModuleBase -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" + It "Publish a module with that has an invalid version format, should throw" { + $testFileFolder = "testFiles" + $testModulesFolder = "testModules" + $moduleName = "incorrectmoduleversion" + $testModulesPath = Join-Path -Path $relativeCurrentPath -ChildPath $testFileFolder -AdditionalChildPath $testModulesFolder + $incorrectmoduleversion = Join-Path -Path $testModulesPath -ChildPath $moduleName + + {Publish-PSResource -Path $incorrectmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" } - It "Publish a module with a dependency that has an invalid version format, should throw -- Requires Manual testing" { - $correctVersion = '1.0.0' - $incorrectVersion = '1..0.0' - New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $correctVersion -Description "$script:PublishModuleName module" -RequiredModules @(@{ModuleName = 'PackageManagement'; ModuleVersion = $incorrectVersion }) + It "Publish a module with a dependency that has an invalid version format, should throw" { + $testFileFolder = "testFiles" + $testModulesFolder = "testModules" + $moduleName = "incorrectdepmoduleversion" + $testModulesPath = Join-Path -Path $relativeCurrentPath -ChildPath $testFileFolder -AdditionalChildPath $testModulesFolder + $incorrectdepmoduleversion = Join-Path -Path $testModulesPath -ChildPath $moduleName - {Publish-PSResource -Path $script:PublishModuleBase -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" + {Publish-PSResource -Path $incorrectdepmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" } - #> } diff --git a/test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 b/test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 new file mode 100644 index 000000000..b0d235c3c --- /dev/null +++ b/test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 @@ -0,0 +1,133 @@ +# +# Module manifest for module 'testmodule-incorrect' +# +# Generated by: americks +# +# Generated on: 5/18/2022 +# + +@{ + + # Script module or binary module file associated with this manifest. + # RootModule = '' + + # Version number of this module. + ModuleVersion = '1.0.0.0' + + # Supported PSEditions + # CompatiblePSEditions = @() + + # ID used to uniquely identify this module + GUID = '4f56ed2b-72af-4c29-bda6-ef3dc91c1624' + + # Author of this module + Author = 'americks' + + # Company or vendor of this module + CompanyName = 'Unknown' + + # Copyright statement for this module + Copyright = '(c) americks. All rights reserved.' + + # Description of the functionality provided by this module + Description = 'incorrectdepmoduleversion module' + + # Minimum version of the PowerShell engine required by this module + # PowerShellVersion = '' + + # Name of the PowerShell host required by this module + # PowerShellHostName = '' + + # Minimum version of the PowerShell host required by this module + # PowerShellHostVersion = '' + + # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. + # DotNetFrameworkVersion = '' + + # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. + # ClrVersion = '' + + # Processor architecture (None, X86, Amd64) required by this module + # ProcessorArchitecture = '' + + # Modules that must be imported into the global environment prior to importing this module + RequiredModules = @(@{ModuleName = 'PackageManagement'; ModuleVersion = '1..0.0'; }) + + # Assemblies that must be loaded prior to importing this module + # RequiredAssemblies = @() + + # Script files (.ps1) that are run in the caller's environment prior to importing this module. + # ScriptsToProcess = @() + + # Type files (.ps1xml) to be loaded when importing this module + # TypesToProcess = @() + + # Format files (.ps1xml) to be loaded when importing this module + # FormatsToProcess = @() + + # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess + # NestedModules = @() + + # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. + FunctionsToExport = '*' + + # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. + CmdletsToExport = '*' + + # Variables to export from this module + VariablesToExport = '*' + + # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. + AliasesToExport = '*' + + # DSC resources to export from this module + # DscResourcesToExport = @() + + # List of all modules packaged with this module + # ModuleList = @() + + # List of all files packaged with this module + # FileList = @() + + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + # Tags = @() + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + # ProjectUri = '' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + # Prerelease string of this module + # Prerelease = '' + + # Flag to indicate whether the module requires explicit user acceptance for install/update/save + # RequireLicenseAcceptance = $false + + # External dependent modules of this module + # ExternalModuleDependencies = @() + + } # End of PSData hashtable + + } # End of PrivateData hashtable + + # HelpInfo URI of this module + # HelpInfoURI = '' + + # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. + # DefaultCommandPrefix = '' + + } + + \ No newline at end of file diff --git a/test/testFiles/testModules/incorrectmoduleversion/incorrectmoduleversion.psd1 b/test/testFiles/testModules/incorrectmoduleversion/incorrectmoduleversion.psd1 new file mode 100644 index 000000000..864acb695 --- /dev/null +++ b/test/testFiles/testModules/incorrectmoduleversion/incorrectmoduleversion.psd1 @@ -0,0 +1,132 @@ +# +# Module manifest for module 'testmodule-incorrect' +# +# Generated by: americks +# +# Generated on: 5/18/2022 +# + +@{ + +# Script module or binary module file associated with this manifest. +# RootModule = '' + +# Version number of this module. +ModuleVersion = '1..0.0' + +# Supported PSEditions +# CompatiblePSEditions = @() + +# ID used to uniquely identify this module +GUID = '4f56ed2b-72af-4c29-bda6-ef3dc91c1604' + +# Author of this module +Author = 'americks' + +# Company or vendor of this module +CompanyName = 'Unknown' + +# Copyright statement for this module +Copyright = '(c) americks. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'incorrectmoduleversion module' + +# Minimum version of the PowerShell engine required by this module +# PowerShellVersion = '' + +# Name of the PowerShell host required by this module +# PowerShellHostName = '' + +# Minimum version of the PowerShell host required by this module +# PowerShellHostVersion = '' + +# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# DotNetFrameworkVersion = '' + +# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# ClrVersion = '' + +# Processor architecture (None, X86, Amd64) required by this module +# ProcessorArchitecture = '' + +# Modules that must be imported into the global environment prior to importing this module +RequiredModules = @(@{ModuleName = 'PackageManagement'; ModuleVersion = '1.0.0.0'; }) + +# Assemblies that must be loaded prior to importing this module +# RequiredAssemblies = @() + +# Script files (.ps1) that are run in the caller's environment prior to importing this module. +# ScriptsToProcess = @() + +# Type files (.ps1xml) to be loaded when importing this module +# TypesToProcess = @() + +# Format files (.ps1xml) to be loaded when importing this module +# FormatsToProcess = @() + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +# NestedModules = @() + +# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. +FunctionsToExport = '*' + +# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. +CmdletsToExport = '*' + +# Variables to export from this module +VariablesToExport = '*' + +# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. +AliasesToExport = '*' + +# DSC resources to export from this module +# DscResourcesToExport = @() + +# List of all modules packaged with this module +# ModuleList = @() + +# List of all files packaged with this module +# FileList = @() + +# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + # Tags = @() + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + # ProjectUri = '' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + # Prerelease string of this module + # Prerelease = '' + + # Flag to indicate whether the module requires explicit user acceptance for install/update/save + # RequireLicenseAcceptance = $false + + # External dependent modules of this module + # ExternalModuleDependencies = @() + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +# HelpInfo URI of this module +# HelpInfoURI = '' + +# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. +# DefaultCommandPrefix = '' + +} + From 711fe6c3d9722ace03e81198cb7844f4c61eed4a Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 18 May 2022 15:36:19 -0700 Subject: [PATCH 2/5] Remove relative path and add tmp directory to test files --- test/PublishPSResource.Tests.ps1 | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/test/PublishPSResource.Tests.ps1 b/test/PublishPSResource.Tests.ps1 index af0246463..a24d49564 100644 --- a/test/PublishPSResource.Tests.ps1 +++ b/test/PublishPSResource.Tests.ps1 @@ -6,7 +6,6 @@ Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe "Test Publish-PSResource" { BeforeAll { Get-NewPSResourceRepositoryFile - $relativeCurrentPath = Get-Location # Register temporary repositories $tmpRepoPath = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath" @@ -41,6 +40,17 @@ Describe "Test Publish-PSResource" { # Create temp destination path $script:destinationPath = [IO.Path]::GetFullPath((Join-Path -Path $TestDrive -ChildPath "tmpDestinationPath")) New-Item $script:destinationPath -ItemType directory -Force + + #Create folder where we shall place all module files to be published for these tests + $script:tmpModulesFolderPath = Join-Path -Path $TestDrive -ChildPath "tmpModulesPath" + if(!(Test-Path $script:tmpModulesFolderPath)) + { + New-Item -Path $script:tmpModulesFolderPath -ItemType Directory -Force + } + + #Path to folder, within our test folder, where we store invalid module files used for testing + $script:testFilesFolderPath = Join-Path $psscriptroot -ChildPath "testFiles" + $script:testModulesFolderPath = Join-Path $testFilesFolderPath -ChildPath "testModules" } AfterAll { # Get-RevertPSResourceRepositoryFile @@ -288,22 +298,17 @@ Describe "Test Publish-PSResource" { } It "Publish a module with that has an invalid version format, should throw" { - $testFileFolder = "testFiles" - $testModulesFolder = "testModules" $moduleName = "incorrectmoduleversion" - $testModulesPath = Join-Path -Path $relativeCurrentPath -ChildPath $testFileFolder -AdditionalChildPath $testModulesFolder - $incorrectmoduleversion = Join-Path -Path $testModulesPath -ChildPath $moduleName + $incorrectmoduleversion = Join-Path -Path $script:testModulesFolderPath -ChildPath $moduleName {Publish-PSResource -Path $incorrectmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" } It "Publish a module with a dependency that has an invalid version format, should throw" { - $testFileFolder = "testFiles" - $testModulesFolder = "testModules" - $moduleName = "incorrectdepmoduleversion" - $testModulesPath = Join-Path -Path $relativeCurrentPath -ChildPath $testFileFolder -AdditionalChildPath $testModulesFolder - $incorrectdepmoduleversion = Join-Path -Path $testModulesPath -ChildPath $moduleName + $moduleName = "incorrectmoduleversion" + $incorrectdepmoduleversion = Join-Path -Path $script:testModulesFolderPath -ChildPath $moduleName + $script:testModulesFolderPath {Publish-PSResource -Path $incorrectdepmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" } } From a8f54060bdd0093610049269f4fefebea527b6c6 Mon Sep 17 00:00:00 2001 From: alerickson <25858831+alerickson@users.noreply.github.com> Date: Wed, 18 May 2022 15:37:18 -0700 Subject: [PATCH 3/5] Update test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 Co-authored-by: Anam Navied --- .../incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 | 1 - 1 file changed, 1 deletion(-) diff --git a/test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 b/test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 index b0d235c3c..54bd102c8 100644 --- a/test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 +++ b/test/testFiles/testModules/incorrectdepmoduleversion/incorrectdepmoduleversion.psd1 @@ -129,5 +129,4 @@ # DefaultCommandPrefix = '' } - \ No newline at end of file From 2bd8b68f23371209c1dab8d5d12b4d8b066465b1 Mon Sep 17 00:00:00 2001 From: alerickson <25858831+alerickson@users.noreply.github.com> Date: Wed, 18 May 2022 16:35:44 -0700 Subject: [PATCH 4/5] Update test/PublishPSResource.Tests.ps1 Co-authored-by: Anam Navied --- test/PublishPSResource.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/test/PublishPSResource.Tests.ps1 b/test/PublishPSResource.Tests.ps1 index a24d49564..34d220e87 100644 --- a/test/PublishPSResource.Tests.ps1 +++ b/test/PublishPSResource.Tests.ps1 @@ -308,7 +308,6 @@ Describe "Test Publish-PSResource" { $moduleName = "incorrectmoduleversion" $incorrectdepmoduleversion = Join-Path -Path $script:testModulesFolderPath -ChildPath $moduleName - $script:testModulesFolderPath {Publish-PSResource -Path $incorrectdepmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" } } From bb30505aa5f3e12a6d5ebd735eb66915efcda400 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Wed, 18 May 2022 16:39:30 -0700 Subject: [PATCH 5/5] Clean up unnecessary code --- test/PublishPSResource.Tests.ps1 | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test/PublishPSResource.Tests.ps1 b/test/PublishPSResource.Tests.ps1 index a24d49564..615271346 100644 --- a/test/PublishPSResource.Tests.ps1 +++ b/test/PublishPSResource.Tests.ps1 @@ -41,14 +41,7 @@ Describe "Test Publish-PSResource" { $script:destinationPath = [IO.Path]::GetFullPath((Join-Path -Path $TestDrive -ChildPath "tmpDestinationPath")) New-Item $script:destinationPath -ItemType directory -Force - #Create folder where we shall place all module files to be published for these tests - $script:tmpModulesFolderPath = Join-Path -Path $TestDrive -ChildPath "tmpModulesPath" - if(!(Test-Path $script:tmpModulesFolderPath)) - { - New-Item -Path $script:tmpModulesFolderPath -ItemType Directory -Force - } - - #Path to folder, within our test folder, where we store invalid module files used for testing + # Path to folder, within our test folder, where we store invalid module files used for testing $script:testFilesFolderPath = Join-Path $psscriptroot -ChildPath "testFiles" $script:testModulesFolderPath = Join-Path $testFilesFolderPath -ChildPath "testModules" } @@ -305,10 +298,9 @@ Describe "Test Publish-PSResource" { } It "Publish a module with a dependency that has an invalid version format, should throw" { - $moduleName = "incorrectmoduleversion" + $moduleName = "incorrectdepmoduleversion" $incorrectdepmoduleversion = Join-Path -Path $script:testModulesFolderPath -ChildPath $moduleName - $script:testModulesFolderPath {Publish-PSResource -Path $incorrectdepmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource" } }