From 093dbcc7b8c76abf5a8d9eeacc08e8bd52785725 Mon Sep 17 00:00:00 2001 From: Albert Date: Thu, 4 Jan 2024 10:02:20 -0800 Subject: [PATCH] tsv path error message --- eng/tools/typespec-validation/src/index.ts | 8 ++++++++ eng/tools/typespec-validation/src/tsv-host.ts | 1 + eng/tools/typespec-validation/src/tsv-runner-host.ts | 6 +++++- eng/tools/typespec-validation/src/utils.ts | 6 +++++- eng/tools/typespec-validation/test/tsv-test-host.ts | 4 ++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/eng/tools/typespec-validation/src/index.ts b/eng/tools/typespec-validation/src/index.ts index 895872d4cc1c..997df10cb64b 100755 --- a/eng/tools/typespec-validation/src/index.ts +++ b/eng/tools/typespec-validation/src/index.ts @@ -21,6 +21,14 @@ export async function main() { const folder = parsedArgs.positionals[0]; const absolutePath = host.normalizePath(folder); + if (!(await host.checkFileExists(absolutePath))) { + console.log(`Folder ${absolutePath} does not exist`); + process.exit(1); + } + if (!(await host.isDirectory(absolutePath))) { + console.log(`Please run TypeSpec Validation on a directory path`); + process.exit(1); + } console.log("Running TypeSpecValidation on folder: ", absolutePath); const rules = [ diff --git a/eng/tools/typespec-validation/src/tsv-host.ts b/eng/tools/typespec-validation/src/tsv-host.ts index b470537bc40e..b274ed2a0a28 100644 --- a/eng/tools/typespec-validation/src/tsv-host.ts +++ b/eng/tools/typespec-validation/src/tsv-host.ts @@ -1,5 +1,6 @@ export interface TsvHost { checkFileExists(file: string): Promise; + isDirectory(path: string): Promise; gitOperation(folder: string): IGitOperation; readTspConfig(folder: string): Promise; runCmd(cmd: string, cwd: string): Promise<[Error | null, string, string]>; diff --git a/eng/tools/typespec-validation/src/tsv-runner-host.ts b/eng/tools/typespec-validation/src/tsv-runner-host.ts index 6876528b1168..d42261acda80 100644 --- a/eng/tools/typespec-validation/src/tsv-runner-host.ts +++ b/eng/tools/typespec-validation/src/tsv-runner-host.ts @@ -3,13 +3,17 @@ import { readFile } from "fs/promises"; import { IGitOperation, TsvHost } from "./tsv-host.js"; import { globby } from "globby"; import { simpleGit } from "simple-git"; -import { checkFileExists, normalizePath, runCmd } from "./utils.js"; +import { checkFileExists, isDirectory, normalizePath, runCmd } from "./utils.js"; export class TsvRunnerHost implements TsvHost { checkFileExists(file: string): Promise { return checkFileExists(file); } + isDirectory(path: string) { + return isDirectory(path); + } + gitOperation(folder: string): IGitOperation { return simpleGit(folder); } diff --git a/eng/tools/typespec-validation/src/utils.ts b/eng/tools/typespec-validation/src/utils.ts index 9675a63882e3..c2471159f9d3 100644 --- a/eng/tools/typespec-validation/src/utils.ts +++ b/eng/tools/typespec-validation/src/utils.ts @@ -1,4 +1,4 @@ -import { access } from "fs/promises"; +import { access, stat } from "fs/promises"; import { exec } from "child_process"; import defaultPath, { PlatformPath } from "path"; @@ -22,6 +22,10 @@ export async function checkFileExists(file: string) { .catch(() => false); } +export async function isDirectory(path: string) { + return (await stat(path)).isDirectory(); +} + export function normalizePath(folder: string, path: PlatformPath = defaultPath) { return path .resolve(folder) diff --git a/eng/tools/typespec-validation/test/tsv-test-host.ts b/eng/tools/typespec-validation/test/tsv-test-host.ts index 3e846e5856b5..f974608896bd 100644 --- a/eng/tools/typespec-validation/test/tsv-test-host.ts +++ b/eng/tools/typespec-validation/test/tsv-test-host.ts @@ -44,6 +44,10 @@ export class TsvTestHost implements TsvHost { return true; } + async isDirectory(_path: string): Promise { + return true; + } + normalizePath(folder: string): string { return normalizePath(folder, this.path); }