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

Adapt Android RN test fixture to work with BitBar #1957

Merged
merged 25 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d3e8e2a
Add naive start of config file implementation
Cawllec Apr 27, 2023
965ed4f
Move implementation into kotlin for simplicity
Cawllec Apr 27, 2023
992ced3
Slight refactor for testing
Cawllec Apr 27, 2023
039dea8
Cut pipeline right down for testing
Cawllec Apr 27, 2023
9b65c44
Add missing imports and variables
Cawllec Apr 27, 2023
a16e8f2
Work around missing references
Cawllec Apr 28, 2023
a06848e
Add log import
Cawllec Apr 28, 2023
a0bda9b
Fix syntax error
Cawllec Apr 28, 2023
10d1931
Remove invalid log
Cawllec Apr 28, 2023
0d123fb
Add debug log
Cawllec Apr 28, 2023
7d8b85d
Add more debugging
Cawllec May 2, 2023
d029426
Add more debugging
Cawllec May 2, 2023
acbe0fe
Use proper async await to return maze-runner address
Cawllec May 2, 2023
509c2fb
Naive implementation of cocoa file reading
Cawllec May 2, 2023
65a2637
More debugging
Cawllec May 2, 2023
96e81f9
Don't pass address back up to JS
Cawllec May 4, 2023
bc1fba8
Remove superfluous function
Cawllec May 4, 2023
a754403
Remove invalid import
Cawllec May 4, 2023
fc30da3
Fix redeclaration error
Cawllec May 4, 2023
9c3b0d3
Add general bridging header import
Cawllec May 4, 2023
521fd6f
Add separate interface for config loader
Cawllec May 4, 2023
dc63d5e
Attempt to use seperate bridging header location
Cawllec May 5, 2023
f01e032
Remove config reading from iOS RN test fixtures due to build issues
Cawllec May 10, 2023
3e1b604
Add feedback and extra guards against missing endpoints
Cawllec May 10, 2023
3cdd0df
Revert changes to buildkite pipeline
Cawllec May 10, 2023
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
16 changes: 10 additions & 6 deletions test/react-native/features/fixtures/app/scenario_js/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export default class App extends Component {
currentScenario: '',
scenarioMetaData: '',
apiKey: '12312312312312312312312312312312',
notifyEndpoint: 'http://bs-local.com:9339/notify',
sessionsEndpoint: 'http://bs-local.com:9339/sessions'
notifyEndpoint: '',
sessionsEndpoint: ''
}
}

Expand All @@ -27,14 +27,18 @@ export default class App extends Component {
}

getConfiguration = () => {
return {
var config = {
apiKey: this.state.apiKey,
endpoints: {
autoTrackSessions: false
}

if (this.state.notifyEndpoint && this.state.sessionsEndpoint) {
config.endpoints = {
notify: this.state.notifyEndpoint,
sessions: this.state.sessionsEndpoint
},
autoTrackSessions: false
}
}
return config
}

setScenario = newScenario => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ BugsnagConfiguration *createConfiguration(NSDictionary * options);

@end


#endif /* BugsnagModule_h */
17 changes: 14 additions & 3 deletions test/react-native/features/fixtures/ios-module/BugsnagModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,21 @@ @implementation BugsnagModule
NSLog(@"key: %@, value: %@ \n", key, [options objectForKey:key]);
}
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:options[@"apiKey"]];
NSDictionary *endpointsIn = options[@"endpoints"];
NSString *notifyEndpoint = endpointsIn[@"notify"];
NSString *sessionsEndpoint = endpointsIn[@"sessions"];
NSString *notifyEndpoint;
NSString *sessionsEndpoint;
if (options[@"endpoints"] != nil && options[@"endpoints"][@"notify"] != nil && options[@"endpoints"][@"sessions"] != nil) {
NSDictionary *endpointsIn = options[@"endpoints"];
notifyEndpoint = endpointsIn[@"notify"];
sessionsEndpoint = endpointsIn[@"sessions"];
} else {
NSString *baseAddress = @"bs-local.com:9339";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be replaced with the file loading logic in the below swift file once it can be compiled correctly

notifyEndpoint = [NSString stringWithFormat:@"http://%@/notify", baseAddress];
sessionsEndpoint = [NSString stringWithFormat:@"http://%@/sessions", baseAddress];
}
NSLog(@"Notify endpoint set to: %@\n", notifyEndpoint);
NSLog(@"Sessions endpoint set to: %@\n", sessionsEndpoint);
BugsnagEndpointConfiguration *endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:notifyEndpoint sessions:sessionsEndpoint];

