Skip to content

Commit

Permalink
Merge pull request chocolatey#2969 from vexx32/tests/unsupported-meta…
Browse files Browse the repository at this point in the history
…data

(chocolatey#508) Add Pester tests for unsupported metadata
  • Loading branch information
gep13 authored Jan 13, 2023
2 parents 7788542 + 51a8f8f commit 9f1188f
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
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
}

0 comments on commit 9f1188f

Please sign in to comment.