[ftr] ensure indentation is reset between configs (#114359)

Co-authored-by: spalger <spalger@users.noreply.github.com>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Spencer 2021-10-11 18:29:10 -05:00 committed by GitHub
parent a0b55b316e
commit 912eb0d937
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 40 deletions

View file

@ -12,6 +12,10 @@ import { toArray, takeUntil } from 'rxjs/operators';
import { ToolingLog } from './tooling_log';
import { Writer } from './writer';
import { ToolingLogTextWriter } from './tooling_log_text_writer';
import { ToolingLogCollectingWriter } from './tooling_log_collecting_writer';
import { createStripAnsiSerializer } from '../serializers/strip_ansi_serializer';
expect.addSnapshotSerializer(createStripAnsiSerializer());
it('creates zero writers without a config', () => {
const log = new ToolingLog();
@ -67,6 +71,30 @@ describe('#indent()', () => {
expect(write.mock.calls).toMatchSnapshot();
});
it('resets the indentation after block executes and promise resolves', async () => {
const log = new ToolingLog();
const writer = new ToolingLogCollectingWriter();
log.setWriters([writer]);
log.info('base');
await log.indent(2, async () => {
log.indent(2);
log.info('hello');
log.indent(2);
log.info('world');
});
log.info('back to base');
expect(writer.messages).toMatchInlineSnapshot(`
Array [
" info base",
" │ info hello",
" │ info world",
" info back to base",
]
`);
});
});
(['verbose', 'debug', 'info', 'success', 'warning', 'error', 'write'] as const).forEach(

View file

@ -13,7 +13,7 @@ import { Writer } from './writer';
import { Message, MessageTypes } from './message';
export class ToolingLog {
private identWidth = 0;
private indentWidth = 0;
private writers: Writer[];
private readonly written$: Rx.Subject<Message>;
@ -22,9 +22,36 @@ export class ToolingLog {
this.written$ = new Rx.Subject();
}
public indent(delta = 0) {
this.identWidth = Math.max(this.identWidth + delta, 0);
return this.identWidth;
/**
* Get the current indentation level of the ToolingLog
*/
public getIndent() {
return this.indentWidth;
}
/**
* Indent the output of the ToolingLog by some character (4 is a good choice usually).
*
* If provided, the `block` function will be executed and once it's promise is resolved
* or rejected the indentation will be reset to its original state.
*
* @param delta the number of spaces to increase/decrease the indentation
* @param block a function to run and reset any indentation changes after
*/
public indent<T>(delta = 0, block?: () => Promise<T>) {
const originalWidth = this.indentWidth;
this.indentWidth = Math.max(this.indentWidth + delta, 0);
if (!block) {
return;
}
return (async () => {
try {
return await block();
} finally {
this.indentWidth = originalWidth;
}
})();
}
public verbose(...args: any[]) {
@ -70,7 +97,7 @@ export class ToolingLog {
private sendToWriters(type: MessageTypes, args: any[]) {
const msg = {
type,
indent: this.identWidth,
indent: this.indentWidth,
args,
};

View file

@ -626,16 +626,46 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
*/
class ToolingLog {
constructor(writerConfig) {
(0, _defineProperty2.default)(this, "identWidth", 0);
(0, _defineProperty2.default)(this, "indentWidth", 0);
(0, _defineProperty2.default)(this, "writers", void 0);
(0, _defineProperty2.default)(this, "written$", void 0);
this.writers = writerConfig ? [new _tooling_log_text_writer.ToolingLogTextWriter(writerConfig)] : [];
this.written$ = new Rx.Subject();
}
/**
* Get the current indentation level of the ToolingLog
*/
indent(delta = 0) {
this.identWidth = Math.max(this.identWidth + delta, 0);
return this.identWidth;
getIndent() {
return this.indentWidth;
}
/**
* Indent the output of the ToolingLog by some character (4 is a good choice usually).
*
* If provided, the `block` function will be executed and once it's promise is resolved
* or rejected the indentation will be reset to its original state.
*
* @param delta the number of spaces to increase/decrease the indentation
* @param block a function to run and reset any indentation changes after
*/
indent(delta = 0, block) {
const originalWidth = this.indentWidth;
this.indentWidth = Math.max(this.indentWidth + delta, 0);
if (!block) {
return;
}
return (async () => {
try {
return await block();
} finally {
this.indentWidth = originalWidth;
}
})();
}
verbose(...args) {
@ -681,7 +711,7 @@ class ToolingLog {
sendToWriters(type, args) {
const msg = {
type,
indent: this.identWidth,
indent: this.indentWidth,
args
};
let written = false;

View file

@ -79,7 +79,7 @@ export function runFtrCli() {
err: err.message,
...flags,
});
log.indent(-log.indent());
log.indent(-log.getIndent());
log.error(err);
process.exitCode = 1;
} else {

View file

@ -78,12 +78,9 @@ export async function runTests(options: RunTestsParams) {
log.write('--- asserting that all tests belong to a ciGroup');
for (const configPath of options.configs) {
log.info('loading', configPath);
log.indent(4);
try {
await log.indent(4, async () => {
await assertNoneExcluded({ configPath, options: { ...options, log } });
} finally {
log.indent(-4);
}
});
continue;
}
@ -94,42 +91,41 @@ export async function runTests(options: RunTestsParams) {
const configPathsWithTests: string[] = [];
for (const configPath of options.configs) {
log.info('testing', configPath);
log.indent(4);
try {
await log.indent(4, async () => {
if (await hasTests({ configPath, options: { ...options, log } })) {
configPathsWithTests.push(configPath);
}
} finally {
log.indent(-4);
}
});
}
for (const configPath of configPathsWithTests) {
log.write(`--- Running ${relative(REPO_ROOT, configPath)}`);
await log.indent(0, async () => {
log.write(`--- Running ${relative(REPO_ROOT, configPath)}`);
await withProcRunner(log, async (procs) => {
const config = await readConfigFile(log, configPath);
await withProcRunner(log, async (procs) => {
const config = await readConfigFile(log, configPath);
let es;
try {
es = await runElasticsearch({ config, options: { ...options, log } });
await runKibanaServer({ procs, config, options });
await runFtr({ configPath, options: { ...options, log } });
} finally {
let es;
try {
const delay = config.get('kbnTestServer.delayShutdown');
if (typeof delay === 'number') {
log.info('Delaying shutdown of Kibana for', delay, 'ms');
await new Promise((r) => setTimeout(r, delay));
}
await procs.stop('kibana');
es = await runElasticsearch({ config, options: { ...options, log } });
await runKibanaServer({ procs, config, options });
await runFtr({ configPath, options: { ...options, log } });
} finally {
if (es) {
await es.cleanup();
try {
const delay = config.get('kbnTestServer.delayShutdown');
if (typeof delay === 'number') {
log.info('Delaying shutdown of Kibana for', delay, 'ms');
await new Promise((r) => setTimeout(r, delay));
}
await procs.stop('kibana');
} finally {
if (es) {
await es.cleanup();
}
}
}
}
});
});
}
}