Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 7abfdde

Browse files
authored
Support x-ps-cmdlet-infos.name (#264)
* Support x-ps-cmdlet-infos.name
1 parent 1acf068 commit 7abfdde

File tree

10 files changed

+358
-32
lines changed

10 files changed

+358
-32
lines changed

PSSwagger/Paths.psm1

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ function Get-SwaggerSpecPathInfo
9797
$operationSecurityObject = $null
9898
}
9999

100+
$cmdletInfoOverrides = @()
101+
if ((Get-Member -InputObject $_.Value -Name 'x-ps-cmdlet-infos') -and $_.Value.'x-ps-cmdlet-infos') {
102+
foreach ($cmdletMetadata in $_.Value.'x-ps-cmdlet-infos') {
103+
$cmdletInfoOverride = @{}
104+
if ((Get-Member -InputObject $cmdletMetadata -Name 'name') -and $cmdletMetadata.name) {
105+
$cmdletInfoOverride['name'] = $cmdletMetadata.name
106+
}
107+
108+
$cmdletInfoOverrides += $cmdletInfoOverride
109+
}
110+
}
111+
100112
if(Get-Member -InputObject $_.Value -Name 'OperationId')
101113
{
102114
$operationId = $_.Value.operationId
@@ -133,9 +145,9 @@ function Get-SwaggerSpecPathInfo
133145
$responses = $_.value.responses
134146
}
135147

136-
if((Get-Member -InputObject $_.value -Name 'x-ms-cmdlet-name') -and $_.value.'x-ms-cmdlet-name')
148+
if($cmdletInfoOverrides)
137149
{
138-
$commandNames = $_.value.'x-ms-cmdlet-name'
150+
$commandNames = $cmdletInfoOverrides
139151
} else {
140152
$commandNames = Get-PathCommandName -OperationId $operationId
141153
}
@@ -176,10 +188,10 @@ function Get-SwaggerSpecPathInfo
176188

177189
$commandNames | ForEach-Object {
178190
$FunctionDetails = @{}
179-
if ($PathFunctionDetails.ContainsKey($_)) {
180-
$FunctionDetails = $PathFunctionDetails[$_]
191+
if ($PathFunctionDetails.ContainsKey($_.name)) {
192+
$FunctionDetails = $PathFunctionDetails[$_.name]
181193
} else {
182-
$FunctionDetails['CommandName'] = $_
194+
$FunctionDetails['CommandName'] = $_.name
183195
$FunctionDetails['x-ms-long-running-operation'] = $longRunningOperation
184196
}
185197

@@ -194,7 +206,7 @@ function Get-SwaggerSpecPathInfo
194206

195207
$ParameterSetDetails += $ParameterSetDetail
196208
$FunctionDetails['ParameterSetDetails'] = $ParameterSetDetails
197-
$PathFunctionDetails[$_] = $FunctionDetails
209+
$PathFunctionDetails[$_.name] = $FunctionDetails
198210
}
199211
}
200212
elseif(-not ((Get-Member -InputObject $_ -Name 'Name') -and ($_.Name -eq 'Parameters')))

PSSwagger/SwaggerUtils.psm1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,18 +1204,20 @@ function Get-PathCommandName
12041204
$cmdNoun = Get-SingularizedValue -Name $cmdNoun
12051205
}
12061206

1207-
$cmdletNames = $cmdVerb | ForEach-Object {
1207+
$cmdletInfos = $cmdVerb | ForEach-Object {
12081208
$Verb = Get-PascalCasedString -Name $_
12091209
if($cmdNoun){
12101210
$CommandName = "$Verb-$cmdNoun"
12111211
} else {
12121212
$CommandName = Get-SingularizedValue -Name $Verb
12131213
}
1214-
$CommandName
1214+
$cmdletInfo = @{}
1215+
$cmdletInfo['name'] = $CommandName
1216+
$cmdletInfo
12151217
Write-Verbose -Message ($LocalizedData.UsingCmdletNameForSwaggerPathOperation -f ($CommandName, $OperationId))
12161218
}
12171219

1218-
return $cmdletNames
1220+
return $cmdletInfos
12191221
}
12201222

12211223
function Get-PathFunctionBody
@@ -1564,7 +1566,7 @@ function Get-CSharpModelName
15641566
$AssemblyName
15651567
)
15661568

