|
| 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/memory.dart'; |
| 8 | +import 'package:file_testing/file_testing.dart'; |
| 9 | +import 'package:flutter_tools/src/base/file_system.dart'; |
| 10 | +import 'package:flutter_tools/src/cmake.dart'; |
| 11 | +import 'package:flutter_tools/src/project.dart'; |
| 12 | + |
| 13 | +import '../src/common.dart'; |
| 14 | +import '../src/context.dart'; |
| 15 | + |
| 16 | +const String _kTestFlutterRoot = '/flutter'; |
| 17 | +const String _kTestWindowsFlutterRoot = r'C:\flutter'; |
| 18 | + |
| 19 | +void main() { |
| 20 | + FileSystem fileSystem; |
| 21 | + |
| 22 | + ProcessManager processManager; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + fileSystem = MemoryFileSystem.test(); |
| 26 | + }); |
| 27 | + |
| 28 | + testUsingContext('parses executable name from cmake file', () async { |
| 29 | + final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory); |
| 30 | + final CmakeBasedProject cmakeProject = _FakeProject.fromFlutter(project); |
| 31 | + |
| 32 | + cmakeProject.cmakeFile |
| 33 | + ..createSync(recursive: true) |
| 34 | + ..writeAsStringSync('set(BINARY_NAME "hello")'); |
| 35 | + |
| 36 | + final String name = getCmakeExecutableName(cmakeProject); |
| 37 | + |
| 38 | + expect(name, 'hello'); |
| 39 | + }, overrides: <Type, Generator>{ |
| 40 | + FileSystem: () => fileSystem, |
| 41 | + ProcessManager: () => processManager, |
| 42 | + }); |
| 43 | + |
| 44 | + testUsingContext('defaults executable name to null if cmake config does not exist', () async { |
| 45 | + final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory); |
| 46 | + final CmakeBasedProject cmakeProject = _FakeProject.fromFlutter(project); |
| 47 | + |
| 48 | + final String name = getCmakeExecutableName(cmakeProject); |
| 49 | + |
| 50 | + expect(name, isNull); |
| 51 | + }, overrides: <Type, Generator>{ |
| 52 | + FileSystem: () => fileSystem, |
| 53 | + ProcessManager: () => processManager, |
| 54 | + }); |
| 55 | + |
| 56 | + testUsingContext('generates config', () async { |
| 57 | + final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory); |
| 58 | + final CmakeBasedProject cmakeProject = _FakeProject.fromFlutter(project); |
| 59 | + final Map<String, String> environment = <String, String>{}; |
| 60 | + |
| 61 | + writeGeneratedCmakeConfig( |
| 62 | + _kTestFlutterRoot, |
| 63 | + cmakeProject, |
| 64 | + environment, |
| 65 | + ); |
| 66 | + |
| 67 | + final File cmakeConfig = cmakeProject.generatedCmakeConfigFile; |
| 68 | + |
| 69 | + expect(cmakeConfig, exists); |
| 70 | + |
| 71 | + final List<String> configLines = cmakeConfig.readAsLinesSync(); |
| 72 | + |
| 73 | + expect(configLines, containsAll(<String>[ |
| 74 | + r'# Generated code do not commit.', |
| 75 | + r'file(TO_CMAKE_PATH "/flutter" FLUTTER_ROOT)', |
| 76 | + r'file(TO_CMAKE_PATH "/" PROJECT_DIR)', |
| 77 | + |
| 78 | + r'# Environment variables to pass to tool_backend.sh', |
| 79 | + r'list(APPEND FLUTTER_TOOL_ENVIRONMENT', |
| 80 | + r' "FLUTTER_ROOT=/flutter"', |
| 81 | + r' "PROJECT_DIR=/"', |
| 82 | + r')', |
| 83 | + ])); |
| 84 | + }, overrides: <Type, Generator>{ |
| 85 | + FileSystem: () => fileSystem, |
| 86 | + ProcessManager: () => processManager, |
| 87 | + }); |
| 88 | + |
| 89 | + testUsingContext('config escapes backslashes', () async { |
| 90 | + fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows); |
| 91 | + |
| 92 | + final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory); |
| 93 | + final CmakeBasedProject cmakeProject = _FakeProject.fromFlutter(project); |
| 94 | + |
| 95 | + final Map<String, String> environment = <String, String>{ |
| 96 | + 'TEST': r'hello\world', |
| 97 | + }; |
| 98 | + |
| 99 | + writeGeneratedCmakeConfig( |
| 100 | + _kTestWindowsFlutterRoot, |
| 101 | + cmakeProject, |
| 102 | + environment, |
| 103 | + ); |
| 104 | + |
| 105 | + final File cmakeConfig = cmakeProject.generatedCmakeConfigFile; |
| 106 | + |
| 107 | + expect(cmakeConfig, exists); |
| 108 | + |
| 109 | + final List<String> configLines = cmakeConfig.readAsLinesSync(); |
| 110 | + |
| 111 | + expect(configLines, containsAll(<String>[ |
| 112 | + r'# Generated code do not commit.', |
| 113 | + r'file(TO_CMAKE_PATH "C:\\flutter" FLUTTER_ROOT)', |
| 114 | + r'file(TO_CMAKE_PATH "C:\\" PROJECT_DIR)', |
| 115 | + |
| 116 | + r'# Environment variables to pass to tool_backend.sh', |
| 117 | + r'list(APPEND FLUTTER_TOOL_ENVIRONMENT', |
| 118 | + r' "FLUTTER_ROOT=C:\\flutter"', |
| 119 | + r' "PROJECT_DIR=C:\\"', |
| 120 | + r' "TEST=hello\\world"', |
| 121 | + r')', |
| 122 | + ])); |
| 123 | + }, overrides: <Type, Generator>{ |
| 124 | + FileSystem: () => fileSystem, |
| 125 | + ProcessManager: () => processManager, |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +class _FakeProject implements CmakeBasedProject { |
| 130 | + _FakeProject.fromFlutter(this._parent); |
| 131 | + |
| 132 | + final FlutterProject _parent; |
| 133 | + |
| 134 | + @override |
| 135 | + bool existsSync() => _editableDirectory.existsSync(); |
| 136 | + |
| 137 | + @override |
| 138 | + File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt'); |
| 139 | + |
| 140 | + @override |
| 141 | + File get managedCmakeFile => _managedDirectory.childFile('CMakeLists.txt'); |
| 142 | + |
| 143 | + @override |
| 144 | + File get generatedCmakeConfigFile => _ephemeralDirectory.childFile('generated_config.cmake'); |
| 145 | + |
| 146 | + @override |
| 147 | + File get generatedPluginCmakeFile => _managedDirectory.childFile('generated_plugins.cmake'); |
| 148 | + |
| 149 | + @override |
| 150 | + Directory get pluginSymlinkDirectory => _ephemeralDirectory.childDirectory('.plugin_symlinks'); |
| 151 | + |
| 152 | + @override |
| 153 | + FlutterProject get parent => _parent; |
| 154 | + |
| 155 | + Directory get _editableDirectory => parent.directory.childDirectory('test'); |
| 156 | + Directory get _managedDirectory => _editableDirectory.childDirectory('flutter'); |
| 157 | + Directory get _ephemeralDirectory => _managedDirectory.childDirectory('ephemeral'); |
| 158 | +} |
0 commit comments