Skip to content

Publish-PSResource Script publishing support #642

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 25 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
560835d
uninstall specify version for perf increase
anamnavi May 3, 2022
b015489
support for publishing scripts with correct ps1 parsing
anamnavi May 5, 2022
8a808c4
check for local repo Uri not being valid before attempting to publish
anamnavi May 11, 2022
560e8f7
have ParseScript() return boolean and take out variables as params
anamnavi May 11, 2022
0d7db4c
check for local repo uri existing must be done against Uri.LocalPath
anamnavi May 11, 2022
61649e6
typo fix
anamnavi May 11, 2022
4255975
error handling for unsupported source paths
anamnavi May 11, 2022
51c4ed7
use constant string instad of hardcoded value in code
anamnavi May 11, 2022
4e8fa9b
clean up
anamnavi May 12, 2022
e014295
add comment
anamnavi May 12, 2022
0034acc
better error handling and method definition for PackNupkg()
anamnavi May 12, 2022
9debdda
resolve merge conflicts
anamnavi May 12, 2022
88ed85a
revert changes to Uninstall tests and take from master
anamnavi May 12, 2022
20610af
add test files for invalid script test files to test against
anamnavi May 12, 2022
b609ce6
revert changes to Get-ModuleResourcePublishedToLocalDrive() in PSGetT…
anamnavi May 13, 2022
415ca94
fix typo
anamnavi May 13, 2022
3a1842b
update comment
anamnavi May 13, 2022
b103ca6
cant use -AdditionalChildPath for windowsPowerShell
anamnavi May 16, 2022
7e9efca
pull Publish -RequiredModules fix from master, merge conflicts
anamnavi May 18, 2022
3952af9
PR feedback- add example script comment back
anamnavi May 18, 2022
f96ce55
PR feedback- remove comment
anamnavi May 18, 2022
c284f4d
remove unncessary comments in PSGEtTestUtils
anamnavi May 18, 2022
81bcb3e
add space after .PRIVATEDATA key in test script that gets created
anamnavi May 18, 2022
b7091b0
revert unintentional change to Uninstall tests
anamnavi May 18, 2022
d1820d5
pull in changes from master, merge conflicts
anamnavi May 19, 2022
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
439 changes: 273 additions & 166 deletions src/code/PublishPSResource.cs

Large diffs are not rendered by default.

28 changes: 18 additions & 10 deletions test/PSGetTestUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -315,33 +315,36 @@ function Get-ScriptResourcePublishedToLocalRepoTestDrive
{
Param(
[string]
$scriptName
$scriptName,

[string]
$scriptRepoName,

[string]
$scriptVersion
)
Get-TestDriveSetUp

$scriptFilePath = Join-Path -Path $script:testIndividualResourceFolder -ChildPath "$scriptName.ps1"
$null = New-Item -Path $scriptFilePath -ItemType File -Force

$version = "1.0.0"
$params = @{
#Path = $scriptFilePath
Version = $version
#GUID =
Version = $scriptVersion
GUID = [guid]::NewGuid()
Author = 'Jane'
CompanyName = 'Microsoft Corporation'
Copyright = '(c) 2020 Microsoft Corporation. All rights reserved.'
Description = "Description for the $scriptName script"
LicenseUri = "https://$scriptName.com/license"
IconUri = "https://$scriptName.com/icon"
ProjectUri = "https://$scriptName.com"
Tags = @('Tag1','Tag2', "Tag-$scriptName-$version")
Tags = @('Tag1','Tag2', "Tag-$scriptName-$scriptVersion")
ReleaseNotes = "$scriptName release notes"
}

$scriptMetadata = Create-PSScriptMetadata @params
Set-Content -Path $scriptFilePath -Value $scriptMetadata

Publish-PSResource -path $scriptFilePath -Repository psgettestlocal
Publish-PSResource -Path $scriptFilePath -Repository $scriptRepoName -Verbose
}

