diff --git a/Tests/PSGetInstallModule.Tests.ps1 b/Tests/PSGetInstallModule.Tests.ps1 index 12c2d1a9..feede7fc 100644 --- a/Tests/PSGetInstallModule.Tests.ps1 +++ b/Tests/PSGetInstallModule.Tests.ps1 @@ -17,7 +17,7 @@ function SuiteSetup { Import-Module "$PSScriptRoot\PSGetTestUtils.psm1" -WarningAction SilentlyContinue Import-Module "$PSScriptRoot\Asserts.psm1" -WarningAction SilentlyContinue - + $script:IsWindowsOS = (-not (Get-Variable -Name IsWindows -ErrorAction Ignore)) -or $IsWindows $script:ProgramFilesModulesPath = Get-AllUsersModulesPath $script:MyDocumentsModulesPath = Get-CurrentUserModulesPath @@ -64,7 +64,7 @@ function SuiteSetup { $script:TestPSModulePath = Join-Path -Path $script:TempPath -ChildPath "PSGet_$(Get-Random)" $null = New-Item -Path $script:TempModulesPath -ItemType Directory -Force $null = New-Item -Path $script:TestPSModulePath -ItemType Directory -Force - + # Set up local "gallery" $script:localGalleryName = [System.Guid]::NewGuid().ToString() $script:PSGalleryRepoPath = Join-Path -Path $script:TempPath -ChildPath 'PSGalleryRepo' @@ -88,7 +88,7 @@ function SuiteSetup { $null = New-ModuleManifest -Path (Join-Path -Path $pesterv1Destination -ChildPath "Pester.psd1") -Description "Test signed module v1" -ModuleVersion 99.99.99.98 $null = New-ModuleManifest -Path (Join-Path -Path $pesterv2Destination -ChildPath "Pester.psd1") -Description "Test signed module v2" -ModuleVersion 99.99.99.99 - + # Move Pester 3.4.0 to $script:TestPSModulePath # If it doesn't exist, attempt to download it. # If this is run offline, just fail the test for now. @@ -150,7 +150,7 @@ function SuiteCleanup { } } } - + RemoveItem $script:TempModulesPath RemoveItem $script:TestPSModulePath } @@ -172,6 +172,28 @@ Describe PowerShell.PSGet.InstallModuleTests -Tags 'BVT','InnerLoop' { PSGetTestUtils\Uninstall-Module DscTestModule } + # Purpose: InstallShouldBeSilent + # + # Action: Install-Module "ContosoServer" + # + # Expected Result: Should pass + # + It "Install-Module ContosoServer should return be silent" { + $result = Install-Module -Name "ContosoServer" + $result | Should -BeNullOrEmpty + } + + # Purpose: InstallShouldReturnOutput + # + # Action: Install-Module "ContosoServer" -PassThru + # + # Expected Result: Should pass + # + It "Install-Module ContosoServer -PassThru should return output" { + $result = Install-Module -Name "ContosoServer" -PassThru + $result | Should -Not -BeNullOrEmpty + } + # Purpose: InstallNotAvailableModuleWithWildCard # # Action: Install-Module "Co[nN]t?soS[a-z]r?eW" @@ -624,7 +646,7 @@ Describe PowerShell.PSGet.InstallModuleTests -Tags 'BVT','InnerLoop' { -RedirectStandardOutput $NonAdminConsoleOutput waitFor {Test-Path $NonAdminConsoleOutput} -timeoutInMilliseconds $script:assertTimeOutms -exceptionMessage "Install-Module on non-admin console failed to complete" $content = Get-Content $NonAdminConsoleOutput - + Assert ($content -and ($content -match 'DscTestModule')) "Install-module with -force should fail when a module version being installed is in use, $content." RemoveItem $NonAdminConsoleOutput } ` @@ -847,7 +869,7 @@ Describe PowerShell.PSGet.InstallModuleTests -Tags 'BVT','InnerLoop' { # Because of TFS:1908563, we changed Get-Package to show only the latest version by default # hence the count is same after the update. - AssertEquals $modules1.count $modules2.count "module count should be same before and after updating a module, before: $($modules1.count), after: $($modules2.count)" + AssertEquals $modules1.count $modules2.count "module count should be same before and after updating a module, before: $($modules1.count), after: $($modules2.count)" } It ValidateGetInstalledModuleAndUninstallModuleCmdletsWithMinimumVersion { @@ -1054,7 +1076,7 @@ Describe PowerShell.PSGet.InstallModuleTests.P1 -Tags 'P1','OuterLoop' { # # Expected Result: should fail with an error # - It "InstallModuleWithRangeWildCards" { + It "InstallModuleWithRangeWildCards" { AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Module -Name "Co[nN]t?soS[a-z]r?er"} ` -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Module' } @@ -1338,13 +1360,13 @@ Describe PowerShell.PSGet.InstallModuleTests.P1 -Tags 'P1','OuterLoop' { # It GetInstalledModuleWithWildcard { $ModuleNames = 'Contoso','ContosoServer','ContosoClient' - + Install-Module -Name $ModuleNames # ModuleName without wildcards $res1 = Get-InstalledModule -Name $ModuleNames[0] AssertEquals $res1.Name $ModuleNames[0] "Get-InstalledModule didn't return the exact module, $res1" - + # ModuleName with wildcards $res2 = Get-InstalledModule -Name "Contoso*" AssertEquals $res2.count $ModuleNames.Count "Get-InstalledModule didn't return the $ModuleNames modules, $res2" @@ -1352,14 +1374,14 @@ Describe PowerShell.PSGet.InstallModuleTests.P1 -Tags 'P1','OuterLoop' { # Purpose: Validate Install-Module cmdlet with same source location registered with NUGet provider # - # Expected Result: Get-InstalledModule should return proper Repository and RepositorySourceLocation values + # Expected Result: Get-InstalledModule should return proper Repository and RepositorySourceLocation values # from the PowerShellGet provider only not from the NuGet provider # It InstallModuleWithSameLocationRegisteredWithNuGetProvider { $ModuleName = 'ContosoServer' $TempNuGetSourceName = "$(Get-Random)" $RepositoryName = "PSGallery" - Register-PackageSource -Provider nuget -Name $TempNuGetSourceName -Location $Global:PSGallerySourceUri -Trusted + Register-PackageSource -Provider nuget -Name $TempNuGetSourceName -Location $Global:PSGallerySourceUri -Trusted try { Install-Module -Name $ModuleName -Repository $RepositoryName @@ -1385,7 +1407,7 @@ Describe PowerShell.PSGet.InstallModuleTests.P1 -Tags 'P1','OuterLoop' { finally { Unregister-PackageSource -ProviderName NuGet -Name $TempNuGetSourceName -Force - } + } } # Purpose: Validate Save-Module cmdlet with Find-Command cmdlet output @@ -1416,7 +1438,7 @@ Describe PowerShell.PSGet.InstallModuleTests.P1 -Tags 'P1','OuterLoop' { } Describe PowerShell.PSGet.InstallModuleTests.P2 -Tags 'P2','OuterLoop' { - # Not executing these tests on MacOS as + # Not executing these tests on MacOS as # the total execution time is exceeding allowed 50 min in TravisCI daily builds. if($IsMacOS) { return diff --git a/Tests/PSGetInstallScript.Tests.ps1 b/Tests/PSGetInstallScript.Tests.ps1 index 5d6a0d46..29b9da0e 100644 --- a/Tests/PSGetInstallScript.Tests.ps1 +++ b/Tests/PSGetInstallScript.Tests.ps1 @@ -19,8 +19,8 @@ function SuiteSetup { Import-Module "$PSScriptRoot\Asserts.psm1" -WarningAction SilentlyContinue $script:IsWindowsOS = (-not (Get-Variable -Name IsWindows -ErrorAction Ignore)) -or $IsWindows - $script:ProgramFilesScriptsPath = Get-AllUsersScriptsPath - $script:MyDocumentsScriptsPath = Get-CurrentUserScriptsPath + $script:ProgramFilesScriptsPath = Get-AllUsersScriptsPath + $script:MyDocumentsScriptsPath = Get-CurrentUserScriptsPath $script:PSGetLocalAppDataPath = Get-PSGetLocalAppDataPath $script:TempPath = Get-TempPath New-Item -Path $script:MyDocumentsScriptsPath -ItemType Directory -ErrorAction SilentlyContinue -WarningAction SilentlyContinue @@ -31,10 +31,9 @@ function SuiteSetup { $script:psgetModuleInfo = Import-Module PowerShellGet -Global -Force -Passthru Import-LocalizedData script:LocalizedData -filename PSGet.Resource.psd1 -BaseDirectory $script:psgetModuleInfo.ModuleBase - $script:moduleSourcesFilePath= Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml" + $script:moduleSourcesFilePath = Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml" $script:moduleSourcesBackupFilePath = Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml_$(get-random)_backup" - if(Test-Path $script:moduleSourcesFilePath) - { + if (Test-Path $script:moduleSourcesFilePath) { Rename-Item $script:moduleSourcesFilePath $script:moduleSourcesBackupFilePath -Force } @@ -45,8 +44,7 @@ function SuiteSetup { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force Get-InstalledScript -Name Fabrikam-ClientScript -ErrorAction SilentlyContinue | Uninstall-Script -Force - if($script:IsWindowsOS) - { + if ($script:IsWindowsOS) { $script:userName = "PSGetUser" $password = "Password1" $null = net user $script:userName $password /add @@ -62,13 +60,12 @@ function SuiteSetup { $script:TempSavePath = Join-Path -Path $script:TempPath -ChildPath "PSGet_$(Get-Random)" $null = New-Item -Path $script:TempSavePath -ItemType Directory -Force - $script:AddedAllUsersInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope AllUsers + $script:AddedAllUsersInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope AllUsers $script:AddedCurrentUserInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope CurrentUser - $script:PSGetSettingsFilePath= Join-Path $script:PSGetLocalAppDataPath 'PowerShellGetSettings.xml' + $script:PSGetSettingsFilePath = Join-Path $script:PSGetLocalAppDataPath 'PowerShellGetSettings.xml' $script:PSGetSettingsBackupFilePath = Join-Path $script:PSGetLocalAppDataPath "PowerShellGetSettings.xml_$(get-random)_backup" - if(Test-Path $script:PSGetSettingsFilePath) - { + if (Test-Path $script:PSGetSettingsFilePath) { Rename-Item $script:PSGetSettingsFilePath $script:PSGetSettingsBackupFilePath -Force } @@ -76,29 +73,24 @@ function SuiteSetup { } function SuiteCleanup { - if(Test-Path $script:moduleSourcesBackupFilePath) - { + if (Test-Path $script:moduleSourcesBackupFilePath) { Move-Item $script:moduleSourcesBackupFilePath $script:moduleSourcesFilePath -Force } - else - { + else { RemoveItem $script:moduleSourcesFilePath } # Import the PowerShellGet provider to reload the repositories. $null = Import-PackageProvider -Name PowerShellGet -Force - if($script:IsWindowsOS) - { + if ($script:IsWindowsOS) { # Delete the user net user $script:UserName /delete | Out-Null # Delete the user profile # run only if cmd is available - if(Get-Command -Name Get-WmiObject -ErrorAction SilentlyContinue) - { - $userProfile = (Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -match $script:UserName}) - if($userProfile) - { + if (Get-Command -Name Get-WmiObject -ErrorAction SilentlyContinue) { + $userProfile = (Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -match $script:UserName }) + if ($userProfile) { RemoveItem $userProfile.LocalPath } } @@ -106,27 +98,23 @@ function SuiteCleanup { RemoveItem $script:TempSavePath - if($script:AddedAllUsersInstallPath) - { + if ($script:AddedAllUsersInstallPath) { Reset-PATHVariableForScriptsInstallLocation -Scope AllUsers } - if($script:AddedCurrentUserInstallPath) - { + if ($script:AddedCurrentUserInstallPath) { Reset-PATHVariableForScriptsInstallLocation -Scope CurrentUser } - if(Test-Path $script:PSGetSettingsBackupFilePath) - { + if (Test-Path $script:PSGetSettingsBackupFilePath) { Move-Item $script:PSGetSettingsBackupFilePath $script:PSGetSettingsFilePath -Force } - else - { + else { RemoveItem $script:PSGetSettingsFilePath } } -Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { +Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT', 'InnerLoop' { BeforeAll { SuiteSetup @@ -140,7 +128,29 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Get-InstalledScript -Name Fabrikam-Script -ErrorAction SilentlyContinue | Uninstall-Script -Force Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force Get-InstalledScript -Name Fabrikam-ClientScript -ErrorAction SilentlyContinue | Uninstall-Script -Force - } + } + + # Purpose: InstallShouldBeSilent + # + # Action: Install-Script "Fabrikam-ServerScript" + # + # Expected Result: Should pass + # + It "Install-Script Fabrikam-ServerScript should be silent" { + $result = Install-Script -Name "Fabrikam-ServerScript" + $result | Should -BeNullOrEmpty + } + + # Purpose: InstallShouldReturnOutput + # + # Action: Install-Script "Fabrikam-ServerScript" -PassThru + # + # Expected Result: Should pass + # + It "Install-Script Fabrikam-ServerScript -PassThru should return output" { + $result = Install-Script -Name "Fabrikam-ServerScript" -PassThru + $result | Should -Not -BeNullOrEmpty + } # Purpose: InstallScriptWithRangeWildCards # @@ -149,8 +159,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # Expected Result: should fail with an error # It "InstallScriptWithRangeWildCards" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script 'Fab[rR]ikam?Ser[a-z]erScr?pt','TempName'} ` - -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script 'Fab[rR]ikam?Ser[a-z]erScr?pt', 'TempName' } ` + -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' } # Purpose: InstallNotAvailableScriptWithWildCard @@ -162,8 +172,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { It "InstallNotAvailableScriptWithWildCard" { Install-Script -Name "Fabrikam-ServerScriptW" -ErrorAction SilentlyContinue - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript Fabrikam-ServerScript} ` - -expectedFullyQualifiedErrorId 'NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage' + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript Fabrikam-ServerScript } ` + -expectedFullyQualifiedErrorId 'NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage' } # Purpose: InstallMultipleScripts @@ -173,8 +183,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # Expected Result: two scripts should be installed # It "InstallMultipleScripts" { - Install-Script Fabrikam-ClientScript,Fabrikam-ServerScript - $res = Get-InstalledScript Fabrikam-ClientScript,Fabrikam-ServerScript + Install-Script Fabrikam-ClientScript, Fabrikam-ServerScript + $res = Get-InstalledScript Fabrikam-ClientScript, Fabrikam-ServerScript Assert ($res.Count -eq 2) "Install-Script with multiple names should not fail" } @@ -186,7 +196,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # It "InstallSingleScript" { $scriptName = 'Fabrikam-ServerScript' - + $findScriptOutput = Find-Script $scriptName $DateTimeBeforeInstall = Get-Date @@ -199,7 +209,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { AssertEquals $res.Description $findScriptOutput.Description "Invalid Description value in Get-InstalledScript metadata, $res" AssertEquals $res.Author $findScriptOutput.Author "Invalid Author value in Get-InstalledScript metadata, $res" AssertEquals $res.CompanyName $findScriptOutput.CompanyName "Invalid CompanyName value in Get-InstalledScript metadata, $res" - AssertEquals $res.Copyright $findScriptOutput.Copyright "Invalid Copyright value in Get-InstalledScript metadata, $res" + AssertEquals $res.Copyright $findScriptOutput.Copyright "Invalid Copyright value in Get-InstalledScript metadata, $res" AssertEquals $res.PublishedDate $findScriptOutput.PublishedDate "Invalid PublishedDate value in Get-InstalledScript metadata, $res" AssertEquals $res.LicenseUri $findScriptOutput.LicenseUri "Invalid LicenseUri value in Get-InstalledScript metadata, $res" AssertEquals $res.ProjectUri $findScriptOutput.ProjectUri "Invalid ProjectUri value in Get-InstalledScript metadata, $res" @@ -258,11 +268,11 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # It "InstallScriptShouldNotFailIfReqVersionAlreadyInstalled" { Install-Script Fabrikam-ServerScript -RequiredVersion 2.0 - $MyError=$null + $MyError = $null Install-Script Fabrikam-ServerScript -RequiredVersion 2.0 -ErrorVariable MyError - Assert ((-not $MyError) -or -not ($MyError | ?{-not (($_.Message -match 'StopUpstreamCommandsException') -or ($_.Message -eq 'System error.'))})) "There should not be any error from second install with required, $MyError" + Assert ((-not $MyError) -or -not ($MyError | ? { -not (($_.Message -match 'StopUpstreamCommandsException') -or ($_.Message -eq 'System error.')) })) "There should not be any error from second install with required, $MyError" } ` - -Skip:$($PSCulture -ne 'en-US') + -Skip:$($PSCulture -ne 'en-US') # Purpose: InstallScriptShouldNotFailIfMinVersionAlreadyInstalled # @@ -272,11 +282,11 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # It "InstallScriptShouldNotFailIfMinVersionAlreadyInstalled" { Install-Script Fabrikam-ServerScript -RequiredVersion 2.5 - $MyError=$null + $MyError = $null Install-Script Fabrikam-ServerScript -MinimumVersion 2.0 -ErrorVariable MyError - Assert ((-not $MyError) -or -not ($MyError | ?{-not (($_.Message -match 'StopUpstreamCommandsException') -or ($_.Message -eq 'System error.'))})) "There should not be any error from second install with min version, $MyError" + Assert ((-not $MyError) -or -not ($MyError | ? { -not (($_.Message -match 'StopUpstreamCommandsException') -or ($_.Message -eq 'System error.')) })) "There should not be any error from second install with min version, $MyError" } ` - -Skip:$($PSCulture -ne 'en-US') + -Skip:$($PSCulture -ne 'en-US') # Purpose: InstallScriptWithForce # @@ -289,7 +299,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { It InstallScriptWithForce { $scriptName = 'Fabrikam-ServerScript' Install-Script $scriptName -RequiredVersion 1.0 - $MyError=$null + $MyError = $null Install-Script $scriptName -RequiredVersion 1.5 -Force -ErrorVariable MyError Assert ($MyError.Count -eq 0) "There should not be any error from force install, $MyError" @@ -310,7 +320,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $scriptName = 'Fabrikam-ServerScript' $version = '1.5' Install-Script $scriptName -RequiredVersion $version - $MyError=$null + $MyError = $null Install-Script $scriptName -RequiredVersion $version -Force -ErrorVariable MyError Assert ($MyError.Count -eq 0) "There should not be any error from force install, $MyError" $res = Get-InstalledScript $scriptName @@ -328,8 +338,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $expectedFullyQualifiedErrorId = 'NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage' - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script Fabrikam-ServerScript -MinimumVersion 10.0} ` - -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script Fabrikam-ServerScript -MinimumVersion 10.0 } ` + -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId } # Purpose: Install a script using non available RequiredVersion @@ -342,8 +352,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $expectedFullyQualifiedErrorId = 'NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage' - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script Fabrikam-ServerScript -RequiredVersion 1.44} ` - -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script Fabrikam-ServerScript -RequiredVersion 1.44 } ` + -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId } # Purpose: Install a script using RequiredVersion @@ -386,8 +396,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $expectedFullyQualifiedErrorId = 'NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage' - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script NonExistentScript} ` - -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script NonExistentScript } ` + -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId } # Purpose: InstallScriptWithPipelineInput @@ -410,8 +420,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # Expected Result: Fabrikam-ServerScript and Fabrikam-ClientScript should be installed # It "InstallMultipleScriptsWithPipelineInput" { - Find-Script Fabrikam-ClientScript,Fabrikam-ServerScript | Install-Script - $res = Get-InstalledScript Fabrikam-ClientScript,Fabrikam-ServerScript + Find-Script Fabrikam-ClientScript, Fabrikam-ServerScript | Install-Script + $res = Get-InstalledScript Fabrikam-ClientScript, Fabrikam-ServerScript Assert ($res.Count -eq 2) "Install-Script failed to install multiple scripts from Find-Script output" } @@ -422,9 +432,9 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # Expected Result: Fabrikam-ServerScript and Fabrikam-ClientScript should be installed # It "InstallMultipleScriptsUsingInputObjectParam" { - $items = Find-Script Fabrikam-ClientScript,Fabrikam-ServerScript + $items = Find-Script Fabrikam-ClientScript, Fabrikam-ServerScript Install-Script -InputObject $items - $res = Get-InstalledScript Fabrikam-ClientScript,Fabrikam-ServerScript + $res = Get-InstalledScript Fabrikam-ClientScript, Fabrikam-ServerScript Assert ($res.Count -eq 2) "Install-Script failed to install multiple scripts with -InputObject parameter" } @@ -440,15 +450,13 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $res = Get-InstalledScript $scriptName AssertEquals $res.InstalledLocation $script:ProgramFilesScriptsPath "Install-Script with AllUsers scope did not install Fabrikam-ServerScript to program files scripts folder, $script:ProgramFilesScriptsPath" - if($IsWindows -ne $False) - { + if ($IsWindows -ne $False) { $cmdInfo = Get-Command -Name $scriptName - AssertNotNull $cmdInfo "Script installed to the current user scope is not found by the Get-Command cmdlet" - AssertEquals $cmdInfo.Name "$scriptName.ps1" "Script installed to the current user scope is not found by the Get-Command cmdlet, $cmdlInfo" - + AssertNotNull $cmdInfo "Script installed to the current user scope is not found by the Get-Command cmdlet" + AssertEquals $cmdInfo.Name "$scriptName.ps1" "Script installed to the current user scope is not found by the Get-Command cmdlet, $cmdlInfo" + # CommandInfo.Source is not available on 3.0 and 4.0 downlevel PS versions - if($PSVersionTable.PSVersion -ge '5.0.0') - { + if ($PSVersionTable.PSVersion -ge '5.0.0') { AssertEquals $cmdInfo.Source "$($res.InstalledLocation)\$scriptName.ps1" "Script installed to the current user scope is not found by the Get-Command cmdlet, $($cmdlInfo.Source)" } } @@ -465,15 +473,13 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Install-Script $scriptName -Scope CurrentUser $res = Get-InstalledScript $scriptName AssertEquals $res.InstalledLocation $script:MyDocumentsScriptsPath "Install-Script with CurrentUser scope did not install Fabrikam-ServerScript to user documents folder, $script:MyDocumentsScriptsPath" - if($IsWindows -ne $False) - { + if ($IsWindows -ne $False) { $cmdInfo = Get-Command -Name $scriptName - AssertNotNull $cmdInfo "Script installed to the current user scope is not found by the Get-Command cmdlet" - AssertEquals $cmdInfo.Name "$scriptName.ps1" "Script installed to the current user scope is not found by the Get-Command cmdlet, $cmdlInfo" + AssertNotNull $cmdInfo "Script installed to the current user scope is not found by the Get-Command cmdlet" + AssertEquals $cmdInfo.Name "$scriptName.ps1" "Script installed to the current user scope is not found by the Get-Command cmdlet, $cmdlInfo" # CommandInfo.Source is not available on 3.0 and 4.0 downlevel PS versions - if($PSVersionTable.PSVersion -ge '5.0.0') - { + if ($PSVersionTable.PSVersion -ge '5.0.0') { AssertEquals $cmdInfo.Source "$($res.InstalledLocation)\$scriptName.ps1" "Script installed to the current user scope is not found by the Get-Command cmdlet, $($cmdlInfo.Source)" } } @@ -508,8 +514,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $NonAdminConsoleOutput = Join-Path ([System.IO.Path]::GetTempPath()) 'nonadminconsole-out.txt' $psProcess = "PowerShell.exe" - if ($script:IsCoreCLR) - { + if ($script:IsCoreCLR) { $psProcess = "pwsh.exe" } @@ -518,19 +523,19 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { } Install-Script -Name Fabrikam-Script -NoPathUpdate -Scope AllUsers -ErrorVariable ev -ErrorAction SilentlyContinue; Write-Output "$ev"' ` - -Credential $script:credential ` - -Wait ` - -WorkingDirectory $PSHOME ` - -RedirectStandardOutput $NonAdminConsoleOutput + -Credential $script:credential ` + -Wait ` + -WorkingDirectory $PSHOME ` + -RedirectStandardOutput $NonAdminConsoleOutput - waitFor {Test-Path $NonAdminConsoleOutput} -timeoutInMilliseconds $script:assertTimeOutms -exceptionMessage "Install-Script on non-admin console failed to complete" + waitFor { Test-Path $NonAdminConsoleOutput } -timeoutInMilliseconds $script:assertTimeOutms -exceptionMessage "Install-Script on non-admin console failed to complete" $content = Get-Content $NonAdminConsoleOutput RemoveItem $NonAdminConsoleOutput AssertNotNull ($content) "Install-Script with AllUsers scope on non-admin user console should not succeed" Assert ($content -match "Administrator rights are required to install" ) "Install script with AllUsers scope on non-admin user console should fail, $content" } ` - -Skip:$( + -Skip:$( $whoamiValue = (whoami) ($whoamiValue -eq "NT AUTHORITY\SYSTEM") -or @@ -552,19 +557,18 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $NonAdminConsoleOutput = Join-Path ([System.IO.Path]::GetTempPath()) 'nonadminconsole-out.txt' $psProcess = "PowerShell.exe" - if ($script:IsCoreCLR) - { + if ($script:IsCoreCLR) { $psProcess = "pwsh.exe" } Start-Process $psProcess -ArgumentList '-command Install-Script -Name Fabrikam-ServerScript -NoPathUpdate; Get-InstalledScript Fabrikam-ServerScript | Format-List Name, InstalledLocation' ` - -Credential $script:credential ` - -Wait ` - -WorkingDirectory $PSHOME ` - -RedirectStandardOutput $NonAdminConsoleOutput + -Credential $script:credential ` + -Wait ` + -WorkingDirectory $PSHOME ` + -RedirectStandardOutput $NonAdminConsoleOutput - waitFor {Test-Path $NonAdminConsoleOutput} -timeoutInMilliseconds $script:assertTimeOutms -exceptionMessage "Install-Script on non-admin console failed to complete" + waitFor { Test-Path $NonAdminConsoleOutput } -timeoutInMilliseconds $script:assertTimeOutms -exceptionMessage "Install-Script on non-admin console failed to complete" $content = Get-Content $NonAdminConsoleOutput RemoveItem $NonAdminConsoleOutput @@ -572,7 +576,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Assert ($content -match "Fabrikam-ServerScript") "Script did not install correctly" Assert ($content -match "Documents") "Script did not install to the correct location" } ` - -Skip:$( + -Skip:$( $whoamiValue = (whoami) ($whoamiValue -eq "NT AUTHORITY\SYSTEM") -or @@ -599,25 +603,22 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Reset-PATHVariableForScriptsInstallLocation -Scope AllUsers $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 1 is mapped to No in prompt - $Global:proxy.UI.ChoiceToMake=1 + $Global:proxy.UI.ChoiceToMake = 1 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Script Fabrikam-ServerScript -Repository PSGallery' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -638,7 +639,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallScript_AllUsers_YES_toThePromptForAddingtoPATHVariable # @@ -655,25 +656,22 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Reset-PATHVariableForScriptsInstallLocation -Scope AllUsers $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Script Fabrikam-ServerScript -Repository PSGallery' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -686,8 +684,8 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $res = Get-InstalledScript Fabrikam-ServerScript AssertEquals $res.Name 'Fabrikam-ServerScript' "Install-Script should install a script even when prompt is not accepted, $res" - - Assert (($env:PATH -split ';') -contains $script:ProgramFilesScriptsPath) "Install-Script should add AllUsers scope path to PATH environment variable." + + Assert (($env:PATH -split ';') -contains $script:ProgramFilesScriptsPath) "Install-Script should add AllUsers scope path to PATH environment variable." } finally { # Set the PATH variable to not have the scripts install location in the PATH variable. @@ -696,7 +694,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallScript_CurrentUser_NoPathUpdate_NoPromptForAddingtoPATHVariable # @@ -727,7 +725,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$($IsWindows -eq $false) + -Skip:$($IsWindows -eq $false) # Purpose: InstallScript_CurrentUser_Force_NoPromptForAddingtoPATHVariable # @@ -749,7 +747,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $currentPATHValue = & $script:psgetModuleInfo Get-EnvironmentVariable -Name 'PATH' -Target $script:EnvironmentVariableTarget.User Assert (($currentPATHValue -split ';') -notcontains $script:MyDocumentsScriptsPath) "PATH environment variable is not reset properly. $currentPATHValue" - + Install-Script Fabrikam-ServerScript -Repository PSGallery -Scope CurrentUser -Force $res = Get-InstalledScript Fabrikam-ServerScript @@ -764,11 +762,11 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # Set the PATH variable to not have the scripts install location in the PATH variable. Set-PATHVariableForScriptsInstallLocation -Scope CurrentUser Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force - + $script:psgetModuleInfo = Import-Module -Name PowerShellGet -Force -PassThru } } ` - -Skip:$($IsWindows -eq $false) + -Skip:$($IsWindows -eq $false) # Purpose: InstallScript_CurrentUser_NO_toThePromptForAddingtoPATHVariable # @@ -785,25 +783,22 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Reset-PATHVariableForScriptsInstallLocation -Scope CurrentUser $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 1 is mapped to No in prompt - $Global:proxy.UI.ChoiceToMake=1 + $Global:proxy.UI.ChoiceToMake = 1 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Script Fabrikam-ServerScript -Repository PSGallery -Scope CurrentUser' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -823,7 +818,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallScript_CurrentUser_YES_toThePromptForAddingtoPATHVariable # @@ -840,25 +835,22 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Reset-PATHVariableForScriptsInstallLocation -Scope CurrentUser $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Script Fabrikam-ServerScript -Repository PSGallery -Scope CurrentUser' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -880,7 +872,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallScriptWithWhatIf # @@ -890,21 +882,18 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # It "InstallScriptWithWhatIf" { $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Script -Name Fabrikam-ServerScript -WhatIf' } - finally - { + finally { $fileName = "WriteLine-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -919,7 +908,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $res = Get-InstalledScript Fabrikam-ServerScript -ErrorAction SilentlyContinue -WarningAction SilentlyContinue Assert (-not $res) "Install-Script should not install the script with -WhatIf option" } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallScriptWithConfirmAndNoToPrompt # @@ -929,24 +918,21 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # It "InstallScriptWithConfirmAndNoToPrompt" { $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 2 is mapped to NO in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=2 + $Global:proxy.UI.ChoiceToMake = 2 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Script Fabrikam-ServerScript -Repository PSGallery -Confirm' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -962,7 +948,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $res = Get-InstalledScript Fabrikam-ServerScript -ErrorAction SilentlyContinue -WarningAction SilentlyContinue AssertNull $res "Install-Script should not install a script if Confirm is not accepted" } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallScriptWithConfirmAndYesToPrompt # @@ -972,24 +958,21 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { # It "InstallScriptWithConfirmAndYesToPrompt" { $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Find-Script Fabrikam-ServerScript | Install-Script -Confirm' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -1005,7 +988,7 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { $res = Get-InstalledScript Fabrikam-ServerScript AssertEquals $res.Name 'Fabrikam-ServerScript' "Install-Script should install a script if Confirm is accepted, $res" } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) <# Purpose: Validate the Get-InstalledScript @@ -1041,11 +1024,11 @@ Describe PowerShell.PSGet.InstallScriptTests -Tags 'BVT','InnerLoop' { } } -Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { +Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1', 'OuterLoop' { - # Not executing these tests on MacOS as + # Not executing these tests on MacOS as # the total execution time is exceeding allowed 50 min in TravisCI daily builds. - if($IsMacOS) { + if ($IsMacOS) { return } @@ -1061,7 +1044,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-InstalledScript -Name Fabrikam-Script -ErrorAction SilentlyContinue | Uninstall-Script -Force Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force Get-InstalledScript -Name Fabrikam-ClientScript -ErrorAction SilentlyContinue | Uninstall-Script -Force - } + } # Purpose: Install a script with prefixed wildcard # @@ -1069,9 +1052,9 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # # Expected Result: should fail # - It "InstallScriptWithPrefixWildCard" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script *kam-ServerScript} ` - -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' + It "InstallScriptWithPrefixWildCard" { + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script *kam-ServerScript } ` + -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' } # Purpose: Install a script with postfixed wildcard @@ -1081,8 +1064,8 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # Expected Result: should fail # It "InstallScriptWithPostfixWildCard" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script Fabrikam-ServerScri*} ` - -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script Fabrikam-ServerScri* } ` + -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' } # Purpose: Install a script with wildcard @@ -1092,8 +1075,8 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # Expected Result: should fail with an error # It "InstallScriptWithWildCards" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script *abrikam-ServerScrip*} ` - -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script *abrikam-ServerScrip* } ` + -expectedFullyQualifiedErrorId 'NameShouldNotContainWildcardCharacters,Install-Script' } # Purpose: InstallScriptWithVersionParams @@ -1103,8 +1086,8 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # Expected Result: Should fail with an error id # It "InstallScriptWithVersionParams" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script Fabrikam-ServerScript -MinimumVersion 1.0 -RequiredVersion 5.0} ` - -expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Install-Script" + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script Fabrikam-ServerScript -MinimumVersion 1.0 -RequiredVersion 5.0 } ` + -expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Install-Script" } # Purpose: InstallMultipleNamesWithReqVersion @@ -1114,8 +1097,8 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # Expected Result: Should fail with an error id # It "InstallMultipleNamesWithReqVersion" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script Fabrikam-ClientScript,Fabrikam-ServerScript -RequiredVersion 2.0} ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Install-Script" + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script Fabrikam-ClientScript, Fabrikam-ServerScript -RequiredVersion 2.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Install-Script" } # Purpose: InstallMultipleNamesWithMinVersion @@ -1125,8 +1108,8 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # Expected Result: Should fail with an error id # It "InstallMultipleNamesWithMinVersion" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script Fabrikam-ClientScript,Fabrikam-ServerScript -MinimumVersion 2.0} ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Install-Script" + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script Fabrikam-ClientScript, Fabrikam-ServerScript -MinimumVersion 2.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Install-Script" } # Purpose: InstallScriptShouldFailIfReqVersionNotAlreadyInstalled @@ -1143,18 +1126,17 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $wv = $null Install-Script $scriptName -RequiredVersion 2.0 -WarningAction SilentlyContinue -WarningVariable wv - $message = $script:LocalizedData.ScriptAlreadyInstalled -f ($InstalledScriptInfo.Version, - $InstalledScriptInfo.Name, - $InstalledScriptInfo.InstalledLocation, - $InstalledScriptInfo.Version, - '2.0') + $message = $script:LocalizedData.ScriptAlreadyInstalled -f ($InstalledScriptInfo.Version, + $InstalledScriptInfo.Name, + $InstalledScriptInfo.InstalledLocation, + $InstalledScriptInfo.Version, + '2.0') # WarningVariable value doesnt get the warning messages on PS 3.0 and 4.0, known issue. - if($PSVersionTable.PSVersion -ge '5.0.0') - { + if ($PSVersionTable.PSVersion -ge '5.0.0') { AssertEqualsCaseInsensitive $wv.Message $message "Install-Script should not re-install a script if it is already installed" } } ` - -Skip:$($PSCulture -ne 'en-US') + -Skip:$($PSCulture -ne 'en-US') # Purpose: InstallScriptShouldFailIfMinVersionNotAlreadyInstalled # @@ -1170,14 +1152,14 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $wv = $null Install-Script $scriptName -MinimumVersion 2.0 -WarningAction SilentlyContinue -WarningVariable wv - $message = $script:LocalizedData.ScriptAlreadyInstalled -f ($InstalledScriptInfo.Version, - $InstalledScriptInfo.Name, - $InstalledScriptInfo.InstalledLocation, - $InstalledScriptInfo.Version, - '2.0') + $message = $script:LocalizedData.ScriptAlreadyInstalled -f ($InstalledScriptInfo.Version, + $InstalledScriptInfo.Name, + $InstalledScriptInfo.InstalledLocation, + $InstalledScriptInfo.Version, + '2.0') Assert ($message -match $scriptName) "Install-Script should not re-install a script if it is already installed, $($wv.Message)" } ` - -Skip:$($PSCulture -ne 'en-US') + -Skip:$($PSCulture -ne 'en-US') # Purpose: InstallPackage_Script_AllUsers_NO_toThePromptForAddingtoPATHVariable # @@ -1196,25 +1178,22 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $null = PackageManagement\Import-PackageProvider -Name PowerShellGet -Force $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 1 is mapped to No in prompt - $Global:proxy.UI.ChoiceToMake=1 + $Global:proxy.UI.ChoiceToMake = 1 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Package -Provider PowerShellGet -Type Script -Name Fabrikam-ServerScript -Source PSGallery' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -1235,7 +1214,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallPackage_Script_AllUsers_YES_toThePromptForAddingtoPATHVariable # @@ -1254,25 +1233,22 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $null = PackageManagement\Import-PackageProvider -Name PowerShellGet -Force $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Package -Provider PowerShellGet -Type Script -Name Fabrikam-ServerScript -Source PSGallery' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -1295,7 +1271,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallPackage_Script_AllUsers_NoPathUpdate_NoPromptForAddingtoPATHVariable # @@ -1328,7 +1304,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$($IsWindows -eq $false) + -Skip:$($IsWindows -eq $false) # Purpose: InstallPackage_Script_Default_User_Force_NoPromptForAddingtoPATHVariable # @@ -1354,8 +1330,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { if ($script:IsCoreCLR) { Assert (($env:PATH -split ';') -contains $script:MyDocumentsScriptsPath) "Install-Package should add CurrentUser scope path to PATH environment variable." } - else - { + else { Assert (($env:PATH -split ';') -contains $script:ProgramFilesScriptsPath) "Install-Package should add AllUsers scope path to PATH environment variable." } } @@ -1366,7 +1341,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$($IsWindows -eq $false) + -Skip:$($IsWindows -eq $false) # Purpose: InstallPackage_Script_CurrentUser_NO_toThePromptForAddingtoPATHVariable # @@ -1384,25 +1359,22 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $null = PackageManagement\Import-PackageProvider -Name PowerShellGet -Force $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 1 is mapped to No in prompt - $Global:proxy.UI.ChoiceToMake=1 + $Global:proxy.UI.ChoiceToMake = 1 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Package -Provider PowerShellGet -Type Script -Name Fabrikam-ServerScript -Source PSGallery -Scope CurrentUser' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -1422,7 +1394,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: InstallPackage_Script_CurrentUser_YES_toThePromptForAddingtoPATHVariable # @@ -1440,25 +1412,22 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $null = PackageManagement\Import-PackageProvider -Name PowerShellGet -Force $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Install-Package -Provider PowerShellGet -Type Script -Name Fabrikam-ServerScript -Source PSGallery -Scope CurrentUser' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -1480,7 +1449,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } } ` - -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) + -Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US')) # Purpose: Install a script from an untrusted repository and press No to the prompt # @@ -1496,32 +1465,27 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { AssertEqualsCaseInsensitive $scriptRepo.ScriptSourceLocation $script:UntrustedRepoSourceLocation "Test repository 'UntrustedTestRepo' is not registered properly" $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 - if($PSVersionTable.PSVersion -ge '4.0.0') - { + if ($PSVersionTable.PSVersion -ge '4.0.0') { # 2 is mapped to NO in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=2 + $Global:proxy.UI.ChoiceToMake = 2 } - else - { + else { # 1 is mapped to No in prompt - $Global:proxy.UI.ChoiceToMake=1 + $Global:proxy.UI.ChoiceToMake = 1 } $content = $null - try - { + try { $result = ExecuteCommand $runspace "Install-Script Fabrikam-ServerScript -Repository UntrustedTestRepo" } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -1539,7 +1503,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-PSRepository -Name UntrustedTestRepo -ErrorAction SilentlyContinue | Unregister-PSRepository -ErrorAction SilentlyContinue } } ` - -Skip:$(($PSCulture -ne 'en-US') -or ($PSVersionTable.PSVersion -lt '4.0.0') -or ($PSEdition -eq 'Core')) + -Skip:$(($PSCulture -ne 'en-US') -or ($PSVersionTable.PSVersion -lt '4.0.0') -or ($PSEdition -eq 'Core')) # Purpose: Install a script from an untrusted repository and press YES to the prompt # @@ -1555,23 +1519,20 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { AssertEqualsCaseInsensitive $scriptRepo.ScriptSourceLocation $script:UntrustedRepoSourceLocation "Test repository 'UntrustedTestRepo' is not registered properly" $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace "Install-Script Fabrikam-ServerScript -Repository UntrustedTestRepo" } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -1589,54 +1550,54 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-PSRepository -Name UntrustedTestRepo -ErrorAction SilentlyContinue | Unregister-PSRepository -ErrorAction SilentlyContinue } } ` - -Skip:$(($PSCulture -ne 'en-US') -or ($PSVersionTable.PSVersion -lt '4.0.0') -or ($PSEdition -eq 'Core')) + -Skip:$(($PSCulture -ne 'en-US') -or ($PSVersionTable.PSVersion -lt '4.0.0') -or ($PSEdition -eq 'Core')) # Get-InstalledScript error cases It ValidateGetInstalledScriptWithMultiNamesAndRequiredVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-ClientScript,Fabrikam-ServerScript -RequiredVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-ClientScript, Fabrikam-ServerScript -RequiredVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" } It ValidateGetInstalledScriptWithMultiNamesAndMinVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-ClientScript,Fabrikam-ServerScript -MinimumVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-ClientScript, Fabrikam-ServerScript -MinimumVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" } It ValidateGetInstalledScriptWithMultiNamesAndMaxVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-ClientScript,Fabrikam-ServerScript -MaximumVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-ClientScript, Fabrikam-ServerScript -MaximumVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" } It ValidateGetInstalledScriptWithSingleWildcardNameAndRequiredVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-Client*ipt -RequiredVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-Client*ipt -RequiredVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" } It ValidateGetInstalledScriptWithSingleWildcardNameAndMinVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-Client*ipt -MinimumVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-Client*ipt -MinimumVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" } It ValidateGetInstalledScriptWithSingleWildcardNameAndMaxVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-Client*ipt -MaximumVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-Client*ipt -MaximumVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Get-InstalledScript" } It ValidateGetInstalledScriptWithSingleNameRequiredandMinVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-ClientScript -RequiredVersion 3.0 -MinimumVersion 1.0 } ` - -expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-ClientScript -RequiredVersion 3.0 -MinimumVersion 1.0 } ` + -expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Get-InstalledScript" } It ValidateGetInstalledScriptWithSingleNameRequiredandMaxVersion { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-ClientScript -RequiredVersion 3.0 -MaximumVersion 1.0 } ` - -expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-ClientScript -RequiredVersion 3.0 -MaximumVersion 1.0 } ` + -expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Get-InstalledScript" } It ValidateGetInstalledScriptWithSingleNameInvalidMinMaxRange { - AssertFullyQualifiedErrorIdEquals -scriptblock {Get-InstalledScript -Name Fabrikam-ClientScript -MinimumVersion 3.0 -MaximumVersion 1.0 } ` - -expectedFullyQualifiedErrorId "MinimumVersionIsGreaterThanMaximumVersion,Get-InstalledScript" + AssertFullyQualifiedErrorIdEquals -scriptblock { Get-InstalledScript -Name Fabrikam-ClientScript -MinimumVersion 3.0 -MaximumVersion 1.0 } ` + -expectedFullyQualifiedErrorId "MinimumVersionIsGreaterThanMaximumVersion,Get-InstalledScript" } - + # Purpose: Validate Install-Script cmdlet with a script with dependencies # # Action: Install-Script -Name Script-WithDependencies1 @@ -1649,15 +1610,14 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $DepScriptDetails = $null $DepModuleDetails = $null - try - { + try { $res1 = Find-Script -Name $ScriptName -MaximumVersion "1.0" -MinimumVersion "0.1" AssertEquals $res1.Name $ScriptName "Find-Script didn't find the exact script which has dependencies, $res1" $DepencyNames = $res1.Dependencies.Name $res2 = Find-Script -Name $ScriptName -IncludeDependencies -MaximumVersion "1.0" -MinimumVersion "0.1" - Assert ($res2.Count -ge ($DepencyNames.Count+1)) "Find-Script with -IncludeDependencies returned wrong results, $res2" + Assert ($res2.Count -ge ($DepencyNames.Count + 1)) "Find-Script with -IncludeDependencies returned wrong results, $res2" Install-Script -Name $ScriptName -MaximumVersion "1.0" -MinimumVersion "0.1" $ActualScriptDetails = Get-InstalledScript -Name $ScriptName -RequiredVersion $res1.Version @@ -1667,21 +1627,19 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $DepModuleDetails = Get-InstalledModule -Name $DepencyNames -ErrorAction SilentlyContinue $DepencyNames | ForEach-Object { - if((-not $DepScriptDetails -or $DepScriptDetails.Name -notcontains $_) -and - (-not $DepModuleDetails -or $DepModuleDetails.Name -notcontains $_)) - { - Assert $false "Script dependency $_ is not installed" - } - } + if ((-not $DepScriptDetails -or $DepScriptDetails.Name -notcontains $_) -and + (-not $DepModuleDetails -or $DepModuleDetails.Name -notcontains $_)) { + Assert $false "Script dependency $_ is not installed" + } + } } - finally - { + finally { Uninstall-Script -ErrorAction SilentlyContinue $ScriptName - $DepScriptDetails | ForEach-Object {Uninstall-Script $_.Name -Force -ErrorAction SilentlyContinue} - $DepModuleDetails | ForEach-Object {PowerShellGet\Uninstall-Module $_.Name -Force -ErrorAction SilentlyContinue} + $DepScriptDetails | ForEach-Object { Uninstall-Script $_.Name -Force -ErrorAction SilentlyContinue } + $DepModuleDetails | ForEach-Object { PowerShellGet\Uninstall-Module $_.Name -Force -ErrorAction SilentlyContinue } } } ` - -Skip:$($PSVersionTable.PSVersion -lt '5.0.0') + -Skip:$($PSVersionTable.PSVersion -lt '5.0.0') # Purpose: Validate Save-Script cmdlet with a script with dependencies # @@ -1694,18 +1652,17 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $ScriptName = 'Script-WithDependencies1' $res1 = Find-Script -Name $ScriptName -RequiredVersion '1.0' -IncludeDependencies - + Save-Script -Name $ScriptName -MaximumVersion '1.0' -MinimumVersion '0.1' $script:TempSavePath - $res1.Name | ForEach-Object { - $artifactPath = Join-Path -Path $script:TempSavePath -ChildPath $_ - if(-not (Test-Path -Path $artifactPath -PathType Container) -and - -not (Test-Path -Path "$artifactPath.ps1" -PathType Leaf)) - { - Assert $false "$_ is not saved with the Save-Script -Name $ScriptName" - } - } - } + $res1.Name | ForEach-Object { + $artifactPath = Join-Path -Path $script:TempSavePath -ChildPath $_ + if (-not (Test-Path -Path $artifactPath -PathType Container) -and + -not (Test-Path -Path "$artifactPath.ps1" -PathType Leaf)) { + Assert $false "$_ is not saved with the Save-Script -Name $ScriptName" + } + } + } finally { Remove-Item -Path "$script:TempSavePath\*" -Recurse -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue } @@ -1724,14 +1681,13 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Find-Script -Name $ScriptName -RequiredVersion '2.0' | Save-Script -LiteralPath $script:TempSavePath $res1.Name | ForEach-Object { - $artifactPath = Join-Path -Path $script:TempSavePath -ChildPath $_ - if(-not (Test-Path -Path $artifactPath -PathType Container) -and - -not (Test-Path -Path "$artifactPath.ps1" -PathType Leaf)) - { - Assert $false "$_ is not saved with the Save-Script -Name $ScriptName" - } - } - } + $artifactPath = Join-Path -Path $script:TempSavePath -ChildPath $_ + if (-not (Test-Path -Path $artifactPath -PathType Container) -and + -not (Test-Path -Path "$artifactPath.ps1" -PathType Leaf)) { + Assert $false "$_ is not saved with the Save-Script -Name $ScriptName" + } + } + } finally { Remove-Item -Path "$script:TempSavePath\*" -Recurse -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue } @@ -1744,14 +1700,14 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # Expected Result: Should get the installed scripts with/without wildcards # It GetInstalledScriptWithWildcard { - $ScriptNames = 'Fabrikam-Script','Fabrikam-ServerScript','Fabrikam-ClientScript' - + $ScriptNames = 'Fabrikam-Script', 'Fabrikam-ServerScript', 'Fabrikam-ClientScript' + Install-Script -Name $ScriptNames # ScriptName without wildcards $res1 = Get-InstalledScript -Name $ScriptNames[0] AssertEquals $res1.Name $ScriptNames[0] "Get-InstalledScript didn't return the exact script, $res1" - + # ScriptName with wildcards $res2 = Get-InstalledScript -Name "Fabrikam*" AssertEquals $res2.count $ScriptNames.Count "Get-InstalledScript didn't return the $ScriptNames scripts, $res2" @@ -1759,16 +1715,15 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { # Purpose: Validate Install-Script cmdlet with same source location registered with NUGet provider # - # Expected Result: Get-InstalledScript should return proper Repository and RepositorySourceLocation values + # Expected Result: Get-InstalledScript should return proper Repository and RepositorySourceLocation values # from the PSScript provider only not from the NuGet provider # It InstallScriptWithSameLocationRegisteredWithNuGetProvider { $ScriptName = 'Fabrikam-ServerScript' $TempNuGetSourceName = "$(Get-Random)" $RepositoryName = "PSGallery" - Register-PackageSource -Provider nuget -Name $TempNuGetSourceName -Location $Global:PSGallerySourceUri -Trusted - try - { + Register-PackageSource -Provider nuget -Name $TempNuGetSourceName -Location $Global:PSGallerySourceUri -Trusted + try { Install-Script -Name $ScriptName -Repository $RepositoryName $res1 = Get-InstalledScript -Name $ScriptName @@ -1778,16 +1733,14 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { AssertEquals $res1.Repository $RepositoryName "PSGetItemInfo object was created with wrong repository name" $expectedInstalledLocation = $script:ProgramFilesScriptsPath - if($script:IsCoreCLR) - { + if ($script:IsCoreCLR) { $expectedInstalledLocation = $script:MyDocumentsScriptsPath } AssertEquals $res1.InstalledLocation $expectedInstalledLocation "Invalid InstalledLocation value on PSGetItemInfo object" } - finally - { + finally { Unregister-PackageSource -ProviderName NuGet -Name $TempNuGetSourceName -Force - } + } } # Purpose: Script cmdlets without ScriptSourceLocation value in Repository. @@ -1805,14 +1758,14 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $repoName = 'TestRepo' - AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script -Name TestScriptName -Repository $repoName} ` - -expectedFullyQualifiedErrorId 'ScriptSourceLocationIsMissing,Find-Script' + AssertFullyQualifiedErrorIdEquals -scriptblock { Find-Script -Name TestScriptName -Repository $repoName } ` + -expectedFullyQualifiedErrorId 'ScriptSourceLocationIsMissing,Find-Script' - AssertFullyQualifiedErrorIdEquals -scriptblock {Install-Script -Name TestScriptName -Repository $repoName} ` - -expectedFullyQualifiedErrorId 'ScriptSourceLocationIsMissing,Install-Script' + AssertFullyQualifiedErrorIdEquals -scriptblock { Install-Script -Name TestScriptName -Repository $repoName } ` + -expectedFullyQualifiedErrorId 'ScriptSourceLocationIsMissing,Install-Script' - AssertFullyQualifiedErrorIdEquals -scriptblock {Save-Script -Name TestScriptName -Repository $repoName -Path $script:TempPath} ` - -expectedFullyQualifiedErrorId 'ScriptSourceLocationIsMissing,Save-Script' + AssertFullyQualifiedErrorIdEquals -scriptblock { Save-Script -Name TestScriptName -Repository $repoName -Path $script:TempPath } ` + -expectedFullyQualifiedErrorId 'ScriptSourceLocationIsMissing,Save-Script' $wv = $null Find-Package -Name TestScriptName -Source $repoName -ProviderName PowerShellGet -Type Script -WarningVariable wv -ErrorAction SilentlyContinue -WarningAction SilentlyContinue $message = $script:LocalizedData.ScriptSourceLocationIsMissing -f ($repoName) @@ -1822,7 +1775,7 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { Get-PSRepository -Name TestRepo -ErrorAction SilentlyContinue | Unregister-PSRepository -ErrorAction SilentlyContinue } } ` - -Skip:$($PSCulture -ne 'en-US') + -Skip:$($PSCulture -ne 'en-US') It "Get-InstalledScript cmdlet with leading zeros in RequiredVersion value" { $scriptName = 'Fabrikam-ServerScript' @@ -1835,4 +1788,4 @@ Describe PowerShell.PSGet.InstallScriptTests.P1 -Tags 'P1','OuterLoop' { $res = Get-InstalledScript $scriptName -RequiredVersion $version $res.Version | Should Be $version } -} \ No newline at end of file +} diff --git a/Tests/PSGetUpdateModule.Tests.ps1 b/Tests/PSGetUpdateModule.Tests.ps1 index 8bf09008..b07528dd 100644 --- a/Tests/PSGetUpdateModule.Tests.ps1 +++ b/Tests/PSGetUpdateModule.Tests.ps1 @@ -28,10 +28,9 @@ function SuiteSetup { $psgetModuleInfo = Import-Module PowerShellGet -Global -Force -Passthru Import-LocalizedData script:LocalizedData -filename PSGet.Resource.psd1 -BaseDirectory $psgetModuleInfo.ModuleBase - $script:moduleSourcesFilePath= Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml" + $script:moduleSourcesFilePath = Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml" $script:moduleSourcesBackupFilePath = Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml_$(get-random)_backup" - if(Test-Path $script:moduleSourcesFilePath) - { + if (Test-Path $script:moduleSourcesFilePath) { Rename-Item $script:moduleSourcesFilePath $script:moduleSourcesBackupFilePath -Force } @@ -43,12 +42,10 @@ function SuiteSetup { } function SuiteCleanup { - if(Test-Path $script:moduleSourcesBackupFilePath) - { + if (Test-Path $script:moduleSourcesBackupFilePath) { Move-Item $script:moduleSourcesBackupFilePath $script:moduleSourcesFilePath -Force } - else - { + else { RemoveItem $script:moduleSourcesFilePath } @@ -192,7 +189,7 @@ Describe UpdateModuleFromAlternateRepo -Tags 'BVT' { } } -Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { +Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT', 'InnerLoop' { BeforeAll { SuiteSetup @@ -218,21 +215,18 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { Install-Module ContosoServer -RequiredVersion $installedVersion $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Import-Module PowerShellGet -Global -Force; Update-Module ContosoServer -WhatIf' } - finally - { + finally { $fileName = "WriteLine-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -241,14 +235,14 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { } $itemInfo = Find-Module ContosoServer -Repository PSGallery - $shouldProcessMessage = ($LocalizedData.UpdateModulewhatIfMessage -replace "__OLDVERSION__",$installedVersion) + $shouldProcessMessage = ($LocalizedData.UpdateModulewhatIfMessage -replace "__OLDVERSION__", $installedVersion) $shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version)) Assert ($content -and ($content -match $shouldProcessMessage)) "update module whatif message is missing, Expected:$shouldProcessMessage, Actual:$content" $res = Get-Module ContosoServer -ListAvailable Assert (($res.Count -eq 1) -and ($res.Name -eq "ContosoServer") -and ($res.Version -eq [Version]"1.0")) "Update-Module should not update the module with -WhatIf option" } ` - -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) + -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) # Purpose: UpdateModuleWithFalseConfirm # @@ -260,12 +254,10 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { Install-Module -Name ContosoServer -RequiredVersion 1.0 Update-Module ContosoServer -Confirm:$false - if(Test-ModuleSxSVersionSupport) - { - $res = Get-Module -FullyQualifiedName @{ModuleName='ContosoServer';ModuleVersion='1.1'} -ListAvailable + if (Test-ModuleSxSVersionSupport) { + $res = Get-Module -FullyQualifiedName @{ModuleName = 'ContosoServer'; ModuleVersion = '1.1' } -ListAvailable } - else - { + else { $res = Get-Module ContosoServer -ListAvailable } @@ -299,10 +291,10 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { # Expected Result: Should fail with error id # It "UpdateMultipleModulesWithReqVersion" { - Install-Module ContosoClient,ContosoServer + Install-Module ContosoClient, ContosoServer - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Module ContosoClient,ContosoServer -RequiredVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Module" + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Module ContosoClient, ContosoServer -RequiredVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Module" } @@ -315,8 +307,8 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { It "UpdateModulesWithReqVersionAndWildcard" { Install-Module ContosoServer - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Module Conto*erver -RequiredVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Module" + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Module Conto*erver -RequiredVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Module" } @@ -341,8 +333,8 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { # Expected Result: Should fail # It "UpdateNotInstalledModule" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Module ModuleNotInstalled} ` - -expectedFullyQualifiedErrorId "ModuleNotInstalledOnThisMachine,Update-Module" + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Module ModuleNotInstalled } ` + -expectedFullyQualifiedErrorId "ModuleNotInstalledOnThisMachine,Update-Module" } @@ -353,8 +345,34 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { # Expected Result: Should fail # It "UpdateAModuleNotInstalledUsingPSGet" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Module PSWorkflow} ` - -expectedFullyQualifiedErrorId "ModuleNotInstalledUsingInstallModuleCmdlet,Update-Module" + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Module PSWorkflow } ` + -expectedFullyQualifiedErrorId "ModuleNotInstalledUsingInstallModuleCmdlet,Update-Module" + } + + + # Purpose: Update a module silently + # + # Action: Update-Module ContosoServer + # + # Expected Result: Update-Module should update the module with RequiredVersion + # + It "Update-Module should be silent" { + Install-Module ContosoServer -RequiredVersion 1.0 + $result = Update-Module ContosoServer + $result | Should -BeNullOrEmpty + } + + + # Purpose: Update a module and return output + # + # Action: Update-Module ContosoServer -PassThru + # + # Expected Result: Update-Module should update the module and return output + # + It "Update-Module should return output" { + Install-Module ContosoServer -RequiredVersion 1.0 + $result = Update-Module ContosoServer -PassThru + $result | Should -Not -BeNullOrEmpty } @@ -368,12 +386,10 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { Install-Module ContosoServer -RequiredVersion 1.0 Update-Module ContosoServer -RequiredVersion 2.0 - if(Test-ModuleSxSVersionSupport) - { - $res = Get-Module -FullyQualifiedName @{ModuleName='ContosoServer';RequiredVersion='2.0'} -ListAvailable + if (Test-ModuleSxSVersionSupport) { + $res = Get-Module -FullyQualifiedName @{ModuleName = 'ContosoServer'; RequiredVersion = '2.0' } -ListAvailable } - else - { + else { $res = Get-Module ContosoServer -ListAvailable } @@ -390,12 +406,10 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { Install-Module ContosoServer Update-Module ContosoServer -RequiredVersion 1.0 -Force - if(Test-ModuleSxSVersionSupport) - { - $res = Get-Module -FullyQualifiedName @{ModuleName='ContosoServer';RequiredVersion='1.0'} -ListAvailable + if (Test-ModuleSxSVersionSupport) { + $res = Get-Module -FullyQualifiedName @{ModuleName = 'ContosoServer'; RequiredVersion = '1.0' } -ListAvailable } - else - { + else { $res = Get-Module ContosoServer -ListAvailable } @@ -424,15 +438,13 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { It "UpdateMultipleModules" { Install-Module ContosoClient -RequiredVersion 1.0 Install-Module ContosoServer -RequiredVersion 1.0 - Update-Module ContosoClient,ContosoServer + Update-Module ContosoClient, ContosoServer - if(Test-ModuleSxSVersionSupport) - { - $res = Get-Module -ListAvailable -FullyQualifiedName @{ModuleName="ContosoServer";ModuleVersion="1.1"},@{ModuleName="ContosoClient";ModuleVersion="1.1"} + if (Test-ModuleSxSVersionSupport) { + $res = Get-Module -ListAvailable -FullyQualifiedName @{ModuleName = "ContosoServer"; ModuleVersion = "1.1" }, @{ModuleName = "ContosoClient"; ModuleVersion = "1.1" } } - else - { - $res = Get-Module -ListAvailable -Name ContosoServer,ContosoClient + else { + $res = Get-Module -ListAvailable -Name ContosoServer, ContosoClient } Assert (($res.Count -eq 2) -and ($res[0].Version -gt [Version]"1.0") -and ($res[1].Version -gt [Version]"1.0")) "Multiple module should be updated" @@ -453,23 +465,21 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' { #if we have other modules not from test repo they will error, keep the noise down but complain about real problems $err | ? { $_.FullyQualifiedErrorId -notmatch "SourceNotFound" } | % { Write-Error $_ } - if(Test-ModuleSxSVersionSupport) - { - $res = Get-Module -ListAvailable -FullyQualifiedName @{ModuleName="ContosoServer";ModuleVersion="1.1"},@{ModuleName="ContosoClient";ModuleVersion="1.1"} + if (Test-ModuleSxSVersionSupport) { + $res = Get-Module -ListAvailable -FullyQualifiedName @{ModuleName = "ContosoServer"; ModuleVersion = "1.1" }, @{ModuleName = "ContosoClient"; ModuleVersion = "1.1" } } - else - { - $res = Get-Module -ListAvailable -Name ContosoServer,ContosoClient + else { + $res = Get-Module -ListAvailable -Name ContosoServer, ContosoClient } Assert (($res.Count -eq 2) -and ($res[0].Version -gt [Version]"1.0") -and ($res[1].Version -gt [Version]"1.0")) "Multiple module should be updated" } } -Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' { +Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1', 'OuterLoop' { # Not executing these tests on MacOS as # the total execution time is exceeding allowed 50 min in TravisCI daily builds. - if($IsMacOS) { + if ($IsMacOS) { return } @@ -527,8 +537,8 @@ Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' { $expectedFullyQualifiedErrorId = 'NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage' - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Module ContosoServer -RequiredVersion 10.0} ` - -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Module ContosoServer -RequiredVersion 10.0 } ` + -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId } # Purpose: UpdateMultipleModulesWithForce @@ -538,9 +548,9 @@ Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' { # Expected Result: both modules should be refreshed # It "UpdateMultipleModulesWithForce" { - Install-Module ContosoClient,ContosoServer + Install-Module ContosoClient, ContosoServer $MyError = $null - Update-Module ContosoClient,ContosoServer -Force -ErrorVariable MyError + Update-Module ContosoClient, ContosoServer -Force -ErrorVariable MyError Assert ($MyError.Count -eq 0) "There should not be any error from force update for multiple modules, $MyError" $res = Get-Module ContosoServer -ListAvailable Assert (($res.Count -eq 1) -and ($res.Name -eq "ContosoServer") -and ($res.Version -gt [Version]"1.0")) "Update-Module should update when multiple modules are specified" @@ -558,12 +568,10 @@ Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' { Install-Module ContosoServer -Scope CurrentUser -RequiredVersion 1.0 Update-Module ContosoServer - if(Test-ModuleSxSVersionSupport) - { - $res = Get-Module -ListAvailable -FullyQualifiedName @{ModuleName="ContosoServer";ModuleVersion="1.1"} + if (Test-ModuleSxSVersionSupport) { + $res = Get-Module -ListAvailable -FullyQualifiedName @{ModuleName = "ContosoServer"; ModuleVersion = "1.1" } } - else - { + else { $res = Get-Module ContosoServer -ListAvailable } @@ -572,11 +580,11 @@ Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' { } } -Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' { +Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2', 'OuterLoop' { # Not executing these tests on MacOS as # the total execution time is exceeding allowed 50 min in TravisCI daily builds. - if($IsMacOS) { + if ($IsMacOS) { return } @@ -605,24 +613,21 @@ Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' { $installedVersion = "1.5" Install-Module ContosoServer -RequiredVersion $installedVersion -Force $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 2 is mapped to NO in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=2 + $Global:proxy.UI.ChoiceToMake = 2 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Import-Module PowerShellGet -Global -Force; Update-Module ContosoServer -Confirm' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -631,14 +636,14 @@ Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' { } $itemInfo = Find-Module ContosoServer -Repository PSGallery - $shouldProcessMessage = ($LocalizedData.UpdateModulewhatIfMessage -replace "__OLDVERSION__",$installedVersion) + $shouldProcessMessage = ($LocalizedData.UpdateModulewhatIfMessage -replace "__OLDVERSION__", $installedVersion) $shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version)) Assert ($content -and ($content -match $shouldProcessMessage)) "update module confirm prompt is not working, Expected:$shouldProcessMessage, Actual:$content" $res = Get-InstalledModule -Name ContosoServer Assert (($res.Name -eq "ContosoServer") -and ($res.Version -eq ([Version]$installedVersion))) "Update-Module should not update the ContosoServer module when pressed NO to Confirm." } ` - -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) + -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) # Purpose: UpdateModuleWithConfirmAndYesToPrompt # @@ -650,24 +655,21 @@ Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' { $installedVersion = "1.0" Install-Module ContosoServer -RequiredVersion $installedVersion $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Import-Module PowerShellGet -Global -Force; Update-Module ContosoServer -Confirm' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -676,22 +678,20 @@ Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' { } $itemInfo = Find-Module ContosoServer -Repository PSGallery - $shouldProcessMessage = ($LocalizedData.UpdateModulewhatIfMessage -replace "__OLDVERSION__",$installedVersion) + $shouldProcessMessage = ($LocalizedData.UpdateModulewhatIfMessage -replace "__OLDVERSION__", $installedVersion) $shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version)) Assert ($content -and ($content -match $shouldProcessMessage)) "update module confirm prompt is not working, Expected:$shouldProcessMessage, Actual:$content" - if(Test-ModuleSxSVersionSupport) - { - $res = Get-Module -FullyQualifiedName @{ModuleName='ContosoServer';ModuleVersion='1.1'} -ListAvailable + if (Test-ModuleSxSVersionSupport) { + $res = Get-Module -FullyQualifiedName @{ModuleName = 'ContosoServer'; ModuleVersion = '1.1' } -ListAvailable } - else - { + else { $res = Get-Module ContosoServer -ListAvailable } Assert (($res.Count -eq 1) -and ($res.Name -eq "ContosoServer") -and ($res.Version -gt [Version]"1.0")) "Update-Module should not update the ContosoServer module when pressed NO to Confirm." } ` - -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) + -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) # Purpose: AdminPrivilegesAreRequiredForUpdatingAllUsersModule # @@ -709,7 +709,7 @@ Update-Module -Name ContosoServer $updatedModule = Get-InstalledModule ContosoServer Assert ($updatedModule.Version -gt 1.0) "Module wasn't updated" } ` - -Skip:$( + -Skip:$( $whoamiValue = (whoami) ($whoamiValue -eq "NT AUTHORITY\SYSTEM") -or @@ -729,15 +729,14 @@ Update-Module -Name ContosoServer $ModuleName = "ModuleWithDependencies2" $DepencyModuleNames = @() - try - { + try { $res1 = Find-Module -Name $ModuleName -RequiredVersion "1.0" AssertEquals $res1.Name $ModuleName "Find-Module didn't find the exact module which has dependencies, $res1" $DepencyModuleNames = $res1.Dependencies.Name $res2 = Find-Module -Name $ModuleName -IncludeDependencies -RequiredVersion "1.0" - Assert ($res2.Count -ge ($DepencyModuleNames.Count+1)) "Find-Module with -IncludeDependencies returned wrong results, $res2" + Assert ($res2.Count -ge ($DepencyModuleNames.Count + 1)) "Find-Module with -IncludeDependencies returned wrong results, $res2" Install-Module -Name $ModuleName -RequiredVersion "1.0" -AllowClobber $ActualModuleDetails = Get-InstalledModule -Name $ModuleName -RequiredVersion $res1.Version @@ -748,56 +747,55 @@ Update-Module -Name ContosoServer Assert ($DepModuleDetails.Count -ge $DepencyModuleNames.Count) "$DepencyModuleNames dependencies is not installed properly" - if($PSVersionTable.PSVersion -ge '5.0.0') - { + if ($PSVersionTable.PSVersion -ge '5.0.0') { $res2 | ForEach-Object { $mod = Get-InstalledModule -Name $_.Name -MinimumVersion $_.Version AssertNotNull $mod "$($_.Name) module is not installed properly" } - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule2'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule2' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion + -MinimumVersion $depModuleDetails.MinimumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with MinimumVersion is not installed properly" - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'RequiredModule2'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'RequiredModule2' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion + -MinimumVersion $depModuleDetails.MinimumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with MinimumVersion is not installed properly" - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule3'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule3' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -RequiredVersion $depModuleDetails.RequiredVersion + -RequiredVersion $depModuleDetails.RequiredVersion AssertNotNull $mod "$($depModuleDetails.Name) module with exact version is not installed properly" AssertEquals $depModuleDetails.RequiredVersion '2.0' "Dependencies details in Find-module output is not proper for $($depModuleDetails.Name)" - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'RequiredModule3'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'RequiredModule3' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -RequiredVersion $depModuleDetails.RequiredVersion + -RequiredVersion $depModuleDetails.RequiredVersion AssertNotNull $mod "$($depModuleDetails.Name) module with exact version is not installed properly" AssertEquals $depModuleDetails.RequiredVersion '2.0' "Dependencies details in Find-module output is not proper for $($depModuleDetails.Name)" - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule4'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule4' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MinimumVersion $depModuleDetails.MinimumVersion ` + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with version range is not installed properly" - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'RequiredModule4'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'RequiredModule4' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MinimumVersion $depModuleDetails.MinimumVersion ` + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with version range is not installed properly" - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule5'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule5' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with maximum version is not installed properly" - $depModuleDetails = $res1.Dependencies | Where-Object {$_.Name -eq 'RequiredModule5'} + $depModuleDetails = $res1.Dependencies | Where-Object { $_.Name -eq 'RequiredModule5' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with maximum version is not installed properly" } @@ -806,7 +804,7 @@ Update-Module -Name ContosoServer AssertEquals $res3.Name $ModuleName "Find-Module didn't find the exact module which has dependencies, $res3" $res4 = Find-Module -Name $ModuleName -IncludeDependencies -RequiredVersion "2.0" - Assert ($res4.Count -ge ($DepencyModuleNames.Count+1)) "Find-Module with -IncludeDependencies returned wrong results, $res4" + Assert ($res4.Count -ge ($DepencyModuleNames.Count + 1)) "Find-Module with -IncludeDependencies returned wrong results, $res4" Update-Module -Name $ModuleName $ActualModuleDetails = Get-InstalledModule -Name $ModuleName -RequiredVersion $res3.Version @@ -816,53 +814,51 @@ Update-Module -Name ContosoServer AssertNotNull $DepModuleDetails "$DepencyModuleNames dependencies is not updated properly" Assert ($DepModuleDetails.Count -ge $DepencyModuleNames.Count) "$DepencyModuleNames dependencies is not installed properly" - if($PSVersionTable.PSVersion -ge '5.0.0') - { - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule2'} + if ($PSVersionTable.PSVersion -ge '5.0.0') { + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule2' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion + -MinimumVersion $depModuleDetails.MinimumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with MinimumVersion is not installed properly" - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'RequiredModule2'} + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'RequiredModule2' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion + -MinimumVersion $depModuleDetails.MinimumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with MinimumVersion is not installed properly" - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule3'} + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule3' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -RequiredVersion $depModuleDetails.RequiredVersion + -RequiredVersion $depModuleDetails.RequiredVersion AssertNotNull $mod "$($depModuleDetails.Name) module with exact version is not updated properly" - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'RequiredModule3'} + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'RequiredModule3' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -RequiredVersion $depModuleDetails.RequiredVersion + -RequiredVersion $depModuleDetails.RequiredVersion AssertNotNull $mod "$($depModuleDetails.Name) module with exact version is not updated properly" - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule4'} + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule4' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MinimumVersion $depModuleDetails.MinimumVersion ` + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with version range is not updated properly" - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'RequiredModule4'} + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'RequiredModule4' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MinimumVersion $depModuleDetails.MinimumVersion ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MinimumVersion $depModuleDetails.MinimumVersion ` + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with version range is not updated properly" - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'NestedRequiredModule5'} + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'NestedRequiredModule5' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with maximum version is not updated properly" - $depModuleDetails = $res3.Dependencies | Where-Object {$_.Name -eq 'RequiredModule5'} + $depModuleDetails = $res3.Dependencies | Where-Object { $_.Name -eq 'RequiredModule5' } $mod = Get-InstalledModule -Name $depModuleDetails.Name ` - -MaximumVersion $depModuleDetails.MaximumVersion + -MaximumVersion $depModuleDetails.MaximumVersion AssertNotNull $mod "$($depModuleDetails.Name) module with maximum version is not updated properly" } } - finally - { + finally { Get-InstalledModule -Name $ModuleName -AllVersions | PowerShellGet\Uninstall-Module -Force $DepencyModuleNames | ForEach-Object { Get-InstalledModule -Name $_ -AllVersions | PowerShellGet\Uninstall-Module -Force } } diff --git a/Tests/PSGetUpdateScript.Tests.ps1 b/Tests/PSGetUpdateScript.Tests.ps1 index 4c9f63d9..82cc3d4e 100644 --- a/Tests/PSGetUpdateScript.Tests.ps1 +++ b/Tests/PSGetUpdateScript.Tests.ps1 @@ -29,10 +29,9 @@ function SuiteSetup { $psgetModuleInfo = Import-Module PowerShellGet -Global -Force -Passthru Import-LocalizedData script:LocalizedData -filename PSGet.Resource.psd1 -BaseDirectory $psgetModuleInfo.ModuleBase - $script:moduleSourcesFilePath= Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml" + $script:moduleSourcesFilePath = Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml" $script:moduleSourcesBackupFilePath = Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml_$(get-random)_backup" - if(Test-Path $script:moduleSourcesFilePath) - { + if (Test-Path $script:moduleSourcesFilePath) { Rename-Item $script:moduleSourcesFilePath $script:moduleSourcesBackupFilePath -Force } @@ -45,17 +44,15 @@ function SuiteSetup { $script:TempSavePath = Join-Path -Path $script:TempPath -ChildPath "PSGet_$(Get-Random)" $null = New-Item -Path $script:TempSavePath -ItemType Directory -Force - $script:AddedAllUsersInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope AllUsers + $script:AddedAllUsersInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope AllUsers $script:AddedCurrentUserInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope CurrentUser } function SuiteCleanup { - if(Test-Path $script:moduleSourcesBackupFilePath) - { + if (Test-Path $script:moduleSourcesBackupFilePath) { Move-Item $script:moduleSourcesBackupFilePath $script:moduleSourcesFilePath -Force } - else - { + else { RemoveItem $script:moduleSourcesFilePath } @@ -65,18 +62,16 @@ function SuiteCleanup { RemoveItem $script:TempSavePath - if($script:AddedAllUsersInstallPath) - { + if ($script:AddedAllUsersInstallPath) { Reset-PATHVariableForScriptsInstallLocation -Scope AllUsers } - if($script:AddedCurrentUserInstallPath) - { + if ($script:AddedCurrentUserInstallPath) { Reset-PATHVariableForScriptsInstallLocation -Scope CurrentUser } } -Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { +Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT', 'InnerLoop' { BeforeAll { SuiteSetup @@ -101,24 +96,21 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { $installedVersion = "1.0" Install-Script Fabrikam-ServerScript -RequiredVersion $installedVersion $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 2 is mapped to NO in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=2 + $Global:proxy.UI.ChoiceToMake = 2 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Update-Script Fabrikam-ServerScript -Confirm' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -127,7 +119,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { } $itemInfo = Find-Script Fabrikam-ServerScript -Repository PSGallery - $shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__",$installedVersion) + $shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__", $installedVersion) $shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version)) Assert ($content -and ($content -match $shouldProcessMessage)) "update script confirm prompt is not working, Expected:$shouldProcessMessage, Actual:$content" @@ -135,7 +127,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { AssertEquals $res.Name 'Fabrikam-ServerScript' "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm." AssertEquals $res.Version $installedVersion "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm." } ` - -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) + -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) # Purpose: UpdateScriptWithConfirmAndYesToPrompt # @@ -147,24 +139,21 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { $installedVersion = '1.0' Install-Script Fabrikam-ServerScript -RequiredVersion $installedVersion $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 # 0 is mapped to YES in ShouldProcess prompt - $Global:proxy.UI.ChoiceToMake=0 + $Global:proxy.UI.ChoiceToMake = 0 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Update-Script Fabrikam-ServerScript -Confirm' } - finally - { + finally { $fileName = "PromptForChoice-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -173,7 +162,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { } $itemInfo = Find-Script Fabrikam-ServerScript -Repository PSGallery - $shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__",$installedVersion) + $shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__", $installedVersion) $shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version)) Assert ($content -and ($content -match $shouldProcessMessage)) "update script confirm prompt is not working, Expected:$shouldProcessMessage, Actual:$content" @@ -181,7 +170,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { AssertEquals $res.Name 'Fabrikam-ServerScript' "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm, $res" Assert ($res.Version -gt [Version]"1.0") "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm, $res" } ` - -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) + -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) # Purpose: UpdateScriptWithWhatIf # @@ -194,21 +183,18 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { Install-Script Fabrikam-ServerScript -RequiredVersion $installedVersion $outputPath = $script:TempPath - $guid = [system.guid]::newguid().tostring() + $guid = [system.guid]::newguid().tostring() $outputFilePath = Join-Path $outputPath "$guid" $runspace = CreateRunSpace $outputFilePath 1 $content = $null - try - { + try { $result = ExecuteCommand $runspace 'Update-Script Fabrikam-ServerScript -WhatIf' } - finally - { + finally { $fileName = "WriteLine-0.txt" $path = join-path $outputFilePath $fileName - if(Test-Path $path) - { + if (Test-Path $path) { $content = get-content $path } @@ -217,7 +203,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { } $itemInfo = Find-Script Fabrikam-ServerScript -Repository PSGallery - $shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__",$installedVersion) + $shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__", $installedVersion) $shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version)) Assert ($content -and ($content -match $shouldProcessMessage)) "update script whatif message is missing, Expected:$shouldProcessMessage, Actual:$content" @@ -225,7 +211,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { AssertEquals $res.Name 'Fabrikam-ServerScript' "Update-Script should not update the script with -WhatIf option, $res" Assert ($res.Version -eq [Version]"1.0") "Update-Script should not update the script with -WhatIf option, $res" } ` - -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) + -Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0')) # Purpose: UpdateScriptWithFalseConfirm # @@ -271,10 +257,10 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { # Expected Result: Should fail with error id # It "UpdateMultipleScriptsWithReqVersion" { - Install-Script Fabrikam-ClientScript,Fabrikam-ServerScript + Install-Script Fabrikam-ClientScript, Fabrikam-ServerScript - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Script Fabrikam-ClientScript,Fabrikam-ServerScript -RequiredVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Script" + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script Fabrikam-ClientScript, Fabrikam-ServerScript -RequiredVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Script" } @@ -287,8 +273,8 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { It "UpdateScriptsWithReqVersionAndWildcard" { Install-Script Fabrikam-ServerScript - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Script Fabrikam-*rScript -RequiredVersion 3.0 } ` - -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Script" + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script Fabrikam-*rScript -RequiredVersion 3.0 } ` + -expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Script" } @@ -339,8 +325,36 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { # Expected Result: Should fail # It "UpdateNotInstalledScript" { - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Script ScriptNotInstalled} ` - -expectedFullyQualifiedErrorId "ScriptNotInstalledOnThisMachine,Update-Script" + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script ScriptNotInstalled } ` + -expectedFullyQualifiedErrorId "ScriptNotInstalledOnThisMachine,Update-Script" + } + + # Purpose: Update a script silently + # + # Action: Update-Script Fabrikam-ServerScript + # + # Expected Result: Update-Script should update the script silently + # + It "Update-Script should be silent" { + $scriptName = 'Fabrikam-ServerScript' + Install-Script $scriptName -RequiredVersion 1.0 + + $result = Update-Script $scriptName + $result | Should -BeNullOrEmpty + } + + # Purpose: Update a script and return output + # + # Action: Update-Script Fabrikam-ServerScript -PassThru + # + # Expected Result: Update-Script should update the script and return output + # + It "Update-Script should return output" { + $scriptName = 'Fabrikam-ServerScript' + Install-Script $scriptName -RequiredVersion 1.0 + + $result = Update-Script $scriptName -PassThru + $result | Should -Not -BeNullOrEmpty } # Purpose: Update a script with requiredversion @@ -369,8 +383,8 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { $expectedFullyQualifiedErrorId = 'NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage' - AssertFullyQualifiedErrorIdEquals -scriptblock {Update-Script Fabrikam-ServerScript -RequiredVersion 10.0} ` - -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId + AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script Fabrikam-ServerScript -RequiredVersion 10.0 } ` + -expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId } @@ -404,8 +418,8 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { $updatedScript = Get-InstalledScript Fabrikam-ServerScript Assert ($updatedScript.Version -gt 1.0) "Update-Script failed to updated script running as non-admin: $content" - } ` - -Skip:$( + } ` + -Skip:$( $whoamiValue = (whoami) ($whoamiValue -eq "NT AUTHORITY\SYSTEM") -or ($whoamiValue -eq "NT AUTHORITY\LOCAL SERVICE") -or @@ -436,9 +450,9 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { It "UpdateMultipleScripts" { Install-Script Fabrikam-ClientScript -RequiredVersion 1.0 Install-Script Fabrikam-ServerScript -RequiredVersion 1.0 - Update-Script Fabrikam-ClientScript,Fabrikam-ServerScript + Update-Script Fabrikam-ClientScript, Fabrikam-ServerScript - $res = Get-InstalledScript -Name Fabrikam-ServerScript,Fabrikam-ClientScript + $res = Get-InstalledScript -Name Fabrikam-ServerScript, Fabrikam-ClientScript Assert (($res.Count -eq 2) -and ($res[0].Version -gt [Version]"1.0") -and ($res[1].Version -gt [Version]"1.0")) "Multiple script should be updated" } @@ -454,9 +468,9 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { Install-Script Fabrikam-ClientScript -RequiredVersion 1.0 Install-Script Fabrikam-ServerScript -RequiredVersion 1.0 Update-Script -ErrorAction SilentlyContinue -ErrorVariable err - $err | ? { $_.FullyQualifiedErrorId -notmatch "NoMatchFoundForCriteria"} | % { Write-Error $_ } + $err | ? { $_.FullyQualifiedErrorId -notmatch "NoMatchFoundForCriteria" } | % { Write-Error $_ } - $res = Get-InstalledScript -Name Fabrikam-ServerScript,Fabrikam-ClientScript + $res = Get-InstalledScript -Name Fabrikam-ServerScript, Fabrikam-ClientScript Assert (($res.Count -eq 2) -and ($res[0].Version -gt [Version]"1.0") -and ($res[1].Version -gt [Version]"1.0")) "Multiple script should be updated" } @@ -468,10 +482,10 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { # Expected Result: both scripts should be refreshed # It "UpdateMultipleScriptsWithForce" { - Install-Script Fabrikam-ClientScript,Fabrikam-ServerScript + Install-Script Fabrikam-ClientScript, Fabrikam-ServerScript $MyError = $null - Update-Script Fabrikam-ClientScript,Fabrikam-ServerScript -Force -ErrorVariable MyError + Update-Script Fabrikam-ClientScript, Fabrikam-ServerScript -Force -ErrorVariable MyError Assert ($MyError.Count -eq 0) "There should not be any error from force update for multiple scripts, $MyError" $res = Get-InstalledScript Fabrikam-ServerScript @@ -516,13 +530,14 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' { Assert (($res.Name -eq $scriptName) -and ($res.Version -gt [Version]"1.0")) "Update-Script should update the script, $res" if ($shouldBeInAllUsers) { AssertEquals $res.InstalledLocation $script:ProgramFilesScriptsPath "Update-Script should put update in all users scope, but updated script base: $($res.InstalledLocation)" - } else { + } + else { AssertEquals $res.InstalledLocation $script:MyDocumentsScriptsPath "Update-Script should put update in current user scope, updated script base: $($res.InstalledLocation)" } } } -Describe PowerShell.PSGet.UpdateScriptTests.P1 -Tags 'P1','OuterLoop' { +Describe PowerShell.PSGet.UpdateScriptTests.P1 -Tags 'P1', 'OuterLoop' { BeforeAll { SuiteSetup @@ -547,51 +562,47 @@ Describe PowerShell.PSGet.UpdateScriptTests.P1 -Tags 'P1','OuterLoop' { $ScriptName = 'Script-WithDependencies2' $NamesToUninstall = @() - try - { + try { $res1 = Find-Script -Name $ScriptName -MaximumVersion "1.0" -MinimumVersion "0.1" AssertEquals $res1.Name $ScriptName "Find-Script didn't find the exact script which has dependencies, $res1" $DepencyNames = $res1.Dependencies.Name $res2 = Find-Script -Name $ScriptName -IncludeDependencies -MaximumVersion "1.0" -MinimumVersion "0.1" - Assert ($res2.Count -ge ($DepencyNames.Count+1)) "Find-Script with -IncludeDependencies returned wrong results, $res2" + Assert ($res2.Count -ge ($DepencyNames.Count + 1)) "Find-Script with -IncludeDependencies returned wrong results, $res2" Install-Script -Name $ScriptName -MaximumVersion "1.0" -MinimumVersion "0.1" $ActualScriptDetails = Get-InstalledScript -Name $ScriptName -RequiredVersion $res1.Version AssertNotNull $ActualScriptDetails "$ScriptName script with dependencies is not installed properly" - $NamesToUninstall += $res2.Name + $NamesToUninstall += $res2.Name $res2 | ForEach-Object { - if(-not (Get-InstalledScript -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue) -and - -not (Get-InstalledModule -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue)) - { - Assert $false "Script dependency $_ is not installed" - } - } + if (-not (Get-InstalledScript -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue) -and + -not (Get-InstalledModule -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue)) { + Assert $false "Script dependency $_ is not installed" + } + } # Find the latest available version $res3 = Find-Script -Name $ScriptName -IncludeDependencies Update-Script -Name $ScriptName - $NamesToUninstall += $res3.Name + $NamesToUninstall += $res3.Name $res3 | ForEach-Object { - if(-not (Get-InstalledScript -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue) -and - -not (Get-InstalledModule -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue)) - { - Assert $false "Script dependency $_ is not updated properly" - } - } + if (-not (Get-InstalledScript -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue) -and + -not (Get-InstalledModule -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue)) { + Assert $false "Script dependency $_ is not updated properly" + } + } } - finally - { + finally { # Uninstall the installed artifacts $NamesToUninstall | ForEach-Object { - PowerShellGet\Uninstall-Script $_ -Force -ErrorAction SilentlyContinue - PowerShellGet\Uninstall-Module $_ -Force -ErrorAction SilentlyContinue - } + PowerShellGet\Uninstall-Script $_ -Force -ErrorAction SilentlyContinue + PowerShellGet\Uninstall-Module $_ -Force -ErrorAction SilentlyContinue + } } } -Skip:$($PSVersionTable.PSVersion -lt '5.0.0') } diff --git a/src/PowerShellGet/public/psgetfunctions/Install-Module.ps1 b/src/PowerShellGet/public/psgetfunctions/Install-Module.ps1 index 4246f182..a3a10eb6 100644 --- a/src/PowerShellGet/public/psgetfunctions/Install-Module.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Install-Module.ps1 @@ -83,7 +83,11 @@ function Install-Module { [Parameter()] [switch] - $AcceptLicense + $AcceptLicense, + + [Parameter()] + [switch] + $PassThru ) Begin { @@ -130,6 +134,7 @@ function Install-Module { $PSBoundParameters[$script:AllowPrereleaseVersions] = $true } $null = $PSBoundParameters.Remove("AllowPrerelease") + $null = $PSBoundParameters.Remove("PassThru") if ($PSCmdlet.ParameterSetName -eq "NameParameterSet") { $ValidationResult = Validate-VersionParameters -CallerPSCmdlet $PSCmdlet ` @@ -155,7 +160,11 @@ function Install-Module { if ($ev) { return } } - $null = PackageManagement\Install-Package @PSBoundParameters + $installedPackages = PackageManagement\Install-Package @PSBoundParameters + + if ($PassThru) { + $installedPackages | Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeModule } + } } elseif ($PSCmdlet.ParameterSetName -eq "InputObject") { $null = $PSBoundParameters.Remove("InputObject") @@ -218,7 +227,7 @@ function Install-Module { #Check if module is already installed $InstalledModuleInfo = Test-ModuleInstalled -Name $psgetModuleInfo.Name -RequiredVersion $psgetModuleInfo.Version - if (-not $Force -and $InstalledModuleInfo -ne $null) { + if (-not $Force -and $null -ne $InstalledModuleInfo) { $message = $LocalizedData.ModuleAlreadyInstalledVerbose -f ($InstalledModuleInfo.Version, $InstalledModuleInfo.Name, $InstalledModuleInfo.ModuleBase) Write-Verbose -Message $message } @@ -249,7 +258,11 @@ function Install-Module { if ($installationPolicy.Equals("trusted", [StringComparison]::OrdinalIgnoreCase) -or $SourceSGrantedTrust.Contains($source) -or $YesToAll -or $Force) { $PSBoundParameters["Force"] = $true - $null = PackageManagement\Install-Package @PSBoundParameters + $installedPackages = PackageManagement\Install-Package @PSBoundParameters + + if ($PassThru) { + $installedPackages | Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeModule } + } } } } diff --git a/src/PowerShellGet/public/psgetfunctions/Install-Script.ps1 b/src/PowerShellGet/public/psgetfunctions/Install-Script.ps1 index 4a5be9fb..8a17e163 100644 --- a/src/PowerShellGet/public/psgetfunctions/Install-Script.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Install-Script.ps1 @@ -1,55 +1,54 @@ -function Install-Script -{ +function Install-Script { <# .ExternalHelp PSModule-help.xml #> - [CmdletBinding(DefaultParameterSetName='NameParameterSet', - HelpUri='https://go.microsoft.com/fwlink/?LinkId=619784', - SupportsShouldProcess=$true)] + [CmdletBinding(DefaultParameterSetName = 'NameParameterSet', + HelpUri = 'https://go.microsoft.com/fwlink/?LinkId=619784', + SupportsShouldProcess = $true)] Param ( - [Parameter(Mandatory=$true, - ValueFromPipelineByPropertyName=$true, - Position=0, - ParameterSetName='NameParameterSet')] + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true, + Position = 0, + ParameterSetName = 'NameParameterSet')] [ValidateNotNullOrEmpty()] [string[]] $Name, - [Parameter(Mandatory=$true, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - Position=0, - ParameterSetName='InputObject')] + [Parameter(Mandatory = $true, + ValueFromPipeline = $true, + ValueFromPipelineByPropertyName = $true, + Position = 0, + ParameterSetName = 'InputObject')] [ValidateNotNull()] [PSCustomObject[]] $InputObject, - [Parameter(ValueFromPipelineByPropertyName=$true, - ParameterSetName='NameParameterSet')] + [Parameter(ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'NameParameterSet')] [ValidateNotNull()] [string] $MinimumVersion, - [Parameter(ValueFromPipelineByPropertyName=$true, - ParameterSetName='NameParameterSet')] + [Parameter(ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'NameParameterSet')] [ValidateNotNull()] [string] $MaximumVersion, - [Parameter(ValueFromPipelineByPropertyName=$true, - ParameterSetName='NameParameterSet')] + [Parameter(ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'NameParameterSet')] [ValidateNotNull()] [string] $RequiredVersion, - [Parameter(ParameterSetName='NameParameterSet')] + [Parameter(ParameterSetName = 'NameParameterSet')] [ValidateNotNullOrEmpty()] [string[]] $Repository, [Parameter()] - [ValidateSet("CurrentUser","AllUsers")] + [ValidateSet("CurrentUser", "AllUsers")] [string] $Scope, @@ -57,16 +56,16 @@ function Install-Script [Switch] $NoPathUpdate, - [Parameter(ValueFromPipelineByPropertyName=$true)] + [Parameter(ValueFromPipelineByPropertyName = $true)] [ValidateNotNullOrEmpty()] [Uri] $Proxy, - [Parameter(ValueFromPipelineByPropertyName=$true)] + [Parameter(ValueFromPipelineByPropertyName = $true)] [PSCredential] $ProxyCredential, - [Parameter(ValueFromPipelineByPropertyName=$true)] + [Parameter(ValueFromPipelineByPropertyName = $true)] [PSCredential] $Credential, @@ -74,54 +73,52 @@ function Install-Script [switch] $Force, - [Parameter(ParameterSetName='NameParameterSet')] + [Parameter(ParameterSetName = 'NameParameterSet')] [switch] $AllowPrerelease, [Parameter()] [switch] - $AcceptLicense + $AcceptLicense, + + [Parameter()] + [switch] + $PassThru ) - Begin - { - if($Scope -eq "AllUsers" -and -not (Test-RunningAsElevated)) - { + Begin { + if ($Scope -eq "AllUsers" -and -not (Test-RunningAsElevated)) { # Throw an error when Install-Script is used as a non-admin user and '-Scope AllUsers' $message = $LocalizedData.InstallScriptAdminPrivilegeRequiredForAllUsersScope -f @($script:ProgramFilesScriptsPath, $script:MyDocumentsScriptsPath) ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $message ` - -ErrorId "InstallScriptAdminPrivilegeRequiredForAllUsersScope" ` - -CallerPSCmdlet $PSCmdlet ` - -ErrorCategory InvalidArgument + -ExceptionMessage $message ` + -ErrorId "InstallScriptAdminPrivilegeRequiredForAllUsersScope" ` + -CallerPSCmdlet $PSCmdlet ` + -ErrorCategory InvalidArgument } # If no scope is specified, default installation will be to AllUsers only # If running admin on Windows with PowerShell less than v6. - if (-not $Scope) - { + if (-not $Scope) { $Scope = "CurrentUser" - if(-not $script:IsCoreCLR -and (Test-RunningAsElevated)) - { + if (-not $script:IsCoreCLR -and (Test-RunningAsElevated)) { $Scope = "AllUsers" } } # Check and add the scope path to PATH environment variable - if($Scope -eq 'AllUsers') - { + if ($Scope -eq 'AllUsers') { $scopePath = $script:ProgramFilesScriptsPath } - else - { + else { $scopePath = $script:MyDocumentsScriptsPath } ValidateAndSet-PATHVariableIfUserAccepts -Scope $Scope ` - -ScopePath $scopePath ` - -NoPathUpdate:$NoPathUpdate ` - -Force:$Force + -ScopePath $scopePath ` + -NoPathUpdate:$NoPathUpdate ` + -Force:$Force Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -Proxy $Proxy -ProxyCredential $ProxyCredential @@ -134,8 +131,7 @@ function Install-Script $SourcesDeniedTrust = @() } - Process - { + Process { $RepositoryIsNotTrusted = $LocalizedData.RepositoryIsNotTrusted $QueryInstallUntrustedPackage = $LocalizedData.QueryInstallUntrustedScriptPackage $PackageTarget = $LocalizedData.InstallScriptwhatIfMessage @@ -144,81 +140,71 @@ function Install-Script $PSBoundParameters["MessageResolver"] = $script:PackageManagementInstallScriptMessageResolverScriptBlock $PSBoundParameters[$script:PSArtifactType] = $script:PSArtifactTypeScript $PSBoundParameters['Scope'] = $Scope - if($AllowPrerelease) { + if ($AllowPrerelease) { $PSBoundParameters[$script:AllowPrereleaseVersions] = $true } $null = $PSBoundParameters.Remove("AllowPrerelease") + $null = $PSBoundParameters.Remove("PassThru") - if($PSCmdlet.ParameterSetName -eq "NameParameterSet") - { + if ($PSCmdlet.ParameterSetName -eq "NameParameterSet") { $ValidationResult = Validate-VersionParameters -CallerPSCmdlet $PSCmdlet ` - -Name $Name ` - -TestWildcardsInName ` - -MinimumVersion $MinimumVersion ` - -MaximumVersion $MaximumVersion ` - -RequiredVersion $RequiredVersion ` - -AllowPrerelease:$AllowPrerelease - - if(-not $ValidationResult) - { + -Name $Name ` + -TestWildcardsInName ` + -MinimumVersion $MinimumVersion ` + -MaximumVersion $MaximumVersion ` + -RequiredVersion $RequiredVersion ` + -AllowPrerelease:$AllowPrerelease + + if (-not $ValidationResult) { # Validate-VersionParameters throws the error. # returning to avoid further execution when different values are specified for -ErrorAction parameter return } - if($PSBoundParameters.ContainsKey("Repository")) - { + if ($PSBoundParameters.ContainsKey("Repository")) { $PSBoundParameters["Source"] = $Repository $null = $PSBoundParameters.Remove("Repository") $ev = $null $repositories = Get-PSRepository -Name $Repository -ErrorVariable ev -verbose:$false - if($ev) { return } + if ($ev) { return } $RepositoriesWithoutScriptSourceLocation = $false - foreach($repo in $repositories) - { - if(-not $repo.ScriptSourceLocation) - { + foreach ($repo in $repositories) { + if (-not $repo.ScriptSourceLocation) { $message = $LocalizedData.ScriptSourceLocationIsMissing -f ($repo.Name) Write-Error -Message $message ` - -ErrorId 'ScriptSourceLocationIsMissing' ` - -Category InvalidArgument ` - -TargetObject $repo.Name ` - -Exception 'System.ArgumentException' + -ErrorId 'ScriptSourceLocationIsMissing' ` + -Category InvalidArgument ` + -TargetObject $repo.Name ` + -Exception 'System.ArgumentException' $RepositoriesWithoutScriptSourceLocation = $true } } - if($RepositoriesWithoutScriptSourceLocation) - { + if ($RepositoriesWithoutScriptSourceLocation) { return } } - if(-not $Force) - { - foreach($scriptName in $Name) - { + if (-not $Force) { + foreach ($scriptName in $Name) { # Throw an error if there is a command with the same name and -force is not specified. $cmd = Microsoft.PowerShell.Core\Get-Command -Name $scriptName ` - -ErrorAction Ignore ` - -WarningAction SilentlyContinue - if($cmd) - { + -ErrorAction Ignore ` + -WarningAction SilentlyContinue + if ($cmd) { # Check if this script was already installed, may be with -Force $InstalledScriptInfo = Test-ScriptInstalled -Name $scriptName ` - -ErrorAction SilentlyContinue ` - -WarningAction SilentlyContinue - if(-not $InstalledScriptInfo) - { + -ErrorAction SilentlyContinue ` + -WarningAction SilentlyContinue + if (-not $InstalledScriptInfo) { $message = $LocalizedData.CommandAlreadyAvailable -f ($scriptName) Write-Error -Message $message -ErrorId CommandAlreadyAvailableWitScriptName -Category InvalidOperation # return if only single name is specified - if($scriptName -eq $Name) - { + if ($scriptName -eq $Name) { return } } @@ -226,39 +212,38 @@ function Install-Script } } - $null = PackageManagement\Install-Package @PSBoundParameters + $installedPackages = PackageManagement\Install-Package @PSBoundParameters + + if ($PassThru) { + $installedPackages | Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeScript } + } } - elseif($PSCmdlet.ParameterSetName -eq "InputObject") - { + elseif ($PSCmdlet.ParameterSetName -eq "InputObject") { $null = $PSBoundParameters.Remove("InputObject") - foreach($inputValue in $InputObject) - { + foreach ($inputValue in $InputObject) { if (($inputValue.PSTypeNames -notcontains "Microsoft.PowerShell.Commands.PSRepositoryItemInfo") -and - ($inputValue.PSTypeNames -notcontains "Deserialized.Microsoft.PowerShell.Commands.PSRepositoryItemInfo")) - { + ($inputValue.PSTypeNames -notcontains "Deserialized.Microsoft.PowerShell.Commands.PSRepositoryItemInfo")) { ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $LocalizedData.InvalidInputObjectValue ` - -ErrorId "InvalidInputObjectValue" ` - -CallerPSCmdlet $PSCmdlet ` - -ErrorCategory InvalidArgument ` - -ExceptionObject $inputValue + -ExceptionMessage $LocalizedData.InvalidInputObjectValue ` + -ErrorId "InvalidInputObjectValue" ` + -CallerPSCmdlet $PSCmdlet ` + -ErrorCategory InvalidArgument ` + -ExceptionObject $inputValue } $psRepositoryItemInfo = $inputValue # Skip the script name if it is already tried in the current pipeline - if($scriptNamesInPipeline -contains $psRepositoryItemInfo.Name) - { + if ($scriptNamesInPipeline -contains $psRepositoryItemInfo.Name) { continue } $scriptNamesInPipeline += $psRepositoryItemInfo.Name if ($psRepositoryItemInfo.PowerShellGetFormatVersion -and - ($script:SupportedPSGetFormatVersionMajors -notcontains $psRepositoryItemInfo.PowerShellGetFormatVersion.Major)) - { + ($script:SupportedPSGetFormatVersionMajors -notcontains $psRepositoryItemInfo.PowerShellGetFormatVersion.Major)) { $message = $LocalizedData.NotSupportedPowerShellGetFormatVersionScripts -f ($psRepositoryItemInfo.Name, $psRepositoryItemInfo.PowerShellGetFormatVersion, $psRepositoryItemInfo.Name) Write-Error -Message $message -ErrorId "NotSupportedPowerShellGetFormatVersion" -Category InvalidOperation continue @@ -278,21 +263,17 @@ function Install-Script $PSBoundParameters["PackageManagementProvider"] = (Get-ProviderName -PSCustomObject $psRepositoryItemInfo) $InstalledScriptInfo = Test-ScriptInstalled -Name $psRepositoryItemInfo.Name - if(-not $Force -and $InstalledScriptInfo) - { + if (-not $Force -and $InstalledScriptInfo) { $message = $LocalizedData.ScriptAlreadyInstalledVerbose -f ($InstalledScriptInfo.Version, $InstalledScriptInfo.Name, $InstalledScriptInfo.ScriptBase) Write-Verbose -Message $message } - else - { + else { # Throw an error if there is a command with the same name and -force is not specified. - if(-not $Force) - { + if (-not $Force) { $cmd = Microsoft.PowerShell.Core\Get-Command -Name $psRepositoryItemInfo.Name ` - -ErrorAction Ignore ` - -WarningAction SilentlyContinue - if($cmd) - { + -ErrorAction Ignore ` + -WarningAction SilentlyContinue + if ($cmd) { $message = $LocalizedData.CommandAlreadyAvailable -f ($psRepositoryItemInfo.Name) Write-Error -Message $message -ErrorId CommandAlreadyAvailableWitScriptName -Category InvalidOperation @@ -300,45 +281,41 @@ function Install-Script } } - $source = $psRepositoryItemInfo.Repository + $source = $psRepositoryItemInfo.Repository $installationPolicy = (Get-PSRepository -Name $source).InstallationPolicy $ShouldProcessMessage = $PackageTarget -f ($psRepositoryItemInfo.Name, $psRepositoryItemInfo.Version) - if($psCmdlet.ShouldProcess($ShouldProcessMessage)) - { - if($installationPolicy.Equals("Untrusted", [StringComparison]::OrdinalIgnoreCase)) - { - if(-not($YesToAll -or $NoToAll -or $SourceSGrantedTrust.Contains($source) -or $sourcesDeniedTrust.Contains($source) -or $Force)) - { + if ($psCmdlet.ShouldProcess($ShouldProcessMessage)) { + if ($installationPolicy.Equals("Untrusted", [StringComparison]::OrdinalIgnoreCase)) { + if (-not($YesToAll -or $NoToAll -or $SourceSGrantedTrust.Contains($source) -or $sourcesDeniedTrust.Contains($source) -or $Force)) { $message = $QueryInstallUntrustedPackage -f ($psRepositoryItemInfo.Name, $psRepositoryItemInfo.RepositorySourceLocation) - if($PSVersionTable.PSVersion -ge '5.0.0') - { - $sourceTrusted = $psCmdlet.ShouldContinue("$message", "$RepositoryIsNotTrusted",$true, [ref]$YesToAll, [ref]$NoToAll) + if ($PSVersionTable.PSVersion -ge '5.0.0') { + $sourceTrusted = $psCmdlet.ShouldContinue("$message", "$RepositoryIsNotTrusted", $true, [ref]$YesToAll, [ref]$NoToAll) } - else - { + else { $sourceTrusted = $psCmdlet.ShouldContinue("$message", "$RepositoryIsNotTrusted", [ref]$YesToAll, [ref]$NoToAll) } - if($sourceTrusted) - { - $SourcesGrantedTrust+=$source + if ($sourceTrusted) { + $SourcesGrantedTrust += $source } - else - { - $SourcesDeniedTrust+=$source + else { + $SourcesDeniedTrust += $source } } - } - } - if($installationPolicy.Equals("trusted", [StringComparison]::OrdinalIgnoreCase) -or $SourcesGrantedTrust.Contains($source) -or $YesToAll -or $Force) - { + } + } + if ($installationPolicy.Equals("trusted", [StringComparison]::OrdinalIgnoreCase) -or $SourcesGrantedTrust.Contains($source) -or $YesToAll -or $Force) { $PSBoundParameters["Force"] = $true - $null = PackageManagement\Install-Package @PSBoundParameters - } + $installedPackages = PackageManagement\Install-Package @PSBoundParameters + + if ($PassThru) { + $installedPackages | Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeScript } + } + } } } } } -} \ No newline at end of file +} diff --git a/src/PowerShellGet/public/psgetfunctions/Update-Module.ps1 b/src/PowerShellGet/public/psgetfunctions/Update-Module.ps1 index 92adff7d..f5c68d58 100644 --- a/src/PowerShellGet/public/psgetfunctions/Update-Module.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Update-Module.ps1 @@ -50,21 +50,25 @@ function Update-Module { [Parameter()] [switch] - $AcceptLicense + $AcceptLicense, + + [Parameter()] + [switch] + $PassThru ) Begin { Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -Proxy $Proxy -ProxyCredential $ProxyCredential if ($Scope -eq "AllUsers" -and -not (Test-RunningAsElevated)) { - # Throw an error when Update-Module is used as a non-admin user and '-Scope AllUsers' - $message = $LocalizedData.UpdateModuleAdminPrivilegeRequiredForAllUsersScope -f @($script:programFilesModulesPath, $script:MyDocumentsModulesPath) - - ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage $message ` - -ErrorId "UpdateModuleAdminPrivilegeRequiredForAllUsersScope" ` - -CallerPSCmdlet $PSCmdlet ` - -ErrorCategory InvalidArgument + # Throw an error when Update-Module is used as a non-admin user and '-Scope AllUsers' + $message = $LocalizedData.UpdateModuleAdminPrivilegeRequiredForAllUsersScope -f @($script:programFilesModulesPath, $script:MyDocumentsModulesPath) + + ThrowError -ExceptionName "System.ArgumentException" ` + -ExceptionMessage $message ` + -ErrorId "UpdateModuleAdminPrivilegeRequiredForAllUsersScope" ` + -CallerPSCmdlet $PSCmdlet ` + -ErrorCategory InvalidArgument } # Module names already tried in the current pipeline @@ -84,7 +88,7 @@ function Update-Module { return } - $GetPackageParameters = @{} + $GetPackageParameters = @{ } $GetPackageParameters[$script:PSArtifactType] = $script:PSArtifactTypeModule $GetPackageParameters["Provider"] = $script:PSModuleProviderName $GetPackageParameters["MessageResolver"] = $script:PackageManagementMessageResolverScriptBlock @@ -94,6 +98,7 @@ function Update-Module { $PSBoundParameters[$script:AllowPrereleaseVersions] = $true } $null = $PSBoundParameters.Remove("AllowPrerelease") + $null = $PSBoundParameters.Remove("PassThru") $PSGetItemInfos = @() @@ -121,8 +126,8 @@ function Update-Module { } $installedPackages | - Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeModule} | - Microsoft.PowerShell.Core\ForEach-Object { $PSGetItemInfos += $_ } + Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeModule } | + Microsoft.PowerShell.Core\ForEach-Object { $PSGetItemInfos += $_ } } $PSBoundParameters["Provider"] = $script:PSModuleProviderName @@ -152,12 +157,16 @@ function Update-Module { $PSBoundParameters["InstallUpdate"] = $true if (-not $Scope) { - $Scope = Get-InstallationScope -PreviousInstallLocation $psgetItemInfo.InstalledLocation -CurrentUserPath $script:MyDocumentsModulesPath + $Scope = Get-InstallationScope -PreviousInstallLocation $psgetItemInfo.InstalledLocation -CurrentUserPath $script:MyDocumentsModulesPath } $PSBoundParameters["Scope"] = $Scope $sid = PackageManagement\Install-Package @PSBoundParameters + + if ($PassThru) { + $sid | Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeModule } + } } } } diff --git a/src/PowerShellGet/public/psgetfunctions/Update-Script.ps1 b/src/PowerShellGet/public/psgetfunctions/Update-Script.ps1 index 75cf9db5..0e285bff 100644 --- a/src/PowerShellGet/public/psgetfunctions/Update-Script.ps1 +++ b/src/PowerShellGet/public/psgetfunctions/Update-Script.ps1 @@ -45,7 +45,11 @@ function Update-Script { [Parameter()] [switch] - $AcceptLicense + $AcceptLicense, + + [Parameter()] + [switch] + $PassThru ) Begin { @@ -70,7 +74,7 @@ function Update-Script { return } - if(-not $Name) { + if (-not $Name) { $Name = @('*') } @@ -87,7 +91,7 @@ function Update-Script { foreach ($scriptFilePath in $availableScriptPaths) { # Check if this script got installed with PowerShellGet $installedScriptFilePath = Get-InstalledScriptFilePath -Name ([System.IO.Path]::GetFileNameWithoutExtension($scriptFilePath)) | - Microsoft.PowerShell.Core\Where-Object {$_ -eq $scriptFilePath } + Microsoft.PowerShell.Core\Where-Object { $_ -eq $scriptFilePath } if ($installedScriptFilePath) { $scriptFilePathsToUpdate += $installedScriptFilePath @@ -162,9 +166,14 @@ function Update-Script { $PSBoundParameters[$script:AllowPrereleaseVersions] = $true } $null = $PSBoundParameters.Remove("AllowPrerelease") + $null = $PSBoundParameters.Remove("PassThru") $PSBoundParameters["Scope"] = Get-InstallationScope -PreviousInstallLocation $scriptFilePath -CurrentUserPath $script:MyDocumentsScriptsPath $sid = PackageManagement\Install-Package @PSBoundParameters + + if ($PassThru) { + $sid | Microsoft.PowerShell.Core\ForEach-Object { New-PSGetItemInfo -SoftwareIdentity $_ -Type $script:PSArtifactTypeScript } + } } } }