linux_test.dart 11.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
9
import 'package:flutter_tools/src/artifacts.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11
import 'package:flutter_tools/src/base/logger.dart';
12
import 'package:flutter_tools/src/build_info.dart';
13
import 'package:flutter_tools/src/build_system/build_system.dart';
14
import 'package:flutter_tools/src/build_system/targets/common.dart';
15
import 'package:flutter_tools/src/build_system/targets/linux.dart';
16
import 'package:flutter_tools/src/convert.dart';
17

18
import '../../../src/common.dart';
19
import '../../../src/context.dart';
20 21

void main() {
22
  testWithoutContext('Copies files to correct cache directory, excluding unrelated code on a x64 host', () async {
23
    final FileSystem fileSystem = MemoryFileSystem.test();
24 25
    final Artifacts artifacts = Artifacts.test();
    setUpCacheDirectory(fileSystem, artifacts);
26 27 28 29 30 31

    final Environment testEnvironment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'debug',
      },
32
      artifacts: artifacts,
33 34 35 36 37 38
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
    );
    testEnvironment.buildDir.createSync(recursive: true);

39
    await const UnpackLinux(TargetPlatform.linux_x64).build(testEnvironment);
40

41
    expect(fileSystem.file('linux/flutter/ephemeral/libflutter_linux_gtk.so'), exists);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    expect(fileSystem.file('linux/flutter/ephemeral/unrelated-stuff'), isNot(exists));

    // Check if the target files are copied correctly.
    final String headersPathForX64 = artifacts.getArtifactPath(Artifact.linuxHeaders, platform: TargetPlatform.linux_x64, mode: BuildMode.debug);
    final String headersPathForArm64 = artifacts.getArtifactPath(Artifact.linuxHeaders, platform: TargetPlatform.linux_arm64, mode: BuildMode.debug);
    expect(fileSystem.file('linux/flutter/ephemeral/$headersPathForX64/foo.h'), exists);
    expect(fileSystem.file('linux/flutter/ephemeral/$headersPathForArm64/foo.h'), isNot(exists));

    final String icuDataPathForX64 = artifacts.getArtifactPath(Artifact.icuData, platform: TargetPlatform.linux_x64);
    final String icuDataPathForArm64 = artifacts.getArtifactPath(Artifact.icuData, platform: TargetPlatform.linux_arm64);
    expect(fileSystem.file('linux/flutter/ephemeral/$icuDataPathForX64'), exists);
    expect(fileSystem.file('linux/flutter/ephemeral/$icuDataPathForArm64'), isNot(exists));
  });

  // This test is basically the same logic as the above test.
  // The difference is the target CPU architecture.
  testWithoutContext('Copies files to correct cache directory, excluding unrelated code on a arm64 host', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Artifacts artifacts = Artifacts.test();
    setUpCacheDirectory(fileSystem, artifacts);

    final Environment testEnvironment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'debug',
      },
      artifacts: artifacts,
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
    );
    testEnvironment.buildDir.createSync(recursive: true);
74

75
    await const UnpackLinux(TargetPlatform.linux_arm64).build(testEnvironment);
76

77
    expect(fileSystem.file('linux/flutter/ephemeral/libflutter_linux_gtk.so'), exists);
78
    expect(fileSystem.file('linux/flutter/ephemeral/unrelated-stuff'), isNot(exists));
79 80 81 82 83 84 85 86 87 88 89

    // Check if the target files are copied correctly.
    final String headersPathForX64 = artifacts.getArtifactPath(Artifact.linuxHeaders, platform: TargetPlatform.linux_x64, mode: BuildMode.debug);
    final String headersPathForArm64 = artifacts.getArtifactPath(Artifact.linuxHeaders, platform: TargetPlatform.linux_arm64, mode: BuildMode.debug);
    expect(fileSystem.file('linux/flutter/ephemeral/$headersPathForX64/foo.h'), isNot(exists));
    expect(fileSystem.file('linux/flutter/ephemeral/$headersPathForArm64/foo.h'), exists);

    final String icuDataPathForX64 = artifacts.getArtifactPath(Artifact.icuData, platform: TargetPlatform.linux_x64);
    final String icuDataPathForArm64 = artifacts.getArtifactPath(Artifact.icuData, platform: TargetPlatform.linux_arm64);
    expect(fileSystem.file('linux/flutter/ephemeral/$icuDataPathForX64'), isNot(exists));
    expect(fileSystem.file('linux/flutter/ephemeral/$icuDataPathForArm64'), exists);
