Skip to content
New issue

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

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

Already on GitHub? # to your account

(#508) Add Pester tests for unsupported metadata #2969

Merged
merged 1 commit into from
Jan 13, 2023
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
64 changes: 64 additions & 0 deletions tests/chocolatey-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,70 @@ Describe "choco install" -Tag Chocolatey, InstallCommand {
}
}

Context 'Installing a package with unsupported nuspec elements shows a warning' {

BeforeDiscovery {
$testCases = @(
'<license>'
'<packageTypes>'
'<readme>'
'<repository>'
'<serviceable>'
)
}

BeforeAll {
Restore-ChocolateyInstallSnapshot
$nuspec = @'
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>unsupportedmetadata</id>
<version>1.0.0</version>
<title>unsupportedmetadata (Install)</title>
<authors>Chocolatey Software</authors>
<tags>unsupportedmetadata</tags>

<license type="expression">MIT</license>
<packageTypes>
<packageType name="Unsupported" />
</packageTypes>
<readme>readme.md</readme>
<repository type="git" url="https://github.com/chocolatey/choco.git" />
<serviceable>true</serviceable>

<summary>Test of unsupported metadata</summary>
<description>Some metadata fields are not supported by chocolatey. `choco pack` should fail to pack them, while `choco install` or `upgrade` should allow them with a warning.</description>
</metadata>
</package>
'@
$tempPath = "$env:TEMP/$(New-Guid)"
$packageName = 'unsupportedmetadata'
$nuspecPath = "$tempPath/$packageName/$packageName.nuspec"

$null = New-Item -Path "$tempPath/$packageName" -ItemType Directory
$nuspec | Set-Content -Path $nuspecPath
"readme content" | Set-Content -Path "$tempPath/$packageName/readme.md"

$null = Invoke-Choco install nuget.commandline
$null = & "$env:ChocolateyInstall/bin/nuget.exe" pack $nuspecPath

$Output = Invoke-Choco install $packageName --source $tempPath
}

AfterAll {
Remove-Item $tempPath -Recurse -Force
}

It 'Installs successfully and exits with success (0)' {
$Output.ExitCode | Should -Be 0
}

It 'Shows a warning about the unsupported nuspec metadata element "<_>"' -TestCases $testCases {
$Output.String | Should -Match "$_ elements are not supported in Chocolatey CLI"
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
Test-NuGetPaths
}
56 changes: 56 additions & 0 deletions tests/chocolatey-tests/commands/choco-pack.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,62 @@ Describe "choco pack" -Tag Chocolatey, PackCommand {
}
}

Context 'Attempting to pack a package with unsupported nuspec elements throws an error' {

BeforeDiscovery {
$testCases = @(
'<license>'
'<packageTypes>'
'<readme>'
'<repository>'
'<serviceable>'
)
}

BeforeAll {
Restore-ChocolateyInstallSnapshot
$nuspec = @'
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>unsupportedmetadata</id>
<version>1.0.0</version>
<title>unsupportedmetadata (Install)</title>
<authors>Chocolatey Software</authors>
<tags>unsupportedmetadata</tags>

<license type="expression">MIT</license>
<packageTypes>
<packageType name="Unsupported" />
</packageTypes>
<readme>readme.md</readme>
<repository type="git" url="https://github.com/chocolatey/choco.git" />
<serviceable>true</serviceable>

<summary>Test of unsupported metadata</summary>
<description>Some metadata fields are not supported by chocolatey. `choco pack` should fail to pack them, while `choco install` or `upgrade` should allow them with a warning.</description>
</metadata>
</package>
'@
$packageName = 'unsupportedmetadata'
$nuspecPath = "$packageName\$packageName.nuspec"

$null = New-Item -Path $packageName -ItemType Directory
$nuspec | Set-Content -Path $nuspecPath
"readme content" | Set-Content -Path "$packageName\readme.md"

$Output = Invoke-Choco pack $nuspecPath
}

It 'Fails to pack and exits with an error (1)' {
$Output.ExitCode | Should -Be 1
}

It 'Shows an error about the unsupported nuspec metadata element "<_>"' -TestCases $testCases {
$Output.String | Should -Match "$_ elements are not supported in Chocolatey CLI"
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
Test-NuGetPaths
}
63 changes: 63 additions & 0 deletions tests/chocolatey-tests/commands/choco-upgrade.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,70 @@ Describe "choco upgrade" -Tag Chocolatey, UpgradeCommand {
}
}

Context 'Upgrading a package with unsupported nuspec elements shows a warning' {

BeforeDiscovery {
$testCases = @(
'<license>'
'<packageTypes>'
'<readme>'
'<repository>'
'<serviceable>'
)
}

BeforeAll {
Restore-ChocolateyInstallSnapshot
$nuspec = @'
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>unsupportedmetadata</id>
<version>1.0.0</version>
<title>unsupportedmetadata (Install)</title>
<authors>Chocolatey Software</authors>
<tags>unsupportedmetadata</tags>

<license type="expression">MIT</license>
<packageTypes>
<packageType name="Unsupported" />
</packageTypes>
<readme>readme.md</readme>
<repository type="git" url="https://github.com/chocolatey/choco.git" />
<serviceable>true</serviceable>

<summary>Test of unsupported metadata</summary>
<description>Some metadata fields are not supported by chocolatey. `choco pack` should fail to pack them, while `choco install` or `upgrade` should allow them with a warning.</description>
</metadata>
</package>
'@
$tempPath = "$env:TEMP/$(New-Guid)"
$packageName = 'unsupportedmetadata'
$nuspecPath = "$tempPath/$packageName/$packageName.nuspec"

$null = New-Item -Path "$tempPath/$packageName" -ItemType Directory
$nuspec | Set-Content -Path $nuspecPath
"readme content" | Set-Content -Path "$tempPath/$packageName/readme.md"

$null = Invoke-Choco install nuget.commandline
$null = & "$env:ChocolateyInstall/bin/nuget.exe" pack $nuspecPath

$Output = Invoke-Choco upgrade $packageName --source $tempPath
}

AfterAll {
Remove-Item $tempPath -Recurse -Force
}

It 'Installs successfully and exits with success (0)' {
$Output.ExitCode | Should -Be 0
}

It 'Shows a warning about the unsupported nuspec metadata element "<_>"' -TestCases $testCases {
$Output.String | Should -Match "$_ elements are not supported in Chocolatey CLI"
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
Test-NuGetPaths
}