function Get-CommandResourcePublishedToLocalRepoTestDrive
Expand Down Expand Up @@ -507,8 +510,6 @@ function Create-PSScriptMetadata

.COPYRIGHT$(if ($Copyright) {" $Copyright"})

.DESCRIPTION$(if ($Description) {" $Description"})

.TAGS$(if ($Tags) {" $Tags"})

.LICENSEURI$(if ($LicenseUri) {" $LicenseUri"})
Expand All @@ -528,6 +529,13 @@ $($ReleaseNotes -join "`r`n")

.PRIVATEDATA$(if ($PrivateData) {" $PrivateData"})

#>

<#

.DESCRIPTION
$(if ($Description) {" $Description"})

#>
"@
return $PSScriptInfoString
Expand Down
118 changes: 111 additions & 7 deletions test/PublishPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,32 @@ Describe "Test Publish-PSResource" {
$script:destinationPath = [IO.Path]::GetFullPath((Join-Path -Path $TestDrive -ChildPath "tmpDestinationPath"))
New-Item $script:destinationPath -ItemType directory -Force

# Path to folder, within our test folder, where we store invalid module files used for testing
#Create folder where we shall place all script files to be published for these tests
$script:tmpScriptsFolderPath = Join-Path -Path $TestDrive -ChildPath "tmpScriptsPath"
if(!(Test-Path $script:tmpScriptsFolderPath))
{
New-Item -Path $script:tmpScriptsFolderPath -ItemType Directory -Force
}

# Path to folder, within our test folder, where we store invalid module and script files used for testing
$script:testFilesFolderPath = Join-Path $psscriptroot -ChildPath "testFiles"

# Path to specifically to that invalid test modules folder
$script:testModulesFolderPath = Join-Path $testFilesFolderPath -ChildPath "testModules"

# Path to specifically to that invalid test scripts folder
$script:testScriptsFolderPath = Join-Path $testFilesFolderPath -ChildPath "testScripts"
}
AfterAll {
# Get-RevertPSResourceRepositoryFile
Get-RevertPSResourceRepositoryFile
}
AfterEach {
# Delete all contents of the repository without deleting the repository directory itself
# $pkgsToDelete = Join-Path -Path "$script:repositoryPath" -ChildPath "*"
# Remove-Item $pkgsToDelete -Recurse
# Delete all contents of the repository without deleting the repository directory itself
$pkgsToDelete = Join-Path -Path "$script:repositoryPath" -ChildPath "*"
Remove-Item $pkgsToDelete -Recurse

# $pkgsToDelete = Join-Path -Path "$script:repositoryPath2" -ChildPath "*"
# Remove-Item $pkgsToDelete -Recurse
$pkgsToDelete = Join-Path -Path "$script:repositoryPath2" -ChildPath "*"
Remove-Item $pkgsToDelete -Recurse
}

