backstage

Форк
0
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

17
import { execSync, spawn, SpawnOptionsWithoutStdio } from 'child_process';
18
import path from 'path';
19

20
import findProcess from 'find-process';
21

22
const executeCommand = (
23
  command: string,
24
  args: string[],
25
  options?: SpawnOptionsWithoutStdio,
26
): Promise<{
27
  exit: number;
28
  stdout: string;
29
  stderr: string;
30
}> => {
31
  return new Promise((resolve, reject) => {
32
    const stdout: Buffer[] = [];
33
    const stderr: Buffer[] = [];
34

35
    const shell = process.platform === 'win32';
36
    const proc = spawn(command, args, { ...options, shell });
37

38
    proc.stdout?.on('data', data => {
39
      stdout.push(Buffer.from(data));
40
    });
41

42
    proc.stderr?.on('data', data => {
43
      stderr.push(Buffer.from(data));
44
    });
45

46
    proc.on('error', reject);
47
    proc.on('exit', code => {
48
      resolve({
49
        exit: code ?? 0,
50
        stdout: Buffer.concat(stdout).toString('utf8'),
51
        stderr: Buffer.concat(stderr).toString('utf8'),
52
      });
53
    });
54
  });
55
};
56

57
const timeout = 25000;
58

59
jest.setTimeout(timeout * 2);
60

61
describe('end-to-end', () => {
62
  const cwd = path.resolve(__dirname, '../src/example-docs');
63
  const entryPoint = path.resolve(__dirname, '../bin/techdocs-cli');
64

65
  afterEach(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)
69
    if (process.platform === 'win32') {
70
      const procs = await findProcess('name', 'mkdocs', true);
71
      procs.forEach((proc: { pid: number }) => {
72
        process.kill(proc.pid);
73
      });
74
    }
75
  });
76

77
  it('shows help text', async () => {
78
    const proc = await executeCommand(entryPoint, ['--help']);
79
    expect(proc.stdout).toContain('Usage: techdocs-cli [options]');
80
    expect(proc.exit).toEqual(0);
81
  });
82

83
  it('can generate', async () => {
84
    const proc = await executeCommand(entryPoint, ['generate', '--no-docker'], {
85
      cwd,
86
      timeout,
87
    });
88
    expect(proc.stdout).toContain('Successfully generated docs');
89
    expect(proc.exit).toEqual(0);
90
  });
91

92
  it('can generate with DOCKER_* TLS variables and --no-docker option', async () => {
93
    const env = {
94
      DOCKER_HOST: 'tcp://localhost:2376',
95
      DOCKER_TLS_CERTDIR: '/certs',
96
      DOCKER_TLS_VERIFY: '1',
97
      DOCKER_CERT_PATH: '/certs/client',
98
      ...process.env,
99
    };
100
    const proc = await executeCommand(entryPoint, ['generate', '--no-docker'], {
101
      cwd,
102
      timeout,
103
      env,
104
    });
105
    expect(proc.stdout).toContain('Successfully generated docs');
106
    expect(proc.exit).toEqual(0);
107
  });
108

109
  it('can serve in mkdocs', async () => {
110
    const proc = await executeCommand(
111
      entryPoint,
112
      ['serve:mkdocs', '--no-docker'],
113
      { cwd, timeout },
114
    );
115
    expect(proc.stdout).toContain('Starting mkdocs server');
116
    expect(proc.exit).toEqual(0);
117
  });
118

119
  it('can serve in backstage', async () => {
120
    jest.setTimeout(30000);
121
    const proc = await executeCommand(entryPoint, ['serve', '--no-docker'], {
122
      cwd,
123
      timeout,
124
    });
125
    expect(proc.stdout).toContain('Starting mkdocs server');
126
    expect(proc.stdout).toContain('Serving docs in Backstage at');
127
    expect(proc.exit).toEqual(0);
128
  });
129
});
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.