linux_test.dart 11.6 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
import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
7
import 'package:flutter_tools/src/artifacts.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/build_info.dart';
11
import 'package:flutter_tools/src/build_system/build_system.dart';
12
import 'package:flutter_tools/src/build_system/targets/common.dart';
13
import 'package:flutter_tools/src/build_system/targets/linux.dart';
14
import 'package:flutter_tools/src/convert.dart';
15

16
import '../../../src/common.dart';
17
import '../../../src/context.dart';
18 19

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

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

37
    await const UnpackLinux(TargetPlatform.linux_x64).build(testEnvironment);
38

39
    expect(fileSystem.file('linux/flutter/ephemeral/libflutter_linux_gtk.so'), exists);
40 41 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
    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);
72

73
    await const UnpackLinux(TargetPlatform.linux_arm64).build(testEnvironment);
74

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

    // 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);
88
  });
89

90
  // Only required for the test below that still depends on the context.
91 92
  late FileSystem fileSystem;

93
  setUp(() {
94
    fileSystem = MemoryFileSystem.test();
95
  });
96

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

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

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

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

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

134 135
    expect(output.childFile('kernel_blob.bin'), exists);
    expect(output.childFile('AssetManifest.json'), exists);
136
    expect(output.childFile('version.json'), exists);
137 138 139
    final String versionFile = output.childFile('version.json').readAsStringSync();
    expect(versionFile, contains('"version":"2.0.0"'));
    expect(versionFile, contains('"build_number":"22"'));
140 141 142 143
    // SkSL
    expect(output.childFile('io.flutter.shaders.json'), exists);
    expect(output.childFile('io.flutter.shaders.json').readAsStringSync(), '{"data":{"A":"B"}}');

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

151
  testWithoutContext("DebugBundleLinuxAssets' name depends on target platforms", () async {
152 153 154 155
    expect(const DebugBundleLinuxAssets(TargetPlatform.linux_x64).name, 'debug_bundle_linux-x64_assets');
    expect(const DebugBundleLinuxAssets(TargetPlatform.linux_arm64).name, 'debug_bundle_linux-arm64_assets');
  });

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

    testEnvironment.buildDir.createSync(recursive: true);

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

173
    await const LinuxAotBundle(AotElfProfile(TargetPlatform.linux_x64)).build(testEnvironment);
174
    await const ProfileBundleLinuxAssets(TargetPlatform.linux_x64).build(testEnvironment);
175 176 177 178 179 180 181
    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);
182
    expect(assetsDir.childFile('version.json'), exists);
183 184 185 186 187 188 189
    // No bundled fonts
    expect(assetsDir.childFile('FontManifest.json'), isNot(exists));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });

190
  testWithoutContext("ProfileBundleLinuxAssets' name depends on target platforms", () async {
191 192 193 194
    expect(const ProfileBundleLinuxAssets(TargetPlatform.linux_x64).name, 'profile_bundle_linux-x64_assets');
    expect(const ProfileBundleLinuxAssets(TargetPlatform.linux_arm64).name, 'profile_bundle_linux-arm64_assets');
  });

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

    testEnvironment.buildDir.createSync(recursive: true);

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

212
    await const LinuxAotBundle(AotElfRelease(TargetPlatform.linux_x64)).build(testEnvironment);
213
    await const ReleaseBundleLinuxAssets(TargetPlatform.linux_x64).build(testEnvironment);
214 215 216 217 218 219 220
    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);
221
    expect(assetsDir.childFile('version.json'), exists);
222 223 224 225 226
    // No bundled fonts
    expect(assetsDir.childFile('FontManifest.json'), isNot(exists));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
227
  });
228

229
  testWithoutContext("ReleaseBundleLinuxAssets' name depends on target platforms", () async {
230 231 232
    expect(const ReleaseBundleLinuxAssets(TargetPlatform.linux_x64).name, 'release_bundle_linux-x64_assets');
    expect(const ReleaseBundleLinuxAssets(TargetPlatform.linux_arm64).name, 'release_bundle_linux-arm64_assets');
  });
233 234
}

235
void setUpCacheDirectory(FileSystem fileSystem, Artifacts artifacts) {
236 237 238 239 240 241 242 243 244 245 246
  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);
247 248

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

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