-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathgradle-wrapper.test.ts
More file actions
216 lines (184 loc) · 6.42 KB
/
gradle-wrapper.test.ts
File metadata and controls
216 lines (184 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { doesNotThrow, equal, fail, throws } from "node:assert/strict";
import * as nodefs from "node:fs";
import { afterEach, describe, it } from "node:test";
import { configureGradleWrapper } from "../../android/gradle-wrapper.js";
describe("configureGradleWrapper()", () => {
const args = process.argv;
afterEach(() => {
process.argv = args.slice();
delete process.env["RNTA_CONFIGURE_GRADLE_WRAPPER"];
});
it("only runs when targeting Android (unless disabled)", () => {
const returnEarly: typeof nodefs = {
...nodefs,
existsSync: () => {
fail("Expected to return early");
},
readFileSync: () => {
fail("Expected to return early");
},
writeFileSync: () => {
fail("Expected to return early");
},
};
doesNotThrow(() => configureGradleWrapper("android", returnEarly));
process.argv.push("run-android");
throws(() => configureGradleWrapper("android", returnEarly));
process.argv[process.argv.length - 1] = "build-android";
throws(() => configureGradleWrapper("android", returnEarly));
process.env["RNTA_CONFIGURE_GRADLE_WRAPPER"] = "0";
doesNotThrow(() => configureGradleWrapper("android", returnEarly));
});
it("returns early if Gradle wrapper cannot be found", () => {
const mockfs: typeof nodefs = {
...nodefs,
existsSync: () => false,
readFileSync: () => {
fail("Expected to return early");
},
writeFileSync: () => {
fail("Expected to return early");
},
};
process.argv.push("run-android");
doesNotThrow(() => configureGradleWrapper("android", mockfs));
});
it("returns early if Gradle wrapper cannot be read", (t) => {
const warnMock = t.mock.method(console, "warn", () => null);
const mockfs: typeof nodefs = {
...nodefs,
existsSync: () => true,
readFileSync: () => {
fail("Expected to return early");
},
writeFileSync: () => {
/* noop */
},
};
process.argv.push("run-android");
doesNotThrow(() => configureGradleWrapper("android", mockfs));
equal(warnMock.mock.callCount(), 1);
equal(
warnMock.mock.calls[0].arguments[1],
"Failed to determine Gradle version"
);
});
it("returns early if Gradle wrapper cannot be determined", () => {
let written = "";
const mockfs: typeof nodefs = {
...nodefs,
existsSync: () => true,
// @ts-expect-error Type 'string' is not assignable to type 'Buffer'
readFileSync: (p) => {
if (p.toString().endsWith("gradle-wrapper.properties")) {
return "";
}
fail(`Unexpected file read: ${p}`);
},
writeFileSync: (_, data) => {
// @ts-expect-error Type 'Uint8Array' is not assignable to type 'string'
written = data;
},
};
process.argv.push("run-android");
doesNotThrow(() => configureGradleWrapper("android", mockfs));
equal(written, "");
});
it("bumps Gradle if the version is too old/recent", (t) => {
const warnMock = t.mock.method(console, "warn", () => null);
let written = "";
const mockfs = (
gradleVersion: string,
rnVersion: string
): typeof nodefs => ({
...nodefs,
existsSync: () => true,
// @ts-expect-error Type 'string' is not assignable to type 'Buffer'
readFileSync: (p) => {
if (p.toString().endsWith("gradle-wrapper.properties")) {
return `gradle-${gradleVersion}-bin.zip`;
} else if (p.toString().endsWith("package.json")) {
return JSON.stringify({ name: "react-native", version: rnVersion });
}
fail(`Unexpected file read: ${p}`);
},
writeFileSync: (_, data) => {
// @ts-expect-error Type 'Uint8Array' is not assignable to type 'string'
written = data;
},
});
process.argv.push("run-android");
const cases = [
["8.13", "0.80.0", "gradle-8.14-bin.zip"],
["8.12", "0.79.0", "gradle-8.13-bin.zip"],
["8.11.1", "0.78.0", "gradle-8.12-bin.zip"],
["8.9", "0.76.0", "gradle-8.11.1-bin.zip"],
["8.9", "0.75.0", "gradle-8.8-bin.zip"],
["8.7", "0.75.0", "gradle-8.8-bin.zip"],
["8.9", "0.74.0", "gradle-8.8-bin.zip"],
["8.5", "0.74.0", "gradle-8.6-bin.zip"],
["8.9", "0.73.0", "gradle-8.8-bin.zip"],
["8.2.1", "0.73.0", "gradle-8.3-bin.zip"],
["8.3", "0.72.0", "gradle-8.2.1-bin.zip"],
["8.1", "0.72.0", "gradle-8.1.1-bin.zip"],
["8.0", "0.71.0", "gradle-7.6.4-bin.zip"],
["7.5", "0.71.0", "gradle-7.6.4-bin.zip"],
];
for (const [gradleVersion, rnVersion, expected] of cases) {
written = "";
const fs = mockfs(gradleVersion, rnVersion);
doesNotThrow(() => configureGradleWrapper("android", fs));
equal(warnMock.mock.callCount(), 1);
equal(
warnMock.mock.calls[0].arguments[1],
`Setting Gradle version ${expected.substring("gradle-".length, expected.length - "-bin.zip".length)}`
);
equal(written, expected);
warnMock.mock.resetCalls();
}
});
it("skips writing if Gradle version is recent enough", () => {
let written = "";
const mockfs = (
gradleVersion: string,
rnVersion: string
): typeof nodefs => ({
...nodefs,
existsSync: () => true,
// @ts-expect-error Type 'string' is not assignable to type 'Buffer'
readFileSync: (p) => {
if (p.toString().endsWith("gradle-wrapper.properties")) {
return `gradle-${gradleVersion}-bin.zip`;
} else if (p.toString().endsWith("package.json")) {
return JSON.stringify({ name: "react-native", version: rnVersion });
}
fail(`Unexpected file read: ${p}`);
},
writeFileSync: (_, data) => {
// @ts-expect-error Type 'Uint8Array' is not assignable to type 'string'
written = data;
},
});
process.argv.push("run-android");
const cases = [
["8.14", "0.80.0"],
["8.13", "0.79.0"],
["8.12", "0.78.0"],
["8.11.1", "0.76.0"],
["8.8", "0.75.0"],
["8.8", "0.74.0"],
["8.8", "0.73.0"],
["8.2", "0.72.0"],
["8.6", "0.74.0"],
["8.3", "0.73.0"],
["8.1.1", "0.72.0"],
["7.6.4", "0.71.0"],
["7.5.1", "0.71.0"],
];
for (const [gradleVersion, rnVersion] of cases) {
const fs = mockfs(gradleVersion, rnVersion);
doesNotThrow(() => configureGradleWrapper("android", fs));
equal(written, "");
}
});
});