Skip to content

Add updates to complete Tests for Install, Save, Publish, Get-Installed, and Uninstall tests. #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions src/code/UninstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,41 +154,48 @@ private bool UninstallPkgHelper()
continue;
}

ErrorRecord errRecord = null;
if (pkgPath.EndsWith(".ps1"))
{
successfullyUninstalled = UninstallScriptHelper(pkgPath, pkgName);
successfullyUninstalled = UninstallScriptHelper(pkgPath, pkgName, out errRecord);
}
else
{
successfullyUninstalled = UninstallModuleHelper(pkgPath, pkgName);
successfullyUninstalled = UninstallModuleHelper(pkgPath, pkgName, out errRecord);
}

// if we can't find the resource, write non-terminating error and return
if (!successfullyUninstalled)
if (!successfullyUninstalled || errRecord != null)
{
string message = Version == null || Version.Trim().Equals("*") ?
string.Format("Could not find any version of the resource '{0}' in any path", pkgName) :
string.Format("Could not find verison '{0}' of the resource '{1}' in any path", Version, pkgName);

WriteError(new ErrorRecord(
new PSInvalidOperationException(message),
"ErrorRetrievingSpecifiedResource",
ErrorCategory.ObjectNotFound,
this));
if (errRecord == null)
{
string message = Version == null || Version.Trim().Equals("*") ?
string.Format("Could not find any version of the resource '{0}' in any path", pkgName) :
string.Format("Could not find verison '{0}' of the resource '{1}' in any path", Version, pkgName);

errRecord = new ErrorRecord(
new PSInvalidOperationException(message),
"ErrorRetrievingSpecifiedResource",
ErrorCategory.ObjectNotFound,
this);
}

WriteError(errRecord);
}
}

return successfullyUninstalled;
}

/* uninstalls a module */
private bool UninstallModuleHelper(string pkgPath, string pkgName)
private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorRecord errRecord)
{
errRecord = null;
var successfullyUninstalledPkg = false;

// if -Force is not specified and the pkg is a dependency for another package,
// an error will be written and we return false
if (!Force && CheckIfDependency(pkgName))
if (!Force && CheckIfDependency(pkgName, out errRecord))
{
return false;
}
Expand Down Expand Up @@ -216,23 +223,24 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName)
var exMessage = String.Format("Parent directory '{0}' could not be deleted: {1}", dir.Parent.FullName, e.Message);
var ex = new ArgumentException(exMessage);
var ErrorDeletingParentDirectory = new ErrorRecord(ex, "ErrorDeletingParentDirectory", ErrorCategory.InvalidArgument, null);
WriteError(ErrorDeletingParentDirectory);
errRecord = ErrorDeletingParentDirectory;
}
}
catch (Exception err) {
// write error
var exMessage = String.Format("Directory '{0}' could not be deleted: {1}", dir.FullName, err.Message);
var ex = new ArgumentException(exMessage);
var ErrorDeletingDirectory = new ErrorRecord(ex, "ErrorDeletingDirectory", ErrorCategory.PermissionDenied, null);
WriteError(ErrorDeletingDirectory);
errRecord = ErrorDeletingDirectory;
}

return successfullyUninstalledPkg;
}

/* uninstalls a script */
private bool UninstallScriptHelper(string pkgPath, string pkgName)
private bool UninstallScriptHelper(string pkgPath, string pkgName, out ErrorRecord errRecord)
{
errRecord = null;
var successfullyUninstalledPkg = false;

// delete the appropriate file
Expand All @@ -258,21 +266,22 @@ private bool UninstallScriptHelper(string pkgPath, string pkgName)
var exMessage = String.Format("Script metadata file '{0}' could not be deleted: {1}", scriptXML, e.Message);
var ex = new ArgumentException(exMessage);
var ErrorDeletingScriptMetadataFile = new ErrorRecord(ex, "ErrorDeletingScriptMetadataFile", ErrorCategory.PermissionDenied, null);
WriteError(ErrorDeletingScriptMetadataFile);
errRecord = ErrorDeletingScriptMetadataFile;
}
}
catch (Exception err){
var exMessage = String.Format("Script '{0}' could not be deleted: {1}", pkgPath, err.Message);
var ex = new ArgumentException(exMessage);
var ErrorDeletingScript = new ErrorRecord(ex, "ErrorDeletingScript", ErrorCategory.PermissionDenied, null);
WriteError(ErrorDeletingScript);
errRecord = ErrorDeletingScript;
}

return successfullyUninstalledPkg;
}

