Skip to content

Commit

Permalink
[spec-gen-sdk-runner] Align dependencies, add workflow to run unit te…
Browse files Browse the repository at this point in the history
…sts (#33008)
  • Loading branch information
mikeharder authored Mar 6, 2025
1 parent 963f34e commit a9aad2b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 317 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/spec-gen-sdk-runner-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: spec-gen-sdk-runner - Test

on:
push:
branches:
- main
- typespec-next
pull_request:
paths:
- package-lock.json
- package.json
- tsconfig.json
- .github/workflows/_reusable-eng-tools-test.yaml
- .github/workflows/spec-gen-sdk-runner-test.yaml
- eng/tools/package.json
- eng/tools/tsconfig.json
- eng/tools/spec-gen-sdk-runner/**
workflow_dispatch:

permissions:
contents: read

jobs:
specGenSdkRunner:
name: spec-gen-sdk-runner
uses: ./.github/workflows/_reusable-eng-tools-test.yaml
with:
package: spec-gen-sdk-runner
lint: true
sparse-checkout-paths: |
specification/common-types
specification/contosowidgetmanager
6 changes: 3 additions & 3 deletions eng/tools/spec-gen-sdk-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"test:ci": "vitest run --coverage --reporter=verbose"
},
"engines": {
"node": ">= 18.0.0"
"node": ">=20.0.0"
},
"devDependencies": {
"@types/node": "^18.19.31",
"@vitest/coverage-c8": "^0.33.0",
"@vitest/coverage-v8": "^3.0.5",
"typescript": "~5.6.2",
"vitest": "^3.0.7"
"vitest": "^3.0.5"
}
}
2 changes: 1 addition & 1 deletion eng/tools/spec-gen-sdk-runner/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function getArgumentValue(args: string[], flag: string, defaultValue: str
* Get the relative path from the specification folder
*/
export function getRelativePathFromSpecification(absolutePath: string): string {
const specificationIndex = absolutePath.indexOf("specification/");
const specificationIndex = absolutePath.indexOf(path.normalize("specification/"));
if (specificationIndex !== -1) {
return absolutePath.slice(Math.max(0, specificationIndex));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, test, expect } from "vitest";
import { createCombinedSpecs, type SpecResults } from "../../src/utils.js";
import path from "node:path";

describe("createCombinedSpecs", () => {
test("combines specs from readme and typespec paths", () => {
Expand All @@ -26,9 +27,9 @@ describe("createCombinedSpecs", () => {
expect(result[0].specs).toContain("api.json");
expect(result[0].specs).toContain("main.tsp");
expect(result[0].specs).toContain("client.tsp");
expect(result[0].readmeMd).toBe("specification/apicenter/data-plane/readme.md");
expect(result[0].readmeMd).toBe(path.normalize("specification/apicenter/data-plane/readme.md"));
expect(result[0].typespecProject).toBe(
"specification/apicenter/ApiCenter.DataApi/tspconfig.yaml",
path.normalize("specification/apicenter/ApiCenter.DataApi/tspconfig.yaml"),
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ describe("searchRelatedTypeSpecProjectBySharedLibrary", () => {

test("finds related TypeSpec projects for shared libraries", () => {
const sharedLibraries = [
"specification/contosowidgetmanager/Contoso.WidgetManager.Shared/main.tsp",
path.normalize("specification/contosowidgetmanager/Contoso.WidgetManager.Shared/main.tsp"),
];

const result = searchRelatedTypeSpecProjectBySharedLibrary(sharedLibraries, {
searchFileRegex: /^tspconfig\.yaml$/,
specRepoFolder: repoRoot,
});

const expectedPath = path.normalize("specification/contosowidgetmanager/Contoso.WidgetManager");
expect(Object.keys(result)).toHaveLength(1);
expect(result["specification/contosowidgetmanager/Contoso.WidgetManager"]).toBeDefined();
expect(result["specification/contosowidgetmanager/Contoso.WidgetManager"]).toContain(
expect(result[expectedPath]).toBeDefined();
expect(result[expectedPath]).toContain(
sharedLibraries[0],
);
});
Expand All @@ -35,7 +35,7 @@ describe("searchRelatedTypeSpecProjectBySharedLibrary", () => {
});

test("handles non-existent directories", () => {
const sharedLibraries = ["specification/nonexistent/Shared/main.tsp"];
const sharedLibraries = [path.normalize("specification/nonexistent/Shared/main.tsp")];

const result = searchRelatedTypeSpecProjectBySharedLibrary(sharedLibraries, {
searchFileRegex: /^tspconfig\.yaml$/,
Expand Down
29 changes: 16 additions & 13 deletions eng/tools/spec-gen-sdk-runner/test/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ const repoRoot = path.resolve(path.dirname(currentFilePath), "../../../../../");
describe("Utils", () => {
describe("findFilesRecursive", () => {
test("finds all tspconfig.yaml files recursively", () => {
const results = findFilesRecursive(
`${repoRoot}/specification/contosowidgetmanager`,
"tspconfig.yaml",
);
const searchPath = path.normalize(`${repoRoot}/specification/contosowidgetmanager`);
const results = findFilesRecursive(searchPath, "tspconfig.yaml");
expect(results).toHaveLength(2);
expect(results).toContain(
"specification/contosowidgetmanager/Contoso.Management/tspconfig.yaml",
path.normalize("specification/contosowidgetmanager/Contoso.Management/tspconfig.yaml"),
);
expect(results).toContain(
"specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml",
path.normalize("specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml"),
);
});

Expand All @@ -46,10 +44,15 @@ describe("Utils", () => {

describe("findReadmeFiles", () => {
test("finds all readme.md files in directory", () => {
const results = findReadmeFiles(`${repoRoot}/specification/contosowidgetmanager`);
const searchPath = path.normalize(`${repoRoot}/specification/contosowidgetmanager`);
const results = findReadmeFiles(searchPath);
expect(results).toHaveLength(2);
expect(results).toContain("specification/contosowidgetmanager/resource-manager/readme.md");
expect(results).toContain("specification/contosowidgetmanager/data-plane/readme.md");
expect(results).toContain(
path.normalize("specification/contosowidgetmanager/resource-manager/readme.md"),
);
expect(results).toContain(
path.normalize("specification/contosowidgetmanager/data-plane/readme.md"),
);
});

test("returns empty array for directory without readme files", () => {
Expand All @@ -63,9 +66,9 @@ describe("Utils", () => {
describe("getRelativePathFromSpecification", () => {
test("extracts path from specification folder", () => {
const result = getRelativePathFromSpecification(
"/repo/root/specification/apicenter/resource-manager/readme.md",
path.normalize("/repo/root/specification/apicenter/resource-manager/readme.md"),
);
expect(result).toBe("specification/apicenter/resource-manager/readme.md");
expect(result).toBe(path.normalize("specification/apicenter/resource-manager/readme.md"));
});

test("returns original path if specification is not found", () => {
Expand All @@ -76,9 +79,9 @@ describe("Utils", () => {

test("handles paths with multiple specification occurrences", () => {
const result = getRelativePathFromSpecification(
"/repo/root/specification/old/specification/apicenter/readme.md",
path.normalize("/repo/root/specification/old/specification/apicenter/readme.md"),
);
expect(result).toBe("specification/old/specification/apicenter/readme.md");
expect(result).toBe(path.normalize("specification/old/specification/apicenter/readme.md"));
});

test("handles empty path", () => {
Expand Down
Loading

0 comments on commit a9aad2b

Please sign in to comment.