Skip to content

Commit e997a3a

Browse files
authored
Add tests for migrate command methods (#103466)
1 parent 5e85d23 commit e997a3a

File tree

5 files changed

+242
-41
lines changed

5 files changed

+242
-41
lines changed

packages/flutter_tools/lib/src/commands/migrate.dart

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,20 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// import 'package:process/process.dart';
65

7-
// import '../base/file_system.dart';
86
import '../base/logger.dart';
9-
// import '../base/platform.dart';
107
import '../base/terminal.dart';
118
import '../migrate/migrate_utils.dart';
129
import '../runner/flutter_command.dart';
13-
// TODO(garyq): Add each of these back in as they land.
14-
// import 'migrate_abandon.dart';
15-
// import 'migrate_apply.dart';
16-
// import 'migrate_resolve_conflicts.dart';
17-
// import 'migrate_start.dart';
18-
// import 'migrate_status.dart';
10+
1911

2012
/// Base command for the migration tool.
2113
class MigrateCommand extends FlutterCommand {
2214
MigrateCommand({
23-
// bool verbose = false,
2415
required this.logger,
25-
// TODO(garyq): Add each of these back in as they land.
26-
// required FileSystem fileSystem,
27-
// required Terminal terminal,
28-
// required Platform platform,
29-
// required ProcessManager processManager,
16+
// TODO(garyq): Add each parameters in as subcommands land.
3017
}) {
31-
// TODO(garyq): Add each of these back in as they land.
32-
// addSubcommand(MigrateAbandonCommand(logger: logger, fileSystem: fileSystem, terminal: terminal, platform: platform, processManager: processManager));
33-
// addSubcommand(MigrateApplyCommand(verbose: verbose, logger: logger, fileSystem: fileSystem, terminal: terminal, platform: platform, processManager: processManager));
34-
// addSubcommand(MigrateResolveConflictsCommand(logger: logger, fileSystem: fileSystem, terminal: terminal));
35-
// addSubcommand(MigrateStartCommand(verbose: verbose, logger: logger, fileSystem: fileSystem, platform: platform, processManager: processManager));
36-
// addSubcommand(MigrateStatusCommand(verbose: verbose, logger: logger, fileSystem: fileSystem, platform: platform, processManager: processManager));
18+
// TODO(garyq): Add subcommands.
3719
}
3820

3921
final Logger logger;

packages/flutter_tools/lib/src/migrate/migrate_utils.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,32 +242,32 @@ class MigrateUtils {
242242
bool exit = true,
243243
bool silent = false
244244
}) {
245-
if (!allowedExitCodes.contains(result.exitCode) && !allowedExitCodes.contains(-1)) {
246-
if (!silent) {
247-
_logger.printError('Command encountered an error with exit code ${result.exitCode}.');
248-
if (commandDescription != null) {
249-
_logger.printError('Command:');
250-
_logger.printError(commandDescription, indent: 2);
251-
}
252-
_logger.printError('Stdout:');
253-
_logger.printError(result.stdout, indent: 2);
254-
_logger.printError('Stderr:');
255-
_logger.printError(result.stderr, indent: 2);
256-
}
257-
if (exit) {
258-
throwToolExit('Command failed with exit code ${result.exitCode}', exitCode: result.exitCode);
245+
if (allowedExitCodes.contains(result.exitCode) || allowedExitCodes.contains(-1)) {
246+
return true;
247+
}
248+
if (!silent) {
249+
_logger.printError('Command encountered an error with exit code ${result.exitCode}.');
250+
if (commandDescription != null) {
251+
_logger.printError('Command:');
252+
_logger.printError(commandDescription, indent: 2);
259253
}
260-
return false;
254+
_logger.printError('Stdout:');
255+
_logger.printError(result.stdout, indent: 2);
256+
_logger.printError('Stderr:');
257+
_logger.printError(result.stderr, indent: 2);
261258
}
262-
return true;
259+
if (exit) {
260+
throwToolExit('Command failed with exit code ${result.exitCode}', exitCode: result.exitCode);
261+
}
262+
return false;
263263
}
264264

265265
/// Returns true if the file does not contain any git conflit markers.
266266
bool conflictsResolved(String contents) {
267-
if (contents.contains('>>>>>>>') && contents.contains('=======') && contents.contains('<<<<<<<')) {
268-
return false;
269-
}
270-
return true;
267+
final bool hasMarker = contents.contains('>>>>>>>') ||
268+
contents.contains('=======') ||
269+
contents.contains('<<<<<<<');
270+
return !hasMarker;
271271
}
272272
}
273273

packages/flutter_tools/test/general.shard/migrate/migrate_manifest_test.dart

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:file/memory.dart';
66
import 'package:flutter_tools/src/base/file_system.dart';
7+
import 'package:flutter_tools/src/base/logger.dart';
78
import 'package:flutter_tools/src/migrate/migrate_manifest.dart';
89
import 'package:flutter_tools/src/migrate/migrate_result.dart';
910
import 'package:flutter_tools/src/migrate/migrate_utils.dart';
@@ -13,12 +14,119 @@ import '../../src/common.dart';
1314
void main() {
1415
late FileSystem fileSystem;
1516
late File manifestFile;
17+
late BufferLogger logger;
1618

1719
setUpAll(() {
1820
fileSystem = MemoryFileSystem.test();
21+
logger = BufferLogger.test();
1922
manifestFile = fileSystem.file('.migrate_manifest');
2023
});
2124

25+
group('checkAndPrintMigrateStatus', () {
26+
testWithoutContext('empty MigrateResult produces empty output', () async {
27+
final Directory workingDir = fileSystem.directory('migrate_working_dir');
28+
workingDir.createSync(recursive: true);
29+
final MigrateManifest manifest = MigrateManifest(migrateRootDir: workingDir, migrateResult: MigrateResult(
30+
mergeResults: <MergeResult>[],
31+
addedFiles: <FilePendingMigration>[],
32+
deletedFiles: <FilePendingMigration>[],
33+
mergeTypeMap: <String, MergeType>{},
34+
diffMap: <String, DiffResult>{},
35+
tempDirectories: <Directory>[],
36+
sdkDirs: <String, Directory>{},
37+
));
38+
39+
checkAndPrintMigrateStatus(manifest, workingDir, warnConflict: true, logger: logger);
40+
41+
expect(logger.statusText, contains('\n'));
42+
});
43+
44+
testWithoutContext('populated MigrateResult produces correct output', () async {
45+
final Directory workingDir = fileSystem.directory('migrate_working_dir');
46+
workingDir.createSync(recursive: true);
47+
final MigrateManifest manifest = MigrateManifest(migrateRootDir: workingDir, migrateResult: MigrateResult(
48+
mergeResults: <MergeResult>[
49+
StringMergeResult.explicit(
50+
localPath: 'merged_file',
51+
mergedString: 'str',
52+
hasConflict: false,
53+
exitCode: 0,
54+
),
55+
StringMergeResult.explicit(
56+
localPath: 'conflict_file',
57+
mergedString: 'hello\nwow a bunch of lines\n<<<<<<<\n=======\n<<<<<<<\nhi\n',
58+
hasConflict: true,
59+
exitCode: 1,
60+
),
61+
],
62+
addedFiles: <FilePendingMigration>[FilePendingMigration('added_file', fileSystem.file('added_file'))],
63+
deletedFiles: <FilePendingMigration>[FilePendingMigration('deleted_file', fileSystem.file('deleted_file'))],
64+
// The following are ignored by the manifest.
65+
mergeTypeMap: <String, MergeType>{'test': MergeType.threeWay},
66+
diffMap: <String, DiffResult>{},
67+
tempDirectories: <Directory>[],
68+
sdkDirs: <String, Directory>{},
69+
));
70+
71+
final File conflictFile = workingDir.childFile('conflict_file');
72+
conflictFile.writeAsStringSync('hello\nwow a bunch of lines\n<<<<<<<\n=======\n<<<<<<<\nhi\n', flush: true);
73+
74+
checkAndPrintMigrateStatus(manifest, workingDir, warnConflict: true, logger: logger);
75+
76+
expect(logger.statusText, contains('''
77+
Added files:
78+
- added_file
79+
Deleted files:
80+
- deleted_file
81+
Modified files:
82+
- conflict_file
83+
- merged_file
84+
'''));
85+
});
86+
87+
testWithoutContext('populated MigrateResult detects fixed conflict', () async {
88+
final Directory workingDir = fileSystem.directory('migrate_working_dir');
89+
workingDir.createSync(recursive: true);
90+
final MigrateManifest manifest = MigrateManifest(migrateRootDir: workingDir, migrateResult: MigrateResult(
91+
mergeResults: <MergeResult>[
92+
StringMergeResult.explicit(
93+
localPath: 'merged_file',
94+
mergedString: 'str',
95+
hasConflict: false,
96+
exitCode: 0,
97+
),
98+
StringMergeResult.explicit(
99+
localPath: 'conflict_file',
100+
mergedString: 'hello\nwow a bunch of lines\n<<<<<<<\n=======\n<<<<<<<\nhi\n',
101+
hasConflict: true,
102+
exitCode: 1,
103+
),
104+
],
105+
addedFiles: <FilePendingMigration>[FilePendingMigration('added_file', fileSystem.file('added_file'))],
106+
deletedFiles: <FilePendingMigration>[FilePendingMigration('deleted_file', fileSystem.file('deleted_file'))],
107+
// The following are ignored by the manifest.
108+
mergeTypeMap: <String, MergeType>{'test': MergeType.threeWay},
109+
diffMap: <String, DiffResult>{},
110+
tempDirectories: <Directory>[],
111+
sdkDirs: <String, Directory>{},
112+
));
113+
114+
final File conflictFile = workingDir.childFile('conflict_file');
115+
conflictFile.writeAsStringSync('hello\nwow a bunch of lines\nhi\n', flush: true);
116+
117+
checkAndPrintMigrateStatus(manifest, workingDir, warnConflict: true, logger: logger);
118+
expect(logger.statusText, contains('''
119+
Added files:
120+
- added_file
121+
Deleted files:
122+
- deleted_file
123+
Modified files:
124+
- conflict_file
125+
- merged_file
126+
'''));
127+
});
128+
});
129+
22130
group('manifest file parsing', () {
23131
testWithoutContext('empty fails', () async {
24132
manifestFile.writeAsStringSync('');
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2014 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+
// @dart = 2.8
6+
7+
import 'package:file/file.dart';
8+
import 'package:flutter_tools/src/base/file_system.dart';
9+
import 'package:flutter_tools/src/base/logger.dart';
10+
import 'package:flutter_tools/src/base/process.dart';
11+
import 'package:flutter_tools/src/commands/migrate.dart';
12+
import 'package:flutter_tools/src/globals.dart' as globals;
13+
import 'package:flutter_tools/src/migrate/migrate_utils.dart';
14+
15+
import '../src/common.dart';
16+
17+
void main() {
18+
BufferLogger logger;
19+
FileSystem fileSystem;
20+
Directory projectRoot;
21+
String projectRootPath;
22+
ProcessUtils processUtils;
23+
MigrateUtils utils;
24+
25+
setUpAll(() async {
26+
fileSystem = globals.localFileSystem;
27+
logger = BufferLogger.test();
28+
utils = MigrateUtils(
29+
logger: logger,
30+
fileSystem: fileSystem,
31+
platform: globals.platform,
32+
processManager: globals.processManager,
33+
);
34+
processUtils = ProcessUtils(processManager: globals.processManager, logger: logger);
35+
});
36+
37+
group('git', () {
38+
setUp(() async {
39+
projectRoot = fileSystem.systemTempDirectory.createTempSync('flutter_migrate_command_test');
40+
projectRoot.createSync(recursive: true);
41+
projectRootPath = projectRoot.path;
42+
});
43+
44+
tearDown(() async {
45+
tryToDelete(projectRoot);
46+
});
47+
48+
testWithoutContext('isGitRepo', () async {
49+
expect(projectRoot.existsSync(), true);
50+
expect(projectRoot.childDirectory('.git').existsSync(), false);
51+
52+
expect(await gitRepoExists(projectRootPath, logger, utils), false);
53+
expect(logger.statusText, contains('Project is not a git repo. Please initialize a git repo and try again.'));
54+
55+
await utils.gitInit(projectRootPath);
56+
expect(projectRoot.childDirectory('.git').existsSync(), true);
57+
58+
expect(await gitRepoExists(projectRootPath, logger, utils), true);
59+
});
60+
61+
testWithoutContext('printCommandText produces formatted output', () async {
62+
printCommandText('some command --help', logger);
63+
64+
expect(logger.statusText, contains(r' $ some command --help'));
65+
});
66+
67+
testWithoutContext('hasUncommittedChanges false on clean repo', () async {
68+
expect(projectRoot.existsSync(), true);
69+
expect(projectRoot.childDirectory('.git').existsSync(), false);
70+
await utils.gitInit(projectRootPath);
71+
expect(projectRoot.childDirectory('.git').existsSync(), true);
72+
73+
projectRoot.childFile('.gitignore')
74+
..createSync()
75+
..writeAsStringSync('ignored_file.dart', flush: true);
76+
77+
await processUtils.run(<String>['git', 'add', '.'], workingDirectory: projectRootPath);
78+
await processUtils.run(<String>['git', 'commit', '-m', 'Initial commit'], workingDirectory: projectRootPath);
79+
80+
expect(await hasUncommittedChanges(projectRootPath, logger, utils), false);
81+
});
82+
83+
testWithoutContext('hasUncommittedChanges true on dirty repo', () async {
84+
expect(projectRoot.existsSync(), true);
85+
expect(projectRoot.childDirectory('.git').existsSync(), false);
86+
await utils.gitInit(projectRootPath);
87+
expect(projectRoot.childDirectory('.git').existsSync(), true);
88+
89+
projectRoot.childFile('some_file.dart')
90+
..createSync()
91+
..writeAsStringSync('void main() {}', flush: true);
92+
93+
expect(await hasUncommittedChanges(projectRootPath, logger, utils), true);
94+
});
95+
});
96+
}

packages/flutter_tools/test/integration.shard/migrate_utils_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ void main() {
4040
projectRootPath = projectRoot.path;
4141
});
4242

43+
tearDown(() async {
44+
tryToDelete(projectRoot);
45+
});
46+
4347
testWithoutContext('init', () async {
4448
expect(projectRoot.existsSync(), true);
4549
expect(projectRoot.childDirectory('.git').existsSync(), false);
@@ -220,4 +224,15 @@ void main() {
220224
projectRoot.deleteSync(recursive: true);
221225
});
222226
});
227+
228+
testWithoutContext('conflictsResolved', () async {
229+
expect(utils.conflictsResolved(''), true);
230+
expect(utils.conflictsResolved('hello'), true);
231+
expect(utils.conflictsResolved('hello\n'), true);
232+
expect(utils.conflictsResolved('hello\nwow a bunch of lines\n\nhi\n'), true);
233+
expect(utils.conflictsResolved('hello\nwow a bunch of lines\n>>>>>>>\nhi\n'), false);
234+
expect(utils.conflictsResolved('hello\nwow a bunch of lines\n=======\nhi\n'), false);
235+
expect(utils.conflictsResolved('hello\nwow a bunch of lines\n<<<<<<<\nhi\n'), false);
236+
expect(utils.conflictsResolved('hello\nwow a bunch of lines\n<<<<<<<\n=======\n<<<<<<<\nhi\n'), false);
237+
});
223238
}

0 commit comments

Comments
 (0)