Skip to content

Commit e857988

Browse files
committed
Add tests for tool change
1 parent c8a73ba commit e857988

File tree

1 file changed

+109
-10
lines changed

1 file changed

+109
-10
lines changed

script/tool/test/gradle_check_command_test.dart

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const String _defaultFakeNamespace = 'dev.flutter.foo';
1717
void main() {
1818
late CommandRunner<void> runner;
1919
late Directory packagesDir;
20+
const String javaIncompatabilityIndicator =
21+
'build.gradle(.kts) must set an explicit Java compatibility version.';
2022

2123
setUp(() {
2224
final GitDir gitDir;
@@ -46,6 +48,9 @@ void main() {
4648
bool useDeprecatedCompileSdkVersion = false,
4749
bool usePropertyAssignment = true,
4850
String compileSdk = '36',
51+
bool includeKotlinOptions = true,
52+
bool commentKotlinOptions = false,
53+
bool useDeprecatedJvmTarget = false,
4954
}) {
5055
final File buildGradle = package
5156
.platformDirectory(FlutterPlatform.android)
@@ -69,11 +74,17 @@ java {
6974
7075
''';
7176
final String sourceCompat =
72-
'${commentSourceLanguage ? '// ' : ''}sourceCompatibility = JavaVersion.VERSION_11';
77+
'${commentSourceLanguage ? '// ' : ''}sourceCompatibility = JavaVersion.VERSION_17';
7378
final String targetCompat =
74-
'${commentSourceLanguage ? '// ' : ''}targetCompatibility = JavaVersion.VERSION_11';
79+
'${commentSourceLanguage ? '// ' : ''}targetCompatibility = JavaVersion.VERSION_17';
7580
final String namespace =
7681
" ${commentNamespace ? '// ' : ''}namespace = '$_defaultFakeNamespace'";
82+
final String jvmTarget =
83+
useDeprecatedJvmTarget ? '17' : 'JavaVersion.VERSION_17.toString()';
84+
final String kotlinConfig = '''
85+
${commentKotlinOptions ? '//' : ''}kotlinOptions {
86+
${commentKotlinOptions ? '//' : ''}jvmTarget = $jvmTarget
87+
${commentKotlinOptions ? '//' : ''}}''';
7788

7889
buildGradle.writeAsStringSync('''
7990
group 'dev.flutter.plugins.fake'
@@ -101,6 +112,7 @@ ${warningsConfigured ? warningConfig : ''}
101112
${includeSourceCompat ? sourceCompat : ''}
102113
${includeTargetCompat ? targetCompat : ''}
103114
}
115+
${includeKotlinOptions ? kotlinConfig : ''}
104116
testOptions {
105117
unitTests.includeAndroidResources = true
106118
}
@@ -351,8 +363,7 @@ dependencies {
351363
expect(
352364
output,
353365
containsAllInOrder(<Matcher>[
354-
contains(
355-
'build.gradle must set an explicit Java compatibility version.'),
366+
contains(javaIncompatabilityIndicator),
356367
]),
357368
);
358369
});
@@ -375,8 +386,7 @@ dependencies {
375386
expect(
376387
output,
377388
containsAllInOrder(<Matcher>[
378-
contains(
379-
'build.gradle must set an explicit Java compatibility version.'),
389+
contains(javaIncompatabilityIndicator),
380390
]),
381391
);
382392
});
@@ -457,8 +467,7 @@ dependencies {
457467
expect(
458468
output,
459469
containsAllInOrder(<Matcher>[
460-
contains(
461-
'build.gradle must set an explicit Java compatibility version.'),
470+
contains(javaIncompatabilityIndicator),
462471
]),
463472
);
464473
});
@@ -480,8 +489,7 @@ dependencies {
480489
expect(
481490
output,
482491
containsAllInOrder(<Matcher>[
483-
contains(
484-
'build.gradle must set an explicit Java compatibility version.'),
492+
contains(javaIncompatabilityIndicator),
485493
]),
486494
);
487495
});
@@ -1162,4 +1170,95 @@ dependencies {
11621170
);
11631171
});
11641172
});
1173+
1174+
group('kotlinOptions check', () {
1175+
test('passes when kotlin options are specified', () async {
1176+
final RepositoryPackage package =
1177+
createFakePlugin('a_plugin', packagesDir, examples: <String>[]);
1178+
writeFakePluginBuildGradle(
1179+
package,
1180+
includeLanguageVersion: true,
1181+
// ignore: avoid_redundant_argument_values ensure codepath is tested if defaults change.
1182+
includeKotlinOptions: true,
1183+
);
1184+
writeFakeManifest(package);
1185+
1186+
final List<String> output =
1187+
await runCapturingPrint(runner, <String>['gradle-check']);
1188+
1189+
expect(
1190+
output,
1191+
containsAllInOrder(<Matcher>[
1192+
contains('Validating android/build.gradle'),
1193+
]),
1194+
);
1195+
});
1196+
1197+
test('passes when kotlin options are not specified', () async {
1198+
final RepositoryPackage package =
1199+
createFakePlugin('a_plugin', packagesDir, examples: <String>[]);
1200+
writeFakePluginBuildGradle(
1201+
package,
1202+
includeLanguageVersion: true,
1203+
includeKotlinOptions: false,
1204+
);
1205+
writeFakeManifest(package);
1206+
1207+
final List<String> output =
1208+
await runCapturingPrint(runner, <String>['gradle-check']);
1209+
1210+
expect(
1211+
output,
1212+
containsAllInOrder(<Matcher>[
1213+
contains('Validating android/build.gradle'),
1214+
]),
1215+
);
1216+
});
1217+
1218+
test('passes when kotlin options commented out', () async {
1219+
final RepositoryPackage package =
1220+
createFakePlugin('a_plugin', packagesDir, examples: <String>[]);
1221+
writeFakePluginBuildGradle(
1222+
package,
1223+
includeLanguageVersion: true,
1224+
commentKotlinOptions: true,
1225+
);
1226+
writeFakeManifest(package);
1227+
1228+
final List<String> output =
1229+
await runCapturingPrint(runner, <String>['gradle-check']);
1230+
1231+
expect(
1232+
output,
1233+
containsAllInOrder(<Matcher>[
1234+
contains('Validating android/build.gradle'),
1235+
]),
1236+
);
1237+
});
1238+
1239+
test('fails when kotlin options uses string jvm version', () async {
1240+
final RepositoryPackage package =
1241+
createFakePlugin('a_plugin', packagesDir, examples: <String>[]);
1242+
writeFakePluginBuildGradle(
1243+
package,
1244+
includeLanguageVersion: true,
1245+
useDeprecatedJvmTarget: true,
1246+
);
1247+
writeFakeManifest(package);
1248+
1249+
Error? commandError;
1250+
final List<String> output = await runCapturingPrint(
1251+
runner, <String>['gradle-check'], errorHandler: (Error e) {
1252+
commandError = e;
1253+
});
1254+
1255+
expect(commandError, isA<ToolExit>());
1256+
expect(
1257+
output,
1258+
containsAllInOrder(<Matcher>[
1259+
contains('build.gradle(.kts) sets jvmTarget then it must use JavaVersion syntax'),
1260+
]),
1261+
);
1262+
});
1263+
});
11651264
}

0 commit comments

Comments
 (0)