fix(logging): handle mid-line streaming output in test logger and add streaming tests
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@git.zone/tstest',
|
||||
version: '3.1.5',
|
||||
version: '3.1.6',
|
||||
description: 'a test utility to run tests that match test/**/*.ts'
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ export class TsTestLogger {
|
||||
private currentTestLogFile: string | null = null;
|
||||
private currentTestLogs: string[] = []; // Buffer for current test logs
|
||||
private currentTestFailed: boolean = false;
|
||||
private isOutputMidLine: boolean = false; // Track whether we're mid-line for streaming output
|
||||
|
||||
constructor(options: LogOptions = {}) {
|
||||
this.options = options;
|
||||
@@ -189,6 +190,7 @@ export class TsTestLogger {
|
||||
// Reset test-specific state
|
||||
this.currentTestLogs = [];
|
||||
this.currentTestFailed = false;
|
||||
this.isOutputMidLine = false;
|
||||
|
||||
// Only set up test log file if --logfile option is specified
|
||||
if (this.options.logFile) {
|
||||
@@ -352,17 +354,28 @@ export class TsTestLogger {
|
||||
testConsoleOutput(message: string) {
|
||||
if (this.options.json) return;
|
||||
|
||||
const prefix = ' ';
|
||||
// In verbose mode, show console output immediately
|
||||
if (this.options.verbose) {
|
||||
this.log(this.format(` ${message}`, 'dim'));
|
||||
// Only add prefix if we're starting a new line
|
||||
const output = this.isOutputMidLine ? message : prefix + message;
|
||||
this.log(this.format(output, 'dim'));
|
||||
} else {
|
||||
// In non-verbose mode, buffer the logs
|
||||
this.currentTestLogs.push(message);
|
||||
if (this.isOutputMidLine && this.currentTestLogs.length > 0) {
|
||||
// Append to the last buffered entry since we're mid-line
|
||||
this.currentTestLogs[this.currentTestLogs.length - 1] += message;
|
||||
} else {
|
||||
this.currentTestLogs.push(message);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset mid-line state since we just output a complete line
|
||||
this.isOutputMidLine = false;
|
||||
|
||||
// Always log to test file if --logfile is specified
|
||||
if (this.currentTestLogFile) {
|
||||
this.logToTestFile(` ${message}`);
|
||||
this.logToTestFile(`${prefix}${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -371,13 +384,15 @@ export class TsTestLogger {
|
||||
if (this.options.json) return;
|
||||
|
||||
const prefix = ' ';
|
||||
// Only add prefix if we're starting a new line (not mid-line)
|
||||
const output = this.isOutputMidLine ? message : prefix + message;
|
||||
|
||||
if (this.options.verbose) {
|
||||
// Use process.stdout.write to preserve streaming without adding newlines
|
||||
process.stdout.write(this.format(prefix + message, 'dim'));
|
||||
process.stdout.write(this.format(output, 'dim'));
|
||||
} else {
|
||||
// Buffer without trailing newline - append to last entry if exists and incomplete
|
||||
if (this.currentTestLogs.length > 0) {
|
||||
// Append to the last buffered entry (for streaming segments)
|
||||
// Buffer mode: append to last entry if mid-line
|
||||
if (this.isOutputMidLine && this.currentTestLogs.length > 0) {
|
||||
this.currentTestLogs[this.currentTestLogs.length - 1] += message;
|
||||
} else {
|
||||
this.currentTestLogs.push(message);
|
||||
@@ -386,8 +401,11 @@ export class TsTestLogger {
|
||||
|
||||
// Log to test file without adding newline
|
||||
if (this.currentTestLogFile) {
|
||||
this.logToTestFileRaw(prefix + message);
|
||||
this.logToTestFileRaw(output);
|
||||
}
|
||||
|
||||
// We're now mid-line (no newline was written)
|
||||
this.isOutputMidLine = true;
|
||||
}
|
||||
|
||||
private logToTestFileRaw(message: string) {
|
||||
|
||||
Reference in New Issue
Block a user