1567-
$AutoRestToolsPath = Get-Command -Name 'AutoRest.exe' |
1569+
$AutoRestToolsPath = Get-Command -Name 'AutoRest.exe' |
15681570
Select-Object -First 1 -ErrorAction Ignore |
15691571
ForEach-Object {Split-Path -LiteralPath $_.Source}
15701572

Tests/PSSwagger.Unit.Tests.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,49 @@ Describe "PSSwagger Unit Tests" -Tag @('BVT', 'DRT', 'UnitTest', 'P0') {
1818
InModuleScope PSSwagger {
1919
Context "Get-PathCommandName Unit Tests" {
2020
It "Get-PathCommandName should return command names with proper verb for VM_CreateOrUpdateWithNounSuffix operationid" {
21-
$CommandNames = Get-PathCommandName -OperationId VM_CreateOrUpdateWithNounSuffix
21+
$CommandNames = Get-PathCommandName -OperationId VM_CreateOrUpdateWithNounSuffix | %{ $_.name }
2222
$CommandNames -CContains 'New-VMWithNounSuffix' | Should Be $True
2323
$CommandNames -CContains 'Set-VMWithNounSuffix' | Should Be $True
2424
}
2525

2626
It "Get-PathCommandName should return command names with proper verb for VM_createorupdatewithnounsuffix operationid" {
27-
$CommandNames = Get-PathCommandName -OperationId VM_createorupdatewithnounsuffix
27+
$CommandNames = Get-PathCommandName -OperationId VM_createorupdatewithnounsuffix | %{ $_.name }
2828
$CommandNames -CContains 'New-VMWithnounsuffix' | Should Be $True
2929
$CommandNames -CContains 'Set-VMWithnounsuffix' | Should Be $True
3030
}
3131

3232
It "Get-PathCommandName should return command name with proper verb for VM_createOrWithNounSuffix operationid" {
33-
Get-PathCommandName -OperationId VM_createOrWithNounSuffix | Should BeExactly 'New-VMOrWithNounSuffix'
33+
Get-PathCommandName -OperationId VM_createOrWithNounSuffix | %{ $_.name } | Should BeExactly 'New-VMOrWithNounSuffix'
3434
}
3535

3636
It "Get-PathCommandName should return command name with proper verb for VM_migrateWithNounSuffix operationid" {
37-
Get-PathCommandName -OperationId VM_migrateWithNounSuffix | Should BeExactly 'Move-VMWithNounSuffix'
37+
Get-PathCommandName -OperationId VM_migrateWithNounSuffix | %{ $_.name } | Should BeExactly 'Move-VMWithNounSuffix'
3838
}
3939

4040
It "Get-PathCommandName should return command name with proper verb for CreateFooResource operationid" {
41-
Get-PathCommandName -OperationId CreateFooResource | Should BeExactly 'New-FooResource'
41+
Get-PathCommandName -OperationId CreateFooResource | %{ $_.name } | Should BeExactly 'New-FooResource'
4242
}
4343

4444
It "Get-PathCommandName should return command names with proper verb for createorupdatebarResource operationid" {
45-
$CommandNames = Get-PathCommandName -OperationId createorupdatebarResource
45+
$CommandNames = Get-PathCommandName -OperationId createorupdatebarResource | %{ $_.name }
4646
$CommandNames -CContains 'New-BarResource' | Should BeExactly $True
4747
$CommandNames -CContains 'Set-BarResource' | Should BeExactly $True
4848
}
4949

5050
It "Get-PathCommandName should return command name with proper verb for anotherFooResource_createFoo operationid" {
51-
Get-PathCommandName -OperationId anotherFooResource_createFoo | Should BeExactly 'New-AnotherFooResource'
51+
Get-PathCommandName -OperationId anotherFooResource_createFoo | %{ $_.name } | Should BeExactly 'New-AnotherFooResource'
5252
}
5353

5454
It "Get-PathCommandName should return command name with proper verb for FooResource_createresource operationid" {
55-
Get-PathCommandName -OperationId FooResource_createresource | Should BeExactly 'New-FooResource'
55+
Get-PathCommandName -OperationId FooResource_createresource | %{ $_.name } | Should BeExactly 'New-FooResource'
5656
}
5757

5858
It "Get-PathCommandName should return proper command name for abcd operationid" {
59-
Get-PathCommandName -OperationId abcd | Should BeExactly 'Abcd'
59+
Get-PathCommandName -OperationId abcd | %{ $_.name } | Should BeExactly 'Abcd'
6060
}
6161

6262
It "Get-PathCommandName with NetworkInterfaces_ListVirtualMachineScaleSetVMNetworkInterfaces" {
63-
Get-PathCommandName -OperationId NetworkInterfaces_ListVirtualMachineScaleSetVMNetworkInterfaces | Should BeExactly 'Get-VirtualMachineScaleSetVMNetworkInterface'
63+
Get-PathCommandName -OperationId NetworkInterfaces_ListVirtualMachineScaleSetVMNetworkInterfaces | %{ $_.name } | Should BeExactly 'Get-VirtualMachineScaleSetVMNetworkInterface'
6464
}
6565
}
6666

Tests/PSSwaggerScenario.Tests.ps1

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# PSSwagger Module
88
#
99
#########################################################################################
10+
$script:EnableTracer = $true
1011
Import-Module (Join-Path "$PSScriptRoot" "TestUtilities.psm1")
1112
Describe "Basic API" -Tag ScenarioTest {
1213
BeforeAll {
@@ -25,6 +26,13 @@ Describe "Basic API" -Tag ScenarioTest {
2526
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | `
2627
Join-Path -ChildPath "Generated.Basic.Module")
2728
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "PsSwaggerTestBasic" -TestRoutesFileName "PsSwaggerTestBasicRoutes.json" -Verbose
29+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
30+
$script:EnableTracer = $false
31+
Initialize-PSSwaggerDependencies -AcceptBootstrap
32+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
33+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
34+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
35+
}
2836
}
2937

3038
Context "Basic API tests" {
@@ -70,6 +78,13 @@ Describe "All Operations: Basic" -Tag ScenarioTest {
7078
Join-Path -ChildPath "Generated.TypesTest.Module")
7179

7280
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "OperationTypes" -TestMiddlewareFileNames "OperationTypesMiddleware.js" -TestRoutesFileName "OperationTypesRoutes.json"
81+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
82+
$script:EnableTracer = $false
83+
Initialize-PSSwaggerDependencies -AcceptBootstrap
84+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
85+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
86+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
87+
}
7388
}
7489

7590
Context "All Operations: Basic tests" {
@@ -144,6 +159,13 @@ Describe "Get/List tests" -Tag ScenarioTest {
144159
Join-Path -ChildPath "Generated.GetList.Module")
145160

146161
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "GetListTests" -TestRoutesFileName "GetListTestsRoutes.json"
162+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
163+
$script:EnableTracer = $false
164+
Initialize-PSSwaggerDependencies -AcceptBootstrap
165+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
166+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
167+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
168+
}
147169
}
148170

149171
Context "Get/List tests" {
@@ -178,12 +200,19 @@ Describe "Optional parameter tests" -Tag ScenarioTest {
178200
Write-Verbose "Importing modules"
179201
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger" | Join-Path -ChildPath "PSSwagger.Common.Helpers" | `
180202
Join-Path -ChildPath "PSSwagger.Common.Helpers.psd1") -Force
203+
181204
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger" | Join-Path -ChildPath "PSSwagger.Azure.Helpers" | `
182205
Join-Path -ChildPath "PSSwagger.Azure.Helpers.psd1") -Force
183206
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | `
184207
Join-Path -ChildPath "Generated.Optional.Module")
185-
186208
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "OptionalParametersTests" -TestRoutesFileName "OptionalParametersTestsRoutes.json"
209+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
210+
$script:EnableTracer = $false
211+
Initialize-PSSwaggerDependencies -AcceptBootstrap
212+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
213+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
214+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
215+
}
187216
}
188217

189218
Context "Optional parameter tests" {
@@ -235,6 +264,13 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
235264
Join-Path -ChildPath "Generated.ParamTypes.Module")
236265

237266
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "ParameterTypes"
267+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
268+
$script:EnableTracer = $false
269+
Initialize-PSSwaggerDependencies -AcceptBootstrap
270+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
271+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
272+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
273+
}
238274
}
239275

240276
Context "ParameterTypes tests" {
@@ -381,6 +417,13 @@ Describe "AzureExtensions" {
381417
Join-Path -ChildPath "Generated.AzExt.Module")
382418

383419
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "AzureExtensions" -TestRoutesFileName "AzureExtensionsRoutes.json"
420+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
421+
$script:EnableTracer = $false
422+
Initialize-PSSwaggerDependencies -AcceptBootstrap
423+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
424+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
425+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
426+
}
384427
}
385428

386429
Context "AzureExtensions" {
@@ -578,7 +621,13 @@ Describe "AuthTests" -Tag @('Auth','ScenarioTest') {
578621
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | `
579622
Join-Path -ChildPath "Generated.ApiKeyQueryTest.Module") -Prefix "ApiKeyQuery"
580623

581-
624+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
625+
$script:EnableTracer = $false
626+
Initialize-PSSwaggerDependencies -AcceptBootstrap
627+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
628+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
629+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
630+
}
582631
}
583632

