Skip to content

Commit 4acdf2b

Browse files
authored
test: fix unstable test cases (#85)
1 parent e80696f commit 4acdf2b

7 files changed

Lines changed: 126 additions & 71 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
node_modules
88
dist/
99
test-results
10+
test-temp-*
1011

1112
# IDE
1213
.vscode/*

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"fs-extra": "^11.3.2",
5555
"minimist": "^1.2.8",
5656
"picocolors": "^1.1.1",
57+
"rimraf": "^6.1.2",
5758
"rslog": "^1.3.2",
5859
"simple-git-hooks": "^2.13.1",
5960
"typescript": "^5.9.3"

pnpm-lock.yaml

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ const readJSON = async (path: string) =>
174174
const readPackageJson = async (filePath: string) =>
175175
readJSON(path.join(filePath, 'package.json'));
176176

177-
const parseArgv = () => {
178-
const argv = minimist<Argv>(process.argv.slice(2), {
177+
const parseArgv = (processArgv: string[]) => {
178+
const argv = minimist<Argv>(processArgv.slice(2), {
179179
alias: { h: 'help', d: 'dir', t: 'template' },
180180
});
181181

@@ -210,7 +210,7 @@ type ExtraTool = {
210210
command?: string;
211211
};
212212

213-
async function runCommand(command: string, cwd: string) {
213+
function runCommand(command: string, cwd: string) {
214214
const [bin, ...args] = command.split(' ');
215215
spawn.sync(bin, args, {
216216
stdio: 'inherit',
@@ -228,6 +228,7 @@ export async function create({
228228
version,
229229
noteInformation,
230230
extraTools,
231+
argv: processArgv = process.argv,
231232
}: {
232233
name: string;
233234
root: string;
@@ -248,11 +249,15 @@ export async function create({
248249
* Specify additional tools.
249250
*/
250251
extraTools?: ExtraTool[];
252+
/**
253+
* For test purpose, override the default argv (process.argv).
254+
*/
255+
argv?: string[];
251256
}) {
252257
console.log('');
253258
logger.greet(`◆ Create ${upperFirst(name)} Project`);
254259

255-
const argv = parseArgv();
260+
const argv = parseArgv(processArgv);
256261

257262
if (argv.help) {
258263
logHelpMessage(name, templates);
@@ -346,7 +351,7 @@ export async function create({
346351
await matchedTool.action();
347352
}
348353
if (matchedTool?.command) {
349-
await runCommand(matchedTool.command, distFolder);
354+
runCommand(matchedTool.command, distFolder);
350355
}
351356
continue;
352357
}

test/agents.test.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { assert, beforeEach, expect, test } from '@rstest/core';
5-
import { create } from '../dist/index.js';
5+
import { create } from '../src';
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8-
const testDir = path.join(__dirname, 'temp');
98
const fixturesDir = path.join(__dirname, 'fixtures', 'agents-md');
9+
const testDir = path.join(fixturesDir, 'test-temp-output');
1010

1111
beforeEach(() => {
1212
// Clean up test directory before each test
@@ -15,13 +15,8 @@ beforeEach(() => {
1515
}
1616
fs.mkdirSync(testDir, { recursive: true });
1717

18-
// Store original argv
19-
const originalArgv = process.argv;
20-
2118
// Return cleanup function
2219
return () => {
23-
// Restore original argv and clean up
24-
process.argv = originalArgv;
2520
if (fs.existsSync(testDir)) {
2621
fs.rmSync(testDir, { recursive: true });
2722
}
@@ -30,13 +25,13 @@ beforeEach(() => {
3025

3126
test('should generate AGENTS.md with no tools selected', async () => {
3227
const projectDir = path.join(testDir, 'no-tools');
33-
process.argv = ['node', 'test', '--dir', projectDir, '--template', 'vanilla'];
3428

3529
await create({
3630
name: 'test',
3731
root: fixturesDir,
3832
templates: ['vanilla'],
3933
getTemplateName: async () => 'vanilla',
34+
argv: ['node', 'test', '--dir', projectDir, '--template', 'vanilla'],
4035
});
4136

4237
const agentsPath = path.join(projectDir, 'AGENTS.md');
@@ -75,22 +70,22 @@ test('should generate AGENTS.md with no tools selected', async () => {
7570

7671
test('should generate AGENTS.md with single tool selected', async () => {
7772
const projectDir = path.join(testDir, 'single-tool');
78-
process.argv = [
79-
'node',
80-
'test',
81-
'--dir',
82-
projectDir,
83-
'--template',
84-
'vanilla',
85-
'--tools',
86-
'biome',
87-
];
8873

8974
await create({
9075
name: 'test',
9176
root: fixturesDir,
9277
templates: ['vanilla'],
9378
getTemplateName: async () => 'vanilla',
79+
argv: [
80+
'node',
81+
'test',
82+
'--dir',
83+
projectDir,
84+
'--template',
85+
'vanilla',
86+
'--tools',
87+
'biome',
88+
],
9489
});
9590

9691
const agentsPath = path.join(projectDir, 'AGENTS.md');
@@ -134,16 +129,6 @@ test('should generate AGENTS.md with single tool selected', async () => {
134129

135130
test('should generate AGENTS.md with eslint tool and template mapping', async () => {
136131
const projectDir = path.join(testDir, 'eslint-tool');
137-
process.argv = [
138-
'node',
139-
'test',
140-
'--dir',
141-
projectDir,
142-
'--template',
143-
'vanilla',
144-
'--tools',
145-
'eslint',
146-
];
147132

148133
await create({
149134
name: 'test',
@@ -154,6 +139,16 @@ test('should generate AGENTS.md with eslint tool and template mapping', async ()
154139
if (templateName === 'vanilla') return 'vanilla-ts';
155140
return null;
156141
},
142+
argv: [
143+
'node',
144+
'test',
145+
'--dir',
146+
projectDir,
147+
'--template',
148+
'vanilla',
149+
'--tools',
150+
'eslint',
151+
],
157152
});
158153

159154
const agentsPath = path.join(projectDir, 'AGENTS.md');
@@ -196,13 +191,13 @@ test('should generate AGENTS.md with eslint tool and template mapping', async ()
196191

197192
test('should merge top-level sections from AGENTS.md files', async () => {
198193
const projectDir = path.join(testDir, 'h1-support');
199-
process.argv = ['node', 'test', '--dir', projectDir, '--template', 'vanilla'];
200194

201195
await create({
202196
name: 'test',
203197
root: fixturesDir,
204198
templates: ['vanilla'],
205199
getTemplateName: async () => 'vanilla',
200+
argv: ['node', 'test', '--dir', projectDir, '--template', 'vanilla'],
206201
});
207202

208203
const agentsPath = path.join(projectDir, 'AGENTS.md');

0 commit comments

Comments
 (0)