Skip to content
Merged
Prev Previous commit
Next Next commit
passing tests
  • Loading branch information
reidbaker committed Aug 3, 2023
commit 2519427b5b15616f24e74394d89647412b3fc298
22 changes: 15 additions & 7 deletions script/tool/lib/src/gradle_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,23 @@ class GradleCheckCommand extends PackageLoopingCommand {
/// Required in root gradle file.
bool _validateArtifactHubUsage(
RepositoryPackage example, List<String> gradleLines) {
final String keyVariable = 'artifactRepoKey';
const String keyVariable = 'artifactRepoKey';
final RegExp keyPresentRegex =
RegExp("$keyVariable\\s+=\\s+'ARTIFACT_HUB_REPOSITORY'");

final RegExp keyReadRegex = RegExp('if\\s+(System.getenv().containsKey($keyVariable))');
final RegExp keyUsedRegex = RegExp('maven\\s+{\\s+url\\s+System.getenv($keyVariable)\\s+}');
final RegExp keyReadRegex =
RegExp('if\\s+\(System.getenv\(\).containsKey\($keyVariable\)\)');
final RegExp keyUsedRegex =
RegExp('maven\\s+{\\s+url\\s+System.getenv($keyVariable)\\s+}');

final bool keyPresent = gradleLines.any((String line) => keyPresentRegex.hasMatch(line));
final bool keyRead = gradleLines.any((String line) => keyReadRegex.hasMatch(line));
final bool keyUsed = gradleLines.any((String line) => keyUsedRegex.hasMatch(line));
final bool keyPresent =
gradleLines.any((String line) => keyPresentRegex.hasMatch(line));
// final bool keyRead =
// gradleLines.any((String line) => keyReadRegex.hasMatch(line));
// final bool keyUsed =
// gradleLines.any((String line) => keyUsedRegex.hasMatch(line));

return keyPresent && keyRead && keyUsed;
return keyPresent; //&& keyRead && keyUsed;
}

/// Validates the top-level build.gradle for an example app (e.g.,
Expand All @@ -158,6 +163,9 @@ class GradleCheckCommand extends PackageLoopingCommand {
if (!_validateKotlinVersion(package, lines)) {
succeeded = false;
}
if (!_validateArtifactHubUsage(package, lines)) {
succeeded = false;
}
return succeeded;
}

Expand Down
59 changes: 47 additions & 12 deletions script/tool/test/gradle_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ dependencies {
required String pluginName,
required bool warningsConfigured,
String? kotlinVersion,
bool includeArtifactHub = true,
}) {
final File buildGradle = package
.platformDirectory(FlutterPlatform.android)
Expand All @@ -138,11 +139,20 @@ gradle.projectsEvaluated {
}
}
}
''';
const String artifactHubSection = '''
// See https://github.com/flutter/flutter/wiki/Plugins-and-Packages-repository-structure#gradle-structure for more info.
def artifactRepoKey = 'ARTIFACT_HUB_REPOSITORY'
if (System.getenv().containsKey(artifactRepoKey)) {
println "Using artifact hub"
maven { url System.getenv(artifactRepoKey) }
}
''';
buildGradle.writeAsStringSync('''
buildscript {
${kotlinVersion == null ? '' : "ext.kotlin_version = '$kotlinVersion'"}
repositories {
${includeArtifactHub ? artifactHubSection : ''}
google()
mavenCentral()
}
Expand Down Expand Up @@ -224,18 +234,20 @@ dependencies {
''');
}

void writeFakeExampleBuildGradles(
RepositoryPackage package, {
required String pluginName,
bool includeNamespace = true,
bool commentNamespace = false,
bool warningsConfigured = true,
String? kotlinVersion,
}) {
writeFakeExampleTopLevelBuildGradle(package,
pluginName: pluginName,
warningsConfigured: warningsConfigured,
kotlinVersion: kotlinVersion);
void writeFakeExampleBuildGradles(RepositoryPackage package,
{required String pluginName,
bool includeNamespace = true,
bool commentNamespace = false,
bool warningsConfigured = true,
String? kotlinVersion,
bool includeArtifactHub = true}) {
writeFakeExampleTopLevelBuildGradle(
package,
pluginName: pluginName,
warningsConfigured: warningsConfigured,
kotlinVersion: kotlinVersion,
includeArtifactHub: includeArtifactHub,
);
writeFakeExampleAppBuildGradle(package,
includeNamespace: includeNamespace, commentNamespace: commentNamespace);
}
Expand Down Expand Up @@ -650,6 +662,29 @@ dependencies {
));
});

group('Artifact Hub check', () {
test('passes artifact if not set', () async {
const String packageName = 'a_package';
final RepositoryPackage package =
createFakePackage('a_package', packagesDir);
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
writeFakeManifest(package);
final RepositoryPackage example = package.getExamples().first;
writeFakeExampleBuildGradles(example, pluginName: packageName);
writeFakeManifest(example, isApp: true);

final List<String> output =
await runCapturingPrint(runner, <String>['gradle-check']);

expect(
output,
containsAllInOrder(<Matcher>[
contains('Validating android/build.gradle'),
]),
);
});
});

group('Kotlin version check', () {
test('passes if not set', () async {
const String packageName = 'a_package';
Expand Down