Skip to content

Commit 701a4c6

Browse files
mikeharderzman-ms
authored andcommitted
[TypeSpec Validation] Refactor TsvHost (#26411)
- Export TsvRunnerHost and TsvTestHost as classes rather than consts - Change test names to lower-case for consistency with typespec repo - Delete incomplete FolderStructure rule - TsvRunnerHost.gitOperation should succeed by default
1 parent e259aec commit 701a4c6

File tree

7 files changed

+44
-68
lines changed

7 files changed

+44
-68
lines changed

eng/tools/TypeSpecValidation/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export async function main() {
1919
const folder = parsedArgs.positionals[0];
2020
console.log("Running TypeSpecValidation on folder:", folder);
2121

22-
let rules = [
22+
const host = new TsvRunnerHost();
23+
24+
const rules = [
2325
new FolderStructureRule(),
2426
new NpmPrefixRule(),
2527
new LinterRulesetRule(),
@@ -31,7 +33,7 @@ export async function main() {
3133
for (let i = 0; i < rules.length; i++) {
3234
const rule = rules[i];
3335
console.log("\nExecuting rule: " + rule.name);
34-
const result = await rule.execute(TsvRunnerHost, folder);
36+
const result = await rule.execute(host, folder);
3537
if (result.stdOutput) console.log(result.stdOutput);
3638
if (!result.success) {
3739
success = false;

eng/tools/TypeSpecValidation/src/tsv-host.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export interface TsvHost {
2+
checkFileExists(file: string): Promise<boolean>;
23
gitOperation(folder: string): IGitOperation;
34
runCmd(cmd: string, cwd: string): Promise<[Error | null, string, string]>;
4-
checkFileExists(file: string): Promise<boolean>;
5-
// TODO: Other functions that need mocks
65
}
76

87
export interface IGitOperation {
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { TsvHost } from "./tsv-host.js";
1+
import { IGitOperation, TsvHost } from "./tsv-host.js";
22
import { simpleGit } from "simple-git";
33
import { runCmd, checkFileExists } from "./utils.js";
44

5-
export const TsvRunnerHost: TsvHost = {
6-
gitOperation: simpleGit,
7-
runCmd: runCmd,
8-
checkFileExists: checkFileExists,
9-
// TODO: Other functions that need mocks
10-
};
5+
export class TsvRunnerHost implements TsvHost {
6+
checkFileExists(file: string): Promise<boolean> {
7+
return checkFileExists(file);
8+
}
9+
10+
gitOperation(folder: string): IGitOperation {
11+
return simpleGit(folder);
12+
}
13+
14+
runCmd(cmd: string, cwd: string): Promise<[Error | null, string, string]> {
15+
return runCmd(cmd, cwd);
16+
}
17+
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
// import { error } from "node:console";
21
import { CompileRule } from "../src/rules/compile.js";
3-
import { TsvTestHostGenerator } from "./tsv-test-host.js";
2+
import { TsvTestHost } from "./tsv-test-host.js";
43
import { strict as assert } from "node:assert";
5-
describe("#Compile", function () {
6-
it("Should fail if project cannot compile.", async function () {
7-
let compileRule = new CompileRule();
8-
let TsvTestHost = TsvTestHostGenerator({
9-
runCmd: Promise.resolve([null, "success", "success"]),
10-
});
11-
const result = await compileRule.execute(TsvTestHost, "mockFolder");
4+
describe("compile", function () {
5+
it("should succeed if project can compile", async function () {
6+
const result = await new CompileRule().execute(new TsvTestHost(), TsvTestHost.folder);
7+
128
assert(result.success);
13-
assert(result.stdOutput === "successsuccess");
149
});
1510
});

eng/tools/TypeSpecValidation/test/folder-structure.test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { GitDiffRule } from "../src/rules/git-diff.js";
2-
import { DefaultTsvTestHost } from "./tsv-test-host.js";
2+
import { TsvTestHost } from "./tsv-test-host.js";
33
import { strict as assert } from "node:assert";
44

5-
describe("#GitDiff", function () {
6-
it("Should fail if git diff produces output.", async function () {
7-
let gitDiffRule = new GitDiffRule();
8-
const result = await gitDiffRule.execute(DefaultTsvTestHost, "mockFolder");
9-
assert(result.errorOutput);
10-
assert(result.errorOutput.length !== 0);
5+
describe("git-diff", function () {
6+
it("should succeed if git diff produces no output", async function () {
7+
const result = await new GitDiffRule().execute(new TsvTestHost(), TsvTestHost.folder);
8+
9+
assert(result.success);
1110
});
1211
});
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,36 @@
1-
import { TsvHost } from "../src/tsv-host.js";
1+
import { IGitOperation, TsvHost } from "../src/tsv-host.js";
22

3-
export function TsvTestHostGenerator(fnReturnSub: { [key: string]: Object }) {
4-
let tsvTestHost = Object.assign({}, DefaultTsvTestHost); // Needs deep copy
5-
for (let key in fnReturnSub) {
6-
let typedKey = key as keyof TsvHost;
7-
if (fnReturnSub[typedKey]) {
8-
tsvTestHost = {
9-
...tsvTestHost,
10-
[typedKey]: () => {
11-
return fnReturnSub[typedKey];
12-
},
13-
};
14-
} else {
15-
tsvTestHost = {
16-
...tsvTestHost,
17-
[typedKey]: () => {},
18-
};
19-
}
3+
export class TsvTestHost implements TsvHost {
4+
static get folder() {
5+
return "specification/foo/Foo";
206
}
21-
return tsvTestHost;
22-
}
237

24-
export const DefaultTsvTestHost: TsvHost = {
25-
gitOperation: (folder: string) => {
8+
gitOperation(_folder: string): IGitOperation {
269
return {
2710
status: () => {
2811
return Promise.resolve({
29-
modified: [`${folder}\n`],
30-
isClean: () => {
31-
return false;
32-
},
12+
modified: [],
13+
isClean: () => true,
3314
});
3415
},
3516
diff: () => {
36-
return Promise.resolve("diff");
17+
return Promise.resolve("");
3718
},
3819
revparse: () => {
3920
return Promise.resolve("");
4021
},
4122
};
42-
},
23+
}
4324

44-
runCmd: async (cmd: string, cwd: string) => {
25+
async runCmd(cmd: string, cwd: string): Promise<[Error | null, string, string]> {
4526
let err = null;
4627
let stdout = `default ${cmd} at ${cwd}`;
4728
let stderr = "";
4829

4930
return [err, stdout, stderr];
50-
},
31+
}
5132

52-
checkFileExists: async (file: string) => {
53-
console.log(`file ${file} exists`);
33+
async checkFileExists(_file: string): Promise<boolean> {
5434
return true;
55-
},
56-
// TODO: Other functions that need mocks
57-
};
35+
}
36+
}

0 commit comments

Comments
 (0)