Skip to content

Commit

Permalink
[Tests] Use env in scripts and update docs (#1788)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidubov committed Feb 10, 2020
1 parent f95194f commit 352995d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
36 changes: 17 additions & 19 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ Follow these steps to test your modifications to the plugin manually:

First, make sure you have installed the dependencies for the plugin by following the steps above.

Then, make sure you have installed `gulp`.
Then, make sure you have installed `react-native-cli`.

```
npm install -g gulp
npm install -g react-native-cli
```

To run Android tests, make sure you have `sdk\tools` and `sdk\platform-tools` in your PATH.
To run Android tests, make sure you have `sdk\tools`, `sdk\emulator` and `sdk\platform-tools` in your PATH.

To run iOS tests, make sure you've installed CocoaPods and have `.gem/bin` in your PATH.

Expand All @@ -62,43 +62,41 @@ The tests first build the app.

They then check if the required emulators are currently running.

If an Android emulator is not running, it attempts to boot an Android emulator named `emulator`. You can specify an emulator by adding `--androidemu yourEmulatorNameHere` as a command line option to the gulp task.
If an Android emulator is not running, it attempts to boot the latest Android emulator. You can specify an emulator by adding env variable `ANDROID_EMU=yourEmulatorNameHere` to the npm command. For example: `ANDROID_EMU=yourEmulatorNameHere npm run test:android`.

If an iOS simulator is not running, it attempts to boot the latest iOS iPhone simulator. You can specify a simulator by adding `--iosemu yourSimulatorNameHere` as a command line option to the gulp task.
If an iOS simulator is not running, it attempts to boot the latest iOS iPhone simulator. You can specify a simulator by adding env variable `IOS_EMU=yourSimulatorNameHere` to the npm command. For example: `IOS_EMU="iPhone 8 (0567DFF8-329E-41A3-BD6D-E48E9DD5EF39)" npm run test:ios`.

If all the required emulators are not running and the tests fail to boot them, the tests will fail.

If you would like the tests to always restart the necessary emulators (killing them if they are currently running), add a `--clean` flag to the command.
If you would like the tests to always restart the necessary emulators (killing them if they are currently running), setup a env variable `CLEAN=true` to the command. For example: `CLEAN=true npm run test`.

The desired unit tests are then run.

If you would like to skip building, add a `-fast` to the end of the command you'd like to run. For example, `gulp test-ios` becomes `gulp test-ios-fast`.
If you would like to skip building, add a `:fast` in the command you'd like to run. For example, `npm run test:ios` becomes `npm run test:fast:ios` or `npm run test:android` becomes `npm run test:fast:android`.

There is a both a full unit test suite and a "core" set of unit tests that you may run. If you would like to run only the core tests, add a `--core` flag to the command.
There is a both a full unit test suite and a "core" set of unit tests that you may run. If you would like to run only the core tests, setup a env variable `CORE=true` to the command. For example: `CORE=true npm run test:android`.

If you would like to pull the plugin from NPM rather than running the tests on the local version, add a `--npm` flag to the command.

If you add a `--report` flag to the command, the mocha reporter outputs individual results files for each platform. These are `./test_android.xml`, `./test-ios-ui.xml`, and `./test-ios-wk.xml`.
If you would like to pull the plugin from NPM rather than running the tests on the local version, setup a env variable `NPM=true` to the command. For example: `NPM=true npm run test:ios`.

#### Default

To run all of the unit tests on Android and iOS:
```
gulp test
npm run test
```

#### iOS

To run all of the unit tests on iOS:
```
gulp test-ios
npm run test:ios
```

#### Android

To run all of the unit tests on Android:
```
gulp test-android
npm run test:android
```

#### More examples
Expand All @@ -110,27 +108,27 @@ android, ios

To run the core unit tests on Android:
```
gulp test-android --core
CORE=true npm run test:android
```

To run all of the unit tests on iOS and pull the plugin from NPM:
```
gulp test-ios --npm
NPM=true npm run test:ios
```

To run all of the unit tests on Android and iOS without building first:
```
gulp test-fast
npm run test:fast
```

To run all of the unit tests on iOS and restart the emulators:
```
gulp test-ios --clean
CLEAN=true npm run test:ios
```

To run the core unit tests on Android and pull the plugin from NPM:
```
gulp test-android --core --npm
NPM=true CORE=true npm run test:android
```

...and so on!
13 changes: 5 additions & 8 deletions code-push-plugin-testing-framework/script/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var Android = (function () {
*/
Android.prototype.getServerUrl = function () {
if (!this.serverUrl)
this.serverUrl = testUtil_1.TestUtil.readMochaCommandLineOption(Android.ANDROID_SERVER_URL_OPTION_NAME, Android.DEFAULT_ANDROID_SERVER_URL);
this.serverUrl = process.env.ANDROID_SERVER ? process.env.ANDROID_SERVER : Android.DEFAULT_ANDROID_SERVER_URL;
return this.serverUrl;
};
/**
Expand All @@ -43,7 +43,6 @@ var Android = (function () {
Android.prototype.getDefaultDeploymentKey = function () {
return "mock-android-deployment-key";
};
Android.ANDROID_SERVER_URL_OPTION_NAME = "--androidserver";
Android.DEFAULT_ANDROID_SERVER_URL = "http://10.0.2.2:3001";
return Android;
}());
Expand Down Expand Up @@ -73,7 +72,8 @@ var IOS = (function () {
*/
IOS.prototype.getServerUrl = function () {
if (!this.serverUrl)
this.serverUrl = testUtil_1.TestUtil.readMochaCommandLineOption(IOS.IOS_SERVER_URL_OPTION_NAME, IOS.DEFAULT_IOS_SERVER_URL);
this.serverUrl = process.env.IOS_SERVER ? process.env.IOS_SERVER : IOS.DEFAULT_IOS_SERVER_URL;

return this.serverUrl;
};
/**
Expand All @@ -88,7 +88,6 @@ var IOS = (function () {
IOS.prototype.getDefaultDeploymentKey = function () {
return "mock-ios-deployment-key";
};
IOS.IOS_SERVER_URL_OPTION_NAME = "--iosserver";
IOS.DEFAULT_IOS_SERVER_URL = "http://127.0.0.1:3000";
return IOS;
}());
Expand Down Expand Up @@ -172,7 +171,7 @@ var AndroidEmulatorManager = (function () {
return Q(this.targetEmulator);
else {
const deferred = Q.defer();
const targetAndroidEmulator = testUtil_1.TestUtil.readMochaCommandLineOption(AndroidEmulatorManager.ANDROID_EMULATOR_OPTION_NAME);
const targetAndroidEmulator = process.env.ANDROID_EMU;
if (!targetAndroidEmulator) {
// If no Android simulator is specified, get the most recent Android simulator to run tests on.
testUtil_1.TestUtil.getProcessOutput("emulator -list-avds", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true })
Expand Down Expand Up @@ -280,7 +279,6 @@ var AndroidEmulatorManager = (function () {
AndroidEmulatorManager.prototype.uninstallApplication = function (appId) {
return commandWithCheckAppExistence("adb uninstall", appId);
};
AndroidEmulatorManager.ANDROID_EMULATOR_OPTION_NAME = "--androidemu";
return AndroidEmulatorManager;
}());
exports.AndroidEmulatorManager = AndroidEmulatorManager;
Expand All @@ -296,7 +294,7 @@ var IOSEmulatorManager = (function () {
return Q(this.targetEmulator);
else {
let deferred = Q.defer();
let targetIOSEmulator = testUtil_1.TestUtil.readMochaCommandLineOption(IOSEmulatorManager.IOS_EMULATOR_OPTION_NAME);
let targetIOSEmulator = process.env.IOS_EMU;
if (!targetIOSEmulator) {
// If no iOS simulator is specified, get the most recent iOS simulator to run tests on.
testUtil_1.TestUtil.getProcessOutput("xcrun simctl list", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true })
Expand Down Expand Up @@ -395,7 +393,6 @@ var IOSEmulatorManager = (function () {
IOSEmulatorManager.prototype.uninstallApplication = function (appId) {
return testUtil_1.TestUtil.getProcessOutput("xcrun simctl uninstall booted " + appId).then(function () { return null; });
};
IOSEmulatorManager.IOS_EMULATOR_OPTION_NAME = "--iosemu";
return IOSEmulatorManager;
}());
exports.IOSEmulatorManager = IOSEmulatorManager;
Expand Down
15 changes: 5 additions & 10 deletions code-push-plugin-testing-framework/script/testConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@ var TestUtil_1 = require("./TestUtil");
// Configuration variables.
// What plugin to use, what project directories to use, etc.
// COMMAND LINE OPTION NAMES, FLAGS, AND DEFAULTS
var TEST_RUN_DIRECTORY_OPTION_NAME = "--test-directory";
var DEFAULT_TEST_RUN_DIRECTORY = path.join(os.tmpdir(), TestUtil_1.TestUtil.getPluginName(), "test-run");
var TEST_UPDATES_DIRECTORY_OPTION_NAME = "--updates-directory";
var DEFAULT_UPDATES_DIRECTORY = path.join(os.tmpdir(), TestUtil_1.TestUtil.getPluginName(), "updates");
var CORE_TESTS_ONLY_FLAG_NAME = "--core";
var PULL_FROM_NPM_FLAG_NAME = "--npm";
var DEFAULT_PLUGIN_PATH = path.join(__dirname, "../..");
var NPM_PLUGIN_PATH = TestUtil_1.TestUtil.getPluginName();
var SETUP_FLAG_NAME = "--setup";
var RESTART_EMULATORS_FLAG_NAME = "--clean";
var DEFAULT_PLUGIN_TGZ_NAME = TestUtil_1.TestUtil.getPluginName() + "-" + TestUtil_1.TestUtil.getPluginVersion() + ".tgz";
// CONST VARIABLES
exports.TestAppName = "TestCodePush";
exports.TestNamespace = "com.testcodepush";
exports.AcquisitionSDKPluginName = "code-push";
exports.templatePath = path.join(__dirname, "../../test/template");
exports.thisPluginInstallString = TestUtil_1.TestUtil.readMochaCommandLineFlag(PULL_FROM_NPM_FLAG_NAME) ? `npm install ${NPM_PLUGIN_PATH}` : `npm pack ${DEFAULT_PLUGIN_PATH} && npm install ${DEFAULT_PLUGIN_TGZ_NAME}`;
exports.testRunDirectory = TestUtil_1.TestUtil.readMochaCommandLineOption(TEST_RUN_DIRECTORY_OPTION_NAME, DEFAULT_TEST_RUN_DIRECTORY);
exports.updatesDirectory = TestUtil_1.TestUtil.readMochaCommandLineOption(TEST_UPDATES_DIRECTORY_OPTION_NAME, DEFAULT_UPDATES_DIRECTORY);
exports.onlyRunCoreTests = TestUtil_1.TestUtil.readMochaCommandLineFlag(CORE_TESTS_ONLY_FLAG_NAME);
exports.thisPluginInstallString = TestUtil_1.TestUtil.resolveBooleanVariables(process.env.NPM) ? `npm install ${NPM_PLUGIN_PATH}` : `npm pack ${DEFAULT_PLUGIN_PATH} && npm install ${DEFAULT_PLUGIN_TGZ_NAME}`;
exports.testRunDirectory = process.env.RUN_DIR ? process.env.RUN_DIR: DEFAULT_TEST_RUN_DIRECTORY;
exports.updatesDirectory = process.env.UPDATE_DIR ? process.env.UPDATE_DIR : DEFAULT_UPDATES_DIRECTORY;
exports.onlyRunCoreTests = TestUtil_1.TestUtil.resolveBooleanVariables(process.env.CORE);
exports.shouldSetup = TestUtil_1.TestUtil.readMochaCommandLineFlag(SETUP_FLAG_NAME);
exports.restartEmulators = TestUtil_1.TestUtil.readMochaCommandLineFlag(RESTART_EMULATORS_FLAG_NAME);
exports.restartEmulators = TestUtil_1.TestUtil.resolveBooleanVariables(process.env.CLEAN);
11 changes: 11 additions & 0 deletions code-push-plugin-testing-framework/script/testUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ var TestUtil = (function () {
archive.finalize();
return deferred.promise;
};

/**
* Check that boolean environment variable string is 'true.
*/
TestUtil.resolveBooleanVariables = function(variable) {
if (variable) {
return variable.toLowerCase() === 'true';
}

return false;
}
//// Placeholders
// Used in the template to represent data that needs to be added by the testing framework at runtime.
TestUtil.ANDROID_KEY_PLACEHOLDER = "CODE_PUSH_ANDROID_DEPLOYMENT_KEY";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ declare module 'code-push-plugin-testing-framework/script/platform' {
* Runs when the flag is present, doesn't run otherwise.
*/
getCommandLineFlagName(): string;
private static ANDROID_SERVER_URL_OPTION_NAME;
private static DEFAULT_ANDROID_SERVER_URL;
/**
* Gets the server url used for testing.
Expand Down Expand Up @@ -110,7 +109,6 @@ declare module 'code-push-plugin-testing-framework/script/platform' {
* Runs when the flag is present, doesn't run otherwise.
*/
getCommandLineFlagName(): string;
private static IOS_SERVER_URL_OPTION_NAME;
private static DEFAULT_IOS_SERVER_URL;
/**
* Gets the server url used for testing.
Expand All @@ -126,7 +124,6 @@ declare module 'code-push-plugin-testing-framework/script/platform' {
getDefaultDeploymentKey(): string;
}
export class AndroidEmulatorManager implements IEmulatorManager {
private static ANDROID_EMULATOR_OPTION_NAME;
private targetEmulator;
/**
* Returns the target emulator, which is specified through the command line.
Expand Down Expand Up @@ -162,7 +159,6 @@ declare module 'code-push-plugin-testing-framework/script/platform' {
uninstallApplication(appId: string): Q.Promise<void>;
}
export class IOSEmulatorManager implements IEmulatorManager {
private static IOS_EMULATOR_OPTION_NAME;
private targetEmulator;
/**
* Returns the target emulator, which is specified through the command line.
Expand Down

0 comments on commit 352995d

Please sign in to comment.