90
  });
91

92 93
  // Only required for the test below that still depends on the context.
  FileSystem fileSystem;
94
  setUp(() {
95
    fileSystem = MemoryFileSystem.test();
96
  });
97

98 99 100 101 102 103
  testUsingContext('DebugBundleLinuxAssets copies artifacts to out directory', () async {
    final Environment testEnvironment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'debug',
      },
104 105 106
      inputs: <String, String>{
        kBundleSkSLPath: 'bundle.sksl',
      },
107
      artifacts: Artifacts.test(),
108 109 110
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
111
      engineVersion: '2',
112
    );
113

114
    testEnvironment.buildDir.createSync(recursive: true);
115 116

    // Create input files.
117
    testEnvironment.buildDir.childFile('app.dill').createSync();
118 119 120 121 122 123 124 125 126
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
      <String, Object>{
        'engineRevision': '2',
        'platform': 'ios',
        'data': <String, Object>{
          'A': 'B',
        }
      }
    ));
127

128 129
    await const DebugBundleLinuxAssets(TargetPlatform.linux_x64).build(testEnvironment);

130
    final Directory output = testEnvironment.outputDir
131 132
      .childDirectory('flutter_assets');

133 134
    expect(output.childFile('kernel_blob.bin'), exists);
    expect(output.childFile('AssetManifest.json'), exists);
135
    expect(output.childFile('version.json'), exists);
136 137 138 139
    // SkSL
    expect(output.childFile('io.flutter.shaders.json'), exists);
    expect(output.childFile('io.flutter.shaders.json').readAsStringSync(), '{"data":{"A":"B"}}');

140 141 142 143 144
    // No bundled fonts
    expect(output.childFile('FontManifest.json'), isNot(exists));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
145 146
  });

147
  testWithoutContext("DebugBundleLinuxAssets' name depends on target platforms", () async {
148 149 150 151
    expect(const DebugBundleLinuxAssets(TargetPlatform.linux_x64).name, 'debug_bundle_linux-x64_assets');
    expect(const DebugBundleLinuxAssets(TargetPlatform.linux_arm64).name, 'debug_bundle_linux-arm64_assets');
  });

152 153 154 155 156 157
  testUsingContext('ProfileBundleLinuxAssets copies artifacts to out directory', () async {
    final Environment testEnvironment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'profile',
      },
158
      artifacts: Artifacts.test(),
159 160 161 162 163 164 165 166 167 168
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
    );

    testEnvironment.buildDir.createSync(recursive: true);

    // Create input files.
    testEnvironment.buildDir.childFile('app.so').createSync();

169
    await const LinuxAotBundle(AotElfProfile(TargetPlatform.linux_x64)).build(testEnvironment);
170
    await const ProfileBundleLinuxAssets(TargetPlatform.linux_x64).build(testEnvironment);
171 172 173 174 175 176 177
    final Directory libDir = testEnvironment.outputDir
      .childDirectory('lib');
    final Directory assetsDir = testEnvironment.outputDir
      .childDirectory('flutter_assets');

    expect(libDir.childFile('libapp.so'), exists);
    expect(assetsDir.childFile('AssetManifest.json'), exists);
178
    expect(assetsDir.childFile('version.json'), exists);
179 180 181 182 183 184 185
    // No bundled fonts
    expect(assetsDir.childFile('FontManifest.json'), isNot(exists));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });

