Skip to content

Commit da6d089

Browse files
rubennortefacebook-github-bot
authored andcommitted
Extract logic to get Fantom test config to a standalone module (2nd attempt) (#48119)
Summary: Pull Request resolved: #48119 Changelog: [internal] This is a re-land of #48092 Reviewed By: rshest Differential Revision: D66820309 fbshipit-source-id: 6b07edcca6988eeb014f6b51ec82296d451bee14
1 parent 1243679 commit da6d089

File tree

3 files changed

+65
-51
lines changed

3 files changed

+65
-51
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import fs from 'fs';
13+
// $FlowExpectedError[untyped-import]
14+
import {extract, parse} from 'jest-docblock';
15+
16+
type DocblockPragmas = {[key: string]: string | string[]};
17+
type FantomTestMode = 'dev' | 'opt';
18+
type FantomTestConfig = {
19+
mode: FantomTestMode,
20+
};
21+
22+
const DEFAULT_MODE: FantomTestMode = 'dev';
23+
24+
/**
25+
* Extracts the Fantom configuration from the test file, specified as part of
26+
* the docblock comment. E.g.:
27+
*
28+
* ```
29+
* /**
30+
* * @flow strict-local
31+
* * @fantom mode:opt
32+
* *
33+
* ```
34+
*
35+
* So far the only supported option is `mode`, which can be 'dev' or 'opt'.
36+
*/
37+
export default function getFantomTestConfig(
38+
testPath: string,
39+
): FantomTestConfig {
40+
const docblock = extract(fs.readFileSync(testPath, 'utf8'));
41+
const pragmas = parse(docblock) as DocblockPragmas;
42+
43+
const config = {
44+
mode: DEFAULT_MODE,
45+
};
46+
47+
const maybeMode = pragmas.fantom_mode;
48+
49+
if (maybeMode != null) {
50+
if (Array.isArray(maybeMode)) {
51+
throw new Error('Expected a single value for @fantom_mode');
52+
}
53+
54+
const mode = maybeMode;
55+
56+
if (mode === 'dev' || mode === 'opt') {
57+
config.mode = mode;
58+
} else {
59+
throw new Error(`Invalid Fantom mode: ${mode}`);
60+
}
61+
}
62+
63+
return config;
64+
}

jest/integration/runner/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import type {TestSuiteResult} from '../runtime/setup';
1313

1414
import entrypointTemplate from './entrypoint-template';
15+
import getFantomTestConfig from './getFantomTestConfig';
1516
import {
1617
getBuckModeForPlatform,
1718
getDebugInfoFromCommandResult,
18-
getFantomTestConfig,
1919
getShortHash,
2020
runBuck2,
2121
symbolicateStackTrace,

jest/integration/runner/utils.js

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,10 @@
1212
import {spawnSync} from 'child_process';
1313
import crypto from 'crypto';
1414
import fs from 'fs';
15-
// $FlowExpectedError[untyped-import]
16-
import {extract, parse} from 'jest-docblock';
1715
import os from 'os';
1816
// $FlowExpectedError[untyped-import]
1917
import {SourceMapConsumer} from 'source-map';
2018

21-
type DocblockPragmas = {[key: string]: string | string[]};
22-
type FantomTestMode = 'dev' | 'opt';
23-
type FantomTestConfig = {
24-
mode: FantomTestMode,
25-
};
26-
27-
const DEFAULT_MODE: FantomTestMode = 'dev';
28-
29-
/**
30-
* Extracts the Fantom configuration from the test file, specified as part of
31-
* the docblock comment. E.g.:
32-
*
33-
* ```
34-
* /**
35-
* * @flow strict-local
36-
* * @fantom mode:opt
37-
* *
38-
* ```
39-
*
40-
* So far the only supported option is `mode`, which can be 'dev' or 'opt'.
41-
*/
42-
export function getFantomTestConfig(testPath: string): FantomTestConfig {
43-
const docblock = extract(fs.readFileSync(testPath, 'utf8'));
44-
const pragmas = parse(docblock) as DocblockPragmas;
45-
46-
const config = {
47-
mode: DEFAULT_MODE,
48-
};
49-
50-
const maybeMode = pragmas.fantom_mode;
51-
52-
if (maybeMode != null) {
53-
if (Array.isArray(maybeMode)) {
54-
throw new Error('Expected a single value for @fantom_mode');
55-
}
56-
57-
const mode = maybeMode;
58-
59-
if (mode === 'dev' || mode === 'opt') {
60-
config.mode = mode;
61-
} else {
62-
throw new Error(`Invalid Fantom mode: ${mode}`);
63-
}
64-
}
65-
66-
return config;
67-
}
68-
6919
export function getBuckModeForPlatform(enableRelease: boolean = false): string {
7020
const mode = enableRelease ? 'opt' : 'dev';
7121

0 commit comments

Comments
 (0)