Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions packages/react-native-codegen/scripts/__tests__/build-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @format
*/

'use strict';

const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const vm = require('node:vm');

describe('react-native-codegen build script', () => {
let temporaryRoot;
let packageDir;
let sourceDir;
let sourceFile;
let babel;
let prettier;
let stdout;

beforeEach(() => {
temporaryRoot = fs.mkdtempSync(
path.join(os.tmpdir(), '.react-native-codegen-build-test-'),
);

packageDir = path.join(temporaryRoot, 'packages', 'react-native-codegen');
sourceDir = path.join(packageDir, 'src');

fs.mkdirSync(sourceDir, {recursive: true});

fs.copyFileSync(
path.resolve(__dirname, '..', '..', 'build.prettierrc'),
path.join(packageDir, 'build.prettierrc'),
);

babel = {
transformFileSync: jest.fn(() => ({
code: 'module.exports = "transformed";',
})),
};

prettier = {
format: jest.fn(async code => code),
};

stdout = {
write: jest.fn(),
};
});

afterEach(() => {
fs.rmSync(temporaryRoot, {recursive: true, force: true});
});

async function runBuild(relativeFile, contents) {
sourceFile = path.join(sourceDir, relativeFile);

fs.mkdirSync(path.dirname(sourceFile), {recursive: true});
fs.writeFileSync(sourceFile, contents);

const scriptPath = path.join(packageDir, 'scripts', 'build.js');

fs.mkdirSync(path.dirname(scriptPath), {recursive: true});

const script = fs.readFileSync(
path.resolve(__dirname, '..', 'build.js'),
'utf8',
);

const mockedRequire = request => {
switch (request) {
case '@babel/core':
return babel;
case 'micromatch':
return require('micromatch');
case 'node:fs':
return fs;
case 'node:path':
return path;
case 'node:util':
return require('node:util');
case 'prettier':
return prettier;
case 'tinyglobby':
return {
globSync: jest.fn(() => [sourceFile]),
};
default:
throw new Error(`Unexpected module requested: ${request}`);
}
};

const context = {
__dirname: path.dirname(scriptPath),
require: mockedRequire,
process: {
argv: [],
stdout,
},
Promise,
};

const execution = vm.runInNewContext(script, context, {
filename: scriptPath,
});

await execution;

return path.join(packageDir, 'lib', relativeFile);
}

test('transforms JavaScript files when the checkout is inside a hidden directory', async () => {
const outputFile = await runBuild(
path.join('parsers', 'example.js'),
'module.exports = "original";\n',
);

expect(babel.transformFileSync).toHaveBeenCalledTimes(1);
expect(babel.transformFileSync).toHaveBeenCalledWith(sourceFile, {});

expect(prettier.format).toHaveBeenCalledTimes(1);

expect(fs.readFileSync(outputFile, 'utf8')).toBe(
'module.exports = "transformed";',
);
});

test('ignores test files when the checkout is inside a hidden directory', async () => {
const outputFile = await runBuild(
path.join('__tests__', 'example-test.js'),
'module.exports = "test";\n',
);

expect(babel.transformFileSync).not.toHaveBeenCalled();
expect(fs.existsSync(outputFile)).toBe(false);
});
});
5 changes: 3 additions & 2 deletions packages/react-native-codegen/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,19 @@ function getBuildPath(file, buildFolder) {
}

