Skip to content

Commit

Permalink
Use structured output for Fantom logs and test results
Browse files Browse the repository at this point in the history
Differential Revision: D67600616
  • Loading branch information
rubennorte authored and facebook-github-bot committed Dec 23, 2024
1 parent 39e441a commit d0d7208
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
47 changes: 36 additions & 11 deletions packages/react-native-fantom/runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

import type {TestSuiteResult} from '../runtime/setup';
import type {ConsoleLogMessage} from './utils';

import entrypointTemplate from './entrypoint-template';
import getFantomTestConfig from './getFantomTestConfig';
Expand All @@ -22,6 +23,7 @@ import {
getBuckModesForPlatform,
getDebugInfoFromCommandResult,
getShortHash,
printConsoleLogs,
runBuck2,
symbolicateStackTrace,
} from './utils';
Expand All @@ -42,27 +44,50 @@ const BUILD_OUTPUT_PATH = fs.mkdtempSync(
const PRINT_FANTOM_OUTPUT: false = false;

function parseRNTesterCommandResult(result: ReturnType<typeof runBuck2>): {
logs: string,
logs: $ReadOnlyArray<ConsoleLogMessage>,
testResult: TestSuiteResult,
} {
const stdout = result.stdout.toString();

const outputArray = stdout.trim().split('\n');
const logs = [];
let testResult;

// The last line should be the test output in JSON format
const testResultJSON = outputArray.pop();
const lines = stdout
.split('\n')
.map(line => line.trim())
.filter(Boolean);

for (const line of lines) {
let parsed;
try {
parsed = JSON.parse(line);
} catch {}

switch (parsed?.type) {
case 'test-result':
testResult = parsed;
break;
case 'console-log':
logs.push(parsed);
break;
default:
logs.push({
type: 'console-log',
message: line,
level: 'info',
});
break;
}
}

let testResult;
try {
testResult = JSON.parse(nullthrows(testResultJSON));
} catch (error) {
if (testResult == null) {
throw new Error(
'Failed to parse test results from RN tester binary result.\n' +
'Failed to find test results in RN tester binary output.\n' +
getDebugInfoFromCommandResult(result),
);
}

return {logs: outputArray.join('\n'), testResult};
return {logs, testResult};
}

function generateBytecodeBundle({
Expand Down Expand Up @@ -216,7 +241,7 @@ module.exports = async function runTest(
const endTime = Date.now();

if (process.env.SANDCASTLE == null) {
console.log(rnTesterParsedOutput.logs);
printConsoleLogs(rnTesterParsedOutput.logs);
}

const testResults =
Expand Down
28 changes: 28 additions & 0 deletions packages/react-native-fantom/runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,31 @@ export function symbolicateStackTrace(
})
.join('\n');
}

export type ConsoleLogMessage = {
type: 'console-log',
level: 'info' | 'warn' | 'error',
message: string,
};

export function printConsoleLogs(
logs: $ReadOnlyArray<ConsoleLogMessage>,
): void {
for (const log of logs) {
switch (log.type) {
case 'console-log':
switch (log.level) {
case 'info':
console.log(log.message);
break;
case 'warn':
console.warn(log.message);
break;
case 'error':
console.error(log.message);
break;
}
break;
}
}
}
8 changes: 7 additions & 1 deletion packages/react-native-fantom/runtime/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import type {SnapshotConfig, TestSnapshotResults} from './snapshotContext';

import NativeFantom from '../src/specs/NativeFantom';
import expect from './expect';
import {createMockFunction} from './mocks';
import {setupSnapshotConfig, snapshotContext} from './snapshotContext';
Expand Down Expand Up @@ -190,7 +191,12 @@ function executeTests() {
}

function reportTestSuiteResult(testSuiteResult: TestSuiteResult): void {
console.log(JSON.stringify(testSuiteResult));
NativeFantom.reportTestSuiteResultsJSON(
JSON.stringify({
type: 'test-result',
...testSuiteResult,
}),
);
}

global.$$RunTests$$ = () => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-fantom/src/specs/NativeFantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface Spec extends TurboModule {
getMountingManagerLogs: (surfaceId: number) => Array<string>;
flushMessageQueue: () => void;
getRenderedOutput: (surfaceId: number, config: RenderFormatOptions) => string;
reportTestSuiteResultsJSON: (results: string) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>(
Expand Down

0 comments on commit d0d7208

Please sign in to comment.