Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-6128] RN-CLI: Use a more exhaustive search for MainApplication.java #1365

Merged
merged 19 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9b9233f
feat(react-native-cli): Use a more exhaustive search for MainApplicat…
bengourley Apr 21, 2021
fa6c841
test(react-native-cli): Add ejected Expo fixture
bengourley Apr 22, 2021
ffc020e
test(react-native-cli): Enable ejected Expo end to end tests on CI
bengourley Apr 22, 2021
c3a630d
test(react-native-cli): Correct path in fixture logic
bengourley Apr 22, 2021
1087da0
test(react-native-cli): Add workarounds for failed stdout parsing
bengourley Apr 22, 2021
e1d64aa
test(react-native-cli): Disable OTA updates in Expo fixture
bengourley Apr 22, 2021
25ffde7
test(react-native-cli): Refactor to avoid global variable
bengourley Apr 22, 2021
c0bb007
test(react-native-cli): Rename files
bengourley Apr 22, 2021
40fd36e
test(react-native-cli): Add crashy package to fixture
bengourley Apr 22, 2021
01e09c4
test(react-native-cli): Set up iOS fixture app signing
bengourley Apr 22, 2021
41fc71a
test(react-native-cli): Add missing semi colon. Java!!!
bengourley Apr 22, 2021
04d0c5c
test(react-native-cli): Update crashy package path
bengourley Apr 22, 2021
f827015
test(react-native-cli): Disable incomplete steps
bengourley Apr 26, 2021
c5fdfa8
chore: Remove extraneous whitespace
bengourley Apr 27, 2021
edbda3d
test(react-native-cli): Remove uneccesary dropping of lines
bengourley Apr 27, 2021
4344510
Merge branch 'next' into bengourley/rn-cli-main-application-search
bengourley Apr 27, 2021
57365f3
chore: Update changelog
bengourley Apr 27, 2021
667565e
Merge branch 'bengourley/rn-cli-main-application-search' of github.co…
bengourley Apr 27, 2021
b15479b
test(react-native-cli): Don't npm install where unnecessary
bengourley Apr 28, 2021
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
77 changes: 77 additions & 0 deletions packages/react-native-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/react-native-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"command-line-args": "^5.1.1",
"command-line-usage": "^6.1.0",
"consola": "^2.15.0",
"glob": "^7.1.6",
"plist": "^3.0.1",
"prompts": "^2.4.0",
"xcode": "^3.0.1"
Expand Down
23 changes: 14 additions & 9 deletions packages/react-native-cli/src/lib/Insert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Logger } from '../Logger'
import path from 'path'
import { promises as fs } from 'fs'
import { promisify } from 'util'
import glob from 'glob'

const asyncGlob = promisify(glob)