186
  testWithoutContext("ProfileBundleLinuxAssets' name depends on target platforms", () async {
187 188 189 190
    expect(const ProfileBundleLinuxAssets(TargetPlatform.linux_x64).name, 'profile_bundle_linux-x64_assets');
    expect(const ProfileBundleLinuxAssets(TargetPlatform.linux_arm64).name, 'profile_bundle_linux-arm64_assets');
  });

191 192 193 194 195 196
  testUsingContext('ReleaseBundleLinuxAssets copies artifacts to out directory', () async {
    final Environment testEnvironment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'release',
      },
197
      artifacts: Artifacts.test(),
198 199 200 201 202 203 204 205 206 207
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
    );

    testEnvironment.buildDir.createSync(recursive: true);

    // Create input files.
    testEnvironment.buildDir.childFile('app.so').createSync();

208
    await const LinuxAotBundle(AotElfRelease(TargetPlatform.linux_x64)).build(testEnvironment);
209
    await const ReleaseBundleLinuxAssets(TargetPlatform.linux_x64).build(testEnvironment);
210 211 212 213 214 215 216
    final Directory libDir = testEnvironment.outputDir
      .childDirectory('lib');
    final Directory assetsDir = testEnvironment.outputDir
      .childDirectory('flutter_assets');

    expect(libDir.childFile('libapp.so'), exists);
    expect(assetsDir.childFile('AssetManifest.json'), exists);
217
    expect(assetsDir.childFile('version.json'), exists);
218 219 220 221 222
    // No bundled fonts
    expect(assetsDir.childFile('FontManifest.json'), isNot(exists));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
223
  });
224

225
  testWithoutContext("ReleaseBundleLinuxAssets' name depends on target platforms", () async {
226 227 228
    expect(const ReleaseBundleLinuxAssets(TargetPlatform.linux_x64).name, 'release_bundle_linux-x64_assets');
    expect(const ReleaseBundleLinuxAssets(TargetPlatform.linux_arm64).name, 'release_bundle_linux-arm64_assets');
  });
229 230
}

231
void setUpCacheDirectory(FileSystem fileSystem, Artifacts artifacts) {
232 233 234 235 236 237 238 239 240 241 242
  final String desktopPathForX64 = artifacts.getArtifactPath(Artifact.linuxDesktopPath, platform: TargetPlatform.linux_x64, mode: BuildMode.debug);
  final String desktopPathForArm64 = artifacts.getArtifactPath(Artifact.linuxDesktopPath, platform: TargetPlatform.linux_arm64, mode: BuildMode.debug);
  fileSystem.file('$desktopPathForX64/unrelated-stuff').createSync(recursive: true);
  fileSystem.file('$desktopPathForX64/libflutter_linux_gtk.so').createSync(recursive: true);
  fileSystem.file('$desktopPathForArm64/unrelated-stuff').createSync(recursive: true);
  fileSystem.file('$desktopPathForArm64/libflutter_linux_gtk.so').createSync(recursive: true);

  final String headersPathForX64 = artifacts.getArtifactPath(Artifact.linuxHeaders, platform: TargetPlatform.linux_x64, mode: BuildMode.debug);
  final String headersPathForArm64 = artifacts.getArtifactPath(Artifact.linuxHeaders, platform: TargetPlatform.linux_arm64, mode: BuildMode.debug);
  fileSystem.file('$headersPathForX64/foo.h').createSync(recursive: true);
  fileSystem.file('$headersPathForArm64/foo.h').createSync(recursive: true);
243 244

  fileSystem.file(artifacts.getArtifactPath(Artifact.icuData, platform: TargetPlatform.linux_x64)).createSync();
245 246
  fileSystem.file(artifacts.getArtifactPath(Artifact.icuData, platform: TargetPlatform.linux_arm64)).createSync();

247
  fileSystem.file('packages/flutter_tools/lib/src/build_system/targets/linux.dart').createSync(recursive: true);
248
}