backstage
129 строк · 3.7 Кб
1/*
2* Copyright 2021 The Backstage Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17import { execSync, spawn, SpawnOptionsWithoutStdio } from 'child_process';
18import path from 'path';
19
20import findProcess from 'find-process';
21
22const executeCommand = (
23command: string,
24args: string[],
25options?: SpawnOptionsWithoutStdio,
26): Promise<{
27exit: number;
28stdout: string;
29stderr: string;
30}> => {
31return new Promise((resolve, reject) => {
32const stdout: Buffer[] = [];
33const stderr: Buffer[] = [];
34
35const shell = process.platform === 'win32';
36const proc = spawn(command, args, { ...options, shell });
37
38proc.stdout?.on('data', data => {
39stdout.push(Buffer.from(data));
40});
41
42proc.stderr?.on('data', data => {
43stderr.push(Buffer.from(data));
44});
45
46proc.on('error', reject);
47proc.on('exit', code => {
48resolve({
49exit: code ?? 0,
50stdout: Buffer.concat(stdout).toString('utf8'),
51stderr: Buffer.concat(stderr).toString('utf8'),
52});
53});
54});
55};
56
57const timeout = 25000;
58
59jest.setTimeout(timeout * 2);
60
61describe('end-to-end', () => {
62const cwd = path.resolve(__dirname, '../src/example-docs');
63const entryPoint = path.resolve(__dirname, '../bin/techdocs-cli');
64
65afterEach(async () => {
66// On Windows the pid of a spawned process may be wrong
67// Because of this, we should stop the MKDocs after the test
68// (e.g. https://github.com/nodejs/node/issues/4289#issuecomment-854270414)
69if (process.platform === 'win32') {
70const procs = await findProcess('name', 'mkdocs', true);
71procs.forEach((proc: { pid: number }) => {
72process.kill(proc.pid);
73});
74}
75});
76
77it('shows help text', async () => {
78const proc = await executeCommand(entryPoint, ['--help']);
79expect(proc.stdout).toContain('Usage: techdocs-cli [options]');
80expect(proc.exit).toEqual(0);
81});
82
83it('can generate', async () => {
84const proc = await executeCommand(entryPoint, ['generate', '--no-docker'], {
85cwd,
86timeout,
87});
88expect(proc.stdout).toContain('Successfully generated docs');
89expect(proc.exit).toEqual(0);
90});
91
92it('can generate with DOCKER_* TLS variables and --no-docker option', async () => {
93const env = {
94DOCKER_HOST: 'tcp://localhost:2376',
95DOCKER_TLS_CERTDIR: '/certs',
96DOCKER_TLS_VERIFY: '1',
97DOCKER_CERT_PATH: '/certs/client',
98...process.env,
99};
100const proc = await executeCommand(entryPoint, ['generate', '--no-docker'], {
101cwd,
102timeout,
103env,
104});
105expect(proc.stdout).toContain('Successfully generated docs');
106expect(proc.exit).toEqual(0);
107});
108
109it('can serve in mkdocs', async () => {
110const proc = await executeCommand(
111entryPoint,
112['serve:mkdocs', '--no-docker'],
113{ cwd, timeout },
114);
115expect(proc.stdout).toContain('Starting mkdocs server');
116expect(proc.exit).toEqual(0);
117});
118
119it('can serve in backstage', async () => {
120jest.setTimeout(30000);
121const proc = await executeCommand(entryPoint, ['serve', '--no-docker'], {
122cwd,
123timeout,
124});
125expect(proc.stdout).toContain('Starting mkdocs server');
126expect(proc.stdout).toContain('Serving docs in Backstage at');
127expect(proc.exit).toEqual(0);
128});
129});
130