|
1 | 1 | import { execa } from "execa";
|
| 2 | +import glob from "fast-glob"; |
2 | 3 | import fs from "node:fs";
|
| 4 | +import path from "node:path/posix"; // prevent issues with `\` on windows |
3 | 5 | import { URL, fileURLToPath } from "node:url";
|
4 | 6 | import os from "node:os";
|
5 | 7 |
|
6 | 8 | const root = new URL("../", import.meta.url);
|
7 | 9 | const cwd = os.platform() === "win32" ? fileURLToPath(root) : root; // execa bug: fileURLToPath required on Windows
|
8 | 10 | const cmd = "./bin/cli.js";
|
| 11 | +const inputDir = "test/fixtures/cli-outputs/"; |
| 12 | +const outputDir = path.join(inputDir, "out/"); |
9 | 13 | const TIMEOUT = 90000;
|
10 | 14 |
|
| 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 | + |
11 | 20 | describe("CLI", () => {
|
12 | 21 | // note: the snapshots in index.test.ts test the Node API; these test the CLI
|
13 | 22 | describe("snapshots", () => {
|
@@ -76,4 +85,59 @@ describe("CLI", () => {
|
76 | 85 | expect(stdout).toEqual(expect.stringMatching(/^v[\d.]+(-.*)?$/));
|
77 | 86 | });
|
78 | 87 | });
|
| 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("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(["test/fixtures/cli-outputs/file-a.ts", "test/fixtures/cli-outputs/file-b.ts"]); |
| 128 | + }); |
| 129 | + |
| 130 | + test("multiple nested files to directory", async () => { |
| 131 | + const inputFile = path.join(inputDir, "**/*.yaml"); |
| 132 | + await execa(cmd, [inputFile, "--output", outputDir], { cwd }); |
| 133 | + const result = await getOutputFiles(); |
| 134 | + expect(result).toEqual([ |
| 135 | + "test/fixtures/cli-outputs/file-a.ts", |
| 136 | + "test/fixtures/cli-outputs/file-b.ts", |
| 137 | + "test/fixtures/cli-outputs/nested/deep/file-e.ts", |
| 138 | + "test/fixtures/cli-outputs/nested/file-c.ts", |
| 139 | + "test/fixtures/cli-outputs/nested/file-d.ts", |
| 140 | + ]); |
| 141 | + }); |
| 142 | + }); |
79 | 143 | });
|
0 commit comments