const BUGSNAG_JS_IMPORT_INIT =
`import Bugsnag from "@bugsnag/react-native";
Expand Down Expand Up @@ -76,20 +80,21 @@ export async function insertIos (projectRoot: string, logger: Logger): Promise<v

export async function insertAndroid (projectRoot: string, logger: Logger): Promise<void> {
logger.info('Adding Bugsnag to the Android layer')
const comDir = path.join(projectRoot, 'android', 'app', 'src', 'main', 'java', 'com')
let appPackagePath

let mainApplicationPath
try {
appPackagePath = (await fs.readdir(comDir))[0]
if (!appPackagePath) {
logger.warn(FAIL_MSG('MainApplication.java'))
return
}
const comDir = path.join(projectRoot, 'android', 'app', 'src', 'main', 'java', 'com')
const relativeMainApplicationPath = (await asyncGlob('**/*/MainApplication.java', {
cwd: comDir
}))[0]
if (!relativeMainApplicationPath) return logger.warn(FAIL_MSG('MainApplication.java'))
mainApplicationPath = path.join(comDir, relativeMainApplicationPath)
} catch (e) {
logger.error(FAIL_MSG('MainApplication.java'))
logger.warn(FAIL_MSG('MainApplication.java'))
return
}

try {
const mainApplicationPath = path.join(comDir, appPackagePath, 'MainApplication.java')
const mainApplication = await fs.readFile(mainApplicationPath, 'utf8')

if (mainApplication.includes(BUGSNAG_JAVA_IMPORT) || mainApplication.includes(BUGSNAG_JAVA_INIT)) {
Expand Down
41 changes: 19 additions & 22 deletions packages/react-native-cli/src/lib/__test__/Insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { insertJs, insertAndroid, insertIos } from '../Insert'
import logger from '../../Logger'
import path from 'path'
import { promises as fs } from 'fs'
import glob from 'glob'

async function loadFixture (fixture: string) {
return jest.requireActual('fs').promises.readFile(fixture, 'utf8')
Expand All @@ -19,6 +20,9 @@ jest.mock('../../Logger')
jest.mock('fs', () => {
return { promises: { readFile: jest.fn(), writeFile: jest.fn(), readdir: jest.fn() } }
})
jest.mock('glob', () => {
return jest.fn()
})

afterEach(() => jest.resetAllMocks())

Expand Down Expand Up @@ -170,9 +174,8 @@ test('insertIos(): no identifiable app launch method', async () => {
})

test('insertAndroid(): success', async () => {
type readdir = (path: string) => Promise<string[]>
const readdirMock = fs.readdir as unknown as jest.MockedFunction<readdir>
readdirMock.mockResolvedValue(['bugsnagreactnativeclitest'])
const globMock = glob as unknown as jest.MockedFunction<typeof glob>
globMock.mockImplementation((glob, opts, cb) => cb(null, ['bugsnagreactnativeclitest/MainApplication.java']))

const mainApplication = await loadFixture(path.join(__dirname, 'fixtures', 'MainApplication-before.java'))
const readFileMock = fs.readFile as jest.MockedFunction<typeof fs.readFile>
Expand All @@ -193,9 +196,8 @@ test('insertAndroid(): success', async () => {
})

test('insertAndroid(): success, tolerates some differences in source', async () => {
type readdir = (path: string) => Promise<string[]>
const readdirMock = fs.readdir as unknown as jest.MockedFunction<readdir>
readdirMock.mockResolvedValue(['bugsnagreactnativeclitest'])
const globMock = glob as unknown as jest.MockedFunction<typeof glob>
globMock.mockImplementation((glob, opts, cb) => cb(null, ['bugsnagreactnativeclitest/MainApplication.java']))

const mainApplication = await loadFixture(path.join(__dirname, 'fixtures', 'MainApplication-before-2.java'))
const readFileMock = fs.readFile as jest.MockedFunction<typeof fs.readFile>
Expand All @@ -216,9 +218,8 @@ test('insertAndroid(): success, tolerates some differences in source', async ()
})

test('insertAndroid(): already present', async () => {
type readdir = (path: string) => Promise<string[]>
const readdirMock = fs.readdir as unknown as jest.MockedFunction<readdir>
readdirMock.mockResolvedValue(['bugsnagreactnativeclitest'])
const globMock = glob as unknown as jest.MockedFunction<typeof glob>
globMock.mockImplementation((glob, opts, cb) => cb(null, ['bugsnagreactnativeclitest/MainApplication.java']))

const mainApplication = await loadFixture(path.join(__dirname, 'fixtures', 'MainApplication-after.java'))
const readFileMock = fs.readFile as jest.MockedFunction<typeof fs.readFile>
Expand All @@ -237,9 +238,8 @@ test('insertAndroid(): already present', async () => {
})

test('insertAndroid(): failure to locate file', async () => {
type readdir = (path: string) => Promise<string[]>
const readdirMock = fs.readdir as unknown as jest.MockedFunction<readdir>
readdirMock.mockResolvedValue(['bugsnagreactnativeclitest'])
const globMock = glob as unknown as jest.MockedFunction<typeof glob>
globMock.mockImplementation((glob, opts, cb) => cb(null, ['bugsnagreactnativeclitest/MainApplication.java']))

const readFileMock = fs.readFile as jest.MockedFunction<typeof fs.readFile>
readFileMock.mockRejectedValue(await generateNotFoundError())
Expand All @@ -257,9 +257,8 @@ test('insertAndroid(): failure to locate file', async () => {
})

test('insertAndroid(): failure to locate package directory', async () => {
type readdir = (path: string) => Promise<string[]>
const readdirMock = fs.readdir as unknown as jest.MockedFunction<readdir>
readdirMock.mockResolvedValue([])
const globMock = glob as unknown as jest.MockedFunction<typeof glob>
globMock.mockImplementation((glob, opts, cb) => cb(null, []))

const readFileMock = fs.readFile as jest.MockedFunction<typeof fs.readFile>
const writeFileMock = fs.writeFile as jest.MockedFunction<typeof fs.writeFile>
Expand All @@ -272,9 +271,8 @@ test('insertAndroid(): failure to locate package directory', async () => {
})

test('insertAndroid(): project directory error', async () => {
type readdir = (path: string) => Promise<string[]>
const readdirMock = fs.readdir as unknown as jest.MockedFunction<readdir>
readdirMock.mockRejectedValue(await generateNotFoundError())
const globMock = glob as unknown as jest.MockedFunction<typeof glob>
globMock.mockImplementation((glob, opts, cb) => cb(new Error('oh no'), []))

const readFileMock = fs.readFile as jest.MockedFunction<typeof fs.readFile>
const writeFileMock = fs.writeFile as jest.MockedFunction<typeof fs.writeFile>
Expand All @@ -283,13 +281,12 @@ test('insertAndroid(): project directory error', async () => {
expect(readFileMock).not.toHaveBeenCalled()
expect(writeFileMock).not.toHaveBeenCalled()

expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Failed to update "MainApplication.java" automatically.'))
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Failed to update "MainApplication.java" automatically.'))
})

test('insertAndroid(): no identifiable onCreate method', async () => {
type readdir = (path: string) => Promise<string[]>
const readdirMock = fs.readdir as unknown as jest.MockedFunction<readdir>
readdirMock.mockResolvedValue(['bugsnagreactnativeclitest'])
const globMock = glob as unknown as jest.MockedFunction<typeof glob>
globMock.mockImplementation((glob, opts, cb) => cb(null, ['bugsnagreactnativeclitest/MainApplication.java']))

const readFileMock = fs.readFile as jest.MockedFunction<typeof fs.readFile>
readFileMock.mockResolvedValue('not good java')
Expand Down
1 change: 1 addition & 0 deletions test/react-native-cli/features/fixtures/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ COPY . /app
WORKDIR /app

RUN npm i -g bugsnag-react-native-cli-*.tgz
RUN npm i
bengourley marked this conversation as resolved.
Show resolved Hide resolved

ENTRYPOINT ["/bin/sh"]
11 changes: 11 additions & 0 deletions test/react-native-cli/features/fixtures/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ services:
aliases:
- maze-runner

rn0_63_expo_ejected:
build:
context: rn0_63_expo_ejected
dockerfile: ../Dockerfile
environment:
DEBUG:
networks:
default:
aliases:
- maze-runner

networks:
default:
name: ${NETWORK_NAME:-react-native-cli-maze-runner}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore lockfiles as they can influence test runs
package-lock.json
yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
node_modules/
.expo/
npm-debug.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision
*.orig.*
web-build/

# macOS
.DS_Store

# @generated expo-cli sync-2138f1e3e130677ea10ea873f6d498e3890e677b
# The following patterns were generated by expo-cli

# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml
*.hprof

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore
!debug.keystore

# Bundle artifacts
*.jsbundle

# CocoaPods
/ios/Pods/

# Expo
.expo/
web-build/

# @end expo-cli
Loading