diff --git a/.azure-pipelines/test-module-upgrade.yml b/.azure-pipelines/test-module-upgrade.yml index 1f684e35fc..e128344281 100644 --- a/.azure-pipelines/test-module-upgrade.yml +++ b/.azure-pipelines/test-module-upgrade.yml @@ -3,6 +3,11 @@ pool: demands: npm steps: +- task: NodeTool@0 + displayName: 'Use Node 10.16.0' + inputs: + versionSpec: 10.16.0 + - task: Npm@1 displayName: 'Install autorest@beta' inputs: @@ -10,10 +15,39 @@ steps: verbose: false customCommand: 'install -g "@autorest/autorest"' +- task: Npm@1 + displayName: 'Install @microsoft/rush' + inputs: + command: custom + verbose: false + customCommand: 'install -g @microsoft/rush@5.12.0' + +- task: CmdLine@2 + displayName: 'Rush sync-versions' + inputs: + script: 'rush sync-versions' + +- task: CmdLine@2 + displayName: 'Rush Update' + inputs: + script: 'rush update' + +- task: CmdLine@2 + displayName: 'Rush Rebuild' + inputs: + script: 'rush rebuild' + - task: PowerShell@2 displayName: Verify Upgrade inputs: targetType: inline - script: './AutoRestUpgradeTest.ps1 -Configuration' + script: './AutoRestUpgradeTest.ps1 -AllowList' workingDirectory: 'tests-upgrade' - failOnStderr: true \ No newline at end of file + failOnStderr: true + +- task: PublishPipelineArtifact@0 + displayName: 'Save artifacts' + inputs: + artifactName: CompareResult + targetPath: tests-upgrade\CompareResult + condition: succeededOrFailed() \ No newline at end of file diff --git a/tests-upgrade/AutoRestUpgradeTest.ps1 b/tests-upgrade/AutoRestUpgradeTest.ps1 index 21b6ac0080..c5d01e2116 100644 --- a/tests-upgrade/AutoRestUpgradeTest.ps1 +++ b/tests-upgrade/AutoRestUpgradeTest.ps1 @@ -1 +1,325 @@ -Write-Host "Hello" \ No newline at end of file +param([switch]$Generate,[string]$TestName,[switch]$SourceVersion,[switch]$TargetVersion,[switch]$AllowList,[switch]$BlackList) +#Need to use the right version of node.js +#nvs use 10.16.0 +#Create folder to save Compare Results +New-Item CompareResult -ItemType "directory" +#Define the global param 'isError' to determine wheather throw the error +$global:isError = $false +#Import the Configuration Json +$conf = (Get-Content 'Configuration.json') | ConvertFrom-Json +if($AllowList) +{ + #Get the whiteList from json + $whiteList = $conf.WhiteList +} +if($BlackList) +{ + #Get the blackList from json + $blackTestList = $conf.BlackList +} + +#Determine whether the difference is command +function IsCommand([Object]$SourceFile , [Object]$TargetFile) +{ + $isCommandResult = $True + $difference = Compare-Object $SourceFile $TargetFile + foreach($line in $difference) + { + $lineInfo = $line.InputObject.Replace(' ','') + $lineCompareResult =$lineInfo.Startswith('//') + if(!$lineCompareResult) + { + $isCommandResult = $false + break + } + } + return $isCommandResult +} + +#Determine whether the file needs to be ignored +function IsNeedIgnore([string]$inputFileName , [Array]$ignoreArray) +{ + $Ignore = $false + foreach($ignoreDetail in $ignoreArray) + { + if($inputFileName.Startswith($ignoreDetail)) + { + $Ignore =$True + break + } + } + return $Ignore +} + +#Code generation +function GenerateCode() +{ + $GenerateResult=$Ture + #source and generate codes all need to be generated + if((-not $SourceVersion) -and (-not $TargetVersion)) + { + #generate source code + Write-Host -ForegroundColor Green 'M3' + $GenerateSourceResult = autorest-beta --use:@autorest/powershell@2.1.386 --output-folder:.\generate\m3 --Debug | Out-string + #generate target code + Write-Host -ForegroundColor Green 'M4' + $GenerateTargetResult = autorest-beta --use:..\..\ --output-folder:.\generate\m4 --Debug | Out-string + if(!$GenerateSourceResult.Contains('Generation Complete') -or !$GenerateTargetResult.Contains('Generation Complete')) + { + $GenerateResult = $false + } + }elseif($SourceVersion) + { + Write-Host -ForegroundColor Green 'M3' + $GenerateSourceResult = autorest-beta --use:@autorest/powershell@2.1.386 --output-folder:.\generate\m3 --Debug | Out-string + if(!$GenerateSourceResult.Contains('Generation Complete')) + { + $GenerateResult = $false + } + }else + { + Write-Host -ForegroundColor Green 'M4' + $GenerateTargetResult = autorest-beta --use:..\..\ --output-folder:.\generate\m4 --Debug | Out-string + if(!$GenerateTargetResult.Contains('Generation Complete')) + { + $GenerateResult = $false + } + } + return $GenerateResult +} + +#Compare the gap between the two versions +function CompareGeneratedCode([string]$inputSourcePath,[string]$inputTargetPath,[string]$testFileName) +{ + #to creare ecah dictionary (the struct is (string,obj)) + #the key is the path of each file,and the obj has two parameters(hashcodevalue,status) + $initialDict = @{} + #in m3Path + cd $inputSourcePath + $initFileList = Get-ChildItem -Recurse -force + $initIgnoreFileList = (($inputSourcePath+'\generated\modules'), ($inputSourcePath+'\.gitignore'),($inputSourcePath+'\tools\Resources\.gitignore')) + $targetIgnoreFileList = (($inputTargetPath+'\generated\modules'), ($inputTargetPath+'\.gitignore'),($inputTargetPath+'\tools\Resources\.gitignore')) + #foreach initFileList and get the hashcode of them + foreach( $initFile in $initFileList) + { + $ignoreResult = IsNeedIgnore -inputFileName $initFile.FullName -ignoreArray $initIgnoreFileList + if(!$ignoreResult) + { + # if(!$initFile.FullName.Startswith($initIgnoreFileList)){ + $obj = "what" | Select-Object -Property HashCode, Status + #if the file is not filefolder + if($initFile.mode -eq '-a---') + { + #get the hashcode of the file + $hashTable = $initFile.PSPath.Replace('Microsoft.PowerShell.Core\FileSystem::','') | get-filehash + # $initFile.FullName + # $hashTable + $obj.HashCode = $hashTable.Hash + #get the path of the file + $detailPath = $hashTable.Path.Replace($inputSourcePath,'') + $initialDict.Add($detailPath,$obj) + } + } + } + $targetDict = @{} + #in TargetPath + cd $inputTargetPath + $targetFileList = Get-ChildItem -Recurse -force + #foreach initFileList and get the hashcode of them + foreach( $targetFile in $targetFileList) + { + $ignoreResult = IsNeedIgnore -inputFileName $targetFile.FullName -ignoreArray $targetIgnoreFileList + if(!$ignoreResult) + { + $obj = "waht2" | Select-Object -Property HashCode, Status + #if the file is not filefolder + if($targetFile.mode -eq '-a---') + { + #get the hashcode of the file + $hashTable = $targetFile.PSPath.Replace('Microsoft.PowerShell.Core\FileSystem::','') | get-filehash + $obj.HashCode = $hashTable.Hash + #get the path of the file + $detailPath = $hashTable.path.Replace($inputTargetPath,'') + $targetDict.Add($detailPath,$obj) + } + } + } + [object[]] $difArray=@() + + #search each dictDetail in targetDict + #the status means: 0 this file do not exist in anouther filefolder + # 1 the hashcode of the file is the same as that in another filefolder + # 2 the hashcode of the file is different from that in another filefolder + foreach($initDictDetail in $initialDict.Keys) + { + $difDetail = "what"| Select-Object -Property fileName,Path,fileFolderName,Status + #if the file not exists in targetDict + if($targetDict[$initDictDetail] -eq $null) + { + $difDetail.Path = $initDictDetail + $difDetail.fileFolderName = 'M3' + $splitStrings = $initDictDetail.Split('\') + $difDetail.fileName = $splitStrings[$splitStrings.count-1] + $difDetail.status = 'lack in M4' + #sign up the status of the file + $initialDict[$initDictDetail].status = 0 + $difArray+= $difDetail + }elseif($targetDict[$initDictDetail].HashCode -ne $initialDict[$initDictDetail].HashCode) + { + $M3CompareFile = Get-Content ($inputSourcePath + $initDictDetail) + $M4CompareFile = Get-Content ($inputTargetPath + $initDictDetail) + $isCommandResult = IsCommand -SourceFile $M3CompareFile -TargetFile $M4CompareFile + if( $isCommandResult -ne $True) + { + $difDetail.Path = $initDictDetail + $difDetail.fileFolderName = 'M3' + $splitStrings = $initDictDetail.Split('\') + $difDetail.fileName = $splitStrings[$splitStrings.count-1] + $difDetail.status = 'different' + #sign up the status of the file + $initialDict[$initDictDetail].status = 2 + $targetDict[$initDictDetail].status = 2 + $difArray+=$difDetail + }else + { + $initialDict[$initDictDetail].status = 1 + $targetDict[$initDictDetail].status = 1 + } + }else + { + $initialDict[$initDictDetail].status = 1 + $targetDict[$initDictDetail].status = 1 + } + } + #search those files which status is null + foreach($targetDetail in $targetDict.Keys) + { + $difDetail = "what"| Select-Object -Property fileName,Path,fileFolderName,Status + if($targetDict[$targetDetail].Status -eq $null) + { + $difDetail.Path = $targetDetail + $difDetail.fileFolderName = 'M4' + $splitStrings = $targetDetail.Split('\') + $difDetail.fileName = $splitStrings[$splitStrings.count-1] + $difDetail.Status = 'lack in m3' + $difArray+=$difDetail + } + } + if($difArray.Count -gt 0) + { + $global:isError=$True + } + #Export the differ csv to the 'CompareResult' folder + $filename = $PSScriptRoot + '\CompareResult\' + $testFileName + (get-date -format 'yyyyMMddhhmmss')+'.csv' + $difArray | Select-Object -Property fileName,Path,fileFolderName,Status | Export-CSV -path $filename +} + +$currentPath = Get-Location +$fileList = Get-ChildItem +#if only one case +if($TestName -ne $null -and ($TestName -ne '')) +{ + cd ($PSScriptRoot+'\'+$TestName) + $deatilPath = $PSScriptRoot + 'generate' + try + { + $IsGenerateSuccess = GenerateCode + if(-not $Generate -and $IsGenerateSuccess) + { + $sourceFilePath = $PSScriptRoot +'\'+$TestName + '\generate\m3' + $targetFilePath = $PSScriptRoot +'\'+$TestName + '\generate\m4' + CompareGeneratedCode -inputSourcePath $sourceFilePath -inputTargetPath $targetFilePath -testFileName $TestName + } + } + catch + { + Write-Host -ForegroundColor yellow 'Generate error:' + $fileDetail.Name + } +}elseif($AllowList) +{ + #get each testfolder in whiteList + foreach($eachTest in $whiteList) + { + $eachTest + cd ($PSScriptRoot+'\'+$eachTest) + $deatilPath = $PSScriptRoot + 'generate' + try + { + $IsGenerateSuccess = GenerateCode + if(-not $Generate -and $IsGenerateSuccess) + { + $sourceFilePath = $PSScriptRoot +'\'+$eachTest + '\generate\m3' + $targetFilePath = $PSScriptRoot +'\'+$eachTest + '\generate\m4' + CompareGeneratedCode -inputSourcePath $sourceFilePath -inputTargetPath $targetFilePath -testFileName $eachTest + } + } + catch + { + Write-Host -ForegroundColor yellow 'Generate error:' + $fileDetail.Name + } + } +}elseif($BlackList) +{ + #get each testfolder and except those tests in blacklist + foreach($fileDetail in $fileList) + { + foreach($blackTestName in $blackTestList) + { + if(($fileDetail.Mode -eq 'd----') -and (!$fileDetail.Name.Startswith($blackTestName))) + { + try + { + cd ($PSScriptRoot+'\'+$fileDetail.Name) + $deatilPath = $PSScriptRoot + 'generate' + $IsGenerateSuccess = GenerateCode + if(-not $Generate -and $IsGenerateSuccess) + { + $sourceFilePath = $PSScriptRoot +'\'+$fileDetail.Name + '\generate\m3' + $targetFilePath = $PSScriptRoot +'\'+$fileDetail.Name + '\generate\m4' + CompareGeneratedCode -inputSourcePath $sourceFilePath -inputTargetPath $targetFilePath -testFileName $fileDeatil.path + } + } + catch + { + Write-Host -ForegroundColor yellow 'Generate error:' + $fileDetail.Name + } + } + } + } +} +else +{ + foreach($fileDetail in $fileList) + { + if($fileDetail.Mode -eq 'd----' -and (!$fileDetail.Name.Startswith('Compare'))) + { + $g1 = $PSScriptRoot +'\' +$fileDetail.Name + cd ($PSScriptRoot +'\' +$fileDetail.Name) + $deatilPath = $PSScriptRoot +'\' +$fileDetail.Name + try + { + Write-Host -ForegroundColor Blue $fileDetail.Name + $IsGenerateSuccess = GenerateCode + if(-not $Generate -and $IsGenerateSuccess) + { + $sourceFilePath = $deatilPath + '\generate\m3' + $targetFilePath = $deatilPath + '\generate\m4' + CompareGeneratedCode -inputSourcePath $sourceFilePath -inputTargetPath $targetFilePath -testFileName $fileDetail.Name + } + } + catch + { + Write-Host -ForegroundColor yellow 'Generate error:' + $fileDetail.Name + } + } + } +} +cd $PSScriptRoot +#Throw error if there are some different +if($global:isError) +{ + throw 'Error: The code generated by the target file is different from the code generated by the source file.' +}else +{ + Write-Host -ForegroundColor blue 'All generated codes are the same' +} diff --git a/tests-upgrade/Configuration.json b/tests-upgrade/Configuration.json new file mode 100644 index 0000000000..510add0a1f --- /dev/null +++ b/tests-upgrade/Configuration.json @@ -0,0 +1,36 @@ +{ + "WhiteList": [ + "basic-disableazure-get", + "basic-disableazure-response" + ], + "BlackList": [ + "basic-get-querystr", + "basic-get-delete", + "basic-get-delete-put-patch", + "basic-request-methods", + "basic-get-response-operation", + "basic-response-multioperation", + "basic-response-defaultoperation", + "basic-disableazure-get", + "component-param", + "component-multiparam", + "component-param-remote", + "component--param-localremote", + "component-param-inbody", + "component-definitions-local", + "component-definitions-remote", + "component-definitions-combined", + "datamodels-datatypes-mixedtypes", + "datamodels-datatypes-integer", + "datamodels-datatypes-string", + "datamodels-datatypes-nullable", + "datamodels-datatypes-array", + "datamodels-datatypes-object", + "datamodels-datatypes-file", + "datamodels-datatypes-anytype", + "datamodels-enums", + "datamodels-keyvalue", + "datamodels-combineschema", + "datamodels-inheritpolymorphism" + ] +} \ No newline at end of file diff --git a/tests-upgrade/basic-disableazure-get/readme.md b/tests-upgrade/basic-disableazure-get/readme.md new file mode 100644 index 0000000000..f64f8c7464 --- /dev/null +++ b/tests-upgrade/basic-disableazure-get/readme.md @@ -0,0 +1,13 @@ +### AutoRest Configuration +> see https://aka.ms/autorest + +``` yaml +require: + - $(this-folder)/../readme.azure.noprofile.md + +azure: false + +input-file: + - $(this-folder)/swagger.json + +``` diff --git a/tests-upgrade/basic-disableazure-get/swagger.json b/tests-upgrade/basic-disableazure-get/swagger.json new file mode 100644 index 0000000000..ec29bdce64 --- /dev/null +++ b/tests-upgrade/basic-disableazure-get/swagger.json @@ -0,0 +1,53 @@ +{ + + "swagger": "2.0", + "info": { + "title": "AutoRestUpgradeClient", + "version": "2018-04-01", + "description": "ARM AutoRestUpgrade" + }, + "host": "management.azure.com", + "schemes": [ + "https" + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "security": [ + { + "azure_auth": [ + "user_impersonation" + ] + } + ], + "securityDefinitions": { + "azure_auth": { + "type": "oauth2", + "authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize", + "flow": "implicit", + "description": "Azure Active Directory OAuth2 Flow", + "scopes": { + "user_impersonation": "impersonate your user account" + } + } + }, + "paths": { + "/subscriptions/resourceGroup": { + "get": { + "tags": [ + "Workspaces" + ], + "operationId": "Workspaces_Get", + "description": "Gets the workspace.", + "responses": { + "200": { + "description": "Ok-Return" + } + } + } + } + } +} \ No newline at end of file diff --git a/tests-upgrade/basic-disableazure-response/readme.md b/tests-upgrade/basic-disableazure-response/readme.md new file mode 100644 index 0000000000..f64f8c7464 --- /dev/null +++ b/tests-upgrade/basic-disableazure-response/readme.md @@ -0,0 +1,13 @@ +### AutoRest Configuration +> see https://aka.ms/autorest + +``` yaml +require: + - $(this-folder)/../readme.azure.noprofile.md + +azure: false + +input-file: + - $(this-folder)/swagger.json + +``` diff --git a/tests-upgrade/basic-disableazure-response/swagger.json b/tests-upgrade/basic-disableazure-response/swagger.json new file mode 100644 index 0000000000..4e3ddee1c7 --- /dev/null +++ b/tests-upgrade/basic-disableazure-response/swagger.json @@ -0,0 +1,61 @@ +{ + + "swagger": "2.0", + "info": { + "title": "AutoRestUpgradeClient", + "version": "2018-04-01", + "description": "ARM AutoRestUpgrade" + }, + "host": "management.azure.com", + "schemes": [ + "https" + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "security": [ + { + "azure_auth": [ + "user_impersonation" + ] + } + ], + "securityDefinitions": { + "azure_auth": { + "type": "oauth2", + "authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize", + "flow": "implicit", + "description": "Azure Active Directory OAuth2 Flow", + "scopes": { + "user_impersonation": "impersonate your user account" + } + } + }, + "paths": { + "/subscriptions/resourceGroup": { + "get": { + "tags": [ + "Workspaces" + ], + "operationId": "Workspaces_Get", + "description": "Gets the workspace.", + "responses": { + "200": { + "description": "Ok-Return", + "schema": { + "$ref": "#/definitions/Workspaces" + } + } + } + } + } + }, + "definitions": { + "Workspaces": { + "type": "string" + } + } +} \ No newline at end of file