diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 4f49ac8646c..ef58dafb56a 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -59,6 +59,8 @@ public class PSVersionInfo private static readonly Version s_psV5Version = new Version(5, 0); private static readonly Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, preReleaseLabel: null, buildLabel: null); + private static readonly SemanticVersion s_psV61Version = new SemanticVersion(6, 1, 0, preReleaseLabel: null, buildLabel: null); + private static readonly SemanticVersion s_psV62Version = new SemanticVersion(6, 2, 0, preReleaseLabel: null, buildLabel: null); private static readonly SemanticVersion s_psSemVersion; private static readonly Version s_psVersion; @@ -105,7 +107,7 @@ static PSVersionInfo() s_psVersionTable[PSVersionInfo.PSVersionName] = s_psSemVersion; s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue; s_psVersionTable[PSGitCommitIdName] = rawGitCommitId; - s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psVersion }; + s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psV61Version, s_psV62Version, s_psVersion }; s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion); s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion; s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion(); diff --git a/test/powershell/Host/PSVersionTable.Tests.ps1 b/test/powershell/Host/PSVersionTable.Tests.ps1 index 573a0a8216e..8806d8ebf29 100644 --- a/test/powershell/Host/PSVersionTable.Tests.ps1 +++ b/test/powershell/Host/PSVersionTable.Tests.ps1 @@ -22,6 +22,10 @@ Describe "PSVersionTable" -Tags "CI" { $expectedGitCommitIdPattern = "^$mainVersionPattern$" $unexpectectGitCommitIdPattern = $fullVersionPattern } + + $powerShellVersions = "1.0", "2.0", "3.0", "4.0", "5.0", "5.1", "6.0", "6.1", "6.2", "7.0" + $powerShellCompatibleVersions = $PSVersionTable.PSCompatibleVersions | + ForEach-Object {$_.ToString(2).SubString(0,3)} } It "Should have version table entries" { @@ -158,4 +162,16 @@ Describe "PSVersionTable" -Tags "CI" { $PSVersionTable.Add("PSEdition", $EditionValue) } } + + It "Verify PSCompatibleVersions has an entry for all known versions of PowerShell" { + foreach ($version in $powerShellVersions) { + $version | Should -BeIn $powerShellCompatibleVersions + } + } + + It "Verify PSCompatibleVersions has no unknown PowerShell entries" { + foreach ($version in $powerShellCompatibleVersions) { + $version | Should -BeIn $powerShellVersions + } + } } diff --git a/test/powershell/Language/Scripting/Requires.Tests.ps1 b/test/powershell/Language/Scripting/Requires.Tests.ps1 index d86e683e26c..03d44accb94 100644 --- a/test/powershell/Language/Scripting/Requires.Tests.ps1 +++ b/test/powershell/Language/Scripting/Requires.Tests.ps1 @@ -36,6 +36,60 @@ Describe "Requires tests" -Tags "CI" { { $ps.AddScript("#requires").Invoke(@(), $settings) } | Should -Not -Throw } } + + Context "Version checks" { + BeforeAll { + $currentVersion = $PSVersionTable.PSVersion + + $powerShellVersions = "1.0", "2.0", "3.0", "4.0", "5.0", "5.1", "6.0", "6.1", "6.2", "7.0" + $latestVersion = [version]($powerShellVersions | Sort-Object -Descending -Top 1) + $nonExistingMinor = "$($latestVersion.Major).$($latestVersion.Minor + 1)" + $nonExistingMajor = "$($latestVersion.Major + 1).0" + + foreach ($version in ($powerShellVersions + $nonExistingMinor + $nonExistingMajor)) { + $filePath = Join-Path -Path $TestDrive -ChildPath "$version.ps1" + $null = New-Item -Path $filePath -Value "#requires -version $version" + } + + $filesSuccessTestCase = foreach ($version in $powerShellVersions) { + @{ + Name = "Check for version $version" + File = Join-Path -Path $TestDrive -ChildPath "$version.ps1" + Version = $version + } + } + + $filesFailTestCase = foreach ($version in @($nonExistingMinor) + @($nonExistingMajor)) { + @{ + Name = "Check for version $version" + File = Join-Path -Path $TestDrive -ChildPath "$version.ps1" + } + } + } + + It "" -TestCase $filesSuccessTestCase { + param( + $Name, + $File, + $Version + ) + + if ($currentVersion -notmatch '^7' -and $Version -match '^7') { + Set-ItResult -Skipped -Because "Test not valid for current version - $currentVersion and test version = $Version" + } + + { . $File } | Should -Not -Throw + } + + It "" -TestCase $filesFailTestCase { + param( + $Name, + $File + ) + + { . $File } | Should -Throw -ExceptionType ([System.Management.Automation.ScriptRequiresException]) + } + } } Describe "#requires -Modules" -Tags "CI" {