Skip to content

Commit

Permalink
http-client-java, e2e test, use clientcore (#5437)
Browse files Browse the repository at this point in the history
fix Azure/autorest.java#3000
fix Azure/autorest.java#3002
fix Azure/autorest.java#2986

Cases that generated code cannot compile is currently skipped.
Cases that not able to pass is disabled.

Current result https://specdashboard.z5.web.core.windows.net/

![image](https://github.com/user-attachments/assets/d14328b4-be18-4536-90f1-ac07a0046f20)

---------

Co-authored-by: hongli750210 <[email protected]>
  • Loading branch information
weidongxu-microsoft and v-hongli1 authored Jan 14, 2025
1 parent 27373e7 commit dd36417
Show file tree
Hide file tree
Showing 996 changed files with 105,811 additions and 131 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ packages/http-client-csharp/generator/TestProjects/**/tspCodeModel.json

# auto generated api view properties files
packages/http-client-java/generator/http-client-generator-test/src/main/**/*.json
packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/**/*.json
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ ignorePaths:
- "**/ThirdPartyNotices.txt"
- packages/compiler/test/formatter/scenarios/**
- packages/http-client-java/generator/http-client-generator-test/**
- packages/http-client-java/generator/http-client-generator-clientcore-test/**
- pnpm-lock.yaml
- "**/*.mp4"
- .git/**
Expand Down
3 changes: 1 addition & 2 deletions packages/http-client-java/eng/pipeline/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extends:
parameters:
BuildPrereleaseVersion: true
UseTypeSpecNext: false
Publish: "public"
Publish: ${{replace(replace('True',eq(variables['Build.SourceBranchName'], 'main'), 'public'),'True','internal')}}
PublishDependsOnTest: true
PackagePath: /packages/http-client-java
EmitterPackageJsonPath: packages/http-client-java/emitter/package.json
Expand All @@ -30,4 +30,3 @@ extends:
LanguageShortName: "java"
HasNugetPackages: false
CadlRanchName: "@typespec/http-client-java"
EnableCadlRanchReport: false
7 changes: 5 additions & 2 deletions packages/http-client-java/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ $env:PATH = "$env:JAVA_HOME\bin;$env:PATH"
Invoke "npm run build:generator"
Invoke "npm run build:emitter"

$testDir = Join-Path $repoRoot 'test'

$generatorTestDir = Join-Path $repoRoot 'generator/http-client-generator-test'
Set-Location $generatorTestDir
./Generate.ps1
Set-Location $PSScriptRoot

$generatorTestDir = Join-Path $repoRoot 'generator/http-client-generator-clientcore-test'
Set-Location $generatorTestDir
./Generate.ps1
Set-Location $PSScriptRoot
16 changes: 13 additions & 3 deletions packages/http-client-java/eng/scripts/Test-Packages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ try {
try {
& ./Setup.ps1
& ./Spector-Tests.ps1
Set-Location $packageRoot
Write-Host "Spector tests passed"
}
finally {
Pop-Location
}

$generatorTestDir = Join-Path $packageRoot 'generator/http-client-generator-clientcore-test'
Push-Location $generatorTestDir
try {
& ./Setup.ps1
& ./Spector-Tests.ps1
}
finally {
Pop-Location
}

Write-Host "Spector tests passed"
}
catch {
Write-Error "Spector tests failed: $_"
Expand All @@ -52,7 +62,7 @@ try {
if (!(Test-Path $coverageReportDir)) {
New-Item -ItemType Directory -Path $coverageReportDir

$sourceFile = Join-Path $packageRoot 'generator/http-client-generator-test/tsp-spector-coverage-java-standard.json'
$sourceFile = Join-Path $packageRoot 'generator/http-client-generator-clientcore-test/tsp-spector-coverage-java-standard.json'
$targetFile = Join-Path $coverageReportDir 'tsp-spector-coverage-java-standard.json'
Copy-Item $sourceFile -Destination $targetFile
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Use case:
#
# The purpose of this script is to compact the steps required to regenerate TypeSpec into a single script.
#
param (
[int] $Parallelization = [Environment]::ProcessorCount
)


$ExitCode = 0

if ($Parallelization -lt 1) {
$Parallelization = 1
}

Write-Host "Parallelization: $Parallelization"


$generateScript = {
$tspFile = $_

$tspClientFile = $tspFile -replace 'main.tsp', 'client.tsp'
if (($tspClientFile -match 'client.tsp$') -and (Test-Path $tspClientFile)) {
$tspFile = $tspClientFile
}

# With TypeSpec code generation being parallelized, we need to make sure that the output directory is unique
# for each test run. We do this by appending a random number to the output directory.
# Without this, we could have multiple runs trying to write to the same directory which introduces race conditions.
$tspOptions = "--option ""@typespec/http-client-java.emitter-output-dir={project-root}/tsp-output/$(Get-Random)"""
if ($tspFile -match "type[\\/]enum[\\/]extensible[\\/]") {
# override namespace for reserved keyword "enum"
$tspOptions += " --option ""@typespec/http-client-java.namespace=type.enums.extensible"""
} elseif ($tspFile -match "type[\\/]enum[\\/]fixed[\\/]") {
# override namespace for reserved keyword "enum"
$tspOptions += " --option ""@typespec/http-client-java.namespace=type.enums.fixed"""
} elseif ($tspFile -match "type[\\/]array" -or $tspFile -match "type[\\/]dictionary") {
# TODO https://github.com/Azure/autorest.java/issues/2964
# also serve as a test for "use-object-for-unknown" emitter option
$tspOptions += " --option ""@typespec/http-client-java.use-object-for-unknown=true"""
}

$tspTrace = "--trace import-resolution --trace projection --trace http-client-java"
$tspCommand = "npx --no-install tsp compile $tspFile $tspOptions $tspTrace"

$timer = [Diagnostics.Stopwatch]::StartNew()
$generateOutput = Invoke-Expression $tspCommand
$timer.Stop()

$global:ExitCode = $global:ExitCode -bor $LASTEXITCODE

if ($LASTEXITCODE -ne 0) {
Write-Host "
========================
$tspCommand
========================
FAILED (Time elapsed: $($timer.ToString()))
$([String]::Join("`n", $generateOutput))
"
} else {
Write-Host "
========================
$tspCommand
========================
SUCCEEDED (Time elapsed: $($timer.ToString()))
"
}

if ($global:ExitCode -ne 0) {
exit $global:ExitCode
}
}

./Setup.ps1

if (Test-Path ./src/main) {
Remove-Item ./src/main -Recurse -Force
}
if (Test-Path ./src/samples) {
Remove-Item ./src/samples -Recurse -Force
}
if (Test-Path ./tsp-output) {
Remove-Item ./tsp-output -Recurse -Force
}

# generate for http-specs/azure-http-specs test sources
Copy-Item -Path node_modules/@typespec/http-specs/specs -Destination ./ -Recurse -Force
# remove xml tests, emitter has not supported xml model
Remove-Item ./specs/payload/xml -Recurse -Force

# TokenCredential not in core
Remove-Item ./specs/authentication/oauth2 -Recurse -Force
Remove-Item ./specs/authentication/union -Recurse -Force
# Base64Url not in core
Remove-Item ./specs/encode/bytes -Recurse -Force
# DateTimeRfc1123 is private in beta.1, should now be public in main
Remove-Item ./specs/encode/datetime -Recurse -Force
Remove-Item ./specs/special-headers -Recurse -Force
# JacksonAdapter not in core
Remove-Item ./specs/encode/duration -Recurse -Force

$job = (Get-ChildItem ./specs -Include "main.tsp","old.tsp" -File -Recurse) | ForEach-Object -Parallel $generateScript -ThrottleLimit $Parallelization -AsJob

$job | Wait-Job -Timeout 1200
$job | Receive-Job

Remove-Item ./specs -Recurse -Force

Copy-Item -Path ./tsp-output/*/src -Destination ./ -Recurse -Force -Exclude @("module-info.java")

Remove-Item ./tsp-output -Recurse -Force

if (Test-Path ./src/main/resources/META-INF/client-structure-service_apiview_properties.json) {
# client structure is generated from multiple client.tsp files and the last one to execute overwrites
# the api view properties file. Because the tests run in parallel, the order is not guaranteed. This
# causes git diff check to fail as the checked in file is not the same as the generated one.
Remove-Item ./src/main/resources/META-INF/client-structure-service_apiview_properties.json -Force
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# re-build http-client-java
Set-Location (Resolve-Path (Join-Path $PSScriptRoot '..' '..'))

./Setup.ps1

Set-Location $PSScriptRoot

npm run clean && npm install
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#Requires -Version 7.0

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 3.0

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) {
exit $LASTEXITCODE
}
Write-Host "Stopping the Spector server"
npm run spector-stop

Write-Host "Finished running the Spector tests"
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@typespec/http-client-java-clientcore-tests",
"version": "0.1.0",
"type": "module",
"scripts": {
"clean": "rimraf ./node_modules/@typespec/http-client-java ./package-lock.json ./tsp-output",
"format": "npm run -s prettier -- --write",
"check-format": "npm run prettier -- --check",
"prettier": "prettier --config ./.prettierrc.yaml **/*.tsp",
"spector-serve": "tsp-spector serve ./node_modules/@typespec/http-specs/specs --coverageFile ./tsp-spector-coverage-java-standard.json",
"spector-start": "tsp-spector server start ./node_modules/@typespec/http-specs/specs --coverageFile ./tsp-spector-coverage-java-standard.json",
"spector-stop": "tsp-spector server stop"
},
"dependencies": {
"@typespec/http-specs": "0.1.0-alpha.5",
"@typespec/http-client-java": "file:../../typespec-http-client-java-0.1.5.tgz",
"@typespec/http-client-java-tests": "file:"
},
"overrides": {
"@typespec/compiler": "~0.63.0",
"@typespec/http": "~0.63.0",
"@typespec/rest": "~0.63.0",
"@typespec/versioning": "~0.63.0",
"@typespec/openapi": "~0.63.0",
"@typespec/xml": "~0.63.0",
"@azure-tools/typespec-azure-core": "~0.49.0",
"@azure-tools/typespec-client-generator-core": "~0.49.1",
"@azure-tools/typespec-azure-resource-manager": "~0.49.0",
"@azure-tools/typespec-autorest": "~0.49.0"
},
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.microsoft.typespec</groupId>
<artifactId>typespec-java-generator</artifactId>
<version>1.0.0-beta.1</version>
</parent>

<groupId>com.microsoft.typespec</groupId>
<artifactId>http-client-generator-clientcore-test</artifactId>
<packaging>jar</packaging>

<name>http-client-generator-test</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spotless.config.path>../</spotless.config.path>
</properties>

<dependencies>
<dependency>
<groupId>io.clientcore</groupId>
<artifactId>core</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.4.41</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<!-- bytebuddy dependencies are required for mockito 4.11.0 to work with Java 21. Mockito 4.11.0 is the last release -->
<!-- of Mockito supporting Java 8 as a baseline. -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.15.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.15.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.11.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit dd36417

Please sign in to comment.