async function buildFile(file, silent) {
const relativeFile = path.relative(srcDir, file);
const destPath = getBuildPath(file, BUILD_DIR);

fs.mkdirSync(path.dirname(destPath), {recursive: true});

if (micromatch.isMatch(file, IGNORE_PATTERN)) {
if (micromatch.isMatch(relativeFile, IGNORE_PATTERN)) {
silent ||
process.stdout.write(
styleText('dim', ' \u2022 ') +
path.relative(PACKAGE_DIR, file) +
' (ignore)\n',
);
} else if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
} else if (!micromatch.isMatch(relativeFile, JS_FILES_PATTERN)) {
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
silent ||
process.stdout.write(
Expand Down
133 changes: 133 additions & 0 deletions scripts/build/__tests__/build-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @format
*/

'use strict';

const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

describe('buildFile', () => {
let temporaryRoot;
let packagesDir;
let buildFile;
let babel;
let prettier;

beforeEach(() => {
jest.resetModules();

// Put the temporary checkout under a hidden directory.
temporaryRoot = fs.mkdtempSync(
path.join(os.tmpdir(), '.react-native-build-test-'),
);
packagesDir = path.join(temporaryRoot, 'packages');

jest.doMock('../../shared/babelRegister', () => ({
registerForScript: jest.fn(),
}));

jest.doMock('../../shared/consts', () => ({
PACKAGES_DIR: packagesDir,
REPO_ROOT: temporaryRoot,
}));

jest.doMock('../config', () => ({
buildConfig: {packages: {}},
getBabelConfig: jest.fn(() => ({})),
getBuildOptions: jest.fn(() => ({
emitFlowDefs: false,
emitTypeScriptDefs: false,
})),
getTypeScriptCompilerOptions: jest.fn(() => ({})),
}));

jest.doMock('@babel/core', () => ({
transformFileAsync: jest.fn(async () => ({
code: 'module.exports = "transformed";',
})),
}));

jest.doMock('prettier', () => ({
format: jest.fn(async code => code),
}));

({buildFile} = require('../build'));

babel = require('@babel/core');
prettier = require('prettier');
});

afterEach(() => {
jest.resetModules();
jest.dontMock('../../shared/babelRegister');
jest.dontMock('../../shared/consts');
jest.dontMock('../config');
jest.dontMock('@babel/core');
jest.dontMock('prettier');

fs.rmSync(temporaryRoot, {recursive: true, force: true});
});

test('transforms JavaScript files when the checkout is inside a hidden directory', async () => {
const sourceDir = path.join(packagesDir, 'example-package', 'src');
const sourceFile = path.join(sourceDir, 'example.js');

fs.mkdirSync(sourceDir, {recursive: true});
fs.writeFileSync(sourceFile, '/* @flow */\nmodule.exports = "original";\n');

await buildFile(sourceFile, {silent: true});

const outputFile = path.join(
packagesDir,
'example-package',
'dist',
'example.js',
);

expect(babel.transformFileAsync).toHaveBeenCalledTimes(1);
expect(babel.transformFileAsync).toHaveBeenCalledWith(
sourceFile,
expect.any(Object),
);
expect(prettier.format).toHaveBeenCalledTimes(1);
expect(fs.readFileSync(outputFile, 'utf8')).toBe(
'module.exports = "transformed";',
);
});

test('ignores test files when the checkout is inside a hidden directory', async () => {
const sourceDir = path.join(
packagesDir,
'example-package',
'src',
'__tests__',
);
const sourceFile = path.join(sourceDir, 'example-test.js');

fs.mkdirSync(sourceDir, {recursive: true});
fs.writeFileSync(sourceFile, 'module.exports = "test";\n');

await buildFile(sourceFile, {silent: true});

expect(babel.transformFileAsync).not.toHaveBeenCalled();
expect(
fs.existsSync(
path.join(
packagesDir,
'example-package',
'dist',
'__tests__',
'example-test.js',
),
),
).toBe(false);
});
});
14 changes: 11 additions & 3 deletions scripts/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ async function buildFile(
) {
const {silent = false} = options;
const packageName = getPackageName(file);

// Match paths relative to the package source directory so hidden checkout
// directory names do not prevent JavaScript files from being transformed.
const relativeFile = path.relative(
path.resolve(PACKAGES_DIR, packageName, SRC_DIR),
file,
);

const buildPath = getBuildPath(file);
const {emitFlowDefs, emitTypeScriptDefs} = getBuildOptions(packageName);

Expand All @@ -175,14 +183,14 @@ async function buildFile(
(desc != null ? ' (' + desc + ')' : ''),
);

if (micromatch.isMatch(file, IGNORE_PATTERN)) {
if (micromatch.isMatch(relativeFile, IGNORE_PATTERN)) {
logResult({copied: false, desc: 'ignore'});
return;
}

await fs.mkdir(path.dirname(buildPath), {recursive: true});

if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
if (!micromatch.isMatch(relativeFile, JS_FILES_PATTERN)) {
await fs.copyFile(file, buildPath);
logResult({copied: true, desc: 'copy'});
return;
Expand Down Expand Up @@ -290,7 +298,7 @@ async function getEntryPoints(
`exports["${subpath}"] must be a string target.`,
);
}
targets.push(targetOrConditionsObject);
targets.push(target);
}

for (const target of targets) {
Expand Down