flutter_root_test.dart 3.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/user_messages.dart';
import 'package:flutter_tools/src/cache.dart';

import '../../src/common.dart';

void main() {
  testWithoutContext('Cache can initialize flutter root from environment variable', () {
    final String defaultFlutterRoot = Cache.defaultFlutterRoot(
16 17 18 19 20 21 22
      fileSystem: MemoryFileSystem.test(),
      userMessages: UserMessages(),
      platform: FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': 'path/to/flutter',
        },
      ),
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    );

    expect(defaultFlutterRoot, '/path/to/flutter');
  });

  testWithoutContext('Cache can initialize flutter root data-scheme platform script', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    // For data-uri, the root is initialized to ../.. and then normalized. Change the
    // current directory to verify this.
    final Directory directory = fileSystem.directory('foo/bar/baz/')
      ..createSync(recursive: true);
    fileSystem.currentDirectory = directory;

    final String defaultFlutterRoot = Cache.defaultFlutterRoot(
        fileSystem: fileSystem,
        userMessages: UserMessages(),
        platform: FakePlatform(
          environment: <String, String>{},
          script: Uri.parse('data:,Hello%2C%20World!'),
        )
    );

    expect(defaultFlutterRoot, '/foo');
  });

  testWithoutContext('Cache can initialize flutter root package-scheme platform script', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final String defaultFlutterRoot = Cache.defaultFlutterRoot(
        fileSystem: fileSystem,
        userMessages: UserMessages(),
        platform: FakePlatform(
            environment: <String, String>{},
            script: Uri.parse('package:flutter_tools/flutter_tools.dart'),
            packageConfig: 'flutter/packages/flutter_tools/.packages'
        )
    );

    expect(defaultFlutterRoot, '/flutter');
  });

  testWithoutContext('Cache can initialize flutter root from snapshot location', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final String defaultFlutterRoot = Cache.defaultFlutterRoot(
        fileSystem: fileSystem,
        userMessages: UserMessages(),
        platform: FakePlatform(
          environment: <String, String>{},
          script: Uri.parse('file:///flutter/bin/cache/flutter_tools.snapshot'),
        )
    );

    expect(defaultFlutterRoot, '/flutter');
  });

  testWithoutContext('Cache can initialize flutter root from script file', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final String defaultFlutterRoot = Cache.defaultFlutterRoot(
        fileSystem: fileSystem,
        userMessages: UserMessages(),
        platform: FakePlatform(
          environment: <String, String>{},
          script: Uri.parse('file:///flutter/packages/flutter_tools/bin/flutter_tools.dart'),
        )
    );

    expect(defaultFlutterRoot, '/flutter');
  });

  testWithoutContext('Cache will default to current directory if there are no matches', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final String defaultFlutterRoot = Cache.defaultFlutterRoot(
        fileSystem: fileSystem,
        userMessages: UserMessages(),
        platform: FakePlatform(
          environment: <String, String>{},
          script: Uri.parse('http://foo.bar'), // does not match any heuristics.
        )
    );

    expect(defaultFlutterRoot, '/');
  });
}