-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor generate-artifacts to improve testability.
Summary: This Diff splits the `generate-artifacts.js` script into: * `generate-artifacts-executor.js`: which contains the logic to generate the code for iOS. * `generate-artifacts.js`: which contains the argument parsing logic and invokes the executor. Finally, it introduces some tests. ## Changelog [iOS][Changed] - Refactor part of the codegen scripts and add tests. Reviewed By: cortinico, dmitryrykun Differential Revision: D35846674 fbshipit-source-id: 14873c3fe762606e9004a29e4a6b986bf6a8f055
- Loading branch information
1 parent
6718500
commit 305a054
Showing
3 changed files
with
525 additions
and
283 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
scripts/codegen/__tests__/generate-artifacts-executor-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* 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. | ||
* | ||
* @emails oncall+react_native | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const underTest = require('../generate-artifacts-executor'); | ||
const path = require('path'); | ||
|
||
describe('generateCode', () => { | ||
it('executeNodes with the right arguents', () => { | ||
// Define variables and expected values | ||
const iosOutputDir = 'app/ios/build/generated/ios'; | ||
const library = {config: {name: 'library', type: 'all'}}; | ||
const tmpDir = 'tmp'; | ||
const node = 'usr/bin/node'; | ||
const pathToSchema = 'app/build/schema.json'; | ||
const rnRoot = path.join(__dirname, '../..'); | ||
const libraryType = 'all'; | ||
|
||
const tmpComponentOutDirs = path.join(tmpDir, 'out', 'components'); | ||
const tmpNativeModulesOutDir = path.join(tmpDir, 'out', 'nativeModules'); | ||
const iOSComponentOutDirs = path.join( | ||
iosOutputDir, | ||
'react/renderer/components', | ||
library.config.name, | ||
); | ||
const iOSNativeModulesOutDir = path.join( | ||
iosOutputDir, | ||
'./', | ||
library.config.name, | ||
); | ||
|
||
// mock used functions | ||
let mkdirSyncInvocationCount = 0; | ||
jest.mock('fs', () => ({ | ||
readdirSync: dirpath => { | ||
return ['test/dir']; //we only require to return something, so that the folder is not empty. | ||
}, | ||
mkdirSync: (location, config) => { | ||
if (mkdirSyncInvocationCount === 0) { | ||
expect(location).toEqual(tmpComponentOutDirs); | ||
} | ||
if (mkdirSyncInvocationCount === 1) { | ||
expect(location).toEqual(tmpNativeModulesOutDir); | ||
} | ||
if (mkdirSyncInvocationCount === 2) { | ||
expect(location).toEqual(iOSComponentOutDirs); | ||
} | ||
if (mkdirSyncInvocationCount === 3) { | ||
expect(location).toEqual(iOSNativeModulesOutDir); | ||
} | ||
mkdirSyncInvocationCount += 1; | ||
}, | ||
})); | ||
|
||
let execSyncInvocationCount = 0; | ||
jest.mock('child_process', () => ({ | ||
execSync: command => { | ||
if (execSyncInvocationCount === 0) { | ||
const expectedCommand = `${node} ${path.join( | ||
rnRoot, | ||
'generate-specs-cli.js', | ||
)} \ | ||
--platform ios \ | ||
--schemaPath ${pathToSchema} \ | ||
--outputDir ${tmpNativeModulesOutDir} \ | ||
--componentsOutputDir ${tmpComponentOutDirs} \ | ||
--modulesOutputDirs ${tmpNativeModulesOutDir} \ | ||
--libraryName ${library.config.name} \ | ||
--libraryType ${libraryType}`; | ||
expect(command).toEqual(expectedCommand); | ||
} | ||
|
||
if (execSyncInvocationCount === 1) { | ||
expect(command).toEqual( | ||
`cp -R ${tmpComponentOutDirs}/* ${iOSComponentOutDirs}`, | ||
); | ||
} | ||
if (execSyncInvocationCount === 2) { | ||
expect(command).toEqual( | ||
`cp -R ${tmpNativeModulesOutDir}/* ${iOSNativeModulesOutDir}`, | ||
); | ||
} | ||
|
||
execSyncInvocationCount += 1; | ||
}, | ||
})); | ||
|
||
underTest._generateCode(iosOutputDir, library, tmpDir, node, pathToSchema); | ||
expect(mkdirSyncInvocationCount).toBe(4); | ||
}); | ||
}); |
Oops, something went wrong.