diff --git a/src/Modules/PSGalleryModules.csproj b/src/Modules/PSGalleryModules.csproj index 82a9db7e533..ae56290cfb3 100644 --- a/src/Modules/PSGalleryModules.csproj +++ b/src/Modules/PSGalleryModules.csproj @@ -13,6 +13,7 @@ + diff --git a/test/powershell/Modules/Microsoft.PowerShell.PSResourceGet/Microsoft.PowerShell.PSResourceGet.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.PSResourceGet/Microsoft.PowerShell.PSResourceGet.Tests.ps1 new file mode 100644 index 00000000000..9a6a7715786 --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.PSResourceGet/Microsoft.PowerShell.PSResourceGet.Tests.ps1 @@ -0,0 +1,215 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +# no progress output during these tests +$ProgressPreference = "SilentlyContinue" + +$RepositoryName = 'PSGallery' +$TestModule = 'newTestModule' +$TestScript = 'TestTestScript' +$Initialized = $false + +#region Install locations for modules and scripts + +if($IsWindows) { + $script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath 'PowerShell' +} +else { + $script:ProgramFilesPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES')) -Parent +} + +try +{ + $script:MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments") +} +catch +{ + $script:MyDocumentsFolderPath = $null +} + + +if($IsWindows) +{ + $script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath) + { + Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath 'PowerShell' + } + else + { + Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath "Documents\PowerShell" + } +} +else +{ + $script:MyDocumentsPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')) -Parent +} + + +$script:ProgramFilesModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath 'Modules' +$script:MyDocumentsModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath 'Modules' + +$script:ProgramFilesScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath 'Scripts' +$script:MyDocumentsScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath 'Scripts' + +$script:ProgramFilesScriptsInfoPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesScriptsPath -ChildPath 'InstalledScriptInfos' +$script:MyDocumentsScriptsInfoPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsScriptsPath -ChildPath 'InstalledScriptInfos' + +if (!(Test-Path $script:ProgramFilesScriptsInfoPath)) { + New-Item -Path $script:ProgramFilesScriptsInfoPath -ItemType Directory +} + +if (!(Test-Path $script:MyDocumentsScriptsPath)) { + New-Item -Path $script:MyDocumentsScriptsInfoPath -ItemType Directory +} + +#endregion + +#region Register a test repository + +function Initialize +{ + $repo = Get-PSResourceRepository $RepositoryName -ErrorAction SilentlyContinue + if($repo) + { + Set-PSResourceRepository -Name $repo.Name -Trusted + } + else + { + Register-PSResourceRepository -PSGallery -Trusted + } +} + +#endregion + +function Remove-InstalledModules +{ + Get-InstalledPSResource -Name $TestModule -Version '*' -ErrorAction SilentlyContinue | Microsoft.PowerShell.PSResourceGet\Uninstall-PSResource +} + +Describe "PSResourceGet - Module tests" -tags "Feature" { + + BeforeAll { + if ($script:Initialized -eq $false) { + Initialize + $script:Initialized = $true + } + } + + BeforeEach { + Remove-InstalledModules + } + + It "Should find a module correctly" { + $psgetModuleInfo = Find-PSResource -Name $TestModule -Repository $RepositoryName + $psgetModuleInfo.Name | Should -Be $TestModule + $psgetModuleInfo.Repository | Should -Be $RepositoryName + } + + It "Should install a module correctly to the required location with default CurrentUser scope" { + Install-PSResource -Name $TestModule -Repository $RepositoryName + $installedModuleInfo = Get-InstalledPSResource -Name $TestModule + + if (!$IsMacOS) { + $installedModuleInfo | Should -Not -BeNullOrEmpty + $installedModuleInfo.Name | Should -Be $TestModule + $installedModuleInfo.InstalledLocation.StartsWith($script:MyDocumentsModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue + + $module = Get-Module $TestModule -ListAvailable + $module.Name | Should -Be $TestModule + $module.ModuleBase.StartsWith($script:MyDocumentsModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue + } + } + + AfterAll { + Remove-InstalledModules + } +} + +Describe "PSResourceGet - Module tests (Admin)" -Tags @('Feature', 'RequireAdminOnWindows', 'RequireSudoOnUnix') { + + BeforeAll { + if ($script:Initialized -eq $false) { + Initialize + $script:Initialized = $true + } + } + + BeforeEach { + Remove-InstalledModules + } + + It "Should install a module correctly to the required location with AllUsers scope" { + Install-PSResource -Name $TestModule -Repository $RepositoryName -Scope AllUsers + + $module = Get-Module $TestModule -ListAvailable + $module.Name | Should -Be $TestModule + $module.ModuleBase.StartsWith($script:programFilesModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue + } + + AfterAll { + Remove-InstalledModules + } +} + +function Remove-InstalledScripts +{ + Get-InstalledPSResource -Name $TestScript -ErrorAction SilentlyContinue | Uninstall-PSResource +} + +Describe "PSResourceGet - Script tests" -tags "Feature" { + + BeforeAll { + if ($script:Initialized -eq $false) { + Initialize + $script:Initialized = $true + } + } + + BeforeEach { + Remove-InstalledScripts + } + + It "Should find a script correctly" { + $psgetScriptInfo = Find-PSResource -Name $TestScript -Repository $RepositoryName + $psgetScriptInfo.Name | Should -Be $TestScript + $psgetScriptInfo.Repository | Should -Be $RepositoryName + } + + It "Should install a script correctly to the required location with default CurrentUser scope" { + Install-PSResource -Name $TestScript -Repository $RepositoryName -Verbose + $installedScriptInfo = Get-InstalledPSResource -Name $TestScript + + if (!$IsMacOS) + { + $installedScriptInfo | Should -Not -BeNullOrEmpty + $installedScriptInfo.Name | Should -Be $TestScript + $installedScriptInfo.InstalledLocation.StartsWith($script:MyDocumentsScriptsPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue + } + } + + AfterAll { + Remove-InstalledScripts + } +} + +Describe "PSResourceGet - Script tests (Admin)" -Tags @('Feature', 'RequireAdminOnWindows', 'RequireSudoOnUnix') { + + BeforeAll { + if ($script:Initialized -eq $false) { + Initialize + $script:Initialized = $true + } + } + + BeforeEach { + Remove-InstalledScripts + } + + It "Should install a script correctly to the required location with AllUsers scope" { + Install-PSResource -Name $TestScript -Repository $RepositoryName -Scope AllUsers + } + + AfterAll { + Remove-InstalledScripts + } +} diff --git a/test/powershell/Modules/PowerShellGet/PowerShellGet.Tests.ps1 b/test/powershell/Modules/PowerShellGet/PowerShellGet.Tests.ps1 index 1186c0f9601..ef9eec8a0c2 100644 --- a/test/powershell/Modules/PowerShellGet/PowerShellGet.Tests.ps1 +++ b/test/powershell/Modules/PowerShellGet/PowerShellGet.Tests.ps1 @@ -159,15 +159,10 @@ Describe "PowerShellGet - Module tests (Admin)" -Tags @('Feature', 'RequireAdmin It "Should install a module correctly to the required location with AllUsers scope" { Install-Module -Name $TestModule -Repository $RepositoryName -Scope AllUsers - $installedModuleInfo = Get-InstalledModule -Name $TestModule - - $installedModuleInfo | Should -Not -BeNullOrEmpty - $installedModuleInfo.Name | Should -Be $TestModule - $installedModuleInfo.InstalledLocation.StartsWith($script:programFilesModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue $module = Get-Module $TestModule -ListAvailable $module.Name | Should -Be $TestModule - $module.ModuleBase | Should -Be $installedModuleInfo.InstalledLocation + $module.ModuleBase.StartsWith($script:programFilesModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue } AfterAll { diff --git a/tools/packaging/boms/linux.json b/tools/packaging/boms/linux.json index f735ea5cf1c..9f5f886a4ca 100644 --- a/tools/packaging/boms/linux.json +++ b/tools/packaging/boms/linux.json @@ -467,6 +467,34 @@ "Pattern": "Modules/PSReadLine/*.txt", "FileType": "NonProduct" }, + { + "Pattern": "Modules/PSResourceGet/*.dll", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/*.ps?1", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/*.ps1xml", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/LICENSE", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/Notice.txt", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/_manifest/spdx_2.2/*.json*", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/_manifest/spdx_2.2/*.cat", + "FileType": "NonProduct" + }, { "Pattern": "Modules/ThreadJob/*.dll", "FileType": "NonProduct" diff --git a/tools/packaging/boms/mac.json b/tools/packaging/boms/mac.json index 29f2bb8532a..a4b5bc2bffb 100644 --- a/tools/packaging/boms/mac.json +++ b/tools/packaging/boms/mac.json @@ -347,6 +347,34 @@ "Pattern": "Modules/PSReadLine/*.txt", "FileType": "NonProduct" }, + { + "Pattern": "Modules/PSResourceGet/*.dll", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/*.ps?1", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/*.ps1xml", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/LICENSE", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/Notice.txt", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/_manifest/spdx_2.2/*.json*", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/PSResourceGet/_manifest/spdx_2.2/*.cat", + "FileType": "NonProduct" + }, { "Pattern": "Modules/ThreadJob/*.dll", "FileType": "NonProduct" diff --git a/tools/packaging/boms/windows.json b/tools/packaging/boms/windows.json index 308342e8f10..39c0679891f 100644 --- a/tools/packaging/boms/windows.json +++ b/tools/packaging/boms/windows.json @@ -875,6 +875,38 @@ "Pattern": "Modules/ThreadJob/*.psd1", "FileType": "NonProduct" }, + { + "Pattern": "Modules/Microsoft.PowerShell.PSResourceGet/_manifest/spdx_2.2/manifest.cat", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/Microsoft.PowerShell.PSResourceGet/LICENSE", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/Microsoft.PowerShell.PSResourceGet/Notice.txt", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/Microsoft.PowerShell.PSResourceGet/dependencies/*.dll", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/Microsoft.PowerShell.PSResourceGet/Microsoft.PowerShell.PSResourceGet.dll", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/Microsoft.PowerShell.PSResourceGet/Microsoft.PowerShell.PSResourceGet.psd1", + "FileType": "NonProduct" + }, + { + "Pattern": "Modules/Microsoft.PowerShell.PSResourceGet/PSGet.Format.ps1xml", + "FileType": "NonProduct" + }, + { + "Pattern": "mscordaccore_*.dll", + "FileType": "NonProduct" + }, { "Pattern": "mscordaccore_*.dll", "FileType": "NonProduct"