Skip to content

Commit 79a6b2b

Browse files
authored
Merge pull request #161 from han-dreamer/test/windows-temp-file-fixtures
test: avoid /dev/null in command tests
2 parents 16f45a5 + 8c5a4e1 commit 79a6b2b

2 files changed

Lines changed: 54 additions & 19 deletions

File tree

test/commands/file/upload.test.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { describe, it, expect } from 'bun:test';
2+
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
3+
import { tmpdir } from 'os';
4+
import { join } from 'path';
25
import { default as uploadCommand } from '../../../src/commands/file/upload';
36

47
const baseConfig = {
@@ -27,6 +30,22 @@ const baseFlags = {
2730
async: false,
2831
};
2932

33+
async function captureStdout(fn: () => Promise<void>): Promise<string> {
34+
const originalWrite = process.stdout.write;
35+
let output = '';
36+
process.stdout.write = ((chunk: string | Uint8Array) => {
37+
output += typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf-8');
38+
return true;
39+
}) as typeof process.stdout.write;
40+
41+
try {
42+
await fn();
43+
return output;
44+
} finally {
45+
process.stdout.write = originalWrite;
46+
}
47+
}
48+
3049
describe('file upload command', () => {
3150
it('has correct name', () => {
3251
expect(uploadCommand.name).toBe('file upload');
@@ -45,17 +64,22 @@ describe('file upload command', () => {
4564
});
4665

4766
it('shows dry-run output with file info', async () => {
48-
let captured = '';
49-
const origWrite = process.stdout.write;
50-
process.stdout.write = (chunk: any): any => { captured += String(chunk); return true; };
51-
52-
await uploadCommand.execute(
53-
{ ...baseConfig, dryRun: true },
54-
{ ...baseFlags, dryRun: true, file: '/dev/null', purpose: 'vision' },
55-
);
56-
57-
process.stdout.write = origWrite;
58-
expect(captured).toContain('/dev/null');
59-
expect(captured).toContain('vision');
67+
const tempDir = mkdtempSync(join(tmpdir(), 'mmx-upload-test-'));
68+
const filePath = join(tempDir, 'fixture.bin');
69+
writeFileSync(filePath, 'fixture');
70+
71+
try {
72+
const captured = await captureStdout(async () => {
73+
await uploadCommand.execute(
74+
{ ...baseConfig, dryRun: true },
75+
{ ...baseFlags, dryRun: true, file: filePath, purpose: 'vision' },
76+
);
77+
});
78+
79+
expect(captured).toContain(filePath);
80+
expect(captured).toContain('vision');
81+
} finally {
82+
rmSync(tempDir, { recursive: true, force: true });
83+
}
6084
});
61-
});
85+
});

test/commands/music/generate.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { describe, it, expect } from 'bun:test';
2+
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
3+
import { tmpdir } from 'os';
4+
import { join } from 'path';
25
import { default as generateCommand } from '../../../src/commands/music/generate';
36

47
const baseConfig = {
@@ -107,12 +110,20 @@ describe('music generate command', () => {
107110
});
108111

109112
it('rejects --instrumental with --lyrics-file', async () => {
110-
await expect(
111-
generateCommand.execute(
112-
{ ...baseConfig, dryRun: true },
113-
{ ...baseFlags, prompt: 'Folk', instrumental: true, lyricsFile: '/dev/null' },
114-
),
115-
).rejects.toThrow('Cannot use --instrumental with --lyrics');
113+
const tempDir = mkdtempSync(join(tmpdir(), 'mmx-lyrics-test-'));
114+
const lyricsFile = join(tempDir, 'lyrics.txt');
115+
writeFileSync(lyricsFile, 'Hello');
116+
117+
try {
118+
await expect(
119+
generateCommand.execute(
120+
{ ...baseConfig, dryRun: true },
121+
{ ...baseFlags, prompt: 'Folk', instrumental: true, lyricsFile },
122+
),
123+
).rejects.toThrow('Cannot use --instrumental with --lyrics');
124+
} finally {
125+
rmSync(tempDir, { recursive: true, force: true });
126+
}
116127
});
117128

118129
it('handles "无歌词" as instrumental', async () => {

0 commit comments

Comments
 (0)