Skip to content

Commit cf58ba9

Browse files
committed
test: build and test script test
1 parent 78b9c88 commit cf58ba9

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "build-script-fixtures",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"build": "build"
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "test-script-fixtures",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"test": "test"
6+
}
7+
}

tests/options.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from "vitest";
2+
import { runPubmCli } from "./utils/cli";
3+
import path from "node:path";
4+
5+
test('build script options', async () => {
6+
const { controller } = runPubmCli('0.0.2', '--contents', path.resolve(import.meta.dirname, './fixtures/build-script'), '--no-pre-check', '--no-tests', '--build-script', 'not-exist-build-scripts');
7+
8+
await controller.waitForStderr("✖ Checking if test and build scripts exist");
9+
});

tests/utils/cli.ts

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Readable, Writable } from 'node:stream';
22
import { stripVTControlCharacters } from 'node:util';
33
import { isCI } from 'std-env';
44
import { type Options, exec } from 'tinyexec';
5+
import path from 'node:path';
56

67
// Based on https://github.com/vitest-dev/vitest/blob/main/test/test-utils/index.ts
78

@@ -116,55 +117,44 @@ function isWritable(stream: any): stream is Writable {
116117
return stream && typeof stream?.write === 'function';
117118
}
118119

119-
export async function runPubmCli(
120-
command: string,
121-
_options?: Partial<Options>,
120+
export function runPubmCli(
121+
_options?: Partial<Options> | string,
122122
...args: string[]
123-
): Promise<{
123+
): {
124124
controller: CliController;
125125
exitCode: number | undefined;
126126
stdout: string;
127127
stderr: string;
128128
waitForClose: () => Promise<unknown>;
129-
}> {
129+
} {
130130
let options = _options;
131131

132132
if (typeof _options === 'string') {
133133
args.unshift(_options);
134134
options = undefined;
135135
}
136136

137-
const subprocess = exec(command, args, options as Options).process;
137+
const subprocess = exec(path.resolve(import.meta.dirname, '../../bin/cli.js'), args, options as Options).process!;
138138
const controller = new CliController({
139139
// biome-ignore lint/style/noNonNullAssertion: <explanation>
140-
stdin: subprocess!.stdin!,
140+
stdin: subprocess.stdin!,
141141
// biome-ignore lint/style/noNonNullAssertion: <explanation>
142-
stdout: subprocess!.stdout!,
142+
stdout: subprocess.stdout!,
143143
// biome-ignore lint/style/noNonNullAssertion: <explanation>
144-
stderr: subprocess!.stderr!,
144+
stderr: subprocess.stderr!,
145145
});
146146

147-
let setDone: (value?: unknown) => void;
148-
149147
const isDone = new Promise((resolve) => {
150-
setDone = resolve;
148+
subprocess?.on('exit', resolve);
151149
});
152150

153-
subprocess?.on('exit', () => setDone());
154-
155-
function output() {
156-
return {
157-
controller,
158-
exitCode: subprocess?.exitCode ?? undefined,
159-
stdout: controller.stdout || '',
160-
stderr: controller.stderr || '',
161-
waitForClose: () => isDone,
162-
};
163-
}
164-
165-
await isDone;
166-
167-
return output();
151+
return {
152+
controller,
153+
exitCode: subprocess?.exitCode ?? undefined,
154+
stdout: controller.stdout || '',
155+
stderr: controller.stderr || '',
156+
waitForClose: () => isDone,
157+
};
168158
}
169159

170160
export const DOWN = '\x1B\x5B\x42';

0 commit comments

Comments
 (0)