From 69cc41211b23a3c39f033c1b2ff78aeabd5fbda6 Mon Sep 17 00:00:00 2001 From: Sarangan Rajamanickam Date: Thu, 31 Oct 2024 10:39:41 -0700 Subject: [PATCH] [@typespec/spector] [@typespec/spec-coverage-sdk] - Handle Upload Scenario Manifest (#4920) Earlier, in the `upload-scenario-manifest` script, I had handled the multiple paths in a different way (one by one). After working with the `cadl-ranch-dashboard`, the correct approach is to generate the manifest file in the same way as coverage file. This PR performs this task. Here is a snapshot of the modified manifest file: ![image](https://github.com/user-attachments/assets/cf752245-452a-424f-b1ed-86b2965103a0) Please review and approve the PR. Thanks --- .../RefactorManifest-2024-9-30-14-30-49.md | 8 +++++++ .gitignore | 1 + packages/spec-coverage-sdk/CHANGELOG.md | 11 ++++++++- packages/spec-coverage-sdk/package.json | 2 +- packages/spec-coverage-sdk/src/client.ts | 2 +- packages/spec-coverage-sdk/src/types.ts | 2 +- packages/spector/CHANGELOG.md | 14 ++++++++++- packages/spector/package.json | 2 +- .../src/actions/upload-scenario-manifest.ts | 23 +++++++++++++------ packages/spector/src/cli/cli.ts | 17 ++++++++------ .../spector/src/coverage/scenario-manifest.ts | 8 ++++--- 11 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 .chronus/changes/RefactorManifest-2024-9-30-14-30-49.md diff --git a/.chronus/changes/RefactorManifest-2024-9-30-14-30-49.md b/.chronus/changes/RefactorManifest-2024-9-30-14-30-49.md new file mode 100644 index 0000000000..9db30ecdf4 --- /dev/null +++ b/.chronus/changes/RefactorManifest-2024-9-30-14-30-49.md @@ -0,0 +1,8 @@ +--- +changeKind: internal +packages: + - "@typespec/spec-coverage-sdk" + - "@typespec/spector" +--- + +Handle multiple scenarios in manifest creation \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0757a3ca00..8d28caaf44 100644 --- a/.gitignore +++ b/.gitignore @@ -129,6 +129,7 @@ lib-cov coverage !packages/spector/src/coverage spec-coverage.json +manifest.json # nyc test coverage .nyc_output diff --git a/packages/spec-coverage-sdk/CHANGELOG.md b/packages/spec-coverage-sdk/CHANGELOG.md index 839ae5217d..35070f2f85 100644 --- a/packages/spec-coverage-sdk/CHANGELOG.md +++ b/packages/spec-coverage-sdk/CHANGELOG.md @@ -1 +1,10 @@ -# @typespec/spec-coverage-sdk +# Change Log - @typespec/spec-coverage-sdk + +## 0.1.0-alpha.1 + +- Enabled uploading modified manifest file with details of more than one scenario paths. + +## 0.1.0-alpha.0 + +- Initial release of the `@typespec/spec-coverage-sdk` package. + diff --git a/packages/spec-coverage-sdk/package.json b/packages/spec-coverage-sdk/package.json index 47dd4f899e..74e7a79b33 100644 --- a/packages/spec-coverage-sdk/package.json +++ b/packages/spec-coverage-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@typespec/spec-coverage-sdk", - "version": "0.1.0-alpha.0", + "version": "0.1.0-alpha.1", "description": "Spec utility to manage the reported coverage", "main": "dist/index.js", "type": "module", diff --git a/packages/spec-coverage-sdk/src/client.ts b/packages/spec-coverage-sdk/src/client.ts index 5dc1007e25..ea31c59826 100644 --- a/packages/spec-coverage-sdk/src/client.ts +++ b/packages/spec-coverage-sdk/src/client.ts @@ -43,7 +43,7 @@ export class SpecManifestOperations { this.#blob = this.#container.getBlockBlobClient("manifest.json"); } - public async upload(manifest: ScenarioManifest): Promise { + public async upload(manifest: ScenarioManifest | ScenarioManifest[]): Promise { const content = JSON.stringify(manifest, null, 2); await this.#blob.upload(content, content.length, { blobHTTPHeaders: { diff --git a/packages/spec-coverage-sdk/src/types.ts b/packages/spec-coverage-sdk/src/types.ts index 384773fa0a..8347560e60 100644 --- a/packages/spec-coverage-sdk/src/types.ts +++ b/packages/spec-coverage-sdk/src/types.ts @@ -2,7 +2,7 @@ export type ScenarioManifest = { commit: string; version: string; scenarios: ScenarioData[]; - modes: string[]; + setName: string; }; export type ScenarioData = { diff --git a/packages/spector/CHANGELOG.md b/packages/spector/CHANGELOG.md index f0b927d827..3eb461a7ba 100644 --- a/packages/spector/CHANGELOG.md +++ b/packages/spector/CHANGELOG.md @@ -1 +1,13 @@ -# @typespec/spector +# Change Log - @typespec/spector + +## 0.1.0-alpha.2 + +- Fix the handling of multiple scenario paths in `manifest.json` file. + +## 0.1.0-alpha.1 + +- Enabled handling of multiple scenario paths + +## 0.1.0-alpha.0 + +- Initial release of the `@typespec/spector` package. diff --git a/packages/spector/package.json b/packages/spector/package.json index 947bc69f60..836d5805e4 100644 --- a/packages/spector/package.json +++ b/packages/spector/package.json @@ -1,6 +1,6 @@ { "name": "@typespec/spector", - "version": "0.1.0-alpha.1", + "version": "0.1.0-alpha.2", "description": "Typespec Core Tool to validate, run mock api, collect coverage.", "exports": { ".": { diff --git a/packages/spector/src/actions/upload-scenario-manifest.ts b/packages/spector/src/actions/upload-scenario-manifest.ts index 48edbe74b9..fd722ce730 100644 --- a/packages/spector/src/actions/upload-scenario-manifest.ts +++ b/packages/spector/src/actions/upload-scenario-manifest.ts @@ -1,28 +1,37 @@ import { AzureCliCredential } from "@azure/identity"; import { SpecCoverageClient } from "@typespec/spec-coverage-sdk"; import { writeFile } from "fs/promises"; +import { resolve } from "path"; import pc from "picocolors"; import { computeScenarioManifest } from "../coverage/scenario-manifest.js"; import { logger } from "../logger.js"; export interface UploadScenarioManifestConfig { - scenariosPath: string; + scenariosPaths: string[]; storageAccountName: string; + setName: string; } export async function uploadScenarioManifest({ - scenariosPath, + scenariosPaths, storageAccountName, + setName, }: UploadScenarioManifestConfig) { - const [manifest, diagnostics] = await computeScenarioManifest(scenariosPath); - if (manifest === undefined || diagnostics.length > 0) { - process.exit(-1); + const manifests = []; + for (const scenariosPath of scenariosPaths) { + const path = resolve(process.cwd(), scenariosPath); + logger.info(`Computing scenario manifest for ${path}`); + const [manifest, diagnostics] = await computeScenarioManifest(path, setName); + if (manifest === undefined || diagnostics.length > 0) { + process.exit(-1); + } + manifests.push(manifest); } - await writeFile("manifest.json", JSON.stringify(manifest, null, 2)); + await writeFile("manifest.json", JSON.stringify(manifests, null, 2)); const client = new SpecCoverageClient(storageAccountName, new AzureCliCredential()); await client.createIfNotExists(); - await client.manifest.upload(manifest); + await client.manifest.upload(manifests); logger.info( `${pc.green("✓")} Scenario manifest uploaded to ${storageAccountName} storage account.`, diff --git a/packages/spector/src/cli/cli.ts b/packages/spector/src/cli/cli.ts index c20c996a6b..c701ebb1b6 100644 --- a/packages/spector/src/cli/cli.ts +++ b/packages/spector/src/cli/cli.ts @@ -287,6 +287,11 @@ async function main() { array: true, demandOption: true, }) + .option("setName", { + type: "string", + description: "Set used to generate the manifest.", + demandOption: true, + }) .option("storageAccountName", { type: "string", description: "Name of the storage account", @@ -294,13 +299,11 @@ async function main() { .demandOption("storageAccountName"); }, async (args) => { - for (const scenariosPath of args.scenariosPaths) { - logger.info(`Uploading scenario manifest for scenarios at ${scenariosPath}`); - await uploadScenarioManifest({ - scenariosPath: resolve(process.cwd(), scenariosPath), - storageAccountName: args.storageAccountName, - }); - } + await uploadScenarioManifest({ + scenariosPaths: args.scenariosPaths, + storageAccountName: args.storageAccountName, + setName: args.setName, + }); }, ) .command( diff --git a/packages/spector/src/coverage/scenario-manifest.ts b/packages/spector/src/coverage/scenario-manifest.ts index 681fa4490b..08d1bee5e7 100644 --- a/packages/spector/src/coverage/scenario-manifest.ts +++ b/packages/spector/src/coverage/scenario-manifest.ts @@ -1,13 +1,14 @@ import { loadScenarios } from "../scenarios-resolver.js"; import { Diagnostic } from "../utils/diagnostic-reporter.js"; import { getCommit, getPackageJson } from "../utils/misc-utils.js"; -import { ScenarioLocation, ScenarioManifest, GeneratorMode } from "@typespec/spec-coverage-sdk"; +import { ScenarioLocation, ScenarioManifest } from "@typespec/spec-coverage-sdk"; import { getSourceLocation, normalizePath } from "@typespec/compiler"; import { relative } from "path"; import type { Scenario } from "../lib/decorators.js"; export async function computeScenarioManifest( scenariosPath: string, + setName: string ): Promise<[ScenarioManifest | undefined, readonly Diagnostic[]]> { const [scenarios, diagnostics] = await loadScenarios(scenariosPath); if (diagnostics.length > 0) { @@ -16,7 +17,7 @@ export async function computeScenarioManifest( const commit = getCommit(scenariosPath); const pkg = await getPackageJson(scenariosPath); - return [createScenarioManifest(scenariosPath, pkg?.version ?? "?", commit, scenarios), []]; + return [createScenarioManifest(scenariosPath, pkg?.version ?? "?", commit, scenarios, setName), []]; } export function createScenarioManifest( @@ -24,6 +25,7 @@ export function createScenarioManifest( version: string, commit: string, scenarios: Scenario[], + setName: string ): ScenarioManifest { const sortedScenarios = [...scenarios].sort((a, b) => a.name.localeCompare(b.name)); return { @@ -38,6 +40,6 @@ export function createScenarioManifest( }; return { name, scenarioDoc, location }; }), - modes: GeneratorMode, + setName }; }