Skip to content

Commit 7c33758

Browse files
author
Robert Holt
committed
Change PowerShell json to new version type, add test build
1 parent c6fd12b commit 7c33758

File tree

5 files changed

+103
-39
lines changed

5 files changed

+103
-39
lines changed

CrossCompatibility/CrossCompatibility/Utility/PowerShellVersion.cs renamed to CrossCompatibility/CrossCompatibility/Common/PowerShellVersion.cs

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Text;
33
using Newtonsoft.Json;
44

5-
namespace Microsoft.PowerShell.CrossCompatibility.Utility
5+
namespace Microsoft.PowerShell.CrossCompatibility
66
{
77
public class PowerShellVersion
88
{
@@ -11,12 +11,17 @@ public static PowerShellVersion Create(dynamic versionInput)
1111
switch (versionInput)
1212
{
1313
case Version systemVersion:
14-
return new PowerShellVersion(systemVersion);
14+
return (PowerShellVersion)systemVersion;
1515

1616
case string versionString:
1717
return Parse(versionString);
1818
}
1919

20+
if (versionInput.BuildLabel != null)
21+
{
22+
return new PowerShellVersion(versionInput.Major, versionInput.Minor, versionInput.Patch, $"{versionInput.PreReleaseLabel}+{versionInput.BuildLabel}");
23+
}
24+
2025
return new PowerShellVersion(versionInput.Major, versionInput.Minor, versionInput.Patch, versionInput.PreReleaseLabel);
2126
}
2227

@@ -27,7 +32,7 @@ public static PowerShellVersion Parse(string versionStr)
2732
throw new ArgumentNullException(nameof(versionStr));
2833
}
2934

30-
int[] versionParts = new int[3];
35+
int[] versionParts = new int[3] { -1, -1, -1 };
3136

3237
int sectionStartOffset = 0;
3338
int dotCount = 0;
@@ -69,7 +74,7 @@ public static PowerShellVersion Parse(string versionStr)
6974

7075
versionParts[dotCount] = int.Parse(versionStr.Substring(sectionStartOffset, i - sectionStartOffset));
7176

72-
return new PowerShellVersion(versionParts[0], versionParts[1], versionParts[2], preReleaseLabel: null);
77+
return new PowerShellVersion(versionParts[0], versionParts[1], versionParts[2], label: null);
7378
}
7479

7580
public static bool TryParse(string versionStr, out PowerShellVersion version)
@@ -86,45 +91,75 @@ public static bool TryParse(string versionStr, out PowerShellVersion version)
8691
}
8792
}
8893

89-
public static explicit operator Version(PowerShellVersion psVersion)
94+
public static void ValidateVersionArguments(int major, int minor, int build, int revision, string preReleaseLabel)
9095
{
91-
if (psVersion.PreReleaseLabel != null)
96+
if (major < 0)
9297
{
93-
throw new InvalidCastException($"Cannot convert version '{psVersion}' to System.Version, since there is a pre-release label");
98+
throw new ArgumentException();
99+
}
100+
101+
if (minor < 0 && (build >= 0 || revision >= 0))
102+
{
103+
throw new ArgumentException();
94104
}
95105

96-
if (psVersion.Revision != null)
106+
if (build < 0 && revision >= 0)
97107
{
98-
return new Version(psVersion.Major, psVersion.Minor, psVersion.Patch, psVersion.Revision.Value);
108+
throw new ArgumentException();
99109
}
100110

101-
return new Version(psVersion.Major, psVersion.Minor, psVersion.Patch);
111+
if (revision >= 0 && preReleaseLabel != null)
112+
{
113+
throw new ArgumentException();
114+
}
115+
}
116+
117+
public static explicit operator Version(PowerShellVersion psVersion)
118+
{
119+
if (psVersion.PreReleaseLabel != null)
120+
{
121+
throw new InvalidCastException($"Cannot convert version '{psVersion}' to System.Version, since there is a pre-release label");
122+
}
123+
124+
return new Version(psVersion.Major, psVersion.Minor, psVersion.Patch, psVersion.Revision);
102125
}
103126

