Refactor prepare_package.dart (#139277)

I plan to extend the prepare_package.dart script to upload the flutter preview device ([design doc](https://docs.google.com/document/d/1AzI-_Uk2v1LA2kKKFJ7gVD4xcakXJ6yVZiS5Ek6RHtg/edit#heading=h.byp03plw7mg9)).

However, given that that script is one large >1k line file, I decided to organize it into smaller libraries in this PR. There should be no behavioral change in this PR, this is a cleanup only. I made the following changes:

1. Created a //dev/bots/prepare_package/ directory to contain helper libraries
2. Moved everything but the `main()` function in //dev/bots/prepare_package.dart into one of 4 helper libraries under the new directory from step 1:
  a. archive_creator.dart which contains the code that creates archive directory locally on disk
  b. archive_publisher.dart which contains the code that uploads the archive to cloud storage
  c. common.dart for shared constants and definitions
  d. process_runner.dart for an abstraction over running sub-processes
3. Changed all definitions to `File` and `Directory` from `dart:io` to use the testable versions from `package:file`. This allowed me to use the `MemoryFileSystem` in the unit tests, rather than creating real temp file system directories.
This commit is contained in:
Christopher Fujino
2023-11-30 11:25:50 -08:00
committed by GitHub
parent 995a020e48
commit 88710972da
6 changed files with 986 additions and 906 deletions

View File

@@ -3,14 +3,19 @@
// found in the LICENSE file.
import 'dart:convert';
import 'dart:io' hide Platform;
import 'dart:io' show ProcessResult;
import 'dart:typed_data';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:path/path.dart' as path;
import 'package:platform/platform.dart' show FakePlatform, Platform;
import '../../../packages/flutter_tools/test/src/fake_process_manager.dart';
import '../prepare_package.dart';
import '../prepare_package/archive_creator.dart';
import '../prepare_package/archive_publisher.dart';
import '../prepare_package/common.dart';
import '../prepare_package/process_runner.dart';
import 'common.dart';
void main() {
@@ -73,12 +78,14 @@ void main() {
throwsA(isA<PreparePackageException>()));
});
});
group('ArchiveCreator for $platformName', () {
late ArchiveCreator creator;
late Directory tempDir;
Directory flutterDir;
Directory cacheDir;
late FakeProcessManager processManager;
late FileSystem fs;
final List<List<String>> args = <List<String>>[];
final List<Map<Symbol, dynamic>> namedArgs = <Map<Symbol, dynamic>>[];
late String flutter;
@@ -92,16 +99,18 @@ void main() {
processManager = FakeProcessManager.list(<FakeCommand>[]);
args.clear();
namedArgs.clear();
tempDir = Directory.systemTemp.createTempSync('flutter_prepage_package_test.');
flutterDir = Directory(path.join(tempDir.path, 'flutter'));
fs = MemoryFileSystem.test();
tempDir = fs.systemTempDirectory;
flutterDir = fs.directory(path.join(tempDir.path, 'flutter'));
flutterDir.createSync(recursive: true);
cacheDir = Directory(path.join(flutterDir.path, 'bin', 'cache'));
cacheDir = fs.directory(path.join(flutterDir.path, 'bin', 'cache'));
cacheDir.createSync(recursive: true);
creator = ArchiveCreator(
tempDir,
tempDir,
testRef,
Branch.beta,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -196,6 +205,7 @@ void main() {
tempDir,
testRef,
Branch.beta,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -245,6 +255,7 @@ void main() {
tempDir,
testRef,
Branch.beta,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -303,6 +314,7 @@ void main() {
tempDir,
testRef,
Branch.beta,
fs: fs,
strict: false,
processManager: processManager,
subprocessOutput: false,
@@ -355,6 +367,7 @@ void main() {
tempDir,
testRef,
Branch.beta,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -376,6 +389,7 @@ void main() {
group('ArchivePublisher for $platformName', () {
late FakeProcessManager processManager;
late Directory tempDir;
late FileSystem fs;
final String gsutilCall = platform.isWindows
? 'python3 ${path.join("D:", "depot_tools", "gsutil.py")}'
: 'python3 ${path.join("/", "depot_tools", "gsutil.py")}';
@@ -385,8 +399,9 @@ void main() {
final String gsArchivePath = 'gs://flutter_infra_release/releases/stable/$platformName/$archiveName';
setUp(() async {
fs = MemoryFileSystem.test(style: platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix);
processManager = FakeProcessManager.list(<FakeCommand>[]);
tempDir = Directory.systemTemp.createTempSync('flutter_prepage_package_test.');
tempDir = fs.systemTempDirectory.createTempSync('flutter_prepage_package_test.');
});
tearDown(() async {
@@ -435,8 +450,8 @@ void main() {
]
}
''';
File(jsonPath).writeAsStringSync(releasesJson);
File(archivePath).writeAsStringSync('archive contents');
fs.file(jsonPath).writeAsStringSync(releasesJson);
fs.file(archivePath).writeAsStringSync('archive contents');
final Map<String, List<ProcessResult>?> calls = <String, List<ProcessResult>?>{
// This process fails because the file does NOT already exist
'$gsutilCall -- cp $gsJsonPath $jsonPath': null,
@@ -447,7 +462,7 @@ void main() {
'$gsutilCall -- -h Content-Type:application/json -h Cache-Control:max-age=60 cp $jsonPath $gsJsonPath': null,
};
processManager.addCommands(convertResults(calls));
final File outputFile = File(path.join(tempDir.absolute.path, archiveName));
final File outputFile = fs.file(path.join(tempDir.absolute.path, archiveName));
outputFile.createSync();
assert(tempDir.existsSync());
final ArchivePublisher publisher = ArchivePublisher(
@@ -461,6 +476,7 @@ void main() {
},
outputFile,
false,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -469,7 +485,7 @@ void main() {
await publisher.generateLocalMetadata();
await publisher.publishArchive();
final File releaseFile = File(jsonPath);
final File releaseFile = fs.file(jsonPath);
expect(releaseFile.existsSync(), isTrue);
final String contents = releaseFile.readAsStringSync();
// Make sure new data is added.
@@ -536,8 +552,8 @@ void main() {
]
}
''';
File(jsonPath).writeAsStringSync(releasesJson);
File(archivePath).writeAsStringSync('archive contents');
fs.file(jsonPath).writeAsStringSync(releasesJson);
fs.file(archivePath).writeAsStringSync('archive contents');
final Map<String, List<ProcessResult>?> calls = <String, List<ProcessResult>?>{
// This process fails because the file does NOT already exist
'$gsutilCall -- cp $gsJsonPath $jsonPath': null,
@@ -548,7 +564,7 @@ void main() {
'$gsutilCall -- -h Content-Type:application/json -h Cache-Control:max-age=60 cp $jsonPath $gsJsonPath': null,
};
processManager.addCommands(convertResults(calls));
final File outputFile = File(path.join(tempDir.absolute.path, archiveName));
final File outputFile = fs.file(path.join(tempDir.absolute.path, archiveName));
outputFile.createSync();
assert(tempDir.existsSync());
final ArchivePublisher publisher = ArchivePublisher(
@@ -562,6 +578,7 @@ void main() {
},
outputFile,
false,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -570,7 +587,7 @@ void main() {
await publisher.generateLocalMetadata();
await publisher.publishArchive();
final File releaseFile = File(jsonPath);
final File releaseFile = fs.file(jsonPath);
expect(releaseFile.existsSync(), isTrue);
final String contents = releaseFile.readAsStringSync();
expect(contents, contains('"dart_sdk_version": "3.2.1"'));
@@ -601,8 +618,8 @@ void main() {
]
}
''';
File(jsonPath).writeAsStringSync(releasesJson);
File(archivePath).writeAsStringSync('archive contents');
fs.file(jsonPath).writeAsStringSync(releasesJson);
fs.file(archivePath).writeAsStringSync('archive contents');
final Map<String, List<ProcessResult>?> calls = <String, List<ProcessResult>?>{
// This process fails because the file does NOT already exist
'$gsutilCall -- cp $gsJsonPath $jsonPath': null,
@@ -613,7 +630,7 @@ void main() {
'$gsutilCall -- -h Content-Type:application/json -h Cache-Control:max-age=60 cp $jsonPath $gsJsonPath': null,
};
processManager.addCommands(convertResults(calls));
final File outputFile = File(path.join(tempDir.absolute.path, archiveName));
final File outputFile = fs.file(path.join(tempDir.absolute.path, archiveName));
outputFile.createSync();
assert(tempDir.existsSync());
final ArchivePublisher publisher = ArchivePublisher(
@@ -627,6 +644,7 @@ void main() {
},
outputFile,
false,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -635,7 +653,7 @@ void main() {
await publisher.generateLocalMetadata();
await publisher.publishArchive();
final File releaseFile = File(jsonPath);
final File releaseFile = fs.file(jsonPath);
expect(releaseFile.existsSync(), isTrue);
final String contents = releaseFile.readAsStringSync();
final Map<String, dynamic> releases = jsonDecode(contents) as Map<String, dynamic>;
@@ -682,8 +700,8 @@ void main() {
]
}
''';
File(jsonPath).writeAsStringSync(releasesJson);
File(archivePath).writeAsStringSync('archive contents');
fs.file(jsonPath).writeAsStringSync(releasesJson);
fs.file(archivePath).writeAsStringSync('archive contents');
final Map<String, List<ProcessResult>?> calls = <String, List<ProcessResult>?>{
// This process fails because the file does NOT already exist
'$gsutilCall -- cp $gsJsonPath $jsonPath': null,
@@ -694,7 +712,7 @@ void main() {
'$gsutilCall -- -h Content-Type:application/json -h Cache-Control:max-age=60 cp $jsonPath $gsJsonPath': null,
};
processManager.addCommands(convertResults(calls));
final File outputFile = File(path.join(tempDir.absolute.path, archiveName));
final File outputFile = fs.file(path.join(tempDir.absolute.path, archiveName));
outputFile.createSync();
assert(tempDir.existsSync());
final ArchivePublisher publisher = ArchivePublisher(
@@ -708,6 +726,7 @@ void main() {
},
outputFile,
false,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -716,7 +735,7 @@ void main() {
await publisher.generateLocalMetadata();
await publisher.publishArchive();
final File releaseFile = File(jsonPath);
final File releaseFile = fs.file(jsonPath);
expect(releaseFile.existsSync(), isTrue);
final String contents = releaseFile.readAsStringSync();
final Map<String, dynamic> jsonData = json.decode(contents) as Map<String, dynamic>;
@@ -725,7 +744,7 @@ void main() {
test('publishArchive throws if forceUpload is false and artifact already exists on cloud storage', () async {
final String archiveName = platform.isLinux ? 'archive.tar.xz' : 'archive.zip';
final File outputFile = File(path.join(tempDir.absolute.path, archiveName));
final File outputFile = fs.file(path.join(tempDir.absolute.path, archiveName));
final ArchivePublisher publisher = ArchivePublisher(
tempDir,
testRef,
@@ -737,6 +756,7 @@ void main() {
},
outputFile,
false,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -751,7 +771,7 @@ void main() {
test('publishArchive does not throw if forceUpload is true and artifact already exists on cloud storage', () async {
final String archiveName = platform.isLinux ? 'archive.tar.xz' : 'archive.zip';
final File outputFile = File(path.join(tempDir.absolute.path, archiveName));
final File outputFile = fs.file(path.join(tempDir.absolute.path, archiveName));
final ArchivePublisher publisher = ArchivePublisher(
tempDir,
testRef,
@@ -763,6 +783,7 @@ void main() {
},
outputFile,
false,
fs: fs,
processManager: processManager,
subprocessOutput: false,
platform: platform,
@@ -805,8 +826,8 @@ void main() {
]
}
''';
File(jsonPath).writeAsStringSync(releasesJson);
File(archivePath).writeAsStringSync('archive contents');
fs.file(jsonPath).writeAsStringSync(releasesJson);
fs.file(archivePath).writeAsStringSync('archive contents');
final Map<String, List<ProcessResult>?> calls = <String, List<ProcessResult>?>{
'$gsutilCall -- cp $gsJsonPath $jsonPath': null,
'$gsutilCall -- rm $gsArchivePath': null,