It "Publish a module with -Path to the highest priority repo" {
Expand Down Expand Up @@ -290,6 +302,98 @@ Describe "Test Publish-PSResource" {
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}

It "publish a script locally"{
$scriptName = "PSGetTestScript"
$scriptVersion = "1.0.0"

$params = @{
Version = $scriptVersion
GUID = [guid]::NewGuid()
Author = 'Jane'
CompanyName = 'Microsoft Corporation'
Copyright = '(c) 2020 Microsoft Corporation. All rights reserved.'
Description = "Description for the $scriptName script"
LicenseUri = "https://$scriptName.com/license"
IconUri = "https://$scriptName.com/icon"
ProjectUri = "https://$scriptName.com"
Tags = @('Tag1','Tag2', "Tag-$scriptName-$scriptVersion")
ReleaseNotes = "$scriptName release notes"
}

$scriptPath = (Join-Path -Path $script:tmpScriptsFolderPath -ChildPath "$scriptName.ps1")
New-ScriptFileInfo @params -Path $scriptPath

Publish-PSResource -Path $scriptPath

$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
}

It "should write error and not publish script when Author property is missing" {
$scriptName = "InvalidScriptMissingAuthor.ps1"
$scriptVersion = "1.0.0"

$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "MissingAuthorInScriptMetadata,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource"

$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}

It "should write error and not publish script when Version property is missing" {
$scriptName = "InvalidScriptMissingVersion.ps1"

$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "MissingVersionInScriptMetadata,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource"

$publishedPkgs = Get-ChildItem -Path $script:repositoryPath -Filter *.nupkg
$publishedPkgs.Count | Should -Be 0
}

It "should write error and not publish script when Guid property is missing" {
$scriptName = "InvalidScriptMissingGuid.ps1"
$scriptVersion = "1.0.0"

$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "MissingGuidInScriptMetadata,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource"

$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}

It "should write error and not publish script when Description property is missing" {
$scriptName = "InvalidScriptMissingDescription.ps1"
$scriptVersion = "1.0.0"

$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "MissingOrInvalidDescriptionInScriptMetadata,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource"

$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}

It "should write error and not publish script when Description block altogether is missing" {
# we expect .ps1 files to have a separate comment block for .DESCRIPTION property, not to be included in the PSScriptInfo commment block
$scriptName = "InvalidScriptMissingDescriptionCommentBlock.ps1"
$scriptVersion = "1.0.0"

$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PSScriptMissingHelpContentCommentBlock,Microsoft.PowerShell.PowerShellGet.Cmdlets.PublishPSResource"

$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}

It "Publish a module with that has an invalid version format, should throw" {
$moduleName = "incorrectmoduleversion"
$incorrectmoduleversion = Join-Path -Path $script:testModulesFolderPath -ChildPath $moduleName
Expand Down
43 changes: 43 additions & 0 deletions test/testFiles/testScripts/InvalidScriptMissingAuthor.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

<#PSScriptInfo

.VERSION 1.0

.GUID 3951be04-bd06-4337-8dc3-a620bf539fbd

.AUTHOR

.COMPANYNAME

.COPYRIGHT

.TAGS

.LICENSEURI

.PROJECTURI

.ICONURI

.EXTERNALMODULEDEPENDENCIES

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES

.RELEASENOTES


.PRIVATEDATA

#>

<#

.DESCRIPTION
this is a test for a script that will be published remotely

#>
Param()


42 changes: 42 additions & 0 deletions test/testFiles/testScripts/InvalidScriptMissingDescription.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

<#PSScriptInfo

.VERSION 1.0

.GUID 3951be04-bd06-4337-8dc3-a620bf539fbd

.AUTHOR annavied

.COMPANYNAME

.COPYRIGHT

.TAGS

.LICENSEURI

.PROJECTURI

.ICONURI

.EXTERNALMODULEDEPENDENCIES

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES

.RELEASENOTES


.PRIVATEDATA

#>

<#

.DESCRIPTION

#>
Param()


Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

<#PSScriptInfo

.VERSION 1.0

.GUID 3951be04-bd06-4337-8dc3-a620bf539fbd

.AUTHOR annavied

.COMPANYNAME

.COPYRIGHT

.TAGS

.LICENSEURI

.PROJECTURI

.ICONURI

.EXTERNALMODULEDEPENDENCIES

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES

.RELEASENOTES


.PRIVATEDATA

#>

Param()


43 changes: 43 additions & 0 deletions test/testFiles/testScripts/InvalidScriptMissingGuid.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

<#PSScriptInfo

.VERSION 1.0

.GUID

.AUTHOR annavied

.COMPANYNAME

.COPYRIGHT

.TAGS

.LICENSEURI

.PROJECTURI

.ICONURI

.EXTERNALMODULEDEPENDENCIES

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES

.RELEASENOTES


.PRIVATEDATA

#>

<#

.DESCRIPTION
this is a test for a script that will be published remotely

#>
Param()


Loading