Skip to content

Commit

Permalink
http-client-java, add unit test to emitter (#5742)
Browse files Browse the repository at this point in the history
Unit test with clientcore causes downstream module build failure.
Here we are on JDK 17 (and current clientcore depends on 17), but
downstream of autorest.java at 11.

Add TypeScript unit tests.

![image](https://github.com/user-attachments/assets/78b12d8c-0825-4d1a-84f4-89ec54211057)
  • Loading branch information
weidongxu-microsoft authored Jan 28, 2025
1 parent 22dc8c3 commit 67d284c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 175 deletions.
22 changes: 9 additions & 13 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ import {
HttpStatusCodesEntry,
Visibility,
getAuthentication,
isCookieParam,
} from "@typespec/http";
import { getSegment } from "@typespec/rest";
import { getAddedOnVersions } from "@typespec/versioning";
Expand Down Expand Up @@ -138,13 +137,13 @@ import {
getNonNullSdkType,
getUnionDescription,
getUsage,
isStable,
modelIs,
pushDistinct,
} from "./type-utils.js";
import {
DiagnosticError,
getNamespace,
isStableApiVersion,
pascalCase,
removeClientSuffix,
stringArrayContainsIgnoreCase,
Expand Down Expand Up @@ -764,7 +763,10 @@ export class CodeModelBuilder {
}
return versions
.slice(0, versions.indexOf(pinnedApiVersion) + 1)
.filter((version) => !excludePreview || !isStable(pinnedApiVersion) || isStable(version));
.filter(
(version) =>
!excludePreview || !isStableApiVersion(pinnedApiVersion) || isStableApiVersion(version),
);
}

private needToSkipProcessingOperation(
Expand Down Expand Up @@ -909,8 +911,7 @@ export class CodeModelBuilder {
clientContext.hostParameters.forEach((it) => codeModelOperation.addParameter(it));
// path/query/header parameters
for (const param of httpOperation.parameters) {
// TODO, switch to TCGC param.kind=="cookie"
if (param.__raw && isCookieParam(this.program, param.__raw)) {
if (param.kind === "cookie") {
// ignore cookie parameter
continue;
}
Expand Down Expand Up @@ -1287,12 +1288,7 @@ export class CodeModelBuilder {
}
}

// TODO: use param.onClient after TCGC fix
const parameterOnClient =
!isApiVersion(this.sdkContext, param) &&
param.correspondingMethodParams &&
param.correspondingMethodParams.length > 0 &&
param.correspondingMethodParams[0].onClient;
const parameterOnClient = param.onClient;

const nullable = param.type.kind === "nullable";
const parameter = new Parameter(param.name, param.doc ?? "", schema, {
Expand Down Expand Up @@ -2311,9 +2307,9 @@ export class CodeModelBuilder {
extensions["x-ms-mutability"] = mutability;
}

if (prop.kind === "property" && prop.multipartOptions) {
if (prop.kind === "property" && prop.serializationOptions.multipart) {
// TODO: handle MultipartOptions.isMulti
if (prop.multipartOptions.isFilePart) {
if (prop.serializationOptions.multipart?.isFilePart) {
schema = this.processMultipartFormDataFilePropertySchema(prop);
} else if (
prop.type.kind === "model" &&
Expand Down
4 changes: 0 additions & 4 deletions packages/http-client-java/emitter/src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ export class ProcessingCache<In, Out> {
}
}

export function isStable(version: string): boolean {
return !version.toLowerCase().includes("preview");
}

/** adds only if the item is not in the collection already
*
* @note While this isn't very efficient, it doesn't disturb the original
Expand Down
4 changes: 4 additions & 0 deletions packages/http-client-java/emitter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export function trace(program: Program, msg: string) {
program.trace("http-client-java", msg);
}

export function isStableApiVersion(version: string): boolean {
return !version.toLowerCase().includes("preview");
}

export function pascalCase(name: string): string {
if (name.length > 0) {
return name[0].toUpperCase() + name.slice(1);
Expand Down
32 changes: 32 additions & 0 deletions packages/http-client-java/emitter/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import {
isStableApiVersion,
pascalCase,
removeClientSuffix,
stringArrayContainsIgnoreCase,
} from "../src/utils";

describe("utils", () => {
it("isStableApiVersion", () => {
expect(isStableApiVersion("2022-09-01")).toBe(true);
expect(isStableApiVersion("2023-12-01-preview")).toBe(false);
});

it("pascalCase", () => {
expect(pascalCase("foo")).toBe("Foo");
expect(pascalCase("fooBar")).toBe("FooBar");
expect(pascalCase("FooBar")).toBe("FooBar");
expect(pascalCase("foo bar")).toBe("Foo bar");
});

it("stringArrayContainsIgnoreCase", () => {
expect(stringArrayContainsIgnoreCase(["foo", "bar"], "foo")).toBe(true);
expect(stringArrayContainsIgnoreCase(["foo", "bar"], "Bar")).toBe(true);
expect(stringArrayContainsIgnoreCase(["foo", "bar"], "del")).toBe(false);
});

it("removeClientSuffix", () => {
expect(removeClientSuffix("FooClient")).toBe("Foo");
expect(removeClientSuffix("client")).toBe("client");
});
});
11 changes: 11 additions & 0 deletions packages/http-client-java/emitter/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig, mergeConfig } from "vitest/config";
import { defaultTypeSpecVitestConfig } from "../../../vitest.workspace.js";

export default mergeConfig(
defaultTypeSpecVitestConfig,
defineConfig({
test: {
include: ["emitter/test/**/*.test.ts"],
},
}),
);
3 changes: 3 additions & 0 deletions packages/http-client-java/eng/scripts/Test-Packages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Invoke-LoggedCommand "mvn -version"
Push-Location $packageRoot
try {
if ($UnitTests) {
Invoke-LoggedCommand "npm run test"
Write-Host "Emitter unit tests passed"

Write-Host "Current PATH: $env:PATH"
Write-Host "Current JAVA_HOME: $Env:JAVA_HOME"
$env:JAVA_HOME = $env:JAVA_HOME_21_X64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@
<version>5.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.clientcore</groupId>
<artifactId>core</artifactId>
<version>1.0.0-beta.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down

This file was deleted.

7 changes: 6 additions & 1 deletion packages/http-client-java/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"type": "module",
"main": "dist/emitter/index.js",
"exports": {
".": "./dist/emitter/index.js"
".": {
"types": "./dist/emitter/index.d.ts",
"default": "./dist/emitter/index.js"
}
},
"engines": {
"node": ">=18.0.0"
Expand All @@ -29,6 +32,8 @@
"build": "npm run build:generator && npm run build:emitter",
"build:emitter": "tsc -p ./emitter/tsconfig.build.json",
"build:generator": "mvn clean install --no-transfer-progress -T 1C -f ./generator/pom.xml",
"test": "npm run test:emitter",
"test:emitter": "vitest run -c ./emitter/vitest.config.ts",
"format": "pnpm -w format:dir packages/http-client-java && mvn spotless:apply --no-transfer-progress -T 1C --activate-profiles test -f ./generator/pom.xml",
"lint": "eslint . --max-warnings=0",
"lint:fix": "eslint . --fix",
Expand Down

0 comments on commit 67d284c

Please sign in to comment.