104127
public static explicit operator PowerShellVersion(string versionString)
105128
{
106129
return PowerShellVersion.Parse(versionString);
107130
}
108131

109-
public PowerShellVersion(Version version)
110-
: this(version.Major, version.Minor, version.Build, version.Revision)
132+
public static explicit operator PowerShellVersion(Version version)
111133
{
134+
return new PowerShellVersion(version.Major, version.Minor, version.Build, version.Revision);
112135
}
113136

114137
public PowerShellVersion(int major, int minor, int build, int revision)
115138
{
139+
ValidateVersionArguments(major, minor, build, revision, preReleaseLabel: null);
116140
Major = major;
117141
Minor = minor;
118142
Build = build;
119143
Revision = revision;
120144
}
121145

122-
public PowerShellVersion(int major, int minor, int patch, string preReleaseLabel)
146+
public PowerShellVersion(int major, int minor, int patch, string label)
123147
{
148+
ValidateVersionArguments(major, minor, patch, -1, label);
124149
Major = major;
125150
Minor = minor;
126151
Build = patch;
127-
PreReleaseLabel = preReleaseLabel;
152+
153+
int plusIdx = label?.IndexOf('+') ?? -1;
154+
if (plusIdx < 0)
155+
{
156+
PreReleaseLabel = label;
157+
}
158+
else
159+
{
160+
PreReleaseLabel = label.Substring(0, plusIdx);
161+
BuildLabel = label.Substring(plusIdx + 1, label.Length - plusIdx - 1);
162+
}
128163
}
129164

