Open History to see the last commit
1import * as NodeFS from "node:fs";
2import * as NodeModule from "node:module";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import { afterEach, describe, expect, it } from "vite-plus/test";
6
7const require = NodeModule.createRequire(import.meta.url);
8const { compileModsAsync } = require("expo/config-plugins");
9const { getConfig } = require("expo/config");
10const withAndroidFbjniVersion = require("./withAndroidFbjniVersion.cjs");
11
12const catalog = NodeFS.readFileSync(
13 NodePath.join(require.resolve("react-native/package.json"), "..", "gradle", "libs.versions.toml"),
14 "utf8",
15);
16const shippedFbjniVersion = /^fbjni\s*=\s*"([^"]+)"/m.exec(catalog)?.[1];
17
18// The root build.gradle generated by Expo SDK 58.
19const ROOT_BUILD_GRADLE = `// Top-level build file where you can add configuration options common to all sub-projects/modules.
20
21buildscript {
22 repositories {
23 google()
24 mavenCentral()
25 }
26 dependencies {
27 classpath('com.android.tools.build:gradle')
28 classpath('com.facebook.react:react-native-gradle-plugin')
29 def kotlinVersion = findProperty('android.kotlinVersion')
30 classpath(kotlinVersion ? "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" : 'org.jetbrains.kotlin:kotlin-gradle-plugin')
31 }
32}
33
34allprojects {
35 repositories {
36 google()
37 mavenCentral()
38 maven { url 'https://www.jitpack.io' }
39 }
40}
41
42apply plugin: "expo-root-project"
43apply plugin: "com.facebook.react.rootproject"
44`;
45
46const projectRoots = [];
47
48afterEach(() => {
49 for (const root of projectRoots.splice(0)) NodeFS.rmSync(root, { recursive: true, force: true });
50});
51
52/** Runs the plugin through Expo's own prebuild mod pipeline against a real file. */
53async function prebuildRootGradle(fileName, contents, plugin = withAndroidFbjniVersion) {
54 const projectRoot = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "fbjni-plugin-"));
55 projectRoots.push(projectRoot);
56 NodeFS.mkdirSync(NodePath.join(projectRoot, "android"));
57 NodeFS.writeFileSync(NodePath.join(projectRoot, "android", fileName), contents);
58 await compileModsAsync(plugin({ name: "t3", slug: "t3" }), {
59 projectRoot,
60 platforms: ["android"],
61 });
62 return NodeFS.readFileSync(NodePath.join(projectRoot, "android", fileName), "utf8");
63}
64
65function expectProjectWideForce(result, language = "groovy") {
66 expect(shippedFbjniVersion).toBeDefined();
67 const coordinate = `com.facebook.fbjni:fbjni:${shippedFbjniVersion}`;
68 const force = language === "groovy" ? ` '${coordinate}'` : `("${coordinate}")`;
69 // Anchor the complete top-level block, including scope, at the end of the file.
70 const expected = `\nallprojects {\n configurations.all {\n resolutionStrategy.force${force}\n }\n}\n`;
71 expect(result.slice(-expected.length)).toBe(expected);
72}
73
74describe("withAndroidFbjniVersion", () => {
75 it("forces the React Native fbjni version for every Gradle project", async () => {
76 const result = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE);
77
78 expect(result.startsWith(ROOT_BUILD_GRADLE.trimEnd())).toBe(true);
79 expectProjectWideForce(result);
80 });
81
82 it("adds the force once when prebuild runs again", async () => {
83 const once = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE);
84 const twice = await prebuildRootGradle("build.gradle", once);
85
86 expect(twice).toBe(once);
87 });
88
89 it("writes Kotlin syntax into a Kotlin build script", async () => {
90 const result = await prebuildRootGradle("build.gradle.kts", "plugins {}\n");
91
92 expectProjectWideForce(result, "kt");
93 });
94
95 it("applies the force through the registered app config", async () => {
96 const { exp } = getConfig(NodePath.resolve(import.meta.dirname, ".."), {
97 isModdedConfig: true,
98 });
99 const projectBuildGradle = exp.mods?.android?.projectBuildGradle;
100 // Run the app's registered root-Gradle mods without its unrelated native-file mods.
101 const result = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE, (config) => ({
102 ...config,
103 mods: { android: projectBuildGradle ? { projectBuildGradle } : {} },
104 }));
105
106 expectProjectWideForce(result);
107 });
108});