diff --git a/packages/techdocs-cli/src/e2e.test.ts b/packages/techdocs-cli/src/e2e.test.ts index 9dbbf58431..e6cf4e84c5 100644 --- a/packages/techdocs-cli/src/e2e.test.ts +++ b/packages/techdocs-cli/src/e2e.test.ts @@ -14,115 +14,93 @@ * limitations under the License. */ -import { spawn } from 'child_process'; +import { execSync, spawn } from 'child_process'; import path from 'path'; -const PROJECT_ROOT_DIR = path.resolve(__dirname, '..'); -const FIXTURE_DIR = path.resolve(PROJECT_ROOT_DIR, 'src/fixture'); +const executeCommand = ( + command: string, + args: string[], + options?: Object, +): Promise<{ + exit: number; + stdout: string; + stderr: string; +}> => { + return new Promise(resolve => { + const stdout: Buffer[] = []; + const stderr: Buffer[] = []; + const proc = + process.platform === 'win32' + ? spawn('cmd', ['/s', '/c', command, ...args], options) + : spawn(command, args, options); + + proc.stdout?.on('data', data => { + stdout.push(Buffer.from(data)); + }); + + proc.stderr?.on('data', data => { + stderr.push(Buffer.from(data)); + }); + + proc.on('exit', code => { + resolve({ + exit: code ?? 0, + stdout: Buffer.concat(stdout).toString('utf8'), + stderr: Buffer.concat(stderr).toString('utf8'), + }); + }); + }); +}; describe('end-to-end', () => { + const cwd = path.resolve(__dirname, 'fixture'); + + beforeAll(() => { + execSync('yarn workspace @techdocs/cli link', { stdio: 'ignore' }); + }); + + afterAll(() => { + execSync('yarn workspace @techdocs/cli unlink', { stdio: 'ignore' }); + }); + it('shows help text', async () => { jest.setTimeout(30000); - const proc = await executeTechDocsCliCommand(['--help']); - - expect(proc.combinedStdOutErr).toContain('Usage: techdocs-cli [options]'); + const proc = await executeCommand('techdocs-cli', ['--help']); + expect(proc.stdout).toContain('Usage: techdocs-cli [options]'); expect(proc.exit).toEqual(0); }); it('can generate', async () => { jest.setTimeout(30000); - const proc = await executeTechDocsCliCommand(['generate', '--no-docker'], { - cwd: FIXTURE_DIR, - killAfter: 16000, - }); - - expect(proc.combinedStdOutErr).toContain('Successfully generated docs'); + const proc = await executeCommand( + 'techdocs-cli', + ['generate', '--no-docker'], + { cwd, timeout: 25000 }, + ); + expect(proc.stdout).toContain('Successfully generated docs'); expect(proc.exit).toEqual(0); }); it('can serve in mkdocs', async () => { jest.setTimeout(30000); - const proc = await executeTechDocsCliCommand( + const proc = await executeCommand( + 'techdocs-cli', ['serve:mkdocs', '--no-docker'], - { - cwd: FIXTURE_DIR, - killAfter: 16000, - }, + { cwd, timeout: 25000 }, ); - - expect(proc.combinedStdOutErr).toContain('Starting mkdocs server'); + expect(proc.stdout).toContain('Starting mkdocs server'); expect(proc.exit).toEqual(0); }); it('can serve in backstage', async () => { jest.setTimeout(30000); - const proc = await executeTechDocsCliCommand(['serve', '--no-docker'], { - cwd: FIXTURE_DIR, - killAfter: 16000, - }); - - expect(proc.combinedStdOutErr).toContain('Starting mkdocs server'); - expect(proc.combinedStdOutErr).toContain('Serving docs in Backstage at'); + const proc = await executeCommand( + 'techdocs-cli', + ['serve', '--no-docker'], + { cwd, timeout: 25000 }, + ); + expect(proc.stdout).toContain('Starting mkdocs server'); + expect(proc.stdout).toContain('Serving docs in Backstage at'); expect(proc.exit).toEqual(0); }); }); - -type CommandResponse = { - stdout: string; - stderr: string; - combinedStdOutErr: string; - exit: number; -}; - -type ExecuteCommandOptions = { - killAfter?: number; - cwd?: string; -}; - -function executeTechDocsCliCommand( - args: string[], - opts: ExecuteCommandOptions = {}, -): Promise { - return new Promise(resolve => { - const pathToCli = path.resolve(PROJECT_ROOT_DIR, 'bin/techdocs-cli'); - const commandResponse = { - stdout: '', - stderr: '', - combinedStdOutErr: '', - exit: 0, - }; - - const listen = spawn(pathToCli, args, { - cwd: opts.cwd, - }); - - const stdOutChunks: any[] = []; - const stdErrChunks: any[] = []; - const combinedChunks: any[] = []; - - listen.stdout.on('data', data => { - stdOutChunks.push(data); - combinedChunks.push(data); - }); - - listen.stderr.on('data', data => { - stdErrChunks.push(data); - combinedChunks.push(data); - }); - - listen.on('exit', code => { - commandResponse.exit = code as number; - commandResponse.stdout = Buffer.concat(stdOutChunks).toString('utf8'); - commandResponse.stderr = Buffer.concat(stdErrChunks).toString('utf8'); - commandResponse.combinedStdOutErr = - Buffer.concat(combinedChunks).toString('utf8'); - resolve(commandResponse); - }); - - if (opts.killAfter) { - setTimeout(() => { - listen.kill('SIGTERM'); - }, opts.killAfter); - } - }); -}