584633
Context "Basic Authentication" {
@@ -719,4 +768,37 @@ Describe "AuthTests" -Tag @('Auth','ScenarioTest') {
719768
}
720769
}
721770
}
771+
}
772+
773+
Describe "PSMetadataTests" -Tag @('PSMetadata','ScenarioTest') {
774+
BeforeAll {
775+
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger" | Join-Path -ChildPath "PSSwagger.Common.Helpers" | `
776+
Join-Path -ChildPath "PSSwagger.Common.Helpers.psd1") -Force
777+
Initialize-Test -GeneratedModuleName "Generated.PSMetadataTest.Module" -TestApiName "psmetadatatest" `
778+
-TestSpecFileName "PsMetadataModuleTest.json" `
779+
-PsSwaggerPath (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger") -TestRootPath $PSScriptRoot
780+
781+
# Import generated module
782+
Write-Verbose "Importing modules"
783+
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger" | Join-Path -ChildPath "PSSwagger.Common.Helpers" | `
784+
Join-Path -ChildPath "PSSwagger.Common.Helpers.psd1") -Force
785+
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger" | Join-Path -ChildPath "PSSwagger.Azure.Helpers" | `
786+
Join-Path -ChildPath "PSSwagger.Azure.Helpers.psd1") -Force
787+
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | `
788+
Join-Path -ChildPath "Generated.PSMetadataTest.Module")
789+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
790+
$script:EnableTracer = $false
791+
Initialize-PSSwaggerDependencies -AcceptBootstrap
792+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
793+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
794+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
795+
}
796+
}
797+
798+
Context "PSMetadataTest" {
799+
It "Override cmdlet name" {
800+
Get-Command Get-Cupcake -Module Generated.PSMetadataTest.Module -ErrorAction Ignore | should BeNullOrEmpty
801+
Get-Command List-Cupcakes -Module Generated.PSMetadataTest.Module | should not BeNullOrEmpty
802+
}
803+
}
722804
}

Tests/PSSwaggerTestTracing.psm1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class PSSwaggerTestClientTracing : Microsoft.PowerShell.Commands.PSSwagger.PSSwaggerClientTracingBase {
2+
[void] WriteToTraceStream([string]$message) {
3+
Write-Host -Message $message
4+
}
5+
}
6+
7+
function New-PSSwaggerTestClientTracing {
8+
[CmdletBinding()]
9+
param()
10+
11+
return New-Object -TypeName PSSwaggerTestClientTracing
12+
}

Tests/Send-AppVeyorResults.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ param(
2222
[string]
2323
$testResultFilePattern,
2424

25+
[Parameter(Mandatory=$true)]
26+
[string]
27+
$generatedModulesDir,
28+
2529
[Parameter(Mandatory=$false)]
2630
[string]
2731
$_garbage
@@ -34,4 +38,8 @@ $webClient = New-Object 'System.Net.WebClient'
3438
Get-ChildItem -Path "$testResultRootDir" -Filter $testResultFilePattern -File -Recurse | ForEach-Object {
3539
Write-Host "Uploading file: $($_.FullName)"
3640
$webClient.UploadFile($appVeyorUrl, "$($_.FullName)")
37-
}
41+
}
42+
Write-Host "Zipping generated modules dir '$generatedModulesDir' assuming 7z is in path"
43+
7z a .\Generated.zip $generatedModulesDir
44+
Write-Host "Pushing generated modules zip to AppVeyor"
45+
Push-AppveyorArtifact .\Generated.zip

0 commit comments

Comments
 (0)