private bool CheckIfDependency(string pkgName)
private bool CheckIfDependency(string pkgName, out ErrorRecord errRecord)
{
errRecord = null;
// this is a primitive implementation
// TODO: implement a dependencies database for querying dependency info
// cannot uninstall a module if another module is dependent on it
Expand All @@ -296,7 +305,7 @@ private bool CheckIfDependency(string pkgName)
var exMessage = String.Format("Error checking if resource is a dependency: {0}. If you would still like to uninstall, rerun the command with -Force", e.Message);
var ex = new ArgumentException(exMessage);
var DependencyCheckError = new ErrorRecord(ex, "DependencyCheckError", ErrorCategory.OperationStopped, null);
WriteError(DependencyCheckError);
errRecord = DependencyCheckError;
}

if (pkgsWithRequiredModules.Any())
Expand All @@ -307,7 +316,8 @@ private bool CheckIfDependency(string pkgName)
var exMessage = String.Format("Cannot uninstall '{0}', the following package(s) take a dependency on this package: {1}. If you would still like to uninstall, rerun the command with -Force", pkgName, strUniquePkgNames);
var ex = new ArgumentException(exMessage);
var PackageIsaDependency = new ErrorRecord(ex, "PackageIsaDependency", ErrorCategory.OperationStopped, null);
WriteError(PackageIsaDependency);
errRecord = PackageIsaDependency;

return true;
}
}
Expand Down
52 changes: 27 additions & 25 deletions test/GetInstalledPSResource.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

<# // Temporarily commenting out until Install-PSResource is complete
Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force