[config setEndpoints:endpoints];
[config setAutoTrackSessions:[[options objectForKey:@"autoTrackSessions"]boolValue]];
config.enabledErrorTypes.ooms = NO; // Set by default, will add an override as required
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// ConfigFileReader.swift
// reactnative
//
// Created by Alex Moinet on 04/05/2023.
// Copyright © 2023 Facebook. All rights reserved.
//

import Foundation

class FixtureConfig: Codable {
var maze_address: String
}

class ConfigFileReader:NSObject {
func loadMazeRunnerAddress() -> String {
let bsAddress = "http://bs-local.com:9339"

// Only iOS 12 and above will run on BitBar for now
if #available(iOS 12.0, *) {} else {
return bsAddress;
}

for _ in 1...60 {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

NSLog("Reading Maze Runner address from fixture_config.json")
do {
let fileUrl = URL(fileURLWithPath: "fixture_config",
relativeTo: documentsUrl).appendingPathExtension("json")
let savedData = try Data(contentsOf: fileUrl)
if let contents = String(data: savedData, encoding: .utf8) {
let decoder = JSONDecoder()
let jsonData = contents.data(using: .utf8)
let config = try decoder.decode(FixtureConfig.self, from: jsonData!)
let address = "http://" + config.maze_address
NSLog("Using Maze Runner address: " + address)
return address
}
}
catch let error as NSError {
NSLog("Failed to read fixture_config.json: \(error)")
}
NSLog("Waiting for fixture_config.json to appear")
sleep(1)
}

NSLog("Unable to read from fixture_config.json, defaulting to BrowserStack environment")
return bsAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ private Configuration createConfiguration(ReadableMap options) {
Configuration config = new Configuration(options.getString("apiKey"));
config.setAutoTrackSessions(options.getBoolean("autoTrackSessions"));

if (options.hasKey("endpoint")) {
config.setEndpoints(new EndpointConfiguration(options.getString("endpoint"), options.getString("endpoint")));
}
else if (options.hasKey("endpoints")) {
if (options.hasKey("endpoints")) {
ReadableMap endpoints = options.getMap("endpoints");
config.setEndpoints(new EndpointConfiguration(endpoints.getString("notify"), endpoints.getString("sessions")));
} else {
ConfigFileReader configReader = new ConfigFileReader();
String mazeAddress = configReader.getMazeRunnerAddress(reactContext);
String notifyEndpoint = "http://" + mazeAddress + "/notify";
String sessionEndpoint = "http://" + mazeAddress + "/sessions";
config.setEndpoints(new EndpointConfiguration(notifyEndpoint, sessionEndpoint));
}

if (options.hasKey("appVersion")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.reactnative.module

import android.content.Context
import android.util.Log
import org.json.JSONObject
import java.io.File
import java.io.IOException

const val CONFIG_FILE_TIMEOUT = 5000

class ConfigFileReader {

fun getMazeRunnerAddress(context: Context): String {
val externalFilesDir = context.getExternalFilesDir(null)
val configFile = File(externalFilesDir, "fixture_config.json")
var mazeAddress: String? = null
Log.i("Bugsnag", "Attempting to read Maze Runner address from config file ${configFile.path}")

// Poll for the fixture config file
val pollEnd = System.currentTimeMillis() + CONFIG_FILE_TIMEOUT
while (System.currentTimeMillis() < pollEnd) {
if (configFile.exists()) {
val fileContents = configFile.readText()
val fixtureConfig = runCatching { JSONObject(fileContents) }.getOrNull()
mazeAddress = getStringSafely(fixtureConfig, "maze_address")
if (!mazeAddress.isNullOrBlank()) {
Log.i("Bugsnag", "Maze Runner address set from config file: $mazeAddress")
break
}
}

Thread.sleep(250)
}
if (mazeAddress.isNullOrBlank()) {
Log.i("Bugsnag", "Failed to read Maze Runner address from config file, reverting to legacy address")
mazeAddress = "bs-local.com:9339"
}
return mazeAddress
}

private fun getStringSafely(jsonObject: JSONObject?, key: String): String {
return jsonObject?.optString(key) ?: ""
}

}