Skip to content

Commit 520897f

Browse files
committed
add glob tests
1 parent 8a1ae00 commit 520897f

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

packages/openapi-typescript/test/cli.test.ts

+67
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { execa } from "execa";
2+
import glob from "fast-glob";
23
import fs from "node:fs";
4+
import path from "node:path/posix"; // prevent issues with `\` on windows
35
import { URL, fileURLToPath } from "node:url";
46
import os from "node:os";
57

68
const root = new URL("../", import.meta.url);
79
const cwd = os.platform() === "win32" ? fileURLToPath(root) : root; // execa bug: fileURLToPath required on Windows
810
const cmd = "./bin/cli.js";
11+
const inputDir = "test/fixtures/cli-outputs/";
12+
const outputDir = path.join(inputDir, "out/");
913
const TIMEOUT = 90000;
1014

15+
// fast-glob does not sort results
16+
async function getOutputFiles() {
17+
return (await glob("**", { cwd: outputDir })).sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
18+
}
19+
1120
describe("CLI", () => {
1221
// note: the snapshots in index.test.ts test the Node API; these test the CLI
1322
describe("snapshots", () => {
@@ -76,4 +85,62 @@ describe("CLI", () => {
7685
expect(stdout).toEqual(expect.stringMatching(/^v[\d.]+(-.*)?$/));
7786
});
7887
});
88+
89+
describe("outputs", ()=>{
90+
beforeEach(()=>{
91+
fs.rmSync(new URL(outputDir, root), { recursive: true, force: true });
92+
});
93+
94+
test("single file to file", async ()=>{
95+
const inputFile = path.join(inputDir, "file-a.yaml");
96+
const outputFile = path.join(outputDir, "file-a.ts");
97+
await execa(cmd, [inputFile, "--output", outputFile], { cwd });
98+
const result = await getOutputFiles();
99+
expect(result).toEqual([ "file-a.ts" ]);
100+
});
101+
102+
test("single file to directory", async ()=>{
103+
const inputFile = path.join(inputDir, "file-a.yaml");
104+
await execa(cmd, [inputFile, "--output", outputDir], { cwd });
105+
const result = await getOutputFiles();
106+
expect(result).toEqual([ "test/fixtures/cli-outputs/file-a.ts" ]);
107+
});
108+
109+
test("single file (glob) to file", async ()=>{
110+
const inputFile = path.join(inputDir, "*-a.yaml");
111+
const outputFile = path.join(outputDir, "file-a.ts");
112+
await execa(cmd, [inputFile, "--output", outputFile], { cwd });
113+
const result = await getOutputFiles();
114+
expect(result).toEqual([ "file-a.ts" ]);
115+
})
116+
117+
test.todo("multiple files to file", async ()=>{
118+
const inputFile = path.join(inputDir, "*.yaml");
119+
const outputFile = path.join(outputDir, "file-a.ts");
120+
await expect(execa(cmd, [inputFile, "--output", outputFile], { cwd })).rejects.toThrow();
121+
})
122+
123+
test("multiple files to directory", async ()=>{
124+
const inputFile = path.join(inputDir, "*.yaml");
125+
await execa(cmd, [inputFile, "--output", outputDir], { cwd });
126+
const result = await getOutputFiles();
127+
expect(result).toEqual([
128+
"test/fixtures/cli-outputs/file-a.ts",
129+
"test/fixtures/cli-outputs/file-b.ts"
130+
]);
131+
})
132+
133+
test("multiple nested files to directory", async ()=>{
134+
const inputFile = path.join(inputDir, "**/*.yaml");
135+
await execa(cmd, [inputFile, "--output", outputDir], { cwd });
136+
const result = await getOutputFiles();
137+
expect(result).toEqual([
138+
"test/fixtures/cli-outputs/file-a.ts",
139+
"test/fixtures/cli-outputs/file-b.ts",
140+
"test/fixtures/cli-outputs/nested/deep/file-e.ts",
141+
"test/fixtures/cli-outputs/nested/file-c.ts",
142+
"test/fixtures/cli-outputs/nested/file-d.ts",
143+
]);
144+
})
145+
});
79146
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: "3.0"
2+
info:
3+
title: test file a
4+
version: "1.0"
5+
paths:
6+
/endpoint:
7+
get:
8+
description: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: "3.0"
2+
info:
3+
title: test file b
4+
version: "1.0"
5+
paths:
6+
/endpoint:
7+
get:
8+
description: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: "3.0"
2+
info:
3+
title: test file e
4+
version: "1.0"
5+
paths:
6+
/endpoint:
7+
get:
8+
description: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: "3.0"
2+
info:
3+
title: test file c
4+
version: "1.0"
5+
paths:
6+
/endpoint:
7+
get:
8+
description: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: "3.0"
2+
info:
3+
title: test file d
4+
version: "1.0"
5+
paths:
6+
/endpoint:
7+
get:
8+
description: OK

0 commit comments

Comments
 (0)