Skip to content

Commit b63a4ef

Browse files
Robert Holtrjmholt
authored andcommitted
Improve cmdlet experience, add rudimentary tests
1 parent ab506af commit b63a4ef

5 files changed

Lines changed: 81 additions & 3 deletions

File tree

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Commands/ConvertJsonCommands.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ namespace Microsoft.PowerShell.CrossCompatibility.Commands
1515
/// Turns given .NET objects into JSON strings.
1616
/// </summary>
1717
[Cmdlet(VerbsData.ConvertTo, CommandUtilities.MODULE_PREFIX + "Json")]
18+
[OutputType(typeof(string))]
1819
public class ConvertToPSCompatibilityJsonCommand : PSCmdlet
1920
{
2021
private JsonProfileSerializer _serializer;
2122

2223
/// <summary>
2324
/// The object to convert to JSON.
2425
/// </summary>
25-
[Parameter(Mandatory = true, ValueFromPipeline = true)]
26+
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)]
2627
public object[] Item { get; set; }
2728

2829
/// <summary>
@@ -51,15 +52,15 @@ protected override void ProcessRecord()
5152
/// Class defining the ConvertFrom-PSCompatibilityJson JSON.
5253
/// Turns given JSON into .NET compatibility profile objects.
5354
/// </summary>
54-
[Cmdlet(VerbsData.ConvertFrom, CommandUtilities.MODULE_PREFIX + "Json")]
55+
[Cmdlet(VerbsData.ConvertFrom, CommandUtilities.MODULE_PREFIX + "Json", DefaultParameterSetName = "JsonSource")]
5556
public class ConvertFromPSCompatibilityJsonCommand : PSCmdlet
5657
{
5758
private JsonProfileSerializer _serializer;
5859

5960
/// <summary>
6061
/// The source of the JSON to convert from. Can be a JSON string, a FileInfo or a TextReader object.
6162
/// </summary>
62-
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "JsonSource")]
63+
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = "JsonSource")]
6364
public object[] JsonSource { get; set; }
6465

6566
/// <summary>

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Commands/GetPSCompatibilityPlatformDataCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Microsoft.PowerShell.CrossCompatibility.Commands
1212
/// Assembles an object describing the current platform PowerShell is running on.
1313
/// </summary>
1414
[Cmdlet(VerbsCommon.Get, CommandUtilities.MODULE_PREFIX + "PlatformData")]
15+
[OutputType(typeof(PlatformData))]
1516
public class GetPSCompatibilityPlatformDataCommand : Cmdlet
1617
{
1718
/// <summary>

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Commands/GetPSCompatibilityPlatformNameCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Microsoft.PowerShell.CrossCompatibility.Commands
1414
/// Gets the canonical profile ID of the current platform PowerShell is executing on.
1515
/// </summary>
1616
[Cmdlet(VerbsCommon.Get, CommandUtilities.MODULE_PREFIX + "PlatformName")]
17+
[OutputType(typeof(string))]
1718
public class GetPSCompatibilityPlatformNameCommand : Cmdlet
1819
{
1920
/// <summary>

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Commands/NewPSCompatibilityProfileCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.PowerShell.CrossCompatibility.Commands
1818
/// Creates a new PowerShell compatibility profile for the current PowerShell session
1919
/// </summary>
2020
[Cmdlet(VerbsCommon.New, CommandUtilities.MODULE_PREFIX + "Profile", DefaultParameterSetName = "OutFile")]
21+
[OutputType(typeof(CompatibilityProfileData))]
2122
public class NewPSCompatibilityProfileCommand : PSCmdlet
2223
{
2324
/// <summary>
@@ -30,6 +31,7 @@ public class NewPSCompatibilityProfileCommand : PSCmdlet
3031
/// The path of the profile file to create.
3132
/// </summary>
3233
[Parameter(ParameterSetName = "OutFile", Position = 0)]
34+
[ValidateNotNullOrEmpty]
3335
public string OutFile { get; set; }
3436

3537
/// <summary>
@@ -58,6 +60,7 @@ public class NewPSCompatibilityProfileCommand : PSCmdlet
5860
/// Sets the name of the profile file to be created in the default profile directory.
5961
/// </summary>
6062
[Parameter(ParameterSetName = "ProfileName")]
63+
[ValidateNotNullOrEmpty]
6164
public string ProfileName { get; set; }
6265

6366
/// <summary>
@@ -71,13 +74,15 @@ public class NewPSCompatibilityProfileCommand : PSCmdlet
7174
/// </summary>
7275
/// <value></value>
7376
[Parameter]
77+
[ValidateNotNullOrEmpty]
7478
public string[] ExcludeModulePathPrefix { get; set; }
7579

7680
/// <summary>
7781
/// Assemblies on paths starting with any of these will be excluded.
7882
/// </summary>
7983
/// <value></value>
8084
[Parameter]
85+
[ValidateNotNullOrEmpty]
8186
public string[] ExcludeAssemblyPathPrefix { get; set; }
8287

8388
protected override void EndProcessing()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
$script:currentMachineProfile = New-PSCompatibilityProfile -PassThru
5+
6+
Describe "Assert-PSCompatibilityProfileIsValid" {
7+
It "Accepts a good profile" {
8+
Assert-PSCompatibilityProfileIsValid -CompatibilityProfile $script:currentMachineProfile
9+
$? | Should -BeTrue
10+
}
11+
}
12+
13+
Describe "JSON cmdlets" {
14+
It "Serializes a compatibility profile to JSON and back again" {
15+
$script:currentMachineProfile |
16+
ConvertTo-PSCompatibilityJson |
17+
ConvertFrom-PSCompatibilityJson |
18+
Assert-PSCompatibilityProfileIsValid
19+
20+
$? | Should -BeTrue
21+
}
22+
}
23+
24+
Describe "Platform data and naming" {
25+
It "Collects platform data properly" {
26+
$platformData = Get-PSCompatibilityPlatformData
27+
28+
$platformData.PowerShell.Version.Major | Should -Be $PSVersionTable.PSVersion.Major
29+
30+
if ($PSEdition -eq 'Core')
31+
{
32+
$platformData.Dotnet.Edition | Should -Be 'Core'
33+
}
34+
else
35+
{
36+
$platformData.Dotnet.Edition | Should -Be 'Framework'
37+
}
38+
39+
if ($IsLinux)
40+
{
41+
$platformData.OperatingSystem.Family | Should -Be 'Linux'
42+
}
43+
elseif ($IsMacOS)
44+
{
45+
$platformData.OperatingSystem.Family | Should -Be 'MacOS'
46+
}
47+
else
48+
{
49+
$platformData.OperatingSystem.Family | Should -Be 'Windows'
50+
}
51+
}
52+
53+
It "Names the platform appropriately" {
54+
$platformName = Get-PSCompatibilityPlatformName
55+
56+
$platformNameElements = $platformName -split "_"
57+
58+
$platformNameElements | Should -HaveCount 6
59+
}
60+
}
61+
62+
Describe "New-PSCompatibilityProfile" {
63+
It "Generates a compatibility profile" {
64+
$jsonPath = Join-Path $TestDrive "profile.json"
65+
New-PSCompatibilityProfile -OutFile $jsonPath
66+
$json = ConvertFrom-PSCompatibilityJson -Path $jsonPath
67+
Assert-PSCompatibilityProfileIsValid $json
68+
$? | Should -BeTrue
69+
}
70+
}

0 commit comments

Comments
 (0)