Skip to content
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
4 changes: 3 additions & 1 deletion src/System.Management.Automation/engine/PSVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions test/powershell/Host/PSVersionTable.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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
}
}
}
54 changes: 54 additions & 0 deletions test/powershell/Language/Scripting/Requires.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<Name>" -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 "<Name>" -TestCase $filesFailTestCase {
param(
$Name,
$File
)

{ . $File } | Should -Throw -ExceptionType ([System.Management.Automation.ScriptRequiresException])
}
}
}

Describe "#requires -Modules" -Tags "CI" {
Expand Down