diff --git a/.chronus/changes/eng-java-azure-generated-code-lint.md b/.chronus/changes/eng-java-azure-generated-code-lint.md new file mode 100644 index 00000000000..69fbf853c12 --- /dev/null +++ b/.chronus/changes/eng-java-azure-generated-code-lint.md @@ -0,0 +1,7 @@ +--- +changeKind: internal +packages: + - "@typespec/http-client-java" +--- + +Run Azure SDK Checkstyle and SpotBugs rules on generated Java test clients. diff --git a/packages/http-client-java/eng/scripts/Build-And-Test.Tests.ps1 b/packages/http-client-java/eng/scripts/Build-And-Test.Tests.ps1 index 215dc101186..c201003763c 100644 --- a/packages/http-client-java/eng/scripts/Build-And-Test.Tests.ps1 +++ b/packages/http-client-java/eng/scripts/Build-And-Test.Tests.ps1 @@ -45,6 +45,11 @@ function mvn { Directory = (Get-Location).Path }) $global:LASTEXITCODE = [int]$global:JavaBuildFailMaven + if ($LASTEXITCODE -eq 0 -and $args -contains 'verify') { + $null = New-Item -ItemType Directory -Path 'target' -Force + Set-Content -LiteralPath 'target/checkstyle-result.xml' -Value '' + Set-Content -LiteralPath 'target/spotbugsXml.xml' -Value '' + } } function java { $global:LASTEXITCODE = 0 } @@ -99,10 +104,14 @@ try { & './eng/scripts/Test-Packages.ps1' -UnitTests $builds = @($global:JavaBuildCommands | Where-Object Tool -eq 'mvn' | Where-Object { $_.Arguments -contains 'install' }) Assert-Equal 0 $builds.Count - $spectorTests = @($global:JavaBuildCommands | Where-Object Tool -eq 'mvn' | Where-Object { $_.Arguments -contains 'test' }) + $spectorTests = @($global:JavaBuildCommands | Where-Object Tool -eq 'mvn' | Where-Object { $_.Arguments -contains 'verify' -or $_.Arguments -contains 'test' }) Assert-Equal 2 $spectorTests.Count Assert-Equal (Join-Path $fixture 'generator/http-client-generator-test') $spectorTests[0].Directory + Assert-Equal $true ($spectorTests[0].Arguments -contains 'verify') + Assert-Equal $false ($spectorTests[0].Arguments -contains 'test') Assert-Equal (Join-Path $fixture 'generator/http-client-generator-clientcore-test') $spectorTests[1].Directory + Assert-Equal $true ($spectorTests[1].Arguments -contains 'test') + Assert-Equal $false ($spectorTests[1].Arguments -contains 'verify') $global:JavaBuildCommands.Clear() $global:JavaBuildFailMaven = $true diff --git a/packages/http-client-java/generator/http-client-generator-test/Spector-Tests.ps1 b/packages/http-client-java/generator/http-client-generator-test/Spector-Tests.ps1 index 0371b6e55f0..42c00ee2a8c 100644 --- a/packages/http-client-java/generator/http-client-generator-test/Spector-Tests.ps1 +++ b/packages/http-client-java/generator/http-client-generator-test/Spector-Tests.ps1 @@ -7,12 +7,27 @@ Write-Host "Running Spector tests" Write-Host "Starting the Spector server" npm run spector-start -Write-Host "Compile and run the tests" -mvn clean test --no-transfer-progress -T 1C if ($LASTEXITCODE -ne 0) { - throw "Spector tests failed" + throw "Failed to start the Spector server" +} +try { + Write-Host "Compile, run the tests, and analyze generated code" + mvn clean verify --no-transfer-progress -T 1C + if ($LASTEXITCODE -ne 0) { + throw "Spector tests or generated code analysis failed" + } + + foreach ($report in 'checkstyle-result.xml', 'spotbugsXml.xml') { + $reportPath = (Get-Item -LiteralPath (Join-Path $PSScriptRoot "target/$report")).FullName + Write-Host "Generated code analysis report: $reportPath" + if ($env:TF_BUILD -eq 'True') { + Write-Host "##vso[task.uploadfile]$reportPath" + } + } +} +finally { + Write-Host "Stopping the Spector server" + npm run spector-stop } -Write-Host "Stopping the Spector server" -npm run spector-stop Write-Host "Finished running the Spector tests" diff --git a/packages/http-client-java/generator/http-client-generator-test/linting/LICENSE.txt b/packages/http-client-java/generator/http-client-generator-test/linting/LICENSE.txt new file mode 100644 index 00000000000..3423e9584d1 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/linting/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Microsoft + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/http-client-java/generator/http-client-generator-test/linting/checkstyle-suppressions.xml b/packages/http-client-java/generator/http-client-generator-test/linting/checkstyle-suppressions.xml new file mode 100644 index 00000000000..334293adf45 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/linting/checkstyle-suppressions.xml @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/http-client-java/generator/http-client-generator-test/linting/checkstyle.xml b/packages/http-client-java/generator/http-client-generator-test/linting/checkstyle.xml new file mode 100644 index 00000000000..c2e71f20a86 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/linting/checkstyle.xml @@ -0,0 +1,433 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/http-client-java/generator/http-client-generator-test/linting/spotbugs-exclude.xml b/packages/http-client-java/generator/http-client-generator-test/linting/spotbugs-exclude.xml new file mode 100644 index 00000000000..9c26aa72fae --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/linting/spotbugs-exclude.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/http-client-java/generator/http-client-generator-test/linting/spotbugs-include.xml b/packages/http-client-java/generator/http-client-generator-test/linting/spotbugs-include.xml new file mode 100644 index 00000000000..783f86d75b4 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/linting/spotbugs-include.xml @@ -0,0 +1,10 @@ + + + + + + + + + diff --git a/packages/http-client-java/generator/http-client-generator-test/pom.xml b/packages/http-client-java/generator/http-client-generator-test/pom.xml index ad561dc8f94..7d131b856bd 100644 --- a/packages/http-client-java/generator/http-client-generator-test/pom.xml +++ b/packages/http-client-java/generator/http-client-generator-test/pom.xml @@ -78,4 +78,76 @@ slf4j-simple + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.6.0 + + + com.puppycrawl.tools + checkstyle + 9.3 + + + io.clientcore + linting-extensions + 1.0.0-beta.2 + + + + ${project.basedir}/linting/checkstyle.xml + ${project.basedir}/linting/checkstyle-suppressions.xml + + false + false + + + + checkstyle + verify + + check + + + + + + com.github.spotbugs + spotbugs-maven-plugin + 4.8.3.1 + + + com.github.spotbugs + spotbugs + 4.8.3 + + + org.ow2.asm + asm + 9.10.1 + + + + Max + Low + true + ${project.basedir}/linting/spotbugs-include.xml + ${project.basedir}/linting/spotbugs-exclude.xml + + + + spotbugs + verify + + spotbugs + + + + + +