|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:file/file.dart'; |
| 8 | +import 'package:meta/meta.dart'; |
| 9 | +import 'package:path/path.dart' as p; |
| 10 | + |
| 11 | +import 'common/file_filters.dart'; |
| 12 | +import 'common/package_looping_command.dart'; |
| 13 | +import 'common/pub_utils.dart'; |
| 14 | +import 'common/repository_package.dart'; |
| 15 | + |
| 16 | +/// A command to run dart fix tests for packages that have a test_fixes |
| 17 | +/// directory. |
| 18 | +class TestDartFixesCommand extends PackageLoopingCommand { |
| 19 | + /// Creates an instance of the test dart fixes command. |
| 20 | + TestDartFixesCommand(super.packagesDir, {super.processRunner, super.platform, super.gitDir}); |
| 21 | + |
| 22 | + /// A map of the test directory used for each package passed to runForPackage. |
| 23 | + @visibleForTesting |
| 24 | + final testDirectories = <String, Directory>{}; |
| 25 | + |
| 26 | + @override |
| 27 | + final String name = 'test-dart-fixes'; |
| 28 | + |
| 29 | + @override |
| 30 | + final String description = |
| 31 | + 'Runs the Dart fix tests for all packages.\n\n' |
| 32 | + 'This command requires "flutter" to be in your path.'; |
| 33 | + |
| 34 | + @override |
| 35 | + bool shouldIgnoreFile(String path) { |
| 36 | + return isRepoLevelNonCodeImpactingFile(path) || |
| 37 | + isNativeCodeFile(path) || |
| 38 | + isPackageSupportFile(path); |
| 39 | + } |
| 40 | + |
| 41 | + @override |
| 42 | + Future<PackageResult> runForPackage(RepositoryPackage package) async { |
| 43 | + // Only run for packages that have a fix_tests directory. |
| 44 | + if (!package.dartFixTestDirectory.existsSync()) { |
| 45 | + final String localDartFixPath = p.relative( |
| 46 | + package.dartFixTestDirectory.path, |
| 47 | + from: package.directory.path, |
| 48 | + ); |
| 49 | + return PackageResult.skip('No $localDartFixPath directory.'); |
| 50 | + } |
| 51 | + |
| 52 | + final Directory testDirectory; |
| 53 | + try { |
| 54 | + testDirectory = await _createTestDirectory(package); |
| 55 | + } catch (error) { |
| 56 | + return PackageResult.fail(['Failed to create temporary test directory: $error']); |
| 57 | + } |
| 58 | + |
| 59 | + PackageResult result; |
| 60 | + try { |
| 61 | + final int exitCode = await _runDartFixTests(testDirectory); |
| 62 | + result = exitCode == 0 |
| 63 | + ? PackageResult.success() |
| 64 | + : PackageResult.fail(['Dart fix tests failed.']); |
| 65 | + } catch (error) { |
| 66 | + result = PackageResult.fail(['Dart fix tests failed: $error']); |
| 67 | + } |
| 68 | + if (testDirectory.existsSync()) { |
| 69 | + await testDirectory.delete(recursive: true); |
| 70 | + } |
| 71 | + testDirectories[package.displayName] = testDirectory; |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + /// Create and prepare a temporary directory in which to run the dart fix |
| 76 | + /// tests. |
| 77 | + /// |
| 78 | + /// It is the responsibility of the caller to delete this directory and its |
| 79 | + /// contents when done. |
| 80 | + static Future<Directory> _createTestDirectory(RepositoryPackage package) async { |
| 81 | + final FileSystem fileSystem = package.directory.fileSystem; |
| 82 | + final Directory testDirectory = await fileSystem.systemTempDirectory.createTemp(); |
| 83 | + |
| 84 | + // Copy from `test_fixes/` to the temp directory. |
| 85 | + for (final FileSystemEntity entity in package.dartFixTestDirectory.listSync(recursive: true)) { |
| 86 | + final String relativePath = fileSystem.path.relative( |
| 87 | + entity.path, |
| 88 | + from: package.dartFixTestDirectory.path, |
| 89 | + ); |
| 90 | + final String destPath = fileSystem.path.join(testDirectory.path, relativePath); |
| 91 | + if (entity is Directory) { |
| 92 | + fileSystem.directory(destPath).createSync(recursive: true); |
| 93 | + } else if (entity is File) { |
| 94 | + fileSystem.file(destPath).parent.createSync(recursive: true); |
| 95 | + entity.copySync(destPath); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // The pubspec.yaml file to create. |
| 100 | + final File targetPubspecFile = fileSystem.file( |
| 101 | + fileSystem.path.join(testDirectory.path, 'pubspec.yaml'), |
| 102 | + ); |
| 103 | + |
| 104 | + final targetYaml = |
| 105 | + ''' |
| 106 | +name: test_fixes |
| 107 | +publish_to: "none" |
| 108 | +version: 1.0.0 |
| 109 | +
|
| 110 | +environment: |
| 111 | + sdk: ">=2.18.0 <4.0.0" |
| 112 | + flutter: ">=3.3.0" |
| 113 | +
|
| 114 | +dependencies: |
| 115 | + flutter: |
| 116 | + sdk: flutter |
| 117 | + ${package.directory.basename}: |
| 118 | + path: ${package.directory.path} |
| 119 | +'''; |
| 120 | + |
| 121 | + await targetPubspecFile.writeAsString(targetYaml); |
| 122 | + return testDirectory; |
| 123 | + } |
| 124 | + |
| 125 | + /// Run the dart fix tests for the package in the given temporary directory. |
| 126 | + /// |
| 127 | + /// Resolves with the exit code of the command. |
| 128 | + Future<int> _runDartFixTests(Directory testDirectory) async { |
| 129 | + // Run flutter pub get in the temp directory to set it up. |
| 130 | + final bool success = await runPubGet(RepositoryPackage(testDirectory), processRunner, platform); |
| 131 | + |
| 132 | + if (!success) { |
| 133 | + throw Exception('Pub get failed.'); |
| 134 | + } |
| 135 | + |
| 136 | + // Run dart fix --compare-to-golden in the temp directory. |
| 137 | + return processRunner.runAndStream('dart', <String>[ |
| 138 | + 'fix', |
| 139 | + '--compare-to-golden', |
| 140 | + ], workingDir: testDirectory); |
| 141 | + } |
| 142 | +} |
0 commit comments