This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab5e9e6
commit 5bb5129
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:file/file.dart'; | ||
import 'package:platform/platform.dart'; | ||
|
||
import 'common/core.dart'; | ||
import 'common/package_looping_command.dart'; | ||
import 'common/process_runner.dart'; | ||
import 'common/repository_package.dart'; | ||
|
||
/// A command to run Dart's "fix" command on packages. | ||
class FixCommand extends PackageLoopingCommand { | ||
/// Creates a fix command instance. | ||
FixCommand( | ||
Directory packagesDir, { | ||
ProcessRunner processRunner = const ProcessRunner(), | ||
Platform platform = const LocalPlatform(), | ||
}) : super(packagesDir, processRunner: processRunner, platform: platform); | ||
|
||
@override | ||
final String name = 'fix'; | ||
|
||
@override | ||
final String description = 'Fixes packages using dart fix.\n\n' | ||
'This command requires "dart" and "flutter" to be in your path, and ' | ||
'assumes that dependencies have already been fetched (e.g., by running ' | ||
'the analyze command first).'; | ||
|
||
@override | ||
final bool hasLongOutput = false; | ||
|
||
@override | ||
PackageLoopingType get packageLoopingType => | ||
PackageLoopingType.includeAllSubpackages; | ||
|
||
@override | ||
Future<PackageResult> runForPackage(RepositoryPackage package) async { | ||
final int exitCode = await processRunner.runAndStream( | ||
'dart', <String>['fix', '--apply'], | ||
workingDir: package.directory); | ||
if (exitCode != 0) { | ||
printError('Unable to automatically fix package.'); | ||
return PackageResult.fail(); | ||
} | ||
return PackageResult.success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:io' as io; | ||
|
||
import 'package:args/command_runner.dart'; | ||
import 'package:file/file.dart'; | ||
import 'package:file/memory.dart'; | ||
import 'package:flutter_plugin_tools/src/common/core.dart'; | ||
import 'package:flutter_plugin_tools/src/fix_command.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'mocks.dart'; | ||
import 'util.dart'; | ||
|
||
void main() { | ||
late FileSystem fileSystem; | ||
late MockPlatform mockPlatform; | ||
late Directory packagesDir; | ||
late RecordingProcessRunner processRunner; | ||
late CommandRunner<void> runner; | ||
|
||
setUp(() { | ||
fileSystem = MemoryFileSystem(); | ||
mockPlatform = MockPlatform(); | ||
packagesDir = createPackagesDirectory(fileSystem: fileSystem); | ||
processRunner = RecordingProcessRunner(); | ||
final FixCommand command = FixCommand( | ||
packagesDir, | ||
processRunner: processRunner, | ||
platform: mockPlatform, | ||
); | ||
|
||
runner = CommandRunner<void>('fix_command', 'Test for fix_command'); | ||
runner.addCommand(command); | ||
}); | ||
|
||
test('runs fix in top-level packages and subpackages', () async { | ||
final RepositoryPackage package = createFakePackage('a', packagesDir); | ||
final RepositoryPackage plugin = createFakePlugin('b', packagesDir); | ||
|
||
await runCapturingPrint(runner, <String>['fix']); | ||
|
||
expect( | ||
processRunner.recordedCalls, | ||
orderedEquals(<ProcessCall>[ | ||
ProcessCall('dart', const <String>['fix', '--apply'], package.path), | ||
ProcessCall('dart', const <String>['fix', '--apply'], | ||
package.getExamples().first.path), | ||
ProcessCall('dart', const <String>['fix', '--apply'], plugin.path), | ||
ProcessCall('dart', const <String>['fix', '--apply'], | ||
plugin.getExamples().first.path), | ||
])); | ||
}); | ||
|
||
test('fails if "dart fix" fails', () async { | ||
createFakePlugin('foo', packagesDir); | ||
|
||
processRunner.mockProcessesForExecutable['dart'] = <io.Process>[ | ||
MockProcess(exitCode: 1), | ||
]; | ||
|
||
Error? commandError; | ||
final List<String> output = await runCapturingPrint(runner, <String>['fix'], | ||
errorHandler: (Error e) { | ||
commandError = e; | ||
}); | ||
|
||
expect(commandError, isA<ToolExit>()); | ||
expect( | ||
output, | ||
containsAllInOrder(<Matcher>[ | ||
contains('Unable to automatically fix package.'), | ||
]), | ||
); | ||
}); | ||
} |