Fix update_engine_version_test in presence of FLUTTER_PREBUILT_ENGINE_VERSION env vars. (#162270)

Fixes https://github.com/flutter/flutter/issues/162260
This commit is contained in:
Alexander Aprelev 2025-01-27 17:19:47 -08:00 committed by GitHub
parent 9ebd172442
commit 0b33d7b48d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,11 +5,12 @@
@TestOn('vm') @TestOn('vm')
library; library;
import 'dart:io' as io;
import 'package:file/file.dart'; import 'package:file/file.dart';
import 'package:file/local.dart'; import 'package:file/local.dart';
import 'package:file_testing/file_testing.dart'; import 'package:file_testing/file_testing.dart';
import 'package:platform/platform.dart'; import 'package:platform/platform.dart';
import 'package:process_runner/process_runner.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {
@ -19,47 +20,56 @@ void main() {
late Directory tmpDir; late Directory tmpDir;
late _FlutterRootUnderTest testRoot; late _FlutterRootUnderTest testRoot;
late Map<String, String> environment; late Map<String, String> environment;
late ProcessRunner processRunner;
void printIfNotEmpty(String prefix, String string) {
if (string.isNotEmpty) {
string.split(io.Platform.lineTerminator).forEach((String s) {
print('$prefix:>$s<');
});
}
}
io.ProcessResult run(String executable, List<String> args) {
print('Running "$executable ${args.join(" ")}"');
final io.ProcessResult result = io.Process.runSync(
executable,
args,
environment: environment,
workingDirectory: testRoot.root.absolute.path,
includeParentEnvironment: false,
);
if (result.exitCode != 0) {
print('exitCode: ${result.exitCode}');
}
printIfNotEmpty('stdout', (result.stdout as String).trim());
printIfNotEmpty('stderr', (result.stderr as String).trim());
return result;
}
setUp(() async { setUp(() async {
tmpDir = localFs.systemTempDirectory.createTempSync('update_engine_version_test.'); tmpDir = localFs.systemTempDirectory.createTempSync('update_engine_version_test.');
testRoot = _FlutterRootUnderTest.fromPath(tmpDir.childDirectory('flutter').path); testRoot = _FlutterRootUnderTest.fromPath(tmpDir.childDirectory('flutter').path);
environment = <String, String>{}; environment = <String, String>{};
processRunner = ProcessRunner( environment.addAll(io.Platform.environment);
defaultWorkingDirectory: testRoot.root, environment.remove('FLUTTER_PREBUILT_ENGINE_VERSION');
environment: environment,
printOutputDefault: true,
);
// Copy the update_engine_version script and create a rough directory structure. // Copy the update_engine_version script and create a rough directory structure.
flutterRoot.binInternalUpdateEngineVersion.copySyncRecursive( flutterRoot.binInternalUpdateEngineVersion.copySyncRecursive(
testRoot.binInternalUpdateEngineVersion.path, testRoot.binInternalUpdateEngineVersion.path,
); );
// On some systems, copying the file means losing the executable bit.
if (const LocalPlatform().isWindows) {
await processRunner.runProcess(<String>[
'attrib',
'+x',
testRoot.binInternalUpdateEngineVersion.path,
]);
}
}); });
tearDown(() { tearDown(() {
tmpDir.deleteSync(recursive: true); tmpDir.deleteSync(recursive: true);
}); });
Future<void> runUpdateEngineVersion() async { io.ProcessResult runUpdateEngineVersion() {
if (const LocalPlatform().isWindows) { final (String executable, List<String> args) =
await processRunner.runProcess(<String>[ const LocalPlatform().isWindows
'powershell', ? ('powershell', <String>[testRoot.binInternalUpdateEngineVersion.path])
testRoot.binInternalUpdateEngineVersion.path, : (testRoot.binInternalUpdateEngineVersion.path, <String>[]);
]); return run(executable, args);
} else {
await processRunner.runProcess(<String>[testRoot.binInternalUpdateEngineVersion.path]);
}
} }
group('if FLUTTER_PREBUILT_ENGINE_VERSION is set', () { group('if FLUTTER_PREBUILT_ENGINE_VERSION is set', () {
@ -68,7 +78,7 @@ void main() {
}); });
test('writes it to engine.version with no git interaction', () async { test('writes it to engine.version with no git interaction', () async {
await runUpdateEngineVersion(); runUpdateEngineVersion();
expect(testRoot.binInternalEngineVersion, exists); expect(testRoot.binInternalEngineVersion, exists);
expect( expect(
@ -78,96 +88,90 @@ void main() {
}); });
}); });
Future<void> setupRepo({required String branch}) async { void setupRepo({required String branch}) {
for (final File f in <File>[testRoot.deps, testRoot.engineSrcGn]) { for (final File f in <File>[testRoot.deps, testRoot.engineSrcGn]) {
f.createSync(recursive: true); f.createSync(recursive: true);
} }
await processRunner.runProcess(<String>['git', 'init', '--initial-branch', 'master']); run('git', <String>['init', '--initial-branch', 'master']);
await processRunner.runProcess(<String>['git', 'add', '.']); run('git', <String>['add', '.']);
await processRunner.runProcess(<String>['git', 'commit', '-m', 'Initial commit']); run('git', <String>['commit', '-m', 'Initial commit']);
if (branch != 'master') { if (branch != 'master') {
await processRunner.runProcess(<String>['git', 'checkout', '-b', branch]); run('git', <String>['checkout', '-b', branch]);
} }
} }
Future<void> setupRemote({required String remote}) async { void setupRemote({required String remote}) {
await processRunner.runProcess(<String>['git', 'remote', 'add', remote, testRoot.root.path]); run('git', <String>['remote', 'add', remote, testRoot.root.path]);
await processRunner.runProcess(<String>['git', 'fetch', remote]); run('git', <String>['fetch', remote]);
} }
test('writes nothing, even if files are set, if we are on "stable"', () async { test('writes nothing, even if files are set, if we are on "stable"', () async {
await setupRepo(branch: 'stable'); setupRepo(branch: 'stable');
await setupRemote(remote: 'upstream'); setupRemote(remote: 'upstream');
await runUpdateEngineVersion(); runUpdateEngineVersion();
expect(testRoot.binInternalEngineVersion, isNot(exists)); expect(testRoot.binInternalEngineVersion, isNot(exists));
}); });
test('writes nothing, even if files are set, if we are on "beta"', () async { test('writes nothing, even if files are set, if we are on "beta"', () async {
await setupRepo(branch: 'beta'); setupRepo(branch: 'beta');
await setupRemote(remote: 'upstream'); setupRemote(remote: 'upstream');
await runUpdateEngineVersion(); runUpdateEngineVersion();
expect(testRoot.binInternalEngineVersion, isNot(exists)); expect(testRoot.binInternalEngineVersion, isNot(exists));
}); });
group('if DEPS and engine/src/.gn are present, engine.version is derived from', () { group('if DEPS and engine/src/.gn are present, engine.version is derived from', () {
setUp(() async { setUp(() async {
await setupRepo(branch: 'master'); setupRepo(branch: 'master');
}); });
test('merge-base HEAD upstream/master on non-LUCI when upstream is set', () async { test('merge-base HEAD upstream/master on non-LUCI when upstream is set', () async {
await setupRemote(remote: 'upstream'); setupRemote(remote: 'upstream');
final ProcessRunnerResult mergeBaseHeadUpstream = await processRunner.runProcess(<String>[ final io.ProcessResult mergeBaseHeadUpstream = run('git', <String>[
'git',
'merge-base', 'merge-base',
'HEAD', 'HEAD',
'upstream/master', 'upstream/master',
]); ]);
await runUpdateEngineVersion(); runUpdateEngineVersion();
expect(testRoot.binInternalEngineVersion, exists); expect(testRoot.binInternalEngineVersion, exists);
expect( expect(
testRoot.binInternalEngineVersion.readAsStringSync(), testRoot.binInternalEngineVersion.readAsStringSync(),
equalsIgnoringWhitespace(mergeBaseHeadUpstream.stdout), equalsIgnoringWhitespace(mergeBaseHeadUpstream.stdout as String),
); );
}); });
test('merge-base HEAD origin/master on non-LUCI when upstream is not set', () async { test('merge-base HEAD origin/master on non-LUCI when upstream is not set', () async {
await setupRemote(remote: 'origin'); setupRemote(remote: 'origin');
final ProcessRunnerResult mergeBaseHeadOrigin = await processRunner.runProcess(<String>[ final io.ProcessResult mergeBaseHeadOrigin = run('git', <String>[
'git',
'merge-base', 'merge-base',
'HEAD', 'HEAD',
'origin/master', 'origin/master',
]); ]);
await runUpdateEngineVersion(); runUpdateEngineVersion();
expect(testRoot.binInternalEngineVersion, exists); expect(testRoot.binInternalEngineVersion, exists);
expect( expect(
testRoot.binInternalEngineVersion.readAsStringSync(), testRoot.binInternalEngineVersion.readAsStringSync(),
equalsIgnoringWhitespace(mergeBaseHeadOrigin.stdout), equalsIgnoringWhitespace(mergeBaseHeadOrigin.stdout as String),
); );
}); });
test('rev-parse HEAD when running on LUCI', () async { test('rev-parse HEAD when running on LUCI', () async {
environment['LUCI_CONTEXT'] = '_NON_NULL_AND_NON_EMPTY_STRING'; environment['LUCI_CONTEXT'] = '_NON_NULL_AND_NON_EMPTY_STRING';
await runUpdateEngineVersion(); runUpdateEngineVersion();
final ProcessRunnerResult revParseHead = await processRunner.runProcess(<String>[ final io.ProcessResult revParseHead = run('git', <String>['rev-parse', 'HEAD']);
'git',
'rev-parse',
'HEAD',
]);
expect(testRoot.binInternalEngineVersion, exists); expect(testRoot.binInternalEngineVersion, exists);
expect( expect(
testRoot.binInternalEngineVersion.readAsStringSync(), testRoot.binInternalEngineVersion.readAsStringSync(),
equalsIgnoringWhitespace(revParseHead.stdout), equalsIgnoringWhitespace(revParseHead.stdout as String),
); );
}); });
}); });
@ -182,7 +186,7 @@ void main() {
test('[DEPS] engine.version is blank', () async { test('[DEPS] engine.version is blank', () async {
testRoot.deps.deleteSync(); testRoot.deps.deleteSync();
await runUpdateEngineVersion(); runUpdateEngineVersion();
expect(testRoot.binInternalEngineVersion, exists); expect(testRoot.binInternalEngineVersion, exists);
expect(testRoot.binInternalEngineVersion.readAsStringSync(), equalsIgnoringWhitespace('')); expect(testRoot.binInternalEngineVersion.readAsStringSync(), equalsIgnoringWhitespace(''));
@ -191,7 +195,7 @@ void main() {
test('[engine/src/.gn] engine.version is blank', () async { test('[engine/src/.gn] engine.version is blank', () async {
testRoot.engineSrcGn.deleteSync(); testRoot.engineSrcGn.deleteSync();
await runUpdateEngineVersion(); runUpdateEngineVersion();
expect(testRoot.binInternalEngineVersion, exists); expect(testRoot.binInternalEngineVersion, exists);
expect(testRoot.binInternalEngineVersion.readAsStringSync(), equalsIgnoringWhitespace('')); expect(testRoot.binInternalEngineVersion.readAsStringSync(), equalsIgnoringWhitespace(''));