Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeSpec Validation] Improve error for incorrect argument #27248

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions eng/tools/typespec-validation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions eng/tools/typespec-validation/src/tsv-host.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface TsvHost {
checkFileExists(file: string): Promise<boolean>;
isDirectory(path: string): Promise<boolean>;
gitOperation(folder: string): IGitOperation;
readTspConfig(folder: string): Promise<string>;
runCmd(cmd: string, cwd: string): Promise<[Error | null, string, string]>;
Expand Down
6 changes: 5 additions & 1 deletion eng/tools/typespec-validation/src/tsv-runner-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
return checkFileExists(file);
}

isDirectory(path: string) {
return isDirectory(path);
}

gitOperation(folder: string): IGitOperation {
return simpleGit(folder);
}
Expand Down
6 changes: 5 additions & 1 deletion eng/tools/typespec-validation/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions eng/tools/typespec-validation/test/tsv-test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class TsvTestHost implements TsvHost {
return true;
}

async isDirectory(_path: string): Promise<boolean> {
return true;
}

normalizePath(folder: string): string {
return normalizePath(folder, this.path);
}
Expand Down