Skip to content

Commit

Permalink
update unit tests for react native cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Edney committed Jan 24, 2024
1 parent e11caad commit 43abd6d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
7 changes: 3 additions & 4 deletions packages/react-native-cli/src/lib/Xcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ export async function updateXcodeProject (projectRoot: string, endpoint: string|
const buildPhaseMap = proj?.hash?.project?.objects?.PBXShellScriptBuildPhase || []
logger.info('Ensuring React Native build phase outputs source maps')

const didUpdate = await updateXcodeEnv(projectRoot, logger)
logger.info('Adding build phase to upload source maps to Bugsnag')

const didAdd = await addUploadSourceMapsTask(proj, buildPhaseMap, endpoint, logger)
const didUpdate = await updateXcodeEnv(projectRoot, logger)
const didChange = didUpdate || didAdd

if (!didChange) return
Expand All @@ -53,7 +52,7 @@ async function addUploadSourceMapsTask (
): Promise<boolean> {
for (const shellBuildPhaseKey in buildPhaseMap) {
const phase = buildPhaseMap[shellBuildPhaseKey]
if (typeof phase.shellScript === 'string' && (phase.shellScript.includes('bugsnag-react-native-xcode.sh') || phase.shellScript.includes('Upload source maps to Bugsnag'))) {
if (typeof phase.shellScript === 'string' && (phase.shellScript.includes('bugsnag-react-native-xcode.sh') || phase.shellScript.includes('npm run bugsnag:upload-ios'))) {
logger.warn('An "Upload source maps to Bugsnag" build phase already exists')
return false
}
Expand All @@ -79,7 +78,7 @@ async function updateXcodeEnv (projectRoot: string, logger: Logger): Promise<boo

const xcodeEnvData = await fs.readFile(envFilePath, 'utf8')

if (xcodeEnvData.includes('SOURCEMAP_FILE=')) {
if (xcodeEnvData?.includes('SOURCEMAP_FILE=')) {
logger.warn(`The .xcode.env file already contains a section for "${searchString}"`)
return false
} else {
Expand Down
18 changes: 7 additions & 11 deletions packages/react-native-cli/src/lib/__test__/Xcode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ test('updateXcodeProject(): success', async () => {
await updateXcodeProject('/random/path', undefined, logger)

expect(readFileSyncMock).toHaveBeenCalledWith('/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', 'utf8')
expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledTimes(2)

// the added build phase gets a generated build ID, so we need to figure out what that is before doing an exact string match
const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[0][1] as string)
const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[1] as unknown as string)
if (!matches) throw new Error('Failed to detect build ID')
const generatedPhaseId = matches[1]
const expectedOutput = (await loadFixture(path.join(__dirname, 'fixtures', 'project-after.pbxproj')))
.replace(/43CF599E6AE1472FAF1EC029/g, generatedPhaseId)
expect(writeFileMock).toHaveBeenCalledWith(
expect(writeFileMock).toHaveBeenLastCalledWith(
'/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj',
expectedOutput,
'utf8'
Expand All @@ -86,11 +86,10 @@ test('updateXcodeProject(): success with custom endpoint', async () => {
await updateXcodeProject('/random/path', 'https://upload.example.com', logger)

expect(readFileSyncMock).toHaveBeenCalledWith('/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', 'utf8')
expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledTimes(2)

// the added build phase gets a generated build ID, so we need to figure out what that is before doing an exact string match
const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[0][1] as string)

const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[1] as unknown as string)
if (!matches) {
throw new Error('Failed to detect build ID')
}
Expand All @@ -99,7 +98,7 @@ test('updateXcodeProject(): success with custom endpoint', async () => {
const expectedOutput = (await loadFixture(path.join(__dirname, 'fixtures', 'project-after-with-endpoint.pbxproj')))
.replace(/43CF599E6AE1472FAF1EC029/g, generatedPhaseId)

expect(writeFileMock).toHaveBeenCalledWith(
expect(writeFileMock).toHaveBeenLastCalledWith(
'/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj',
expectedOutput,
'utf8'
Expand All @@ -122,11 +121,8 @@ test('updateXcodeProject(): modifications already exist', async () => {
const writeFileMock = fs.writeFile as jest.MockedFunction<typeof fs.writeFile>
await updateXcodeProject('/random/path', undefined, logger)
expect(readFileSyncMock).toHaveBeenCalledWith('/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', 'utf8')
expect(writeFileMock).not.toHaveBeenCalled()
expect(writeFileMock).toHaveBeenCalledTimes(2)
expect(logger.warn).toHaveBeenCalledWith('An "Upload source maps to Bugsnag" build phase already exists')
expect(logger.warn).toHaveBeenCalledWith(expect.stringMatching(
/The "Bundle React Native Code and Images" build phase \([A-F0-9]{24}\) already includes the required arguments/
))
})

test('updateXcodeProject(): can\'t find project', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
};
2D02E4CB1E0B4B27006451C7 /* Bundle React Native Code And Images */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -390,7 +390,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
};
3EBDF05F265B984FA826288C /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
Expand Down Expand Up @@ -566,7 +566,7 @@
outputPaths = (
);
shellPath = /bin/sh;
shellScript = "export ENDPOINT='https://upload.example.com'\nSOURCE_MAP=\"$TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\" ../node_modules/@bugsnag/react-native/bugsnag-react-native-xcode.sh";
shellScript = "npm run bugsnag:upload-ios";
};
/* End PBXShellScriptBuildPhase section */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
};
2D02E4CB1E0B4B27006451C7 /* Bundle React Native Code And Images */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -390,7 +390,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
};
3EBDF05F265B984FA826288C /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
Expand Down Expand Up @@ -566,7 +566,7 @@
outputPaths = (
);
shellPath = /bin/sh;
shellScript = "SOURCE_MAP=\"$TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\" ../node_modules/@bugsnag/react-native/bugsnag-react-native-xcode.sh";
shellScript = "npm run bugsnag:upload-ios";
};
/* End PBXShellScriptBuildPhase section */

Expand Down

0 comments on commit 43abd6d

Please sign in to comment.