130165
public int Major { get; }
@@ -135,25 +170,37 @@ public PowerShellVersion(int major, int minor, int patch, string preReleaseLabel
135170

136171
public int Patch => Build;
137172

138-
public int? Revision { get; }
173+
public int Revision { get; }
139174

140175
public string PreReleaseLabel { get; }
141176

142-
public bool IsSemVer => Revision == null;
177+
public string BuildLabel { get; }
178+
179+
public bool IsSemVer => Revision < 0;
143180

144181
public override string ToString()
145182
{
146-
if (!IsSemVer)
183+
var sb = new StringBuilder(Major);
184+
185+
if (Minor < 0)
186+
{
187+
return sb.ToString();
188+
}
189+
190+
sb.Append('.').Append(Minor);
191+
192+
if (Build < 0)
147193
{
148-
return $"{Major}.{Minor}.{Build}.{Revision}";
194+
return sb.ToString();
149195
}
150196

151-
var sb = new StringBuilder()
152-
.Append(Major).Append('.')
153-
.Append(Minor).Append('.')
154-
.Append(Patch);
197+
sb.Append('.').Append(Build);
155198

156-
if (!string.IsNullOrEmpty(PreReleaseLabel))
199+
if (Revision >= 0)
200+
{
201+
sb.Append('.').Append(Revision);
202+
}
203+
else if (PreReleaseLabel != null)
157204
{
158205
sb.Append('-').Append(PreReleaseLabel);
159206
}
@@ -167,7 +214,6 @@ public class PowerShellVersionJsonConverter : JsonConverter
167214
public override bool CanConvert(Type objectType)
168215
{
169216
return objectType == typeof(PowerShellVersion)
170-
|| objectType == typeof(Version)
171217
|| objectType.FullName == "System.Management.Automation.SemanticVersion";
172218
}
173219

CrossCompatibility/CrossCompatibility/Data/Platform/PowerShellData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PowerShellData : ICloneable
1616
/// From $PSVersionTable.PSVersion.
1717
/// </summary>
1818
[DataMember]
19-
public string Version { get; set; }
19+
public PowerShellVersion Version { get; set; }
2020

2121
/// <summary>
2222
/// The edition of PowerShell, from

CrossCompatibility/CrossCompatibility/Query/Platform/PowerShellData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public PowerShellData(PowerShellDataMut powerShellData)
1313
_powerShellData = powerShellData;
1414
}
1515

16-
public string Version => _powerShellData.Version;
16+
public PowerShellVersion Version => _powerShellData.Version;
1717

1818
public string Edition => _powerShellData.Edition;
1919

CrossCompatibility/Tests/UtilityApi.Tests.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@ Describe "PowerShell version object" {
3333
Context "Version parsing" {
3434
BeforeAll {
3535
$genericVerCases = @(
36-
@{ VerStr = '6'; Major = 6; Minor = 0; Patch = 0 }
37-
@{ VerStr = '6.1'; Major = 6; Minor = 1; Patch = 0 }
36+
@{ VerStr = '6'; Major = 6; Minor = -1; Patch = -1 }
37+
@{ VerStr = '6.1'; Major = 6; Minor = 1; Patch = -1 }
3838
@{ VerStr = '5.2.7'; Major = 5; Minor = 2; Patch = 7 }
3939
@{ VerStr = '512.2124.71'; Major = 512; Minor = 2124; Patch = 71 }
4040
)
4141

4242
$semVerCases = @(
4343
@{ VerStr = '6.1.0-rc.1'; Major = 6; Minor = 1; Patch = 0; Label = 'rc.1' }
44-
@{ VerStr = '6-preview.2'; Major = 6; Minor = 0; Patch = 0; Label = 'preview.2' }
45-
@{ VerStr = '6.2-preview.2'; Major = 6; Minor = 2; Patch = 0; Label = 'preview.2' }
44+
@{ VerStr = '6.2-preview.2'; Major = 6; Minor = 2; Patch = -1; Label = 'preview.2' }
45+
@{ VerStr = '6-preview.2'; Major = 6; Minor = -1; Patch = -1; Label = 'preview.2' }
46+
@{ VerStr = '6.1.0-rc.1+moo'; Major = 6; Minor = 1; Patch = 0; Label = 'rc.1'; BuildLabel = 'moo' }
47+
@{ VerStr = '6.2-preview.2+horse'; Major = 6; Minor = 2; Patch = -1; Label = 'preview.2'; BuildLabel = 'horse' }
48+
@{ VerStr = '6-preview.2+veryimportant'; Major = 6; Minor = -1; Patch = -1; Label = 'preview.2'; BuildLabel = 'veryimportant' }
4649
)
4750

4851
$systemVerCases = @(
@@ -73,15 +76,16 @@ Describe "PowerShell version object" {
7376
$v.Patch | Should -Be $Patch
7477
}
7578

76-
It "Parses version string '<VerStr>' as <Major>.<Minor>.<Patch>-<Label>" -TestCases $semVerCases {
77-
param([string]$VerStr, [int]$Major, [int]$Minor, [int]$Patch, [string]$Label)
79+
It "Parses version string '<VerStr>' as <Major>.<Minor>.<Patch>-<Label>+<BuildLabel>" -TestCases $semVerCases {
80+
param([string]$VerStr, [int]$Major, [int]$Minor, [int]$Patch, [string]$Label, $BuildLabel)
7881

7982
$v = [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion]::Parse($VerStr)
8083

8184
$v.Major | Should -Be $Major
8285
$v.Minor | Should -Be $Minor
8386
$v.Patch | Should -Be $Patch
8487
$v.PreReleaseLabel | Should -BeExactly $Label
88+
$v.BuildLabel | Should -Be $BuildLabel
8589
}
8690

8791
It "Parses version string '<VerStr>' as <Major>.<Minor>.<Patch>.<Revision>" -TestCases $systemVerCases {
@@ -105,7 +109,7 @@ Describe "PowerShell version object" {
105109
Context "Version creation from other versions" {
106110
BeforeAll {
107111
$versionCreationTests = @(
108-
@{ Version = '6.1'; Major = 6; Minor = 1; Patch = 0 }
112+
@{ Version = '6.1'; Major = 6; Minor = 1; Patch = -1 }
109113
@{ Version = '6.1.4'; Major = 6; Minor = 1; Patch = 4; }
110114
@{ Version = '5.1.8-preview.2'; Major = 5; Minor = 1; Patch = 8; Label = 'preview.2' }
111115
@{ Version = [version]'4.2'; Major = 4; Minor = 2; Patch = -1; Revision = -1 }
@@ -120,6 +124,9 @@ Describe "PowerShell version object" {
120124
@{ Version = [semver]'6.1.2-rc.1'; Major = 6; Minor = 1; Patch = 2; Label = 'rc.1' }
121125
@{ Version = [semver]'6.1-rc.1'; Major = 6; Minor = 1; Patch = 0; Label = 'rc.1' }
122126
@{ Version = [semver]'6-rc.1'; Major = 6; Minor = 0; Patch = 0; Label = 'rc.1' }
127+
@{ Version = [semver]'6.1.2-rc.1+duck'; Major = 6; Minor = 1; Patch = 2; Label = 'rc.1'; BuildLabel = 'duck' }
128+
@{ Version = [semver]'6.1-rc.1+duck'; Major = 6; Minor = 1; Patch = 0; Label = 'rc.1'; BuildLabel = 'duck' }
129+
@{ Version = [semver]'6-rc.1+duck'; Major = 6; Minor = 0; Patch = 0; Label = 'rc.1'; BuildLabel = 'duck' }
123130
)
124131
}
125132

@@ -131,14 +138,15 @@ Describe "PowerShell version object" {
131138
}
132139

133140
It "Creates a PowerShellVersion from '<Version>'" -TestCases $versionCreationTests {
134-
param($Version, [int]$Major, [int]$Minor, [int]$Patch, [int]$Revision, $Label)
141+
param($Version, [int]$Major, [int]$Minor, [int]$Patch, [int]$Revision, $Label, $BuildLabel)
135142

136143
$v = [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion]::Create($Version)
137144

138145
$v.Major | Should -Be $Major
139146
$v.Minor | Should -Be $Minor
140147
$v.Patch | Should -Be $Patch
141148
$v.PreReleaseLabel | Should -Be $Label
149+
$v.BuildLabel | Should -Be $BuildLabel
142150

143151
if ($Revision)
144152
{

CrossCompatibility/build.ps1

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ param(
77
[Parameter()]
88
[ValidateSet('netstandard2.0', 'net452')]
99
[string]
10-
$Framework
10+
$Framework,
11+
12+
[switch]
13+
$Test
1114
)
1215

1316
$ErrorActionPreference = 'Stop'
@@ -101,15 +104,22 @@ function Publish-CrossCompatibilityModule
101104
}
102105
}
103106

104-
if (-not $Framework)
107+
if ($Framework)
108+
{
109+
Invoke-CrossCompatibilityModuleBuild -Configuration $Configuration -Framework $Framework
110+
Publish-CrossCompatibilityModule -TargetFramework $Framework
111+
}
112+
else
105113
{
106114
foreach ($f in $script:TargetFrameworks)
107115
{
108116
Invoke-CrossCompatibilityModuleBuild -Framework $f -Configuration $Configuration
109117
}
110118
Publish-CrossCompatibilityModule
111-
return
112119
}
113120

114-
Invoke-CrossCompatibilityModuleBuild @PSBoundParameters
115-
Publish-CrossCompatibilityModule -TargetFramework $Framework
121+
if ($Test)
122+
{
123+
$testPath = "$PSScriptRoot/Tests"
124+
Start-Job { Invoke-Pester -Path $using:testPath } | Wait-Job | Receive-Job
125+
}

0 commit comments

Comments
 (0)