Describe 'Test Get-InstalledPSResource for Module' {
Expand All @@ -10,8 +9,11 @@ Describe 'Test Get-InstalledPSResource for Module' {
$TestGalleryName = Get-PoshTestGalleryName
Get-NewPSResourceRepositoryFile

Register-PSRepository $TestGalleryName -SourceLocation "https://www.poshtestgallery.com/api/v2"
Install-Module ContosoServer -Repository $TestGalleryName
Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository
Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "2.0"
Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "1.5"
Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "1.0"
Install-PSResource TestTestScript -Repository $TestGalleryName -TrustRepository
}

AfterAll {
Expand All @@ -25,39 +27,40 @@ Describe 'Test Get-InstalledPSResource for Module' {

It "Get specific module resource by name" {
$pkg = Get-InstalledPSResource -Name ContosoServer
$pkg.Name | Should -Be "ContosoServer"
$pkg.Name | Should -Contain "ContosoServer"
}

It "Get specific script resource by name" {
$pkg = Get-InstalledPSResource -Name adsql
$pkg.Name | Should -Be "adsql"
$pkg = Get-InstalledPSResource -Name TestTestScript
$pkg.Name | Should -Be "TestTestScript"
}

It "Get resource when given Name to <Reason> <Version>" -TestCases @(
@{Name="*ShellG*"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard at beginning and end of name: *ShellG*"},
@{Name="PowerShell*"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard at end of name: PowerShellG*"},
@{Name="*ShellGet"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard at beginning of name: *ShellGet"},
@{Name="Power*Get"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard in middle of name: Power*Get"}
@{Name="*tosoSer*"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at beginning and end of name: *tosoSer*"},
@{Name="ContosoSer*"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at end of name: ContosoSer*"},
@{Name="*tosoServer"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at beginning of name: *tosoServer"},
@{Name="Cont*erver"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard in middle of name: Cont*erver"}
) {
param($Version, $ExpectedVersion)
$pkgs = Get-InstalledPSResource -Name $Name
$pkgs.Name | Should -Be "PowerShellGet"
$pkgs.Name | Should -Contain "ContosoServer"
}

It "Get resource when given Name to <Reason> <Version>" -TestCases @(
@{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"},
@{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"},
@{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion="2.5.0.0"; Reason="validate version, exact range inclusive"},
@{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact range exclusive"},
@{Version="(1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version exclusive"},
@{Version="[1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version inclusive"},
@{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"},
@{Version="(,1.5.0.0]"; ExpectedVersion="1.5.0.0"; Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
) {
$testCases =
@{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"},
@{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"},
@{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, exact range inclusive"},
@{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion=@("2.0.0.0", "1.5.0.0"); Reason="validate version, exact range exclusive"},
@{Version="(1.0.0.0,)"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0"); Reason="validate version, minimum version exclusive"},
@{Version="[1.0.0.0,)"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, minimum version inclusive"},
@{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"},
@{Version="(,1.5.0.0]"; ExpectedVersion=@("1.5.0.0", "1.0.0.0"); Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion=@("2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"}

It "Get resource when given Name to <Reason> <Version>" -TestCases $testCases {
param($Version, $ExpectedVersion)
$pkgs = Get-InstalledPSResource -Name "ContosoServer" -Version $Version
$pkgs.Name | Should -Be "ContosoServer"
$pkgs.Name | Should -Contain "ContosoServer"
$pkgs.Version | Should -Be $ExpectedVersion
}

Expand Down Expand Up @@ -100,8 +103,7 @@ Describe 'Test Get-InstalledPSResource for Module' {
}

It "Get resources when given Name, and Version is '*'" {
$pkgs = Get-InstalledPSResource -Name Carbon -Version "*"
$pkgs = Get-InstalledPSResource -Name ContosoServer -Version "*"
$pkgs.Count | Should -BeGreaterOrEqual 2
}
}
#>
33 changes: 30 additions & 3 deletions test/InstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,56 @@ Describe 'Test Install-PSResource for Module' {
}

# Windows only
It "Install resource under CurrentUser scope" -Skip:(!$IsWindows) {
It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope CurrentUser
$pkg = Get-Module "TestModule" -ListAvailable
$pkg.Name | Should -Be "TestModule"
$pkg.Path.Contains("Documents") | Should -Be $true
}

# Windows only
It "Install resource under AllUsers scope" -Skip:(!$IsWindows) {
It "Install resource under AllUsers scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope AllUsers
$pkg = Get-Module "TestModule" -ListAvailable
$pkg.Name | Should -Be "TestModule"
$pkg.Path.Contains("Program Files") | Should -Be $true
}

# Windows only
It "Install resource under no specified scope" -Skip:(!$IsWindows) {
It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name "TestModule" -Repository $TestGalleryName
$pkg = Get-Module "TestModule" -ListAvailable
$pkg.Name | Should -Be "TestModule"
$pkg.Path.Contains("Documents") | Should -Be $true
}

# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It "Install resource under CurrentUser scope - Unix only" -Skip:(Get-IsWindows) {
Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope CurrentUser
$pkg = Get-Module "TestModule" -ListAvailable
$pkg.Name | Should -Be "TestModule"
$pkg.Path.Contains("/home/") | Should -Be $true
}

# Unix only
# Expected path should be similar to: '/usr/local/share/powershell/Modules'
It "Install resource under AllUsers scope - Unix only" -Skip:(Get-IsWindows) {
Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope AllUsers
$pkg = Get-Module "TestModule" -ListAvailable
$pkg.Name | Should -Be "TestModule"
$pkg.Path.Contains("/usr/") | Should -Be $true
}

# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It "Install resource under no specified scope - Unix only" -Skip:(Get-IsWindows) {
Install-PSResource -Name "TestModule" -Repository $TestGalleryName
$pkg = Get-Module "TestModule" -ListAvailable
$pkg.Name | Should -Be "TestModule"
$pkg.Path.Contains("/home/") | Should -Be $true
}

It "Should not install resource that is already installed" {
Install-PSResource -Name "TestModule" -Repository $TestGalleryName
$pkg = Get-Module "TestModule" -ListAvailable
Expand Down
10 changes: 9 additions & 1 deletion test/PSGetTestUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ $script:EnvPATHValueBackup = $null

$script:PowerShellGet = 'PowerShellGet'
$script:IsInbox = $PSHOME.EndsWith('\WindowsPowerShell\v1.0', [System.StringComparison]::OrdinalIgnoreCase)
$script:IsWindows = (-not (Get-Variable -Name IsWindows -ErrorAction Ignore)) -or $IsWindows
$script:IsWindows = $IsWindows
if ($IsWindows -eq $null) {
$script:IsWindows = ($PSVersionTable.PSVersion.Major -eq 5)
}

$script:IsLinux = (Get-Variable -Name IsLinux -ErrorAction Ignore) -and $IsLinux
$script:IsMacOS = (Get-Variable -Name IsMacOS -ErrorAction Ignore) -and $IsMacOS
$script:IsCoreCLR = $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTable.PSEdition -eq 'Core'
Expand Down Expand Up @@ -96,6 +100,10 @@ $script:moduleSourcesFilePath = Microsoft.PowerShell.Management\Join-Path -Path
$script:CurrentPSGetFormatVersion = "1.0"
$script:PSGetFormatVersionPrefix = "PowerShellGetFormatVersion_"

function Get-IsWindows {
return $script:IsWindows
}

function Get-AllUsersModulesPath {
return $script:ProgramFilesModulesPath
}
Expand Down
Loading