|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 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:io' as io; |
| 6 | + |
| 7 | +import 'package:args/command_runner.dart'; |
| 8 | +import 'package:file/file.dart'; |
| 9 | +import 'package:file/memory.dart'; |
| 10 | +import 'package:flutter_plugin_tools/src/common/core.dart'; |
| 11 | +import 'package:flutter_plugin_tools/src/fix_command.dart'; |
| 12 | +import 'package:test/test.dart'; |
| 13 | + |
| 14 | +import 'mocks.dart'; |
| 15 | +import 'util.dart'; |
| 16 | + |
| 17 | +void main() { |
| 18 | + late FileSystem fileSystem; |
| 19 | + late MockPlatform mockPlatform; |
| 20 | + late Directory packagesDir; |
| 21 | + late RecordingProcessRunner processRunner; |
| 22 | + late CommandRunner<void> runner; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + fileSystem = MemoryFileSystem(); |
| 26 | + mockPlatform = MockPlatform(); |
| 27 | + packagesDir = createPackagesDirectory(fileSystem: fileSystem); |
| 28 | + processRunner = RecordingProcessRunner(); |
| 29 | + final FixCommand command = FixCommand( |
| 30 | + packagesDir, |
| 31 | + processRunner: processRunner, |
| 32 | + platform: mockPlatform, |
| 33 | + ); |
| 34 | + |
| 35 | + runner = CommandRunner<void>('fix_command', 'Test for fix_command'); |
| 36 | + runner.addCommand(command); |
| 37 | + }); |
| 38 | + |
| 39 | + test('runs fix in top-level packages and subpackages', () async { |
| 40 | + final RepositoryPackage package = createFakePackage('a', packagesDir); |
| 41 | + final RepositoryPackage plugin = createFakePlugin('b', packagesDir); |
| 42 | + |
| 43 | + await runCapturingPrint(runner, <String>['fix']); |
| 44 | + |
| 45 | + expect( |
| 46 | + processRunner.recordedCalls, |
| 47 | + orderedEquals(<ProcessCall>[ |
| 48 | + ProcessCall('dart', const <String>['fix', '--apply'], package.path), |
| 49 | + ProcessCall('dart', const <String>['fix', '--apply'], |
| 50 | + package.getExamples().first.path), |
| 51 | + ProcessCall('dart', const <String>['fix', '--apply'], plugin.path), |
| 52 | + ProcessCall('dart', const <String>['fix', '--apply'], |
| 53 | + plugin.getExamples().first.path), |
| 54 | + ])); |
| 55 | + }); |
| 56 | + |
| 57 | + test('fails if "dart fix" fails', () async { |
| 58 | + createFakePlugin('foo', packagesDir); |
| 59 | + |
| 60 | + processRunner.mockProcessesForExecutable['dart'] = <io.Process>[ |
| 61 | + MockProcess(exitCode: 1), |
| 62 | + ]; |
| 63 | + |
| 64 | + Error? commandError; |
| 65 | + final List<String> output = await runCapturingPrint(runner, <String>['fix'], |
| 66 | + errorHandler: (Error e) { |
| 67 | + commandError = e; |
| 68 | + }); |
| 69 | + |
| 70 | + expect(commandError, isA<ToolExit>()); |
| 71 | + expect( |
| 72 | + output, |
| 73 | + containsAllInOrder(<Matcher>[ |
| 74 | + contains('Unable to automatically fix package.'), |
| 75 | + ]), |
| 76 | + ); |
| 77 | + }); |
| 78 | +} |
0 commit comments