|
1 | 1 | import axios from 'axios'; |
2 | 2 | import { ChildProcess, spawn } from 'child_process'; |
3 | 3 | import path from 'path'; |
4 | | -import fs from 'fs'; |
5 | | -import os from 'os'; |
6 | 4 |
|
7 | 5 | const cliPath = path.resolve(__dirname, '../../bin/mdts'); |
8 | 6 | const testDirectory = path.resolve(__dirname, '../fixtures/mountDirectory'); |
9 | 7 | const port = 8522; |
10 | 8 |
|
11 | 9 | describe('CLI e2e tests', () => { |
12 | 10 | let cliProcess: ChildProcess; |
13 | | - let tempDir: string; |
14 | | - let originalPath: string | undefined; |
15 | 11 |
|
16 | 12 | beforeAll(async () => { |
17 | | - // Create a temporary directory for our dummy 'open' executable |
18 | | - tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mdts-test-')); |
| 13 | + cliProcess = spawn('node', [cliPath, '-p', String(port), '--no-open', testDirectory]); |
19 | 14 |
|
20 | | - // Create a dummy 'open' executable that does nothing |
21 | | - const dummyOpenPath = path.join(tempDir, 'open'); |
22 | | - fs.writeFileSync(dummyOpenPath, '#!/bin/bash\nexit 0'); |
23 | | - fs.chmodSync(dummyOpenPath, '755'); // Make it executable |
24 | | - |
25 | | - // Store original PATH and prepend our temp directory to it |
26 | | - originalPath = process.env.PATH; |
27 | | - process.env.PATH = `${tempDir}${path.delimiter}${process.env.PATH}`; |
28 | | - |
29 | | - cliProcess = spawn('node', [cliPath, '-p', String(port), testDirectory], { |
30 | | - env: { ...process.env, PATH: process.env.PATH }, // Pass the modified PATH to the child process |
31 | | - }); |
32 | | - |
33 | | - // Optional: Log stderr for debugging |
34 | 15 | cliProcess.stderr?.on('data', (data: Buffer) => { |
35 | 16 | console.error(`CLI stderr: ${data}`); |
36 | 17 | }); |
37 | 18 |
|
38 | | - // Poll for server readiness |
39 | 19 | const maxAttempts = 30; |
40 | 20 | const delayMs = 1000; |
41 | 21 | for (let i = 0; i < maxAttempts; i++) { |
42 | 22 | try { |
43 | 23 | const response = await axios.get(`http://localhost:${port}/api/markdown/content/test.md`); |
44 | 24 | if (response.status === 200) { |
45 | 25 | console.log(`Server is ready after ${i + 1} attempts.`); |
46 | | - return; // Server is ready |
| 26 | + return; |
47 | 27 | } |
48 | 28 | } catch { |
49 | 29 | // Server not ready yet, continue polling |
50 | 30 | } |
51 | 31 | await new Promise(resolve => setTimeout(resolve, delayMs)); |
52 | 32 | } |
53 | 33 | throw new Error('Server did not become ready within the timeout.'); |
54 | | - }, 60000); // Keep the overall timeout for beforeAll |
| 34 | + }, 60000); |
55 | 35 |
|
56 | 36 | afterAll(() => { |
57 | 37 | if (cliProcess) { |
58 | 38 | cliProcess.kill(); |
59 | 39 | } |
60 | | - // Clean up the temporary directory and restore original PATH |
61 | | - if (tempDir) { |
62 | | - fs.rmSync(tempDir, { recursive: true, force: true }); |
63 | | - } |
64 | | - if (originalPath !== undefined) { |
65 | | - process.env.PATH = originalPath; |
66 | | - } |
67 | 40 | }); |
68 | 41 |
|
69 | 42 | test('should serve content/test.md', async () => { |
|
0 commit comments