Skip to content

Commit

Permalink
feat(detox): Add optional Test Butler setup in @config-plugins/detox
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmiles committed Nov 19, 2022
1 parent b862911 commit 9476995
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 79 deletions.
2 changes: 2 additions & 0 deletions packages/detox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The plugin provides props for extra customization. Every time you change the pro

- `skipProguard` (_boolean_): Disable adding proguard minification to the `app/build.gradle`. Defaults to `false`.
- `subdomains` (_string[] | '\*'_): Hostnames to add to the network security config. Pass `'*'` to allow all domains. Defaults to `['10.0.2.2', 'localhost']`.
- `includeTestButler` (_boolean_): Enable [Test Butler](https://github.com/linkedin/test-butler) library injection in `app/build.grade` and modifications to JUnit test runner. Defaults to `false`

`app.config.js`

Expand All @@ -73,6 +74,7 @@ export default {
{
skipProguard: false,
subdomains: ["10.0.2.2", "localhost"],
includeTestButler: false
},
],
],
Expand Down
14 changes: 14 additions & 0 deletions packages/detox/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"expo": {
"name": "@config-plugins/detox",
"slug": "@config-pluginsdetox",
"version": "4.0.0",
"description": "Config plugin to auto configure detox on prebuild",
"sdkVersion": "47.0.0",
"platforms": [
"ios",
"android",
"web"
]
}
}
6 changes: 6 additions & 0 deletions packages/detox/build/withDetox.d.ts

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

10 changes: 7 additions & 3 deletions packages/detox/build/withDetox.js

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

8 changes: 5 additions & 3 deletions packages/detox/build/withDetoxTestAppGradle.d.ts

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

63 changes: 48 additions & 15 deletions packages/detox/build/withDetoxTestAppGradle.js

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

2 changes: 1 addition & 1 deletion packages/detox/build/withDetoxTestClass.d.ts

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

34 changes: 20 additions & 14 deletions packages/detox/build/withDetoxTestClass.js

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

18 changes: 14 additions & 4 deletions packages/detox/src/withDetox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {

import withDetoxProjectGradle from "./withDetoxProjectGradle";
import withDetoxTestAppGradle from "./withDetoxTestAppGradle";
import { withDetoxTestClass } from "./withDetoxTestClass";
import withDetoxTestClass from "./withDetoxTestClass";
import withJUnitRunnerClass from "./withJUnitRunnerClass";
import withKotlinGradle from "./withKotlinGradle";
import {
withNetworkSecurityConfigManifest,
SubdomainsType,
} from "./withNetworkSecurityConfig";
import withProguardGradle from "./withProguardGradle";
import withTestButlerProbe from "./withTestButlerProbe";

const withDetox: ConfigPlugin<
{
Expand All @@ -30,27 +32,35 @@ const withDetox: ConfigPlugin<
* @default ['10.0.2.2', 'localhost'] // (Google emulators)
*/
subdomains?: SubdomainsType;
/**
* Enable Test Butler library injection in `app/build.grade` and modifications to JUnit test runner
*
* @default false
*/
includeTestButler?: boolean;
} | void
> = (config, { skipProguard, subdomains } = {}) => {
> = (config, { skipProguard, subdomains, includeTestButler } = {}) => {
return withPlugins(
config,
[
// 3.
withDetoxProjectGradle,
// 3.
withDetoxTestAppGradle,
withDetoxTestAppGradle(includeTestButler),
// 4.
[
withKotlinGradle,
// Minimum version of Kotlin required to work with expo packages in SDK 45
"1.6.10",
],
// 5.
withDetoxTestClass,
withDetoxTestClass(includeTestButler),
// 6.
[withNetworkSecurityConfigManifest, { subdomains }],
// 7.
!skipProguard && withProguardGradle,
includeTestButler && withTestButlerProbe,
includeTestButler && withJUnitRunnerClass,
].filter(Boolean) as ([ConfigPlugin, any] | ConfigPlugin)[]
);
};
Expand Down
74 changes: 56 additions & 18 deletions packages/detox/src/withDetoxTestAppGradle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigPlugin, withAppBuildGradle } from "@expo/config-plugins";
import assert from "node:assert";

/**
* [Step 3](https://github.com/wix/Detox/blob/master/docs/Introduction.Android.md#3-add-the-native-detox-dependency). Add the Native Detox dependency.
Expand All @@ -7,22 +8,41 @@ import { ConfigPlugin, withAppBuildGradle } from "@expo/config-plugins";
* 2. Add `testInstrumentationRunner` to the app/build.gradle
* @param config
*/
const withDetoxTestAppGradle: ConfigPlugin = (config) => {
return withAppBuildGradle(config, (config) => {
if (config.modResults.language === "groovy") {
config.modResults.contents = setGradleAndroidTestImplementation(
config.modResults.contents
);
config.modResults.contents = addDetoxDefaultConfigBlock(
config.modResults.contents
);
} else {
throw new Error(
"Cannot add Detox maven gradle because the project build.gradle is not groovy"
);
}
return config;
});
const withDetoxTestAppGradle: (includeTestButler?: boolean) => ConfigPlugin = (
includeTestButler
) => {
return (config) => {
const packageName = config.android?.package;
assert(packageName, "android.package must be defined");

const testRunnerClass = includeTestButler
? `${packageName}.DetoxTestAppJUnitRunner`
: "androidx.test.runner.AndroidJUnitRunner";

return withAppBuildGradle(config, (config) => {
if (config.modResults.language === "groovy") {
config.modResults.contents = setGradleAndroidTestImplementation(
config.modResults.contents
);
config.modResults.contents = addDetoxDefaultConfigBlock(
config.modResults.contents,
testRunnerClass
);

if (includeTestButler) {
config.modResults.contents =
setGradleAndroidTestImplementationForTestButler(
config.modResults.contents
);
}
} else {
throw new Error(
"Cannot add Detox maven gradle because the project build.gradle is not groovy"
);
}
return config;
});
};
};

export function setGradleAndroidTestImplementation(
Expand All @@ -39,7 +59,25 @@ export function setGradleAndroidTestImplementation(
);
}

export function addDetoxDefaultConfigBlock(buildGradle: string): string {
export function setGradleAndroidTestImplementationForTestButler(
buildGradle: string
): string {
const pattern =
/androidTestImplementation 'com\.linkedin\.testbutler:test-butler-library:2\.2\.1'/g;
if (buildGradle.match(pattern)) {
return buildGradle;
}
return buildGradle.replace(
/dependencies\s?{/,
`dependencies {
androidTestImplementation 'com.linkedin.testbutler:test-butler-library:2.2.1'`
);
}

export function addDetoxDefaultConfigBlock(
buildGradle: string,
testRunnerClass: string
): string {
const pattern = /detox-plugin-default-config/g;
if (buildGradle.match(pattern)) {
return buildGradle;
Expand All @@ -50,7 +88,7 @@ export function addDetoxDefaultConfigBlock(buildGradle: string): string {
`defaultConfig {
// detox-plugin-default-config
testBuildType System.getProperty('testBuildType', 'debug')
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'`
testInstrumentationRunner ${testRunnerClass}`
);
}

Expand Down
Loading

0 comments on commit 9476995

Please sign in to comment.