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

Commit e46e202

Browse files
authored
Test and fix for duplicate assembly name issue (#411)
* Test and fix for duplicate assembly name issue * Remove modules after test runs when possible
1 parent 1826085 commit e46e202

File tree

5 files changed

+118
-8
lines changed

5 files changed

+118
-8
lines changed

PSSwagger/PSSwaggerUtility/PSSwaggerUtility.psm1

+2-2
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ function Initialize-PSSwaggerUtilities {
18111811
$null = New-Item -Path $TempPath -ItemType Directory -Force
18121812

18131813
# Compile the main utility assembly
1814-
$PSSwaggerJobAssemblyPath = Join-Path -Path $TempPath -ChildPath 'Microsoft.PowerShell.PSSwagger.Utility.dll'
1814+
$PSSwaggerJobAssemblyPath = Join-Path -Path $TempPath -ChildPath "$($LocalizedData.CSharpNamespace).Utility.dll"
18151815

18161816
Add-Type -ReferencedAssemblies $RequiredAssemblies `
18171817
-TypeDefinition $PSSwaggerJobSourceString `
@@ -1895,7 +1895,7 @@ function Initialize-PSSwaggerUtilities {
18951895
$null = New-Item -Path $TempPath -ItemType Directory -Force
18961896

18971897
# Compile the main utility assembly
1898-
$PSSwaggerJobAssemblyUnsafePath = Join-Path -Path $TempPath -ChildPath 'Microsoft.PowerShell.PSSwagger.Utility.Unsafe.dll'
1898+
$PSSwaggerJobAssemblyUnsafePath = Join-Path -Path $TempPath -ChildPath "$($LocalizedData.CSharpNamespace).Utility.Unsafe.dll"
18991899
$compilerParameters.OutputAssembly = $PSSwaggerJobAssemblyUnsafePath
19001900

19011901
Add-Type -TypeDefinition $PSSwaggerJobSourceString `

Tests/PSSwaggerScenario.Tests.ps1

+59
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Describe "Basic API" -Tag ScenarioTest {
5454

5555
AfterAll {
5656
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
57+
Remove-Module -Name Generated.Basic.Module
5758
}
5859
}
5960

@@ -147,6 +148,7 @@ Describe "All Operations: Basic" -Tag ScenarioTest {
147148

148149
AfterAll {
149150
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
151+
Remove-Module -Name Generated.TypesTest.Module
150152
}
151153
}
152154

@@ -192,6 +194,7 @@ Describe "Get/List tests" -Tag ScenarioTest {
192194

193195
AfterAll {
194196
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
197+
Remove-Module -Name Generated.GetList.Module
195198
}
196199
}
197200

@@ -253,6 +256,7 @@ Describe "Optional parameter tests" -Tag ScenarioTest {
253256

254257
AfterAll {
255258
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
259+
Remove-Module -Name Generated.Optional.Module
256260
}
257261
}
258262

@@ -458,6 +462,7 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes', 'ScenarioTest') {
458462

459463
AfterAll {
460464
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
465+
Remove-Module -Name Generated.ParamTypes.Module
461466
}
462467
}
463468

@@ -579,6 +584,7 @@ Describe "AzureExtensions" -Tag @('AzureExtension', 'ScenarioTest') {
579584

580585
AfterAll {
581586
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
587+
Remove-Module -Name Generated.AzExt.Module
582588
}
583589
}
584590

@@ -636,6 +642,8 @@ Describe "Composite Swagger Tests" -Tag @('Composite', 'ScenarioTest') {
636642
$command.Parameters.StartDate.ParameterType.Name | Should be 'string'
637643
$command.Parameters.EndDate.ParameterType.Name | Should be 'string'
638644
$command.Parameters.ContainerUrl.ParameterType.Name | Should be 'uri'
645+
646+
Remove-Module -Name CompositeSwaggerModule
639647
}
640648
}
641649
}
@@ -664,6 +672,10 @@ Describe "AllOfDefinition" -Tag @('AllOf', 'ScenarioTest') {
664672
$guitar.NumberOfStrings | should be $null
665673
}
666674
}
675+
676+
AfterAll {
677+
Remove-Module -Name Generated.AllOfDefinition.Module
678+
}
667679
}
668680

669681
Describe "AuthTests" -Tag @('Auth', 'ScenarioTest') {
@@ -844,6 +856,13 @@ Describe "AuthTests" -Tag @('Auth', 'ScenarioTest') {
844856
}
845857
}
846858
}
859+
860+
AfterAll {
861+
Remove-Module -Name "Generated.BasicAuthTest.Module"
862+
Remove-Module -Name "Generated.BasicAuthTestNoChallenge.Module"
863+
Remove-Module -Name "Generated.ApiKeyHeaderTest.Module"
864+
Remove-Module -Name "Generated.ApiKeyQueryTest.Module"
865+
}
847866
}
848867

849868
Describe "PSMetadataTests" -Tag @('PSMetadata', 'ScenarioTest') {
@@ -875,6 +894,10 @@ Describe "PSMetadataTests" -Tag @('PSMetadata', 'ScenarioTest') {
875894
Get-Command List-Cupcakes -Module Generated.PSMetadataTest.Module | should not BeNullOrEmpty
876895
}
877896
}
897+
898+
AfterAll {
899+
Remove-Module -Name Generated.PSMetadataTest.Module
900+
}
878901
}
879902

880903
Describe "Header scenario tests" -Tag @('Header', 'ScenarioTest') {
@@ -1173,6 +1196,10 @@ Describe "Output type scenario tests" -Tag @('OutputType', 'ScenarioTest') {
11731196
$CommandInfo = Get-Command -Name Get-IotHubResourceEventHubConsumerGroup -Module $ModuleName
11741197
$CommandInfo.OutputType.Type.ToString() | Should BeExactly 'System.String'
11751198
}
1199+
1200+
AfterAll {
1201+
Remove-Module -Name Generated.AzExt.OutputType.Module
1202+
}
11761203
}
11771204

11781205
Describe 'New-PSSwaggerModule cmdlet parameter tests' -Tag @('CmdletParameterTest', 'ScenarioTest') {
@@ -1331,6 +1358,10 @@ Describe 'ResourceId and InputObject parameter set tests' -Tag @('InputObject',
13311358
$CommandInfo.Parameters.Name.Aliases -contains 'AccountName' | Should Be $true
13321359
}
13331360
}
1361+
1362+
AfterAll {
1363+
Remove-Module -Name Generated.Module.ArmResourceIdAndInputObject
1364+
}
13341365
}
13351366

13361367
Describe 'Client-side filtering tests (using metadata file)' -Tag @('ClientSideFilter', 'ScenarioTest') {
@@ -1384,6 +1415,7 @@ Describe 'Client-side filtering tests (using metadata file)' -Tag @('ClientSideF
13841415

13851416
AfterAll {
13861417
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
1418+
Remove-Module -Name Generated.ClientSideFilter.Spec
13871419
}
13881420
}
13891421

@@ -1438,6 +1470,7 @@ Describe 'Client-side filtering tests (using spec)' -Tag @('ClientSideFilter', '
14381470

14391471
AfterAll {
14401472
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
1473+
Remove-Module -Name Generated.ClientSideFilter.Metadata
14411474
}
14421475
}
14431476

@@ -1448,6 +1481,21 @@ Describe "Tests for local utility module" -Tag @('ScenarioTest', 'LocalUtilityCo
14481481
Initialize-Test -GeneratedModuleName "Generated.Basic.Module" -GeneratedModuleVersion "0.0.2" -TestApiName "PsSwaggerTestBasic" `
14491482
-TestSpecFileName "PsSwaggerTestBasicSpec.json" -TestDataFileName "PsSwaggerTestBasicData.json" `
14501483
-PsSwaggerPath (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger") -TestRootPath $PSScriptRoot -CopyUtilityModuleToOutput
1484+
Initialize-Test -GeneratedModuleName "Generated.Basic.Module.Dupe1" -GeneratedModuleVersion "0.0.2" -TestApiName "PsSwaggerTestBasic" `
1485+
-TestSpecFileName "PsSwaggerTestBasicSpec.json" -TestDataFileName "PsSwaggerTestBasicData.json" `
1486+
-PsSwaggerPath (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger") -TestRootPath $PSScriptRoot -CopyUtilityModuleToOutput
1487+
Initialize-Test -GeneratedModuleName "Generated.Basic.Module.Dupe2" -GeneratedModuleVersion "0.0.2" -TestApiName "PsSwaggerTestBasic" `
1488+
-TestSpecFileName "PsSwaggerTestBasicSpec.json" -TestDataFileName "PsSwaggerTestBasicData.json" `
1489+
-PsSwaggerPath (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger") -TestRootPath $PSScriptRoot -CopyUtilityModuleToOutput -DefaultCommandPrefix 'Not'
1490+
1491+
Copy-Item -Path (Join-Path -Path $PSScriptRoot -ChildPath "data" | Join-Path -ChildPath "PSSwaggerServiceCredentialsHelpers.psm1") `
1492+
-Destination (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | Join-Path -ChildPath "Generated.Basic.Module.Dupe1" | `
1493+
Join-Path -ChildPath "0.0.2" | Join-Path -ChildPath "PSSwaggerUtility" | Join-Path -ChildPath "PSSwaggerServiceCredentialsHelpers.psm1") `
1494+
-Force
1495+
Copy-Item -Path (Join-Path -Path $PSScriptRoot -ChildPath "data" | Join-Path -ChildPath "PSSwaggerServiceCredentialsHelpers.psm1") `
1496+
-Destination (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | Join-Path -ChildPath "Generated.Basic.Module.Dupe2" | `
1497+
Join-Path -ChildPath "0.0.2" | Join-Path -ChildPath "PSSwaggerUtility" | Join-Path -ChildPath "PSSwaggerServiceCredentialsHelpers.psm1") `
1498+
-Force
14511499

14521500
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "PsSwaggerTestBasic" -TestRoutesFileName "PsSwaggerTestBasicRoutes.json" -Verbose
14531501
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
@@ -1490,9 +1538,20 @@ Describe "Tests for local utility module" -Tag @('ScenarioTest', 'LocalUtilityCo
14901538
$result | should be $null
14911539
}
14921540

1541+
It "Test running commands from two modules with local utility modules" {
1542+
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | Join-Path -ChildPath "Generated.Basic.Module.Dupe1")
1543+
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | Join-Path -ChildPath "Generated.Basic.Module.Dupe2")
1544+
$results = Get-Cupcake -Flavor chocolate -ErrorVariable ev
1545+
$ev | should be $null
1546+
$results = Get-NotCupcake -Flavor chocolate -ErrorVariable ev
1547+
$ev | should be $null
1548+
}
1549+
14931550
AfterAll {
14941551
# Stop node server
14951552
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
1553+
Remove-Module -Name "Generated.Basic.Module.Dupe1" -ErrorAction Ignore
1554+
Remove-Module -Name "Generated.Basic.Module.Dupe2" -ErrorAction Ignore
14961555
}
14971556
}
14981557

Tests/TestUtilities.psm1

+4-3
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ function Initialize-Test {
177177
[string]$TestRootPath,
178178
[string]$GeneratedModuleVersion = "0.0.1",
179179
[switch]$UseAzureCSharpGenerator,
180-
[switch]$CopyUtilityModuleToOutput
180+
[switch]$CopyUtilityModuleToOutput,
181+
[string]$DefaultCommandPrefix = ""
181182
)
182183

183184
# TODO: Pass all these locations dynamically - See issues/17
@@ -199,11 +200,11 @@ function Initialize-Test {
199200
& "$env:SystemRoot\System32\WindowsPowerShell\v1.0\PowerShell.exe" -command "& {`$env:PSModulePath=`$env:PSModulePath_Backup;
200201
Import-Module (Join-Path `"$PsSwaggerPath`" `"PSSwagger.psd1`") -Force;
201202
Initialize-PSSwaggerDependencies -AllFrameworks -AcceptBootstrap -Azure:`$$UseAzureCSharpGenerator;
202-
New-PSSwaggerModule -SpecificationPath (Join-Path -Path `"$testCaseDataLocation`" -ChildPath $TestSpecFileName) -Path "$generatedModulesPath" -Name $GeneratedModuleName -Version $GeneratedModuleVersion -Verbose -NoAssembly -UseAzureCSharpGenerator:`$$UseAzureCSharpGenerator -CopyUtilityModuleToOutput:`$$CopyUtilityModuleToOutput -ConfirmBootstrap;
203+
New-PSSwaggerModule -SpecificationPath (Join-Path -Path `"$testCaseDataLocation`" -ChildPath $TestSpecFileName) -Path "$generatedModulesPath" -Name $GeneratedModuleName -Version $GeneratedModuleVersion -Verbose -NoAssembly -UseAzureCSharpGenerator:`$$UseAzureCSharpGenerator -CopyUtilityModuleToOutput:`$$CopyUtilityModuleToOutput -ConfirmBootstrap -DefaultCommandPrefix $DefaultCommandPrefix;
203204
}"
204205
} else {
205206
Import-Module (Join-Path "$PsSwaggerPath" "PSSwagger.psd1") -Force
206-
New-PSSwaggerModule -SpecificationPath (Join-Path -Path "$testCaseDataLocation" -ChildPath $TestSpecFileName) -Path "$generatedModulesPath" -Name $GeneratedModuleName -Version $GeneratedModuleVersion -Verbose -NoAssembly -UseAzureCSharpGenerator:$UseAzureCSharpGenerator -CopyUtilityModuleToOutput:$CopyUtilityModuleToOutput -ConfirmBootstrap
207+
New-PSSwaggerModule -SpecificationPath (Join-Path -Path "$testCaseDataLocation" -ChildPath $TestSpecFileName) -Path "$generatedModulesPath" -Name $GeneratedModuleName -Version $GeneratedModuleVersion -Verbose -NoAssembly -UseAzureCSharpGenerator:$UseAzureCSharpGenerator -CopyUtilityModuleToOutput:$CopyUtilityModuleToOutput -ConfirmBootstrap -DefaultCommandPrefix $DefaultCommandPrefix
207208
}
208209

209210
if ($TestDataFileName) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#########################################################################################
2+
#
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
#
5+
# Licensed under the MIT license.
6+
#
7+
# PSSwaggerUtility Module
8+
#
9+
#########################################################################################
10+
Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
11+
Microsoft.PowerShell.Utility\Import-LocalizedData LocalizedData -filename PSSwaggerUtility.Resources.psd1
12+
13+
function Get-BasicAuthCredentialInternal {
14+
[CmdletBinding()]
15+
param(
16+
[Parameter(Mandatory=$true)]
17+
[PSCredential]
18+
$Credential
19+
)
20+
}
21+
22+
function Get-ApiKeyCredentialInternal {
23+
[CmdletBinding()]
24+
param(
25+
[Parameter(Mandatory=$true)]
26+
[string]
27+
$APIKey,
28+
29+
[Parameter(Mandatory=$false)]
30+
[string]
31+
$Location,
32+
33+
[Parameter(Mandatory=$false)]
34+
[string]
35+
$Name
36+
)
37+
}
38+
39+
function Get-EmptyAuthCredentialInternal {
40+
[CmdletBinding()]
41+
param()
42+
43+
$refPath = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { ($_.Location) -and ((Split-Path -Path $_.Location -Leaf) -eq 'Microsoft.Rest.ClientRuntime.dll') } | Select-Object -ExpandProperty Location
44+
if ($refPath) {
45+
Add-Type -TypeDefinition "public class PSDummyAuthentication : Microsoft.Rest.ServiceClientCredentials {}" -ReferencedAssemblies @($refPath)
46+
New-Object -TypeName PSDummyAuthentication
47+
}
48+
}

tests/data/PsSwaggerTestBasic/PsSwaggerTestBasicSpec.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@
7474
"responses": {
7575
"200": {
7676
"description": "A cupcake entity with matching flavor.",
77-
"type": "array",
78-
"items": {
79-
"$ref": "#/definitions/Cupcake"
77+
"schema": {
78+
"type": "array",
79+
"items": {
80+
"$ref": "#/definitions/Cupcake"
81+
}
8082
}
8183
},
8284
"default": {

0 commit comments

Comments
 (0)