Skip to content

Commit

Permalink
[tsp-client] Normalize tsp-location.yaml directories before processing (
Browse files Browse the repository at this point in the history
#8390)

* normalize tsp-location.yaml directory

* add test

* add test

* feedback

---------

Co-authored-by: Catalina Peralta <[email protected]>
  • Loading branch information
catalinaperalta and cperaltah authored Jun 7, 2024
1 parent 90aeed6 commit 882b33f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions tools/tsp-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release

## 2024-06-07 - 0.8.1

- Normalize and clean up the directory property in tsp-location.yaml.

## 2024-05-29 - 0.8.0

- Create unique directories for sparse spec checkout.
Expand Down
4 changes: 2 additions & 2 deletions tools/tsp-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tools/tsp-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/typespec-client-generator-cli",
"version": "0.8.0",
"version": "0.8.1",
"description": "A tool to generate Azure SDKs from TypeSpec",
"main": "dist/index.js",
"homepage": "https://github.com/Azure/azure-sdk-tools/tree/main/tools/tsp-client#readme",
Expand Down
12 changes: 11 additions & 1 deletion tools/tsp-client/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mkdir, rm, stat, readFile, access } from "node:fs/promises";
import { Logger } from "./log.js";
import { parse as parseYaml } from "yaml";
import { TspLocation } from "./typespec.js";
import { joinPaths, resolvePath } from "@typespec/compiler";
import { joinPaths, normalizePath, resolvePath } from "@typespec/compiler";

export async function ensureDirectory(path: string) {
await mkdir(path, { recursive: true });
Expand Down Expand Up @@ -32,6 +32,11 @@ export async function readTspLocation(rootDir: string): Promise<TspLocation> {
if (!tspLocation.additionalDirectories) {
tspLocation.additionalDirectories = [];
}

// Normalize the directory path and remove trailing slash
tspLocation.directory = normalizeDirectory(tspLocation.directory);
tspLocation.additionalDirectories = tspLocation.additionalDirectories.map(normalizeDirectory);

return tspLocation;
}
throw new Error("Could not find tsp-location.yaml");
Expand Down Expand Up @@ -59,3 +64,8 @@ export async function getEmitterFromRepoConfig(emitterPath: string): Promise<str
}
throw new Error("Could not find emitter package");
}

export function normalizeDirectory(directory: string): string {
const normalizedDir = normalizePath(directory);
return normalizedDir.endsWith("/") ? normalizedDir.slice(0, -1) : normalizedDir;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
directory: specification\contosowidgetmanager\Contoso.WidgetManager\
commit: 9df71d5a717e4ed5e6728e7e6ba2fead60f62243
repo: Azure\azure-rest-api-specs
additionalDirectories:
- specification\contosowidgetmanager\Contoso.WidgetManager.Shared\
- specification/contosowidgetmanager/Contoso.WidgetManager.Utils/
17 changes: 17 additions & 0 deletions tools/tsp-client/test/tspLocation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assert } from "chai";
import { normalizeDirectory, readTspLocation } from "../src/fs.js";
import { describe, it } from "node:test";

describe("Parse tsp-location.yaml", function () {
it("Normalize tsp-location.yaml directories", async function () {
const tspLocation = await readTspLocation("./test/examples/sdk/badtsplocation");
const directory = normalizeDirectory(tspLocation.directory);
assert.equal(directory, "specification/contosowidgetmanager/Contoso.WidgetManager");
const additionalDirectories = tspLocation.additionalDirectories?.map(normalizeDirectory);
assert.equal(additionalDirectories?.length, 2);
if (additionalDirectories) {
assert.equal(additionalDirectories[0], "specification/contosowidgetmanager/Contoso.WidgetManager.Shared");
assert.equal(additionalDirectories[1], "specification/contosowidgetmanager/Contoso.WidgetManager.Utils");
}
});
});

0 comments on commit 882b33f

Please sign in to comment.