[flutter_tools] Let CMake handle escaping (#70026)

This commit is contained in:
J-P Nurmi
2020-12-22 01:04:04 +01:00
committed by GitHub
parent de448c81ed
commit f2536da16f
10 changed files with 285 additions and 25 deletions

View File

@@ -364,17 +364,17 @@ clang: error: linker command failed with exit code 1 (use -v to see invocation)
expect(configLines, containsAll(<String>[
'file(TO_CMAKE_PATH "$_kTestFlutterRoot" FLUTTER_ROOT)',
'file(TO_CMAKE_PATH "${fileSystem.currentDirectory.path}" PROJECT_DIR)',
r' "DART_DEFINES=\"foo.bar%3D2,fizz.far%3D3\""',
r' "DART_OBFUSCATION=\"true\""',
r' "EXTRA_FRONT_END_OPTIONS=\"--enable-experiment%3Dnon-nullable\""',
r' "EXTRA_GEN_SNAPSHOT_OPTIONS=\"--enable-experiment%3Dnon-nullable\""',
r' "SPLIT_DEBUG_INFO=\"foo/\""',
r' "TRACK_WIDGET_CREATION=\"true\""',
r' "TREE_SHAKE_ICONS=\"true\""',
' "FLUTTER_ROOT=\\"$_kTestFlutterRoot\\""',
' "PROJECT_DIR=\\"${fileSystem.currentDirectory.path}\\""',
r' "FLUTTER_TARGET=\"lib/other.dart\""',
r' "BUNDLE_SKSL_PATH=\"foo/bar.sksl.json\""',
' "DART_DEFINES=foo.bar%3D2,fizz.far%3D3"',
' "DART_OBFUSCATION=true"',
' "EXTRA_FRONT_END_OPTIONS=--enable-experiment%3Dnon-nullable"',
' "EXTRA_GEN_SNAPSHOT_OPTIONS=--enable-experiment%3Dnon-nullable"',
' "SPLIT_DEBUG_INFO=foo/"',
' "TRACK_WIDGET_CREATION=true"',
' "TREE_SHAKE_ICONS=true"',
' "FLUTTER_ROOT=$_kTestFlutterRoot"',
' "PROJECT_DIR=${fileSystem.currentDirectory.path}"',
' "FLUTTER_TARGET=lib/other.dart"',
' "BUNDLE_SKSL_PATH=foo/bar.sksl.json"',
]));
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,

View File

@@ -315,17 +315,17 @@ C:\foo\windows\runner\main.cpp(17,1): error C2065: 'Baz': undeclared identifier
expect(configLines, containsAll(<String>[
r'file(TO_CMAKE_PATH "C:\\flutter" FLUTTER_ROOT)',
r'file(TO_CMAKE_PATH "C:\\" PROJECT_DIR)',
r' "DART_DEFINES=\"foo%3Da,bar%3Db\""',
r' "DART_OBFUSCATION=\"true\""',
r' "EXTRA_FRONT_END_OPTIONS=\"--enable-experiment%3Dnon-nullable\""',
r' "EXTRA_GEN_SNAPSHOT_OPTIONS=\"--enable-experiment%3Dnon-nullable\""',
r' "SPLIT_DEBUG_INFO=\"C:\\foo\\\""',
r' "TRACK_WIDGET_CREATION=\"true\""',
r' "TREE_SHAKE_ICONS=\"true\""',
r' "FLUTTER_ROOT=\"C:\\flutter\""',
r' "PROJECT_DIR=\"C:\\\""',
r' "FLUTTER_TARGET=\"lib\\other.dart\""',
r' "BUNDLE_SKSL_PATH=\"foo\\bar.sksl.json\""',
r' "DART_DEFINES=foo%3Da,bar%3Db"',
r' "DART_OBFUSCATION=true"',
r' "EXTRA_FRONT_END_OPTIONS=--enable-experiment%3Dnon-nullable"',
r' "EXTRA_GEN_SNAPSHOT_OPTIONS=--enable-experiment%3Dnon-nullable"',
r' "SPLIT_DEBUG_INFO=C:\\foo\\"',
r' "TRACK_WIDGET_CREATION=true"',
r' "TREE_SHAKE_ICONS=true"',
r' "FLUTTER_ROOT=C:\\flutter"',
r' "PROJECT_DIR=C:\\"',
r' "FLUTTER_TARGET=lib\\other.dart"',
r' "BUNDLE_SKSL_PATH=foo\\bar.sksl.json"',
]));
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,

View File

@@ -0,0 +1,163 @@
// Copyright 2014 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 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/project_migrator.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/migrations/cmake_custom_command_migration.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:meta/meta.dart';
import 'package:mockito/mockito.dart';
import '../../src/common.dart';
void main () {
group('CMake project migration', () {
testWithoutContext('migrators succeed', () {
final FakeCmakeMigrator fakeCmakeMigrator = FakeCmakeMigrator(succeeds: true);
final ProjectMigration migration = ProjectMigration(<ProjectMigrator>[fakeCmakeMigrator]);
expect(migration.run(), isTrue);
});
testWithoutContext('migrators fail', () {
final FakeCmakeMigrator fakeCmakeMigrator = FakeCmakeMigrator(succeeds: false);
final ProjectMigration migration = ProjectMigration(<ProjectMigrator>[fakeCmakeMigrator]);
expect(migration.run(), isFalse);
});
group('migrate add_custom_command() to use VERBATIM', () {
MemoryFileSystem memoryFileSystem;
BufferLogger testLogger;
MockCmakeProject mockCmakeProject;
File managedCmakeFile;
setUp(() {
memoryFileSystem = MemoryFileSystem.test();
managedCmakeFile = memoryFileSystem.file('CMakeLists.txtx');
testLogger = BufferLogger(
terminal: AnsiTerminal(
stdio: null,
platform: const LocalPlatform(),
),
outputPreferences: OutputPreferences.test(),
);
mockCmakeProject = MockCmakeProject();
when(mockCmakeProject.managedCmakeFile).thenReturn(managedCmakeFile);
});
testWithoutContext('skipped if files are missing', () {
final CmakeCustomCommandMigration cmakeProjectMigration = CmakeCustomCommandMigration(
mockCmakeProject,
testLogger,
);
expect(cmakeProjectMigration.migrate(), isTrue);
expect(managedCmakeFile.existsSync(), isFalse);
expect(testLogger.traceText, contains('CMake project not found, skipping add_custom_command() VERBATIM migration'));
expect(testLogger.statusText, isEmpty);
});
testWithoutContext('skipped if nothing to migrate', () {
const String contents = 'Nothing to migrate';
managedCmakeFile.writeAsStringSync(contents);
final DateTime projectLastModified = managedCmakeFile.lastModifiedSync();
final CmakeCustomCommandMigration cmakeProjectMigration = CmakeCustomCommandMigration(
mockCmakeProject,
testLogger,
);
expect(cmakeProjectMigration.migrate(), isTrue);
expect(managedCmakeFile.lastModifiedSync(), projectLastModified);
expect(managedCmakeFile.readAsStringSync(), contents);
expect(testLogger.statusText, isEmpty);
});
testWithoutContext('skipped if already migrated', () {
const String contents = r'''
add_custom_command(
OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS}
${CMAKE_CURRENT_BINARY_DIR}/_phony_
COMMAND ${CMAKE_COMMAND} -E env
${FLUTTER_TOOL_ENVIRONMENT}
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
linux-x64 ${CMAKE_BUILD_TYPE}
VERBATIM
)
''';
managedCmakeFile.writeAsStringSync(contents);
final DateTime projectLastModified = managedCmakeFile.lastModifiedSync();
final CmakeCustomCommandMigration cmakeProjectMigration = CmakeCustomCommandMigration(
mockCmakeProject,
testLogger,
);
expect(cmakeProjectMigration.migrate(), isTrue);
expect(managedCmakeFile.lastModifiedSync(), projectLastModified);
expect(managedCmakeFile.readAsStringSync(), contents);
expect(testLogger.statusText, isEmpty);
});
testWithoutContext('is migrated to use VERBATIM', () {
managedCmakeFile.writeAsStringSync(r'''
add_custom_command(
OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS}
${CMAKE_CURRENT_BINARY_DIR}/_phony_
COMMAND ${CMAKE_COMMAND} -E env
${FLUTTER_TOOL_ENVIRONMENT}
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
linux-x64 ${CMAKE_BUILD_TYPE}
)
''');
final CmakeCustomCommandMigration cmakeProjectMigration = CmakeCustomCommandMigration(
mockCmakeProject,
testLogger,
);
expect(cmakeProjectMigration.migrate(), isTrue);
expect(managedCmakeFile.readAsStringSync(), r'''
add_custom_command(
OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS}
${CMAKE_CURRENT_BINARY_DIR}/_phony_
COMMAND ${CMAKE_COMMAND} -E env
${FLUTTER_TOOL_ENVIRONMENT}
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
linux-x64 ${CMAKE_BUILD_TYPE}
VERBATIM
)
''');
expect(testLogger.statusText, contains('add_custom_command() missing VERBATIM, updating.'));
});
});
});
}
class MockCmakeProject extends Mock implements CmakeBasedProject {}
class FakeCmakeMigrator extends ProjectMigrator {
FakeCmakeMigrator({@required this.succeeds})
: super(null);
final bool succeeds;
@override
bool migrate() {
return succeeds;
}
@override
String migrateLine(String line) {
return line;
}
}