Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
Simplify flutter_plugin_ffi_test
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Sep 14, 2023
1 parent 1e0fb4c commit 5214ae8
Showing 1 changed file with 70 additions and 112 deletions.
182 changes: 70 additions & 112 deletions test_flutter/flutter_template_tests/flutter_plugin_ffi_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,30 @@ void main() {
final bindingsGeneratedCopyUri =
libDirUri.resolve('${projectName}_bindings_generated_copy.dart');

await Task.serial([
RunProcess(
executable: 'flutter',
arguments: [
'create',
'--template=plugin_ffi',
projectName,
],
workingDirectory: tempDirUri,
),
Copy(
source: bindingsGeneratedUri,
target: bindingsGeneratedCopyUri,
),
RunProcess(
executable: 'flutter',
arguments: [
'pub',
'run',
'ffigen',
'--config',
'ffigen.yaml',
],
workingDirectory: projectDirUri,
),
]).run();
await runProcess(
executable: 'flutter',
arguments: [
'create',
'--template=plugin_ffi',
projectName,
],
workingDirectory: tempDirUri,
);
await copyFile(
source: bindingsGeneratedUri,
target: bindingsGeneratedCopyUri,
);
await runProcess(
executable: 'flutter',
arguments: [
'pub',
'run',
'ffigen',
'--config',
'ffigen.yaml',
],
workingDirectory: projectDirUri,
);

final originalBindings = await readFileAsString(bindingsGeneratedCopyUri);
final regeneratedBindings = await readFileAsString(bindingsGeneratedUri);
Expand All @@ -71,96 +69,56 @@ Future<String> readFileAsString(Uri uri) async {
return contents.replaceAll('\r', '');
}

abstract class Task {
Future<void> run();

factory Task.serial(Iterable<Task> tasks) => _SerialTask(tasks);
}

class _SerialTask implements Task {
final Iterable<Task> tasks;

_SerialTask(this.tasks);

@override
Future<void> run() async {
for (final task in tasks) {
await task.run();
}
}
}

class RunProcess implements Task {
final List<String> arguments;
final String executable;
final Uri? workingDirectory;
Map<String, String>? environment;
final bool throwOnFailure;

RunProcess({
required this.arguments,
required this.executable,
this.workingDirectory,
this.environment,
this.throwOnFailure = true,
Future<void> runProcess({
required List<String> arguments,
required String executable,
Uri? workingDirectory,
Map<String, String>? environment,
bool throwOnFailure = true,
}) async {
// Excluding [workingDirectory].
final String commandString = [
if (workingDirectory != null) '(cd ${workingDirectory.path};',
...?environment?.entries.map((entry) => '${entry.key}=${entry.value}'),
executable,
...arguments.map((a) => a.contains(' ') ? "'$a'" : a),
if (workingDirectory != null) ')',
].join(' ');

final workingDirectoryString = workingDirectory?.toFilePath();

print('Running `$commandString`.');
final process = await Process.start(executable, arguments,
runInShell: true,
includeParentEnvironment: true,
workingDirectory: workingDirectoryString,
environment: environment)
.then((process) {
process.stdout.transform(utf8.decoder).forEach((s) => print(' $s'));
process.stderr.transform(utf8.decoder).forEach((s) => print(' $s'));
return process;
});

/// Excluding [workingDirectory].
String get commandString => [
if (workingDirectory != null) '(cd ${workingDirectory!.path};',
...?environment?.entries.map((entry) => '${entry.key}=${entry.value}'),
executable,
...arguments.map((a) => a.contains(' ') ? "'$a'" : a),
if (workingDirectory != null) ')',
].join(' ');

@override
Future<void> run() async {
final workingDirectoryString = workingDirectory?.toFilePath();

print('Running `$commandString`.');
final process = await Process.start(executable, arguments,
runInShell: true,
includeParentEnvironment: true,
workingDirectory: workingDirectoryString,
environment: environment)
.then((process) {
process.stdout.transform(utf8.decoder).forEach((s) => print(' $s'));
process.stderr.transform(utf8.decoder).forEach((s) => print(' $s'));
return process;
});
final exitCode = await process.exitCode;
if (exitCode != 0) {
final message =
'Command `$commandString` failed with exit code $exitCode.';
print(message);
if (throwOnFailure) {
throw Exception(message);
}
final exitCode = await process.exitCode;
if (exitCode != 0) {
final message = 'Command `$commandString` failed with exit code $exitCode.';
print(message);
if (throwOnFailure) {
throw Exception(message);
}
print('Command `$commandString` done.');
}
print('Command `$commandString` done.');
}

class Copy implements Task {
final Uri source;
final Uri target;

Copy({
required this.source,
required this.target,
});

@override
Future<void> run() async {
final file = File.fromUri(source);
if (!await file.exists()) {
final message =
"File not in expected location: '${source.toFilePath()}'.";
print(message);
throw Exception(message);
}
print('Copying ${source.toFilePath()} to ${target.toFilePath()}.');
await file.copy(target.toFilePath());
Future<void> copyFile({
required Uri source,
required Uri target,
}) async {
final file = File.fromUri(source);
if (!await file.exists()) {
final message = "File not in expected location: '${source.toFilePath()}'.";
print(message);
throw Exception(message);
}
print('Copying ${source.toFilePath()} to ${target.toFilePath()}.');
await file.copy(target.toFilePath());
}

0 comments on commit 5214ae8

Please sign in to comment.