Skip to content

Commit

Permalink
[@typespec/spector] [@typespec/spec-coverage-sdk] - Handle Upload Sce…
Browse files Browse the repository at this point in the history
…nario 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
  • Loading branch information
sarangan12 authored Oct 31, 2024
1 parent 40c8907 commit 69cc412
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 23 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/RefactorManifest-2024-9-30-14-30-49.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: internal
packages:
- "@typespec/spec-coverage-sdk"
- "@typespec/spector"
---

Handle multiple scenarios in manifest creation
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ lib-cov
coverage
!packages/spector/src/coverage
spec-coverage.json
manifest.json

# nyc test coverage
.nyc_output
Expand Down
11 changes: 10 additions & 1 deletion packages/spec-coverage-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

2 changes: 1 addition & 1 deletion packages/spec-coverage-sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/spec-coverage-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SpecManifestOperations {
this.#blob = this.#container.getBlockBlobClient("manifest.json");
}

public async upload(manifest: ScenarioManifest): Promise<void> {
public async upload(manifest: ScenarioManifest | ScenarioManifest[]): Promise<void> {
const content = JSON.stringify(manifest, null, 2);
await this.#blob.upload(content, content.length, {
blobHTTPHeaders: {
Expand Down
2 changes: 1 addition & 1 deletion packages/spec-coverage-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type ScenarioManifest = {
commit: string;
version: string;
scenarios: ScenarioData[];
modes: string[];
setName: string;
};

export type ScenarioData = {
Expand Down
14 changes: 13 additions & 1 deletion packages/spector/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/spector/package.json
Original file line number Diff line number Diff line change
@@ -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": {
".": {
Expand Down
23 changes: 16 additions & 7 deletions packages/spector/src/actions/upload-scenario-manifest.ts
Original file line number Diff line number Diff line change
@@ -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.`,
Expand Down
17 changes: 10 additions & 7 deletions packages/spector/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,23 @@ 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",
})
.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(
Expand Down
8 changes: 5 additions & 3 deletions packages/spector/src/coverage/scenario-manifest.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -16,14 +17,15 @@ 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(
scenariosPath: string,
version: string,
commit: string,
scenarios: Scenario[],
setName: string
): ScenarioManifest {
const sortedScenarios = [...scenarios].sort((a, b) => a.name.localeCompare(b.name));
return {
Expand All @@ -38,6 +40,6 @@ export function createScenarioManifest(
};
return { name, scenarioDoc, location };
}),
modes: GeneratorMode,
setName
};
}

0 comments on commit 69cc412

Please sign in to comment.