Skip to content
Merged
Prev Previous commit
Next Next commit
Add tests for settings.gradle check
  • Loading branch information
reidbaker committed Aug 4, 2023
commit b7e8bead542ed82ab4df3c593db84cd46c7e11e5
2 changes: 2 additions & 0 deletions script/tool/lib/src/gradle_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ class GradleCheckCommand extends PackageLoopingCommand {
return succeeded;
}

/// String printed as example of valid example root settings.gradle repository
/// configuration that enables artifact hub env variable.
@visibleForTesting
static String exampleRootSettingsArtifactHubString = '''
// See https://github.com/flutter/flutter/wiki/Plugins-and-Packages-repository-structure#gradle-structure for more info.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract this URL to a constant that's used in both statics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Expand Down
146 changes: 133 additions & 13 deletions script/tool/test/gradle_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ ${warningsConfigured ? warningConfig : ''}
''');
}

/// Writes a fake android/build.gradle file for an example [package] with the
/// given options.
void writeFakeExampleTopLevelSettingsGradle(
RepositoryPackage package, {
bool includeArtifactHub = true,
}) {
final File settingsGradle = package
.platformDirectory(FlutterPlatform.android)
.childFile('settings.gradle');
settingsGradle.createSync(recursive: true);

settingsGradle.writeAsStringSync('''
include ':app'

def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()

def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withInputStream { stream -> plugins.load(stream) }
}

plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":\$name"
project(":\$name").projectDir = pluginDirectory
}
${includeArtifactHub ? GradleCheckCommand.exampleRootSettingsArtifactHubString : ''}
''');
}

/// Writes a fake android/app/build.gradle file for an example [package] with
/// the given options.
void writeFakeExampleAppBuildGradle(
Expand Down Expand Up @@ -226,22 +257,29 @@ dependencies {
''');
}

void writeFakeExampleBuildGradles(RepositoryPackage package,
{required String pluginName,
bool includeNamespace = true,
bool commentNamespace = false,
bool warningsConfigured = true,
String? kotlinVersion,
bool includeArtifactHub = true}) {
void writeFakeExampleBuildGradles(
RepositoryPackage package, {
required String pluginName,
bool includeNamespace = true,
bool commentNamespace = false,
bool warningsConfigured = true,
String? kotlinVersion,
bool includeBuildArtifactHub = true,
bool includeSettingsArtifactHub = true,
}) {
writeFakeExampleTopLevelBuildGradle(
package,
pluginName: pluginName,
warningsConfigured: warningsConfigured,
kotlinVersion: kotlinVersion,
includeArtifactHub: includeArtifactHub,
includeArtifactHub: includeBuildArtifactHub,
);
writeFakeExampleAppBuildGradle(package,
includeNamespace: includeNamespace, commentNamespace: commentNamespace);
writeFakeExampleTopLevelSettingsGradle(
package,
includeArtifactHub: includeSettingsArtifactHub,
);
}

void writeFakeManifest(
Expand Down Expand Up @@ -655,15 +693,21 @@ dependencies {
});

group('Artifact Hub check', () {
test('passes artifact hub check when set', () async {
test('passes build.gradle artifact hub check when 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;
// ignore: avoid_redundant_argument_values
writeFakeExampleBuildGradles(example, pluginName: packageName, includeArtifactHub: true);
writeFakeExampleBuildGradles(
example,
pluginName: packageName,
// ignore: avoid_redundant_argument_values
includeBuildArtifactHub: true,
// ignore: avoid_redundant_argument_values
includeSettingsArtifactHub: true,
);
writeFakeManifest(example, isApp: true);

final List<String> output =
Expand All @@ -673,19 +717,56 @@ dependencies {
output,
containsAllInOrder(<Matcher>[
contains('Validating android/build.gradle'),
contains('Validating android/settings.gradle'),
]),
);
});
test('fails artifact hub check when missing', () async {
test('fails artifact hub check when build and settings sections missing',
() 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, includeArtifactHub: false);
writeFakeExampleBuildGradles(
example,
pluginName: packageName,
includeBuildArtifactHub: false,
includeSettingsArtifactHub: false,
);
writeFakeManifest(example, isApp: true);

Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['gradle-check'], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('Does not link artifact hub documentation.'),
contains(GradleCheckCommand.exampleRootGradleArtifactHubString),
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString),
]),
);
});

test('fails build.gradle artifact hub check when missing', () 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,
includeBuildArtifactHub: false,
// ignore: avoid_redundant_argument_values
includeSettingsArtifactHub: true);
writeFakeManifest(example, isApp: true);

Error? commandError;
final List<String> output = await runCapturingPrint(
Expand All @@ -701,6 +782,45 @@ dependencies {
contains(GradleCheckCommand.exampleRootGradleArtifactHubString),
]),
);
expect(
output,
isNot(
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString)),
);
});

test('fails settings.gradle artifact hub check when missing', () 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,
// ignore: avoid_redundant_argument_values
includeBuildArtifactHub: true,
includeSettingsArtifactHub: false);
writeFakeManifest(example, isApp: true);

Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['gradle-check'], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('Does not link artifact hub documentation.'),
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString),
]),
);
expect(
output,
isNot(contains(GradleCheckCommand.exampleRootGradleArtifactHubString)),
);
});
});

Expand Down