Use team city messages for mocha test output
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser";
|
import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser";
|
||||||
import {getFilteringAdapter} from "./src/Adapter";
|
import {getFilteringAdapter} from "./src/Adapter";
|
||||||
|
import {runWithTeamCityReporter} from "./src/KotlinTestTeamCityReporter";
|
||||||
|
import {TeamCityMessagesFlow} from "./src/TeamCityMessagesFlow";
|
||||||
|
|
||||||
const kotlin_test = require('kotlin-test');
|
const kotlin_test = require('kotlin-test');
|
||||||
|
|
||||||
@@ -29,5 +31,10 @@ const defaultMochaArgs = [
|
|||||||
const processArgs = process.argv.slice(2, -1 * defaultMochaArgs.length);
|
const processArgs = process.argv.slice(2, -1 * defaultMochaArgs.length);
|
||||||
const untypedArgs = parser.parse(processArgs);
|
const untypedArgs = parser.parse(processArgs);
|
||||||
|
|
||||||
|
// TODO(ilgonmic): Try to detect adapter
|
||||||
const initialAdapter = new kotlin_test.kotlin.test.adapters.JasmineLikeAdapter();
|
const initialAdapter = new kotlin_test.kotlin.test.adapters.JasmineLikeAdapter();
|
||||||
kotlin_test.setAdapter(getFilteringAdapter(initialAdapter, untypedArgs));
|
|
||||||
|
const realConsoleLog = console.log;
|
||||||
|
const teamCity = new TeamCityMessagesFlow(null, (payload) => realConsoleLog(payload));
|
||||||
|
const teamCityAdapter = runWithTeamCityReporter(initialAdapter, teamCity);
|
||||||
|
kotlin_test.setAdapter(getFilteringAdapter(teamCityAdapter, untypedArgs));
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import {KotlinTestRunner} from "./KotlinTestRunner";
|
import {KotlinTestRunner} from "./KotlinTestRunner";
|
||||||
import {TeamCityMessageData, TeamCityMessagesFlow} from "./TeamCityMessagesFlow";
|
import {TeamCityMessageData, TeamCityMessagesFlow} from "./TeamCityMessagesFlow";
|
||||||
import {Timer} from "./Timer";
|
|
||||||
import {format} from "util";
|
import {format} from "util";
|
||||||
|
|
||||||
const kotlin_test = require('kotlin-test');
|
const kotlin_test = require('kotlin-test');
|
||||||
@@ -23,113 +22,48 @@ function withName(name: string, data?: TeamCityMessageData): TeamCityMessageData
|
|||||||
|
|
||||||
export function runWithTeamCityReporter(
|
export function runWithTeamCityReporter(
|
||||||
runner: KotlinTestRunner,
|
runner: KotlinTestRunner,
|
||||||
ignoredTestSuites: IgnoredTestSuitesReporting,
|
teamCity: TeamCityMessagesFlow
|
||||||
teamCity: TeamCityMessagesFlow,
|
|
||||||
timer: Timer<any> | null
|
|
||||||
): KotlinTestRunner {
|
): KotlinTestRunner {
|
||||||
let inIgnoredSuite = false;
|
|
||||||
let currentAssertionResult: { expected: any, actual: any } | null = null;
|
|
||||||
|
|
||||||
kotlin_test.kotlin.test.setAssertHook_4duqou$(function (assertionResult: { expected: any, actual: any }) {
|
|
||||||
currentAssertionResult = assertionResult;
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
suite: function (name: string, isIgnored: boolean, fn: () => void) {
|
suite: function (name: string, isIgnored: boolean, fn: () => void) {
|
||||||
if (isIgnored) {
|
runner.suite(name, isIgnored, fn)
|
||||||
if (ignoredTestSuites == IgnoredTestSuitesReporting.skip) return;
|
|
||||||
else if (ignoredTestSuites == IgnoredTestSuitesReporting.reportAsIgnoredTest) {
|
|
||||||
teamCity.sendMessage('testIgnored', withName(name, {"suite": true}));
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
teamCity.sendMessage('testSuiteStarted', withName(name));
|
|
||||||
|
|
||||||
// noinspection UnnecessaryLocalVariableJS
|
|
||||||
const alreadyInIgnoredSuite = inIgnoredSuite;
|
|
||||||
if (!alreadyInIgnoredSuite && isIgnored) {
|
|
||||||
inIgnoredSuite = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (isIgnored && ignoredTestSuites == IgnoredTestSuitesReporting.reportAllInnerTestsAsIgnored) {
|
|
||||||
fn();
|
|
||||||
} else {
|
|
||||||
runner.suite(name, isIgnored, fn)
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (isIgnored && !alreadyInIgnoredSuite) {
|
|
||||||
inIgnoredSuite = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const data: TeamCityMessageData = withName(name);
|
|
||||||
|
|
||||||
// extension only for Gradle
|
|
||||||
if (isIgnored) data["ignored"] = true;
|
|
||||||
|
|
||||||
teamCity.sendMessage('testSuiteFinished', data);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
test: function (name: string, isIgnored: boolean, fn: () => void) {
|
test: function (name: string, isIgnored: boolean, fn: () => void) {
|
||||||
if (inIgnoredSuite || isIgnored) {
|
let revertLogMethods: CallableFunction[] = [];
|
||||||
teamCity.sendMessage('testIgnored', withName(name));
|
|
||||||
} else {
|
|
||||||
const startTime = timer ? timer.start() : null;
|
|
||||||
teamCity.sendMessage('testStarted', withName(name));
|
|
||||||
|
|
||||||
let revertLogMethods: CallableFunction[] = [];
|
runner.test(name, isIgnored, () => {
|
||||||
|
const log = (type: string) => function (message?: any, ...optionalParams: any[]) {
|
||||||
|
teamCity.sendMessage(
|
||||||
|
"testStdOut",
|
||||||
|
withName(
|
||||||
|
name,
|
||||||
|
{
|
||||||
|
"out": `[${type}] ${format(message, ...optionalParams)}\n`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
const logMethods = ['log', 'info', 'warn', 'error', 'debug'];
|
||||||
|
|
||||||
|
const globalConsole = console as unknown as {
|
||||||
|
[method: string]: (message?: any, ...optionalParams: any[]) => void
|
||||||
|
};
|
||||||
|
|
||||||
|
revertLogMethods = logMethods
|
||||||
|
.map(method => {
|
||||||
|
const realMethod = globalConsole[method];
|
||||||
|
globalConsole[method] = log(method);
|
||||||
|
return () => globalConsole[method] = realMethod
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
runner.test(name, isIgnored, () => {
|
fn();
|
||||||
const log = (type: string) => function (message?: any, ...optionalParams: any[]) {
|
|
||||||
teamCity.sendMessage(
|
|
||||||
"testStdOut",
|
|
||||||
withName(
|
|
||||||
name,
|
|
||||||
{
|
|
||||||
"out": `[${type}] ${format(message, ...optionalParams)}\n`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
const logMethods = ['log', 'info', 'warn', 'error', 'debug'];
|
|
||||||
|
|
||||||
const globalConsole = console as unknown as {
|
|
||||||
[method: string]: (message?: any, ...optionalParams: any[]) => void
|
|
||||||
};
|
|
||||||
|
|
||||||
revertLogMethods = logMethods
|
|
||||||
.map(method => {
|
|
||||||
const realMethod = globalConsole[method];
|
|
||||||
globalConsole[method] = log(method);
|
|
||||||
return () => globalConsole[method] = realMethod
|
|
||||||
});
|
|
||||||
fn();
|
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const data: TeamCityMessageData = withName(name, {
|
throw e;
|
||||||
"message": e.message,
|
|
||||||
"details": e.stack
|
|
||||||
});
|
|
||||||
|
|
||||||
if (currentAssertionResult) {
|
|
||||||
data["type"] = 'comparisonFailure';
|
|
||||||
data["expected"] = currentAssertionResult.expected;
|
|
||||||
data["actual"] = currentAssertionResult.actual;
|
|
||||||
}
|
|
||||||
teamCity.sendMessage('testFailed', data);
|
|
||||||
} finally {
|
} finally {
|
||||||
revertLogMethods.forEach(revert => revert());
|
revertLogMethods.forEach(revert => revert());
|
||||||
currentAssertionResult = null;
|
|
||||||
const data: TeamCityMessageData = withName(name);
|
|
||||||
if (startTime) {
|
|
||||||
data["duration"] = timer!!.end(startTime); // ns to ms
|
|
||||||
}
|
|
||||||
teamCity.sendMessage('testFinished', data);
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user